summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kdbus/test-names.c
blob: e400dc86a2f5f9f9493e5d4dd1c7e138a35cf82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <getopt.h>
#include <stdbool.h>

#include "kdbus-api.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"
#include "kdbus-test.h"

struct test_name {
	const char *name;
	__u64 owner_id;
	__u64 flags;
};

static bool conn_test_names(const struct kdbus_conn *conn,
			    const struct test_name *tests,
			    unsigned int n_tests)
{
	struct kdbus_cmd_list cmd_list = {};
	struct kdbus_info *name, *list;
	unsigned int i;
	int ret;

	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);

	list = (struct kdbus_info *)(conn->buf + cmd_list.offset);

	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;
	}

	return true;
}

static 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)
{
	struct kdbus_conn *conn;
	char *name, *dot_name, *invalid_name, *wildcard_name;
	int ret;

	name = "foo.bla.blaz";
	dot_name = ".bla.blaz";
	invalid_name = "foo";
	wildcard_name = "foo.bla.bl.*";

	/* create a 2nd connection */
	conn = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn != NULL);

	/* acquire name "foo.bar.xxx" name */
	ret = kdbus_name_acquire(conn, "foo.bar.xxx", NULL);
	ASSERT_RETURN(ret == 0);

	/* Name is not valid, must fail */
	ret = kdbus_name_acquire(env->conn, dot_name, NULL);
	ASSERT_RETURN(ret == -EINVAL);

	ret = kdbus_name_acquire(env->conn, invalid_name, NULL);
	ASSERT_RETURN(ret == -EINVAL);

	ret = kdbus_name_acquire(env->conn, wildcard_name, NULL);
	ASSERT_RETURN(ret == -EINVAL);

	/* check that we can acquire a name */
	ret = kdbus_name_acquire(env->conn, name, NULL);
	ASSERT_RETURN(ret == 0);

	ret = conn_is_name_primary_owner(env->conn, name);
	ASSERT_RETURN(ret == true);

	/* ... and release it again */
	ret = kdbus_name_release(env->conn, name);
	ASSERT_RETURN(ret == 0);

	ret = conn_is_name_primary_owner(env->conn, name);
	ASSERT_RETURN(ret == false);

	/* check that we can't release it again */
	ret = kdbus_name_release(env->conn, name);
	ASSERT_RETURN(ret == -ESRCH);

	/* 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);

	/* Name is not valid, must fail */
	ret = kdbus_name_release(env->conn, dot_name);
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_name_release(env->conn, invalid_name);
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_name_release(env->conn, wildcard_name);
	ASSERT_RETURN(ret == -ESRCH);

	kdbus_conn_free(conn);

	return TEST_OK;
}

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);

	/* 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_primary_owner(env->conn, name);
	ASSERT_RETURN(ret == true);

	/* check that we also can't acquire it again from the 2nd connection */
	ret = kdbus_name_acquire(conn, name, NULL);
	ASSERT_RETURN(ret == -EEXIST);

	kdbus_conn_free(conn);

	return TEST_OK;
}

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 = 0;

	/* create a 2nd connection */
	conn = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn != NULL);

	/* 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);

	ret = conn_is_name_primary_owner(env->conn, name);
	ASSERT_RETURN(ret == true);

	/* 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);

	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;
	ret = conn_test_names(conn, t, 2);
	ASSERT_RETURN(ret == true);

	/* release name from 1st connection */
	ret = kdbus_name_release(env->conn, name);
	ASSERT_RETURN(ret == 0);

	/* now the name should be owned by the 2nd connection */
	t[0].name = name;
	t[0].owner_id = conn->id;
	t[0].flags = KDBUS_NAME_PRIMARY | KDBUS_NAME_QUEUE;
	ret = conn_test_names(conn, t, 1);
	ASSERT_RETURN(ret == true);

	kdbus_conn_free(conn);

	return TEST_OK;
}

int kdbus_test_name_takeover(struct kdbus_test_env *env)
{
	struct kdbus_conn *conn;
	struct test_name t;
	const char *name;
	uint64_t flags;
	int ret;

	name = "foo.bla.blaz";

	flags = KDBUS_NAME_ALLOW_REPLACEMENT;

	/* create a 2nd connection */
	conn = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn != NULL);

	/* acquire name for 1st connection */
	ret = kdbus_name_acquire(env->conn, name, &flags);
	ASSERT_RETURN(ret == 0);

	t.name = name;
	t.owner_id = env->conn->id;
	t.flags = KDBUS_NAME_ALLOW_REPLACEMENT | KDBUS_NAME_PRIMARY;
	ret = conn_test_names(conn, &t, 1);
	ASSERT_RETURN(ret == true);

	/* now steal name with 2nd connection */
	flags = KDBUS_NAME_REPLACE_EXISTING;
	ret = kdbus_name_acquire(conn, name, &flags);
	ASSERT_RETURN(ret == 0);
	ASSERT_RETURN(flags & KDBUS_NAME_ACQUIRED);

	ret = conn_is_name_primary_owner(conn, name);
	ASSERT_RETURN(ret == true);

	kdbus_conn_free(conn);

	return TEST_OK;
}