]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 | 2 | |
6e866b33 | 3 | #include <errno.h> |
663996b3 | 4 | #include <getopt.h> |
db2df898 | 5 | #include <stdio.h> |
663996b3 | 6 | #include <sys/epoll.h> |
db2df898 | 7 | #include <unistd.h> |
663996b3 | 8 | |
6e866b33 | 9 | #include "alloc-util.h" |
7c20daf6 | 10 | #include "build.h" |
aa27b158 | 11 | #include "fd-util.h" |
6e866b33 MB |
12 | #include "libudev-list-internal.h" |
13 | #include "libudev-util.h" | |
aa27b158 | 14 | #include "log.h" |
bb4f798a | 15 | #include "main-func.h" |
4c89c718 | 16 | #include "stdio-util.h" |
db2df898 | 17 | #include "string-util.h" |
bb4f798a MB |
18 | #include "tests.h" |
19 | ||
20 | static bool arg_monitor = false; | |
663996b3 | 21 | |
60f067b4 | 22 | static void print_device(struct udev_device *device) { |
663996b3 MS |
23 | const char *str; |
24 | dev_t devnum; | |
25 | int count; | |
26 | struct udev_list_entry *list_entry; | |
27 | ||
aa27b158 | 28 | log_info("*** device: %p ***", device); |
663996b3 | 29 | str = udev_device_get_action(device); |
bb4f798a | 30 | if (str) |
aa27b158 | 31 | log_info("action: '%s'", str); |
663996b3 MS |
32 | |
33 | str = udev_device_get_syspath(device); | |
aa27b158 | 34 | log_info("syspath: '%s'", str); |
663996b3 MS |
35 | |
36 | str = udev_device_get_sysname(device); | |
aa27b158 | 37 | log_info("sysname: '%s'", str); |
663996b3 MS |
38 | |
39 | str = udev_device_get_sysnum(device); | |
bb4f798a | 40 | if (str) |
aa27b158 | 41 | log_info("sysnum: '%s'", str); |
663996b3 MS |
42 | |
43 | str = udev_device_get_devpath(device); | |
aa27b158 | 44 | log_info("devpath: '%s'", str); |
663996b3 MS |
45 | |
46 | str = udev_device_get_subsystem(device); | |
bb4f798a | 47 | if (str) |
aa27b158 | 48 | log_info("subsystem: '%s'", str); |
663996b3 MS |
49 | |
50 | str = udev_device_get_devtype(device); | |
bb4f798a | 51 | if (str) |
aa27b158 | 52 | log_info("devtype: '%s'", str); |
663996b3 MS |
53 | |
54 | str = udev_device_get_driver(device); | |
bb4f798a | 55 | if (str) |
aa27b158 | 56 | log_info("driver: '%s'", str); |
663996b3 MS |
57 | |
58 | str = udev_device_get_devnode(device); | |
bb4f798a | 59 | if (str) |
aa27b158 | 60 | log_info("devname: '%s'", str); |
663996b3 MS |
61 | |
62 | devnum = udev_device_get_devnum(device); | |
63 | if (major(devnum) > 0) | |
aa27b158 | 64 | log_info("devnum: %u:%u", major(devnum), minor(devnum)); |
663996b3 MS |
65 | |
66 | count = 0; | |
67 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { | |
aa27b158 | 68 | log_info("link: '%s'", udev_list_entry_get_name(list_entry)); |
663996b3 MS |
69 | count++; |
70 | } | |
71 | if (count > 0) | |
aa27b158 | 72 | log_info("found %i links", count); |
663996b3 MS |
73 | |
74 | count = 0; | |
75 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) { | |
aa27b158 | 76 | log_info("property: '%s=%s'", |
663996b3 MS |
77 | udev_list_entry_get_name(list_entry), |
78 | udev_list_entry_get_value(list_entry)); | |
79 | count++; | |
80 | } | |
81 | if (count > 0) | |
aa27b158 | 82 | log_info("found %i properties", count); |
663996b3 MS |
83 | |
84 | str = udev_device_get_property_value(device, "MAJOR"); | |
bb4f798a | 85 | if (str) |
aa27b158 | 86 | log_info("MAJOR: '%s'", str); |
663996b3 MS |
87 | |
88 | str = udev_device_get_sysattr_value(device, "dev"); | |
bb4f798a | 89 | if (str) |
aa27b158 | 90 | log_info("attr{dev}: '%s'", str); |
663996b3 MS |
91 | } |
92 | ||
aa27b158 | 93 | static void test_device(struct udev *udev, const char *syspath) { |
b012e921 | 94 | _cleanup_(udev_device_unrefp) struct udev_device *device; |
663996b3 | 95 | |
bb4f798a | 96 | log_info("/* %s, device %s */", __func__, syspath); |
663996b3 | 97 | device = udev_device_new_from_syspath(udev, syspath); |
bb4f798a | 98 | if (device) |
aa27b158 | 99 | print_device(device); |
bb4f798a MB |
100 | else |
101 | log_warning_errno(errno, "udev_device_new_from_syspath: %m"); | |
663996b3 MS |
102 | } |
103 | ||
aa27b158 | 104 | static void test_device_parents(struct udev *udev, const char *syspath) { |
b012e921 | 105 | _cleanup_(udev_device_unrefp) struct udev_device *device; |
663996b3 MS |
106 | struct udev_device *device_parent; |
107 | ||
bb4f798a | 108 | log_info("/* %s, device %s */", __func__, syspath); |
663996b3 MS |
109 | device = udev_device_new_from_syspath(udev, syspath); |
110 | if (device == NULL) | |
aa27b158 | 111 | return; |
663996b3 | 112 | |
aa27b158 | 113 | log_info("looking at parents"); |
663996b3 MS |
114 | device_parent = device; |
115 | do { | |
116 | print_device(device_parent); | |
117 | device_parent = udev_device_get_parent(device_parent); | |
118 | } while (device_parent != NULL); | |
119 | ||
aa27b158 | 120 | log_info("looking at parents again"); |
663996b3 MS |
121 | device_parent = device; |
122 | do { | |
123 | print_device(device_parent); | |
124 | device_parent = udev_device_get_parent(device_parent); | |
125 | } while (device_parent != NULL); | |
663996b3 MS |
126 | } |
127 | ||
aa27b158 | 128 | static void test_device_devnum(struct udev *udev) { |
663996b3 | 129 | dev_t devnum = makedev(1, 3); |
b012e921 | 130 | _cleanup_(udev_device_unrefp) struct udev_device *device; |
663996b3 | 131 | |
bb4f798a MB |
132 | log_info("/* %s, device %d:%d */", __func__, major(devnum), minor(devnum)); |
133 | ||
663996b3 | 134 | device = udev_device_new_from_devnum(udev, 'c', devnum); |
bb4f798a | 135 | if (device) |
aa27b158 | 136 | print_device(device); |
bb4f798a MB |
137 | else |
138 | log_warning_errno(errno, "udev_device_new_from_devnum: %m"); | |
663996b3 MS |
139 | } |
140 | ||
aa27b158 | 141 | static void test_device_subsys_name(struct udev *udev, const char *subsys, const char *dev) { |
b012e921 | 142 | _cleanup_(udev_device_unrefp) struct udev_device *device; |
663996b3 | 143 | |
aa27b158 MP |
144 | log_info("looking up device: '%s:%s'", subsys, dev); |
145 | device = udev_device_new_from_subsystem_sysname(udev, subsys, dev); | |
663996b3 | 146 | if (device == NULL) |
aa27b158 MP |
147 | log_warning_errno(errno, "udev_device_new_from_subsystem_sysname: %m"); |
148 | else | |
149 | print_device(device); | |
663996b3 MS |
150 | } |
151 | ||
bb4f798a | 152 | static int enumerate_print_list(struct udev_enumerate *enumerate) { |
663996b3 MS |
153 | struct udev_list_entry *list_entry; |
154 | int count = 0; | |
155 | ||
156 | udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) { | |
157 | struct udev_device *device; | |
158 | ||
159 | device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate), | |
160 | udev_list_entry_get_name(list_entry)); | |
161 | if (device != NULL) { | |
aa27b158 MP |
162 | log_info("device: '%s' (%s)", |
163 | udev_device_get_syspath(device), | |
164 | udev_device_get_subsystem(device)); | |
663996b3 MS |
165 | udev_device_unref(device); |
166 | count++; | |
167 | } | |
168 | } | |
aa27b158 | 169 | log_info("found %i devices", count); |
663996b3 MS |
170 | return count; |
171 | } | |
172 | ||
aa27b158 | 173 | static void test_monitor(struct udev *udev) { |
b012e921 | 174 | _cleanup_(udev_monitor_unrefp) struct udev_monitor *udev_monitor; |
aa27b158 MP |
175 | _cleanup_close_ int fd_ep; |
176 | int fd_udev; | |
177 | struct epoll_event ep_udev = { | |
178 | .events = EPOLLIN, | |
179 | }, ep_stdin = { | |
180 | .events = EPOLLIN, | |
181 | .data.fd = STDIN_FILENO, | |
182 | }; | |
663996b3 | 183 | |
bb4f798a MB |
184 | log_info("/* %s */", __func__); |
185 | ||
663996b3 | 186 | fd_ep = epoll_create1(EPOLL_CLOEXEC); |
aa27b158 | 187 | assert_se(fd_ep >= 0); |
663996b3 MS |
188 | |
189 | udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); | |
aa27b158 MP |
190 | assert_se(udev_monitor != NULL); |
191 | ||
663996b3 | 192 | fd_udev = udev_monitor_get_fd(udev_monitor); |
aa27b158 | 193 | ep_udev.data.fd = fd_udev; |
663996b3 | 194 | |
aa27b158 MP |
195 | assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) >= 0); |
196 | assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) >= 0); | |
197 | assert_se(udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") >= 0); | |
663996b3 | 198 | |
aa27b158 | 199 | assert_se(udev_monitor_enable_receiving(udev_monitor) >= 0); |
663996b3 | 200 | |
aa27b158 MP |
201 | assert_se(epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) >= 0); |
202 | assert_se(epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) >= 0); | |
663996b3 MS |
203 | |
204 | for (;;) { | |
205 | int fdcount; | |
206 | struct epoll_event ev[4]; | |
207 | struct udev_device *device; | |
208 | int i; | |
209 | ||
210 | printf("waiting for events from udev, press ENTER to exit\n"); | |
aa27b158 | 211 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1); |
663996b3 MS |
212 | printf("epoll fd count: %i\n", fdcount); |
213 | ||
214 | for (i = 0; i < fdcount; i++) { | |
215 | if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) { | |
216 | device = udev_monitor_receive_device(udev_monitor); | |
217 | if (device == NULL) { | |
218 | printf("no device from socket\n"); | |
219 | continue; | |
220 | } | |
221 | print_device(device); | |
222 | udev_device_unref(device); | |
223 | } else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) { | |
224 | printf("exiting loop\n"); | |
aa27b158 | 225 | return; |
663996b3 MS |
226 | } |
227 | } | |
228 | } | |
663996b3 MS |
229 | } |
230 | ||
aa27b158 | 231 | static void test_queue(struct udev *udev) { |
663996b3 | 232 | struct udev_queue *udev_queue; |
aa27b158 | 233 | bool empty; |
663996b3 | 234 | |
bb4f798a MB |
235 | log_info("/* %s */", __func__); |
236 | ||
237 | assert_se(udev_queue = udev_queue_new(udev)); | |
663996b3 | 238 | |
aa27b158 MP |
239 | empty = udev_queue_get_queue_is_empty(udev_queue); |
240 | log_info("queue is %s", empty ? "empty" : "not empty"); | |
663996b3 | 241 | udev_queue_unref(udev_queue); |
663996b3 MS |
242 | } |
243 | ||
60f067b4 | 244 | static int test_enumerate(struct udev *udev, const char *subsystem) { |
663996b3 | 245 | struct udev_enumerate *udev_enumerate; |
e3bff60a | 246 | int r; |
663996b3 | 247 | |
bb4f798a MB |
248 | log_info("/* %s */", __func__); |
249 | ||
aa27b158 | 250 | log_info("enumerate '%s'", subsystem == NULL ? "<all>" : subsystem); |
663996b3 MS |
251 | udev_enumerate = udev_enumerate_new(udev); |
252 | if (udev_enumerate == NULL) | |
253 | return -1; | |
254 | udev_enumerate_add_match_subsystem(udev_enumerate, subsystem); | |
255 | udev_enumerate_scan_devices(udev_enumerate); | |
bb4f798a | 256 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
257 | udev_enumerate_unref(udev_enumerate); |
258 | ||
aa27b158 | 259 | log_info("enumerate 'net' + duplicated scan + null + zero"); |
663996b3 MS |
260 | udev_enumerate = udev_enumerate_new(udev); |
261 | if (udev_enumerate == NULL) | |
262 | return -1; | |
263 | udev_enumerate_add_match_subsystem(udev_enumerate, "net"); | |
264 | udev_enumerate_scan_devices(udev_enumerate); | |
265 | udev_enumerate_scan_devices(udev_enumerate); | |
266 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
267 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
268 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
269 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
270 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
271 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
272 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null"); | |
273 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
274 | udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero"); | |
275 | udev_enumerate_scan_devices(udev_enumerate); | |
bb4f798a | 276 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
277 | udev_enumerate_unref(udev_enumerate); |
278 | ||
aa27b158 | 279 | log_info("enumerate 'block'"); |
663996b3 MS |
280 | udev_enumerate = udev_enumerate_new(udev); |
281 | if (udev_enumerate == NULL) | |
282 | return -1; | |
283 | udev_enumerate_add_match_subsystem(udev_enumerate,"block"); | |
e3bff60a MP |
284 | r = udev_enumerate_add_match_is_initialized(udev_enumerate); |
285 | if (r < 0) { | |
286 | udev_enumerate_unref(udev_enumerate); | |
287 | return r; | |
288 | } | |
663996b3 | 289 | udev_enumerate_scan_devices(udev_enumerate); |
bb4f798a | 290 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
291 | udev_enumerate_unref(udev_enumerate); |
292 | ||
aa27b158 | 293 | log_info("enumerate 'not block'"); |
663996b3 MS |
294 | udev_enumerate = udev_enumerate_new(udev); |
295 | if (udev_enumerate == NULL) | |
296 | return -1; | |
297 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); | |
298 | udev_enumerate_scan_devices(udev_enumerate); | |
bb4f798a | 299 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
300 | udev_enumerate_unref(udev_enumerate); |
301 | ||
aa27b158 | 302 | log_info("enumerate 'pci, mem, vc'"); |
663996b3 MS |
303 | udev_enumerate = udev_enumerate_new(udev); |
304 | if (udev_enumerate == NULL) | |
305 | return -1; | |
306 | udev_enumerate_add_match_subsystem(udev_enumerate, "pci"); | |
307 | udev_enumerate_add_match_subsystem(udev_enumerate, "mem"); | |
308 | udev_enumerate_add_match_subsystem(udev_enumerate, "vc"); | |
309 | udev_enumerate_scan_devices(udev_enumerate); | |
bb4f798a | 310 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
311 | udev_enumerate_unref(udev_enumerate); |
312 | ||
aa27b158 | 313 | log_info("enumerate 'subsystem'"); |
663996b3 MS |
314 | udev_enumerate = udev_enumerate_new(udev); |
315 | if (udev_enumerate == NULL) | |
316 | return -1; | |
317 | udev_enumerate_scan_subsystems(udev_enumerate); | |
bb4f798a | 318 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
319 | udev_enumerate_unref(udev_enumerate); |
320 | ||
aa27b158 | 321 | log_info("enumerate 'property IF_FS_*=filesystem'"); |
663996b3 MS |
322 | udev_enumerate = udev_enumerate_new(udev); |
323 | if (udev_enumerate == NULL) | |
324 | return -1; | |
325 | udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem"); | |
326 | udev_enumerate_scan_devices(udev_enumerate); | |
bb4f798a | 327 | enumerate_print_list(udev_enumerate); |
663996b3 MS |
328 | udev_enumerate_unref(udev_enumerate); |
329 | return 0; | |
330 | } | |
331 | ||
60f067b4 | 332 | static void test_hwdb(struct udev *udev, const char *modalias) { |
14228c0d | 333 | struct udev_hwdb *hwdb; |
663996b3 MS |
334 | struct udev_list_entry *entry; |
335 | ||
bb4f798a MB |
336 | log_info("/* %s */", __func__); |
337 | ||
663996b3 | 338 | hwdb = udev_hwdb_new(udev); |
bb4f798a MB |
339 | if (!hwdb) |
340 | log_warning_errno(errno, "Failed to open hwdb: %m"); | |
663996b3 MS |
341 | |
342 | udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) | |
aa27b158 | 343 | log_info("'%s'='%s'", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); |
663996b3 MS |
344 | |
345 | hwdb = udev_hwdb_unref(hwdb); | |
5eef597e | 346 | assert_se(hwdb == NULL); |
663996b3 MS |
347 | } |
348 | ||
6e866b33 MB |
349 | static void test_util_replace_whitespace_one_len(const char *str, size_t len, const char *expected) { |
350 | _cleanup_free_ char *result = NULL; | |
351 | int r; | |
352 | ||
353 | result = new(char, len + 1); | |
354 | assert_se(result); | |
355 | r = util_replace_whitespace(str, result, len); | |
356 | assert_se((size_t) r == strlen(expected)); | |
357 | assert_se(streq(result, expected)); | |
358 | } | |
359 | ||
360 | static void test_util_replace_whitespace_one(const char *str, const char *expected) { | |
361 | test_util_replace_whitespace_one_len(str, strlen(str), expected); | |
362 | } | |
363 | ||
364 | static void test_util_replace_whitespace(void) { | |
bb4f798a MB |
365 | log_info("/* %s */", __func__); |
366 | ||
6e866b33 MB |
367 | test_util_replace_whitespace_one("hogehoge", "hogehoge"); |
368 | test_util_replace_whitespace_one("hoge hoge", "hoge_hoge"); | |
369 | test_util_replace_whitespace_one(" hoge hoge ", "hoge_hoge"); | |
370 | test_util_replace_whitespace_one(" ", ""); | |
371 | test_util_replace_whitespace_one("hoge ", "hoge"); | |
372 | ||
373 | test_util_replace_whitespace_one_len("hoge hoge ", 9, "hoge_hoge"); | |
374 | test_util_replace_whitespace_one_len("hoge hoge ", 8, "hoge_hog"); | |
375 | test_util_replace_whitespace_one_len("hoge hoge ", 7, "hoge_ho"); | |
376 | test_util_replace_whitespace_one_len("hoge hoge ", 6, "hoge_h"); | |
377 | test_util_replace_whitespace_one_len("hoge hoge ", 5, "hoge"); | |
378 | test_util_replace_whitespace_one_len("hoge hoge ", 4, "hoge"); | |
379 | test_util_replace_whitespace_one_len("hoge hoge ", 3, "hog"); | |
380 | test_util_replace_whitespace_one_len("hoge hoge ", 2, "ho"); | |
381 | test_util_replace_whitespace_one_len("hoge hoge ", 1, "h"); | |
382 | test_util_replace_whitespace_one_len("hoge hoge ", 0, ""); | |
383 | ||
7c20daf6 FS |
384 | test_util_replace_whitespace_one_len(" hoge hoge ", 16, "hoge_hoge"); |
385 | test_util_replace_whitespace_one_len(" hoge hoge ", 15, "hoge_hoge"); | |
386 | test_util_replace_whitespace_one_len(" hoge hoge ", 14, "hoge_hog"); | |
387 | test_util_replace_whitespace_one_len(" hoge hoge ", 13, "hoge_ho"); | |
388 | test_util_replace_whitespace_one_len(" hoge hoge ", 12, "hoge_h"); | |
389 | test_util_replace_whitespace_one_len(" hoge hoge ", 11, "hoge"); | |
390 | test_util_replace_whitespace_one_len(" hoge hoge ", 10, "hoge"); | |
391 | test_util_replace_whitespace_one_len(" hoge hoge ", 9, "hoge"); | |
392 | test_util_replace_whitespace_one_len(" hoge hoge ", 8, "hoge"); | |
393 | test_util_replace_whitespace_one_len(" hoge hoge ", 7, "hog"); | |
394 | test_util_replace_whitespace_one_len(" hoge hoge ", 6, "ho"); | |
395 | test_util_replace_whitespace_one_len(" hoge hoge ", 5, "h"); | |
396 | test_util_replace_whitespace_one_len(" hoge hoge ", 4, ""); | |
397 | test_util_replace_whitespace_one_len(" hoge hoge ", 3, ""); | |
398 | test_util_replace_whitespace_one_len(" hoge hoge ", 2, ""); | |
399 | test_util_replace_whitespace_one_len(" hoge hoge ", 1, ""); | |
400 | test_util_replace_whitespace_one_len(" hoge hoge ", 0, ""); | |
6e866b33 MB |
401 | } |
402 | ||
403 | static void test_util_resolve_subsys_kernel_one(const char *str, bool read_value, int retval, const char *expected) { | |
bb4f798a | 404 | char result[UTIL_PATH_SIZE] = ""; |
6e866b33 MB |
405 | int r; |
406 | ||
407 | r = util_resolve_subsys_kernel(str, result, sizeof(result), read_value); | |
bb4f798a | 408 | log_info("\"%s\" → expect: \"%s\", %d, actual: \"%s\", %d", str, strnull(expected), retval, result, r); |
6e866b33 MB |
409 | assert_se(r == retval); |
410 | if (r >= 0) | |
411 | assert_se(streq(result, expected)); | |
412 | } | |
413 | ||
414 | static void test_util_resolve_subsys_kernel(void) { | |
bb4f798a MB |
415 | log_info("/* %s */", __func__); |
416 | ||
6e866b33 MB |
417 | test_util_resolve_subsys_kernel_one("hoge", false, -EINVAL, NULL); |
418 | test_util_resolve_subsys_kernel_one("[hoge", false, -EINVAL, NULL); | |
419 | test_util_resolve_subsys_kernel_one("[hoge/foo", false, -EINVAL, NULL); | |
420 | test_util_resolve_subsys_kernel_one("[hoge/]", false, -ENODEV, NULL); | |
421 | ||
422 | test_util_resolve_subsys_kernel_one("[net/lo]", false, 0, "/sys/devices/virtual/net/lo"); | |
423 | test_util_resolve_subsys_kernel_one("[net/lo]/", false, 0, "/sys/devices/virtual/net/lo"); | |
424 | test_util_resolve_subsys_kernel_one("[net/lo]hoge", false, 0, "/sys/devices/virtual/net/lo/hoge"); | |
425 | test_util_resolve_subsys_kernel_one("[net/lo]/hoge", false, 0, "/sys/devices/virtual/net/lo/hoge"); | |
426 | ||
427 | test_util_resolve_subsys_kernel_one("[net/lo]", true, -EINVAL, NULL); | |
428 | test_util_resolve_subsys_kernel_one("[net/lo]/", true, -EINVAL, NULL); | |
429 | test_util_resolve_subsys_kernel_one("[net/lo]hoge", true, 0, ""); | |
430 | test_util_resolve_subsys_kernel_one("[net/lo]/hoge", true, 0, ""); | |
431 | test_util_resolve_subsys_kernel_one("[net/lo]address", true, 0, "00:00:00:00:00:00"); | |
432 | test_util_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00"); | |
433 | } | |
434 | ||
435 | static void test_list(void) { | |
436 | struct udev_list list = {}; | |
437 | struct udev_list_entry *e; | |
438 | ||
439 | /* empty list */ | |
440 | udev_list_init(&list, false); | |
441 | assert_se(!udev_list_get_entry(&list)); | |
442 | ||
443 | /* unique == false */ | |
444 | udev_list_init(&list, false); | |
445 | assert_se(udev_list_entry_add(&list, "aaa", "hoge")); | |
446 | assert_se(udev_list_entry_add(&list, "aaa", "hogehoge")); | |
447 | assert_se(udev_list_entry_add(&list, "bbb", "foo")); | |
448 | e = udev_list_get_entry(&list); | |
449 | assert_se(e); | |
450 | assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa")); | |
451 | assert_se(streq_ptr(udev_list_entry_get_value(e), "hoge")); | |
452 | e = udev_list_entry_get_next(e); | |
453 | assert_se(e); | |
454 | assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa")); | |
455 | assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge")); | |
456 | e = udev_list_entry_get_next(e); | |
457 | assert_se(e); | |
458 | assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb")); | |
459 | assert_se(streq_ptr(udev_list_entry_get_value(e), "foo")); | |
460 | assert_se(!udev_list_entry_get_next(e)); | |
461 | ||
462 | assert_se(!udev_list_entry_get_by_name(e, "aaa")); | |
463 | assert_se(!udev_list_entry_get_by_name(e, "bbb")); | |
464 | assert_se(!udev_list_entry_get_by_name(e, "ccc")); | |
465 | udev_list_cleanup(&list); | |
466 | ||
467 | /* unique == true */ | |
468 | udev_list_init(&list, true); | |
469 | assert_se(udev_list_entry_add(&list, "aaa", "hoge")); | |
470 | assert_se(udev_list_entry_add(&list, "aaa", "hogehoge")); | |
471 | assert_se(udev_list_entry_add(&list, "bbb", "foo")); | |
472 | e = udev_list_get_entry(&list); | |
473 | assert_se(e); | |
474 | assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa")); | |
475 | assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge")); | |
476 | e = udev_list_entry_get_next(e); | |
477 | assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb")); | |
478 | assert_se(streq_ptr(udev_list_entry_get_value(e), "foo")); | |
479 | assert_se(!udev_list_entry_get_next(e)); | |
480 | ||
481 | e = udev_list_entry_get_by_name(e, "bbb"); | |
482 | assert_se(e); | |
483 | assert_se(streq_ptr(udev_list_entry_get_name(e), "bbb")); | |
484 | assert_se(streq_ptr(udev_list_entry_get_value(e), "foo")); | |
485 | e = udev_list_entry_get_by_name(e, "aaa"); | |
486 | assert_se(e); | |
487 | assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa")); | |
488 | assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge")); | |
489 | assert_se(!udev_list_entry_get_by_name(e, "ccc")); | |
490 | udev_list_cleanup(&list); | |
491 | } | |
492 | ||
bb4f798a | 493 | static int parse_args(int argc, char *argv[], const char **syspath, const char **subsystem) { |
663996b3 | 494 | static const struct option options[] = { |
aa27b158 | 495 | { "syspath", required_argument, NULL, 'p' }, |
663996b3 | 496 | { "subsystem", required_argument, NULL, 's' }, |
aa27b158 MP |
497 | { "debug", no_argument, NULL, 'd' }, |
498 | { "help", no_argument, NULL, 'h' }, | |
499 | { "version", no_argument, NULL, 'V' }, | |
500 | { "monitor", no_argument, NULL, 'm' }, | |
663996b3 MS |
501 | {} |
502 | }; | |
5eef597e | 503 | int c; |
663996b3 | 504 | |
98393f85 | 505 | while ((c = getopt_long(argc, argv, "p:s:dhVm", options, NULL)) >= 0) |
5eef597e | 506 | switch (c) { |
663996b3 | 507 | case 'p': |
bb4f798a | 508 | *syspath = optarg; |
663996b3 | 509 | break; |
5eef597e | 510 | |
663996b3 | 511 | case 's': |
bb4f798a | 512 | *subsystem = optarg; |
663996b3 | 513 | break; |
5eef597e | 514 | |
663996b3 | 515 | case 'd': |
bb4f798a | 516 | log_set_max_level(LOG_DEBUG); |
663996b3 | 517 | break; |
5eef597e | 518 | |
663996b3 MS |
519 | case 'h': |
520 | printf("--debug --syspath= --subsystem= --help\n"); | |
bb4f798a | 521 | return 0; |
5eef597e | 522 | |
663996b3 | 523 | case 'V': |
7c20daf6 | 524 | printf("%s\n", GIT_VERSION); |
bb4f798a | 525 | return 0; |
aa27b158 MP |
526 | |
527 | case 'm': | |
528 | arg_monitor = true; | |
529 | break; | |
5eef597e MP |
530 | |
531 | case '?': | |
bb4f798a | 532 | return -EINVAL; |
5eef597e MP |
533 | |
534 | default: | |
535 | assert_not_reached("Unhandled option code."); | |
663996b3 | 536 | } |
5eef597e | 537 | |
bb4f798a MB |
538 | return 1; |
539 | } | |
540 | ||
541 | static int run(int argc, char *argv[]) { | |
542 | _cleanup_(udev_unrefp) struct udev *udev = NULL; | |
543 | ||
544 | const char *syspath = "/devices/virtual/mem/null"; | |
545 | const char *subsystem = NULL; | |
546 | int r; | |
547 | ||
548 | test_setup_logging(LOG_INFO); | |
549 | ||
550 | r = parse_args(argc, argv, &syspath, &subsystem); | |
551 | if (r <= 0) | |
552 | return r; | |
553 | ||
554 | assert_se(udev = udev_new()); | |
555 | ||
663996b3 | 556 | /* add sys path if needed */ |
aa27b158 MP |
557 | if (!startswith(syspath, "/sys")) |
558 | syspath = strjoina("/sys/", syspath); | |
663996b3 MS |
559 | |
560 | test_device(udev, syspath); | |
561 | test_device_devnum(udev); | |
aa27b158 MP |
562 | test_device_subsys_name(udev, "block", "sda"); |
563 | test_device_subsys_name(udev, "subsystem", "pci"); | |
564 | test_device_subsys_name(udev, "drivers", "scsi:sd"); | |
565 | test_device_subsys_name(udev, "module", "printk"); | |
663996b3 MS |
566 | test_device_parents(udev, syspath); |
567 | ||
568 | test_enumerate(udev, subsystem); | |
569 | ||
570 | test_queue(udev); | |
571 | ||
572 | test_hwdb(udev, "usb:v0D50p0011*"); | |
573 | ||
aa27b158 MP |
574 | if (arg_monitor) |
575 | test_monitor(udev); | |
576 | ||
6e866b33 MB |
577 | test_util_replace_whitespace(); |
578 | test_util_resolve_subsys_kernel(); | |
579 | ||
580 | test_list(); | |
581 | ||
bb4f798a | 582 | return 0; |
663996b3 | 583 | } |
bb4f798a MB |
584 | |
585 | DEFINE_MAIN_FUNCTION(run); |