summaryrefslogtreecommitdiffstats
path: root/src/test/test-udev-util.c
blob: b0213f8c390b9e7a2511e52b1e077591fe8f332e (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <stdlib.h>
#include <string.h>

#include "macro.h"
#include "string-util.h"
#include "udev-util.h"

static void test_udev_rule_parse_value_one(const char *in, const char *expected_value, int expected_retval) {
        _cleanup_free_ char *str = NULL;
        char *value = UINT_TO_PTR(0x12345678U);
        char *endpos = UINT_TO_PTR(0x87654321U);

        assert_se(str = strdup(in));
        assert_se(udev_rule_parse_value(str, &value, &endpos) == expected_retval);
        if (expected_retval < 0) {
                /* not modified on failure */
                assert_se(value == UINT_TO_PTR(0x12345678U));
                assert_se(endpos == UINT_TO_PTR(0x87654321U));
        } else {
                assert_se(streq_ptr(value, expected_value));
                assert_se(endpos == str + strlen(in));
        }
}

static void test_parse_value(void) {
        /* input: "valid operand"
         * parsed: valid operand
         * use the following command to help generate textual C strings:
         * python3 -c 'import json; print(json.dumps(input()))' */
        test_udev_rule_parse_value_one(
                "\"valid operand\"",
                "valid operand",
                0
        );
}

static void test_parse_value_with_backslashes(void) {
        /* input: "va'l\'id\"op\"erand"
         * parsed: va'l\'id"op"erand */
        test_udev_rule_parse_value_one(
                "\"va'l\\'id\\\"op\\\"erand\"",
                "va'l\\'id\"op\"erand",
                0
        );
}

static void test_parse_value_no_quotes(void) {
        test_udev_rule_parse_value_one(
                "no quotes",
                0,
                -EINVAL
        );
}

static void test_parse_value_noescape(void) {
        test_udev_rule_parse_value_one(
                "\"\\\\a\\b\\x\\y\"",
                "\\\\a\\b\\x\\y",
                0
        );
}

static void test_parse_value_nul(void) {
        test_udev_rule_parse_value_one(
                "\"reject\0nul\"",
                0,
                -EINVAL
        );
}

static void test_parse_value_escape_nothing(void) {
        /* input: e"" */
        test_udev_rule_parse_value_one(
                "e\"\"",
                "",
                0
        );
}

static void test_parse_value_escape_nothing2(void) {
        /* input: e"1234" */
        test_udev_rule_parse_value_one(
                "e\"1234\"",
                "1234",
                0
        );
}

static void test_parse_value_escape_double_quote(void) {
        /* input: e"\"" */
        test_udev_rule_parse_value_one(
                "e\"\\\"\"",
                "\"",
                0
        );
}

static void test_parse_value_escape_backslash(void) {
        /* input: e"\ */
        test_udev_rule_parse_value_one(
                "e\"\\",
                0,
                -EINVAL
        );
        /* input: e"\" */
        test_udev_rule_parse_value_one(
                "e\"\\\"",
                0,
                -EINVAL
        );
        /* input: e"\\" */
        test_udev_rule_parse_value_one(
                "e\"\\\\\"",
                "\\",
                0
        );
        /* input: e"\\\" */
        test_udev_rule_parse_value_one(
                "e\"\\\\\\\"",
                0,
                -EINVAL
        );
        /* input: e"\\\"" */
        test_udev_rule_parse_value_one(
                "e\"\\\\\\\"\"",
                "\\\"",
                0
        );
        /* input: e"\\\\" */
        test_udev_rule_parse_value_one(
                "e\"\\\\\\\\\"",
                "\\\\",
                0
        );
}

static void test_parse_value_newline(void) {
        /* input: e"operand with newline\n" */
        test_udev_rule_parse_value_one(
                "e\"operand with newline\\n\"",
                "operand with newline\n",
                0
        );
}

static void test_parse_value_escaped(void) {
        /* input: e"single\rcharacter\t\aescape\bsequence" */
        test_udev_rule_parse_value_one(
                "e\"single\\rcharacter\\t\\aescape\\bsequence\"",
                "single\rcharacter\t\aescape\bsequence",
                0
        );
}

static void test_parse_value_invalid_escape(void) {
        /* input: e"reject\invalid escape sequence" */
        test_udev_rule_parse_value_one(
                "e\"reject\\invalid escape sequence",
                0,
                -EINVAL
        );
}

static void test_parse_value_invalid_termination(void) {
        /* input: e"\ */
        test_udev_rule_parse_value_one(
                "e\"\\",
                0,
                -EINVAL
        );
}

static void test_parse_value_unicode(void) {
        /* input: "s\u1d1c\u1d04\u029c \u1d1c\u0274\u026a\u1d04\u1d0f\u1d05\u1d07 \U0001d568\U0001d560\U0001d568" */
        test_udev_rule_parse_value_one(
                "e\"s\\u1d1c\\u1d04\\u029c \\u1d1c\\u0274\\u026a\\u1d04\\u1d0f\\u1d05\\u1d07 \\U0001d568\\U0001d560\\U0001d568\"",
                "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",
                0
        );
}

int main(int argc, char **argv) {
        test_parse_value();
        test_parse_value_with_backslashes();
        test_parse_value_no_quotes();
        test_parse_value_nul();
        test_parse_value_noescape();

        test_parse_value_escape_nothing();
        test_parse_value_escape_nothing2();
        test_parse_value_escape_double_quote();
        test_parse_value_escape_backslash();
        test_parse_value_newline();
        test_parse_value_escaped();
        test_parse_value_invalid_escape();
        test_parse_value_invalid_termination();
        test_parse_value_unicode();

        return EXIT_SUCCESS;
}