]>
Commit | Line | Data |
---|---|---|
bd206af4 YW |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include "alloc-util.h" | |
4 | #include "string-util.h" | |
5 | #include "strv.h" | |
6 | #include "tests.h" | |
7 | #include "udev.h" | |
8 | ||
9 | static void test_udev_build_argv_one(const char *c) { | |
10 | _cleanup_strv_free_ char **a = NULL; | |
11 | _cleanup_free_ char *arg = NULL; | |
12 | char *argv[128], **p; | |
13 | int argc; | |
14 | size_t i; | |
15 | ||
16 | assert_se(a = strv_split_full(c, NULL, SPLIT_QUOTES | SPLIT_RELAX)); | |
17 | ||
18 | assert_se(arg = strdup(c)); | |
19 | assert_se(udev_build_argv(arg, &argc, argv) >= 0); | |
20 | ||
21 | log_info("command: %s", c); | |
22 | ||
23 | i = 0; | |
24 | log_info("strv_split:"); | |
25 | STRV_FOREACH(p, a) | |
26 | log_info("argv[%zu] = '%s'", i++, *p); | |
27 | ||
28 | i = 0; | |
29 | log_info("udev_build_argv:"); | |
30 | STRV_FOREACH(p, argv) | |
31 | log_info("argv[%zu] = '%s'", i++, *p); | |
32 | ||
33 | assert_se(strv_equal(argv, a)); | |
34 | assert_se(argc == (int) strv_length(a)); | |
35 | ||
36 | } | |
37 | ||
38 | static void test_udev_build_argv(void) { | |
39 | test_udev_build_argv_one("one two three"); | |
40 | test_udev_build_argv_one("one 'two three ' \" four five \" 'aaa bbb "); | |
41 | test_udev_build_argv_one("/bin/echo -e \\101"); | |
42 | test_udev_build_argv_one("/bin/echo -n special-device"); | |
43 | test_udev_build_argv_one("/bin/echo -n special-device"); | |
44 | test_udev_build_argv_one("/bin/echo test"); | |
45 | test_udev_build_argv_one("/bin/echo -n test-%b"); | |
46 | test_udev_build_argv_one("/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9"); | |
47 | test_udev_build_argv_one("/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'"); | |
48 | test_udev_build_argv_one("/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'"); | |
49 | test_udev_build_argv_one("/bin/sh -c 'printf %%s \\\"foo1 foo2\\\" | grep \\\"foo1 foo2\\\"'"); | |
50 | test_udev_build_argv_one("/bin/sh -c \\\"printf %%s 'foo1 foo2' | grep 'foo1 foo2'\\\""); | |
51 | test_udev_build_argv_one("/bin/sh -c 'printf \\\"%%s %%s\\\" \\\"foo1 foo2\\\" \\\"foo3\\\"| grep \\\"foo1 foo2\\\"'"); | |
52 | test_udev_build_argv_one("/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9"); | |
53 | test_udev_build_argv_one("/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9"); | |
54 | test_udev_build_argv_one("/bin/echo -n foo"); | |
55 | test_udev_build_argv_one("/bin/echo -n usb-%b"); | |
56 | test_udev_build_argv_one("/bin/echo -n scsi-%b"); | |
57 | test_udev_build_argv_one("/bin/echo -n foo-%b"); | |
58 | test_udev_build_argv_one("/bin/echo test"); | |
59 | test_udev_build_argv_one("/bin/echo symlink test this"); | |
60 | test_udev_build_argv_one("/bin/echo symlink test this"); | |
61 | test_udev_build_argv_one("/bin/echo link test this"); | |
62 | test_udev_build_argv_one("/bin/echo -n node link1 link2"); | |
63 | test_udev_build_argv_one("/bin/echo -n node link1 link2 link3 link4"); | |
64 | test_udev_build_argv_one("/usr/bin/test -b %N"); | |
65 | test_udev_build_argv_one("/bin/echo -e name; (/usr/bin/badprogram)"); | |
66 | test_udev_build_argv_one("/bin/echo -e \\xc3\\xbcber"); | |
67 | test_udev_build_argv_one("/bin/echo -e \\xef\\xe8garbage"); | |
68 | test_udev_build_argv_one("/bin/echo 1 1 0400"); | |
69 | test_udev_build_argv_one("/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890"); | |
70 | } | |
71 | ||
72 | int main(int argc, char *argv[]) { | |
73 | test_setup_logging(LOG_DEBUG); | |
74 | ||
75 | test_udev_build_argv(); | |
76 | ||
77 | return 0; | |
78 | } |