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
|
#include <stdio.h>
#include <string.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 <sys/mman.h>
#include <stdbool.h>
#include "kdbus-api.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"
#include "kdbus-test.h"
static struct kdbus_item *kdbus_get_item(struct kdbus_info *info,
uint64_t type)
{
struct kdbus_item *item;
KDBUS_ITEM_FOREACH(item, info, items)
if (item->type == type)
return item;
return NULL;
}
static int test_bus_creator_info(const char *bus_path)
{
int ret;
uint64_t offset;
struct kdbus_conn *conn;
struct kdbus_info *info;
struct kdbus_item *item;
char *tmp, *busname;
/* extract the bus-name from @bus_path */
tmp = strdup(bus_path);
ASSERT_RETURN(tmp);
busname = strrchr(tmp, '/');
ASSERT_RETURN(busname);
*busname = 0;
busname = strrchr(tmp, '/');
ASSERT_RETURN(busname);
++busname;
conn = kdbus_hello(bus_path, 0, NULL, 0);
ASSERT_RETURN(conn);
ret = kdbus_bus_creator_info(conn, _KDBUS_ATTACH_ALL, &offset);
ASSERT_RETURN(ret == 0);
info = (struct kdbus_info *)(conn->buf + offset);
item = kdbus_get_item(info, KDBUS_ITEM_MAKE_NAME);
ASSERT_RETURN(item);
ASSERT_RETURN(!strcmp(item->str, busname));
ret = kdbus_free(conn, offset);
ASSERT_RETURN_VAL(ret == 0, ret);
free(tmp);
kdbus_conn_free(conn);
return 0;
}
int kdbus_test_bus_make(struct kdbus_test_env *env)
{
struct {
struct kdbus_cmd cmd;
/* bloom size item */
struct {
uint64_t size;
uint64_t type;
struct kdbus_bloom_parameter bloom;
} bs;
/* name item */
uint64_t n_size;
uint64_t n_type;
char name[64];
} bus_make;
char s[PATH_MAX], *name;
int ret, control_fd2;
uid_t uid;
name = unique_name("");
ASSERT_RETURN(name);
snprintf(s, sizeof(s), "%s/control", env->root);
env->control_fd = open(s, O_RDWR|O_CLOEXEC);
ASSERT_RETURN(env->control_fd >= 0);
control_fd2 = open(s, O_RDWR|O_CLOEXEC);
ASSERT_RETURN(control_fd2 >= 0);
memset(&bus_make, 0, sizeof(bus_make));
bus_make.bs.size = sizeof(bus_make.bs);
bus_make.bs.type = KDBUS_ITEM_BLOOM_PARAMETER;
bus_make.bs.bloom.size = 64;
bus_make.bs.bloom.n_hash = 1;
bus_make.n_type = KDBUS_ITEM_MAKE_NAME;
uid = getuid();
/* missing uid prefix */
snprintf(bus_make.name, sizeof(bus_make.name), "foo");
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(env->control_fd, &bus_make.cmd);
ASSERT_RETURN(ret == -EINVAL);
/* non alphanumeric character */
snprintf(bus_make.name, sizeof(bus_make.name), "%u-blah@123", uid);
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(env->control_fd, &bus_make.cmd);
ASSERT_RETURN(ret == -EINVAL);
/* '-' at the end */
snprintf(bus_make.name, sizeof(bus_make.name), "%u-blah-", uid);
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(env->control_fd, &bus_make.cmd);
ASSERT_RETURN(ret == -EINVAL);
/* create a new bus */
snprintf(bus_make.name, sizeof(bus_make.name), "%u-%s-1", uid, name);
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(env->control_fd, &bus_make.cmd);
ASSERT_RETURN(ret == 0);
ret = kdbus_cmd_bus_make(control_fd2, &bus_make.cmd);
ASSERT_RETURN(ret == -EEXIST);
snprintf(s, sizeof(s), "%s/%u-%s-1/bus", env->root, uid, name);
ASSERT_RETURN(access(s, F_OK) == 0);
ret = test_bus_creator_info(s);
ASSERT_RETURN(ret == 0);
/* can't use the same fd for bus make twice, even though a different
* bus name is used
*/
snprintf(bus_make.name, sizeof(bus_make.name), "%u-%s-2", uid, name);
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(env->control_fd, &bus_make.cmd);
ASSERT_RETURN(ret == -EBADFD);
/* create a new bus, with different fd and different bus name */
snprintf(bus_make.name, sizeof(bus_make.name), "%u-%s-2", uid, name);
bus_make.n_size = KDBUS_ITEM_HEADER_SIZE + strlen(bus_make.name) + 1;
bus_make.cmd.size = sizeof(struct kdbus_cmd) +
sizeof(bus_make.bs) + bus_make.n_size;
ret = kdbus_cmd_bus_make(control_fd2, &bus_make.cmd);
ASSERT_RETURN(ret == 0);
close(control_fd2);
free(name);
return TEST_OK;
}
|