[systemd-devel] [PATCH 3/5] test: add infrastructure to allow multiple policies per connection

Djalal Harouni tixxdz at opendz.org
Wed Jul 23 09:34:34 PDT 2014


A policy holder should be able to upload multiple policies, so add the
test infrastructure for it.

Convert kdbus_hello_registrar() to allow an array of
'struct kdbus_policy_entry' and add the following helpers:

kdbus_policy_entries_size() to calculate the KDBUS_ITEM policies size

kdbus_policy_make_item_name() to make a policy item composed of:
  KDBUS_ITEM_NAME + (num_access * KDBUS_ITEM_POLICY_ACCESS)

kdbus_policy_make_entries() a helper to construct multiple policies

Signed-off-by: Djalal Harouni <tixxdz at opendz.org>
---
 test/kdbus-util.c | 99 +++++++++++++++++++++++++++++++++++++++----------------
 test/kdbus-util.h |  6 ++--
 2 files changed, 73 insertions(+), 32 deletions(-)

diff --git a/test/kdbus-util.c b/test/kdbus-util.c
index 6bb3bbf..1ce6050 100644
--- a/test/kdbus-util.c
+++ b/test/kdbus-util.c
@@ -111,22 +111,34 @@ struct conn *kdbus_hello(const char *path, uint64_t flags)
 	return __kdbus_hello(path, flags, NULL, 0);
 }
 
-struct conn *
-kdbus_hello_registrar(const char *path, const char *name,
-		      const struct kdbus_policy_access *access,
-		      size_t num_access, uint64_t flags)
+static size_t kdbus_policy_entries_size(const struct kdbus_policy_entry *entries,
+					size_t num_entries)
 {
-	struct kdbus_item *item, *items;
-	size_t i, size;
+	size_t i;
+	size_t size = 0;
 
-	size = KDBUS_ITEM_SIZE(strlen(name) + 1)
-		+ num_access * KDBUS_ITEM_SIZE(sizeof(struct kdbus_policy_access));
+	for (i = 0; i < num_entries; i++) {
+		const struct kdbus_policy_entry *e = entries + i;
 
-	items = alloca(size);
+		size += KDBUS_ITEM_SIZE(strlen(e->name) + 1);
+		size += e->num_access * KDBUS_ITEM_SIZE(sizeof(struct kdbus_policy_access));
+	}
+
+	return size;
+}
+
+/* Make sure that the 'item' parameter points to a valid item. */
+static struct kdbus_item *
+kdbus_policy_make_item_name(const char *name,
+			    const struct kdbus_policy_access *access,
+			    size_t num_access,
+			    struct kdbus_item *item)
+{
+	size_t i;
 
-	item = items;
-	item->size = KDBUS_ITEM_HEADER_SIZE + strlen(name) + 1;
 	item->type = KDBUS_ITEM_NAME;
+	item->size = KDBUS_ITEM_HEADER_SIZE + strlen(name) + 1;
+
 	strcpy(item->str, name);
 	item = KDBUS_ITEM_NEXT(item);
 
@@ -142,6 +154,44 @@ kdbus_hello_registrar(const char *path, const char *name,
 		item = KDBUS_ITEM_NEXT(item);
 	}
 
+	return item;
+}
+
+/* Make sure that the 'item' parameter points to a valid item. */
+static struct kdbus_item *
+kdbus_policy_make_entries(const struct kdbus_policy_entry *entries,
+			  size_t num_entries,
+			  struct kdbus_item *item)
+{
+	size_t i;
+
+	for (i = 0; i < num_entries; i++) {
+		const struct kdbus_policy_entry *e = &entries[i];
+
+		item = kdbus_policy_make_item_name(e->name, e->access,
+						   e->num_access, item);
+	}
+
+	return item;
+}
+
+struct conn *
+kdbus_hello_registrar(const char *path,
+		      const struct kdbus_policy_entry *entries,
+		      size_t num_entries, uint64_t flags)
+{
+	size_t size;
+	struct kdbus_item *item, *items;
+
+	size = kdbus_policy_entries_size(entries, num_entries);
+	items = alloca(size);
+
+	memset(items, 0, size);
+
+	item = items;
+	item = kdbus_policy_make_entries(entries,
+					 num_entries, item);
+
 	return __kdbus_hello(path, flags, items, size);
 }
 
@@ -149,7 +199,13 @@ struct conn *kdbus_hello_activator(const char *path, const char *name,
 				   const struct kdbus_policy_access *access,
 				   size_t num_access)
 {
-	return kdbus_hello_registrar(path, name, access, num_access,
+	struct kdbus_policy_entry entry = {
+		.name = (char *)name,
+		.num_access = num_access,
+		.access = (struct kdbus_policy_access *)access,
+	};
+
+	return kdbus_hello_registrar(path, &entry, 1,
 				     KDBUS_HELLO_ACTIVATOR);
 }
 
@@ -613,7 +669,7 @@ int conn_update(struct conn *conn, const char *name,
 {
 	struct kdbus_cmd_update *update;
 	struct kdbus_item *item;
-	size_t i, size;
+	size_t size;
 	int ret;
 
 	size = sizeof(struct kdbus_cmd_update);
@@ -652,22 +708,7 @@ int conn_update(struct conn *conn, const char *name,
 					  KDBUS_ATTACH_CONN_NAME;
 	item = KDBUS_ITEM_NEXT(item);
 
-	item->type = KDBUS_ITEM_NAME;
-	item->size = KDBUS_ITEM_HEADER_SIZE + strlen(name) + 1;
-	strcpy(item->str, name);
-	item = KDBUS_ITEM_NEXT(item);
-
-	for (i = 0; i < num_access; i++) {
-		item->size = KDBUS_ITEM_HEADER_SIZE +
-			     sizeof(struct kdbus_policy_access);
-		item->type = KDBUS_ITEM_POLICY_ACCESS;
-
-		item->policy_access.type = access[i].type;
-		item->policy_access.access = access[i].access;
-		item->policy_access.id = access[i].id;
-
-		item = KDBUS_ITEM_NEXT(item);
-	}
+	item = kdbus_policy_make_item_name(name, access, num_access, item);
 
 	ret = ioctl(conn->fd, KDBUS_CMD_CONN_UPDATE, update);
 	if (ret < 0) {
diff --git a/test/kdbus-util.h b/test/kdbus-util.h
index 39d7bb5..110ca48 100644
--- a/test/kdbus-util.h
+++ b/test/kdbus-util.h
@@ -51,9 +51,9 @@ char *msg_id(uint64_t id, char *buf);
 int msg_send(const struct conn *conn, const char *name, uint64_t cookie,
 	     uint64_t flags, uint64_t timeout, int64_t priority, uint64_t dst_id);
 struct conn *kdbus_hello(const char *path, uint64_t hello_flags);
-struct conn *kdbus_hello_registrar(const char *path, const char *name,
-				   const struct kdbus_policy_access *access,
-				   size_t num_access, uint64_t flags);
+struct conn *kdbus_hello_registrar(const char *path,
+				   const struct kdbus_policy_entry *entries,
+				   size_t num_entries, uint64_t flags);
 struct conn *kdbus_hello_activator(const char *path, const char *name,
 				   const struct kdbus_policy_access *access,
 				   size_t num_access);
-- 
1.9.3



More information about the systemd-devel mailing list