]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-udev-util.c
test: Use TEST macro in more cases
[thirdparty/systemd.git] / src / test / test-udev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "macro.h"
7 #include "string-util.h"
8 #include "tests.h"
9 #include "udev-util.h"
10
11 static void test_udev_rule_parse_value_one(const char *in, const char *expected_value, int expected_retval) {
12 _cleanup_free_ char *str = NULL;
13 char *value = UINT_TO_PTR(0x12345678U);
14 char *endpos = UINT_TO_PTR(0x87654321U);
15
16 log_info("/* %s (%s, %s, %d) */", __func__, in, expected_value, expected_retval);
17
18 assert_se(str = strdup(in));
19 assert_se(udev_rule_parse_value(str, &value, &endpos) == expected_retval);
20 if (expected_retval < 0) {
21 /* not modified on failure */
22 assert_se(value == UINT_TO_PTR(0x12345678U));
23 assert_se(endpos == UINT_TO_PTR(0x87654321U));
24 } else {
25 assert_se(streq_ptr(value, expected_value));
26 assert_se(endpos == str + strlen(in));
27 }
28 }
29
30 TEST(udev_rule_parse_value) {
31 /* input: "valid operand"
32 * parsed: valid operand
33 * use the following command to help generate textual C strings:
34 * python3 -c 'import json; print(json.dumps(input()))' */
35 test_udev_rule_parse_value_one("\"valid operand\"", "valid operand", 0);
36 /* input: "va'l\'id\"op\"erand"
37 * parsed: va'l\'id"op"erand */
38 test_udev_rule_parse_value_one("\"va'l\\'id\\\"op\\\"erand\"", "va'l\\'id\"op\"erand", 0);
39 test_udev_rule_parse_value_one("no quotes", 0, -EINVAL);
40 test_udev_rule_parse_value_one("\"\\\\a\\b\\x\\y\"", "\\\\a\\b\\x\\y", 0);
41 test_udev_rule_parse_value_one("\"reject\0nul\"", 0, -EINVAL);
42 /* input: e"" */
43 test_udev_rule_parse_value_one("e\"\"", "", 0);
44 /* input: e"1234" */
45 test_udev_rule_parse_value_one("e\"1234\"", "1234", 0);
46 /* input: e"\"" */
47 test_udev_rule_parse_value_one("e\"\\\"\"", "\"", 0);
48 /* input: e"\ */
49 test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL);
50 /* input: e"\" */
51 test_udev_rule_parse_value_one("e\"\\\"", 0, -EINVAL);
52 /* input: e"\\" */
53 test_udev_rule_parse_value_one("e\"\\\\\"", "\\", 0);
54 /* input: e"\\\" */
55 test_udev_rule_parse_value_one("e\"\\\\\\\"", 0, -EINVAL);
56 /* input: e"\\\"" */
57 test_udev_rule_parse_value_one("e\"\\\\\\\"\"", "\\\"", 0);
58 /* input: e"\\\\" */
59 test_udev_rule_parse_value_one("e\"\\\\\\\\\"", "\\\\", 0);
60 /* input: e"operand with newline\n" */
61 test_udev_rule_parse_value_one("e\"operand with newline\\n\"", "operand with newline\n", 0);
62 /* input: e"single\rcharacter\t\aescape\bsequence" */
63 test_udev_rule_parse_value_one(
64 "e\"single\\rcharacter\\t\\aescape\\bsequence\"", "single\rcharacter\t\aescape\bsequence", 0);
65 /* input: e"reject\invalid escape sequence" */
66 test_udev_rule_parse_value_one("e\"reject\\invalid escape sequence", 0, -EINVAL);
67 /* input: e"\ */
68 test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL);
69 /* input: "s\u1d1c\u1d04\u029c \u1d1c\u0274\u026a\u1d04\u1d0f\u1d05\u1d07 \U0001d568\U0001d560\U0001d568" */
70 test_udev_rule_parse_value_one(
71 "e\"s\\u1d1c\\u1d04\\u029c \\u1d1c\\u0274\\u026a\\u1d04\\u1d0f\\u1d05\\u1d07 \\U0001d568\\U0001d560\\U0001d568\"",
72 "s\xe1\xb4\x9c\xe1\xb4\x84\xca\x9c \xe1\xb4\x9c\xc9\xb4\xc9\xaa\xe1\xb4\x84\xe1\xb4\x8f\xe1\xb4\x85\xe1\xb4\x87 \xf0\x9d\x95\xa8\xf0\x9d\x95\xa0\xf0\x9d\x95\xa8",
73 0);
74 }
75
76 static void test_udev_replace_whitespace_one_len(const char *str, size_t len, const char *expected) {
77 _cleanup_free_ char *result = NULL;
78 int r;
79
80 result = new(char, len + 1);
81 assert_se(result);
82 r = udev_replace_whitespace(str, result, len);
83 assert_se((size_t) r == strlen(expected));
84 assert_se(streq(result, expected));
85 }
86
87 static void test_udev_replace_whitespace_one(const char *str, const char *expected) {
88 test_udev_replace_whitespace_one_len(str, strlen(str), expected);
89 }
90
91 TEST(udev_replace_whitespace) {
92 test_udev_replace_whitespace_one("hogehoge", "hogehoge");
93 test_udev_replace_whitespace_one("hoge hoge", "hoge_hoge");
94 test_udev_replace_whitespace_one(" hoge hoge ", "hoge_hoge");
95 test_udev_replace_whitespace_one(" ", "");
96 test_udev_replace_whitespace_one("hoge ", "hoge");
97
98 test_udev_replace_whitespace_one_len("hoge hoge ", 9, "hoge_hoge");
99 test_udev_replace_whitespace_one_len("hoge hoge ", 8, "hoge_hog");
100 test_udev_replace_whitespace_one_len("hoge hoge ", 7, "hoge_ho");
101 test_udev_replace_whitespace_one_len("hoge hoge ", 6, "hoge_h");
102 test_udev_replace_whitespace_one_len("hoge hoge ", 5, "hoge");
103 test_udev_replace_whitespace_one_len("hoge hoge ", 4, "hoge");
104 test_udev_replace_whitespace_one_len("hoge hoge ", 3, "hog");
105 test_udev_replace_whitespace_one_len("hoge hoge ", 2, "ho");
106 test_udev_replace_whitespace_one_len("hoge hoge ", 1, "h");
107 test_udev_replace_whitespace_one_len("hoge hoge ", 0, "");
108
109 test_udev_replace_whitespace_one_len(" hoge hoge ", 16, "hoge_hoge");
110 test_udev_replace_whitespace_one_len(" hoge hoge ", 15, "hoge_hoge");
111 test_udev_replace_whitespace_one_len(" hoge hoge ", 14, "hoge_hog");
112 test_udev_replace_whitespace_one_len(" hoge hoge ", 13, "hoge_ho");
113 test_udev_replace_whitespace_one_len(" hoge hoge ", 12, "hoge_h");
114 test_udev_replace_whitespace_one_len(" hoge hoge ", 11, "hoge");
115 test_udev_replace_whitespace_one_len(" hoge hoge ", 10, "hoge");
116 test_udev_replace_whitespace_one_len(" hoge hoge ", 9, "hoge");
117 test_udev_replace_whitespace_one_len(" hoge hoge ", 8, "hoge");
118 test_udev_replace_whitespace_one_len(" hoge hoge ", 7, "hog");
119 test_udev_replace_whitespace_one_len(" hoge hoge ", 6, "ho");
120 test_udev_replace_whitespace_one_len(" hoge hoge ", 5, "h");
121 test_udev_replace_whitespace_one_len(" hoge hoge ", 4, "");
122 test_udev_replace_whitespace_one_len(" hoge hoge ", 3, "");
123 test_udev_replace_whitespace_one_len(" hoge hoge ", 2, "");
124 test_udev_replace_whitespace_one_len(" hoge hoge ", 1, "");
125 test_udev_replace_whitespace_one_len(" hoge hoge ", 0, "");
126 }
127
128 static void test_udev_resolve_subsys_kernel_one(const char *str, bool read_value, int retval, const char *expected) {
129 char result[PATH_MAX] = "";
130 int r;
131
132 r = udev_resolve_subsys_kernel(str, result, sizeof(result), read_value);
133 log_info("\"%s\" → expect: \"%s\", %d, actual: \"%s\", %d", str, strnull(expected), retval, result, r);
134 assert_se(r == retval);
135 if (r >= 0)
136 assert_se(streq(result, expected));
137 }
138
139 TEST(udev_resolve_subsys_kernel) {
140 test_udev_resolve_subsys_kernel_one("hoge", false, -EINVAL, NULL);
141 test_udev_resolve_subsys_kernel_one("[hoge", false, -EINVAL, NULL);
142 test_udev_resolve_subsys_kernel_one("[hoge/foo", false, -EINVAL, NULL);
143 test_udev_resolve_subsys_kernel_one("[hoge/]", false, -EINVAL, NULL);
144
145 test_udev_resolve_subsys_kernel_one("[net/lo]", false, 0, "/sys/devices/virtual/net/lo");
146 test_udev_resolve_subsys_kernel_one("[net/lo]/", false, 0, "/sys/devices/virtual/net/lo");
147 test_udev_resolve_subsys_kernel_one("[net/lo]hoge", false, 0, "/sys/devices/virtual/net/lo/hoge");
148 test_udev_resolve_subsys_kernel_one("[net/lo]/hoge", false, 0, "/sys/devices/virtual/net/lo/hoge");
149
150 test_udev_resolve_subsys_kernel_one("[net/lo]", true, -EINVAL, NULL);
151 test_udev_resolve_subsys_kernel_one("[net/lo]/", true, -EINVAL, NULL);
152 test_udev_resolve_subsys_kernel_one("[net/lo]hoge", true, 0, "");
153 test_udev_resolve_subsys_kernel_one("[net/lo]/hoge", true, 0, "");
154 test_udev_resolve_subsys_kernel_one("[net/lo]address", true, 0, "00:00:00:00:00:00");
155 test_udev_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00");
156 }
157
158 DEFINE_TEST_MAIN(LOG_INFO);