#include "kdbus-enum.h"
#include "kdbus-test.h"
-static int conn_is_name_owner(const struct kdbus_conn *conn,
- const char *needle)
+struct test_name {
+ const char *name;
+ __u64 owner_id;
+ __u64 flags;
+};
+
+static wur bool conn_test_names(const struct kdbus_conn *conn,
+ const struct test_name *tests,
+ unsigned int n_tests)
{
- struct kdbus_cmd_list cmd_list = { .size = sizeof(cmd_list) };
+ struct kdbus_cmd_list cmd_list = {};
struct kdbus_info *name, *list;
- bool found = false;
- int ret;
+ unsigned int i;
- cmd_list.flags = KDBUS_LIST_NAMES;
+ cmd_list.size = sizeof(cmd_list);
+ cmd_list.flags = KDBUS_LIST_NAMES |
+ KDBUS_LIST_ACTIVATORS |
+ KDBUS_LIST_QUEUED;
- ret = kdbus_cmd_list(conn->fd, &cmd_list);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_cmd_list(conn->fd, &cmd_list));
list = (struct kdbus_info *)(conn->buf + cmd_list.offset);
- KDBUS_FOREACH(name, list, cmd_list.list_size) {
- struct kdbus_item *item;
- const char *n = NULL;
-
- KDBUS_ITEM_FOREACH(item, name, items)
- if (item->type == KDBUS_ITEM_OWNED_NAME)
- n = item->name.name;
-
- if (name->id == conn->id &&
- n && strcmp(needle, n) == 0) {
- found = true;
- break;
+
+ for (i = 0; i < n_tests; i++) {
+ const struct test_name *t = tests + i;
+ bool found = false;
+
+ KDBUS_FOREACH(name, list, cmd_list.list_size) {
+ struct kdbus_item *item;
+
+ KDBUS_ITEM_FOREACH(item, name, items) {
+ if (item->type != KDBUS_ITEM_OWNED_NAME ||
+ strcmp(item->name.name, t->name) != 0)
+ continue;
+
+ if (t->owner_id == name->id &&
+ t->flags == item->name.flags) {
+ found = true;
+ break;
+ }
+ }
}
+
+ if (!found)
+ return false;
}
- ret = kdbus_free(conn, cmd_list.offset);
- ASSERT_RETURN(ret == 0);
+ return true;
+}
- return found ? 0 : -1;
+static wur bool conn_is_name_primary_owner(const struct kdbus_conn *conn,
+ const char *needle)
+{
+ struct test_name t = {
+ .name = needle,
+ .owner_id = conn->id,
+ .flags = KDBUS_NAME_PRIMARY,
+ };
+
+ return conn_test_names(conn, &t, 1);
}
-int kdbus_test_name_basic(struct kdbus_test_env *env)
+wur int kdbus_test_name_basic(struct kdbus_test_env *env)
{
struct kdbus_conn *conn;
char *name, *dot_name, *invalid_name, *wildcard_name;
- int ret;
name = "foo.bla.blaz";
dot_name = ".bla.blaz";
wildcard_name = "foo.bla.bl.*";
/* create a 2nd connection */
- conn = kdbus_hello(env->buspath, 0, NULL, 0);
- ASSERT_RETURN(conn != NULL);
+ ASSERT_NONZERO(conn = kdbus_hello(env->buspath, 0, NULL, 0));
/* acquire name "foo.bar.xxx" name */
- ret = kdbus_name_acquire(conn, "foo.bar.xxx", NULL);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_acquire(conn, "foo.bar.xxx", NULL));
/* Name is not valid, must fail */
- ret = kdbus_name_acquire(env->conn, dot_name, NULL);
- ASSERT_RETURN(ret == -EINVAL);
+ ASSERT_RETURN(-EINVAL,==,kdbus_name_acquire(env->conn, dot_name, NULL));
- ret = kdbus_name_acquire(env->conn, invalid_name, NULL);
- ASSERT_RETURN(ret == -EINVAL);
+ ASSERT_RETURN(-EINVAL,==,kdbus_name_acquire(env->conn, invalid_name, NULL));
- ret = kdbus_name_acquire(env->conn, wildcard_name, NULL);
- ASSERT_RETURN(ret == -EINVAL);
+ ASSERT_RETURN(-EINVAL,==,kdbus_name_acquire(env->conn, wildcard_name, NULL));
/* check that we can acquire a name */
- ret = kdbus_name_acquire(env->conn, name, NULL);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_acquire(env->conn, name, NULL));
- ret = conn_is_name_owner(env->conn, name);
- ASSERT_RETURN(ret == 0);
+ ASSERT_RETURN(true,==,conn_is_name_primary_owner(env->conn, name));
/* ... and release it again */
- ret = kdbus_name_release(env->conn, name);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_release(env->conn, name));
- ret = conn_is_name_owner(env->conn, name);
- ASSERT_RETURN(ret != 0);
+ ASSERT_RETURN(false,==,conn_is_name_primary_owner(env->conn, name));
/* check that we can't release it again */
- ret = kdbus_name_release(env->conn, name);
- ASSERT_RETURN(ret == -ESRCH);
+ ASSERT_RETURN(-ESRCH,==,kdbus_name_release(env->conn, name));
/* check that we can't release a name that we don't own */
- ret = kdbus_name_release(env->conn, "foo.bar.xxx");
- ASSERT_RETURN(ret == -EADDRINUSE);
+ ASSERT_RETURN(-EADDRINUSE,==,kdbus_name_release(env->conn, "foo.bar.xxx"));
/* Name is not valid, must fail */
- ret = kdbus_name_release(env->conn, dot_name);
- ASSERT_RETURN(ret == -ESRCH);
+ ASSERT_RETURN(-ESRCH,==,kdbus_name_release(env->conn, dot_name));
- ret = kdbus_name_release(env->conn, invalid_name);
- ASSERT_RETURN(ret == -ESRCH);
+ ASSERT_RETURN(-ESRCH,==,kdbus_name_release(env->conn, invalid_name));
- ret = kdbus_name_release(env->conn, wildcard_name);
- ASSERT_RETURN(ret == -ESRCH);
+ ASSERT_RETURN(-ESRCH,==,kdbus_name_release(env->conn, wildcard_name));
kdbus_conn_free(conn);
return TEST_OK;
}
-int kdbus_test_name_conflict(struct kdbus_test_env *env)
+wur int kdbus_test_name_conflict(struct kdbus_test_env *env)
{
struct kdbus_conn *conn;
char *name;
- int ret;
name = "foo.bla.blaz";
/* create a 2nd connection */
- conn = kdbus_hello(env->buspath, 0, NULL, 0);
- ASSERT_RETURN(conn != NULL);
+ ASSERT_NONZERO(conn = kdbus_hello(env->buspath, 0, NULL, 0));
/* allow the new connection to own the same name */
/* acquire name from the 1st connection */
- ret = kdbus_name_acquire(env->conn, name, NULL);
- ASSERT_RETURN(ret == 0);
-
- ret = conn_is_name_owner(env->conn, name);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_acquire(env->conn, name, NULL));
- /* check that we can't acquire it again from the 1st connection */
- ret = kdbus_name_acquire(env->conn, name, NULL);
- ASSERT_RETURN(ret == -EALREADY);
+ ASSERT_RETURN(true,==,conn_is_name_primary_owner(env->conn, name));
/* check that we also can't acquire it again from the 2nd connection */
- ret = kdbus_name_acquire(conn, name, NULL);
- ASSERT_RETURN(ret == -EEXIST);
+ ASSERT_RETURN(-EEXIST,==,kdbus_name_acquire(conn, name, NULL));
kdbus_conn_free(conn);
return TEST_OK;
}
-int kdbus_test_name_queue(struct kdbus_test_env *env)
+wur int kdbus_test_name_queue(struct kdbus_test_env *env)
{
struct kdbus_conn *conn;
+ struct test_name t[2];
const char *name;
uint64_t flags;
- int ret;
name = "foo.bla.blaz";
- flags = KDBUS_NAME_ALLOW_REPLACEMENT;
+ flags = 0;
/* create a 2nd connection */
- conn = kdbus_hello(env->buspath, 0, NULL, 0);
- ASSERT_RETURN(conn != NULL);
+ ASSERT_NONZERO(conn = kdbus_hello(env->buspath, 0, NULL, 0));
/* allow the new connection to own the same name */
/* acquire name from the 1st connection */
- ret = kdbus_name_acquire(env->conn, name, &flags);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_acquire(env->conn, name, &flags));
- ret = conn_is_name_owner(env->conn, name);
- ASSERT_RETURN(ret == 0);
+ ASSERT_RETURN(true,==,conn_is_name_primary_owner(env->conn, name));
/* queue the 2nd connection as waiting owner */
flags = KDBUS_NAME_QUEUE;
- ret = kdbus_name_acquire(conn, name, &flags);
- ASSERT_RETURN(ret == 0);
- ASSERT_RETURN(flags & KDBUS_NAME_IN_QUEUE);
+ ASSERT_ZERO(kdbus_name_acquire(conn, name, &flags));
+ ASSERT_NONZERO(flags & KDBUS_NAME_IN_QUEUE);
+
+ t[0].name = name;
+ t[0].owner_id = env->conn->id;
+ t[0].flags = KDBUS_NAME_PRIMARY;
+ t[1].name = name;
+ t[1].owner_id = conn->id;
+ t[1].flags = KDBUS_NAME_QUEUE | KDBUS_NAME_IN_QUEUE;
+ ASSERT_RETURN(true,==,conn_test_names(conn, t, 2));
/* release name from 1st connection */
- ret = kdbus_name_release(env->conn, name);
- ASSERT_RETURN(ret == 0);
+ ASSERT_ZERO(kdbus_name_release(env->conn, name));
/* now the name should be owned by the 2nd connection */
- ret = conn_is_name_owner(conn, name);
- ASSERT_RETURN(ret == 0);
+ t[0].name = name;
+ t[0].owner_id = conn->id;
+ t[0].flags = KDBUS_NAME_PRIMARY | KDBUS_NAME_QUEUE;
+ ASSERT_RETURN(true,==,conn_test_names(conn, t, 1));
+
+ kdbus_conn_free(conn);
+
+ return TEST_OK;
+}
+
+wur int kdbus_test_name_takeover(struct kdbus_test_env *env)
+{
+ struct kdbus_conn *conn;
+ struct test_name t;
+ const char *name;
+ uint64_t flags;
+
+ name = "foo.bla.blaz";
+
+ flags = KDBUS_NAME_ALLOW_REPLACEMENT;
+
+ /* create a 2nd connection */
+ ASSERT_NONZERO(conn = kdbus_hello(env->buspath, 0, NULL, 0));
+
+ /* acquire name for 1st connection */
+ ASSERT_ZERO(kdbus_name_acquire(env->conn, name, &flags));
+
+ t.name = name;
+ t.owner_id = env->conn->id;
+ t.flags = KDBUS_NAME_ALLOW_REPLACEMENT | KDBUS_NAME_PRIMARY;
+ ASSERT_RETURN(true,==,conn_test_names(conn, &t, 1));
+
+ /* now steal name with 2nd connection */
+ flags = KDBUS_NAME_REPLACE_EXISTING;
+ ASSERT_ZERO(kdbus_name_acquire(conn, name, &flags));
+ ASSERT_NONZERO(flags & KDBUS_NAME_ACQUIRED);
+
+ ASSERT_RETURN(true,==,conn_is_name_primary_owner(conn, name));
kdbus_conn_free(conn);