chiark / gitweb /
test-util.c: added hexchar, unhexchar, octchar, unoctchar, decchar, undecchar tests
[elogind.git] / src / test / test-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7   Copyright 2013 Thomas H.P. Andersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <string.h>
24
25 #include "util.h"
26
27 static void test_streq_ptr(void) {
28         assert_se(streq_ptr(NULL, NULL));
29         assert_se(!streq_ptr("abc", "cdef"));
30 }
31
32 static void test_first_word(void) {
33         assert_se(first_word("Hello", ""));
34         assert_se(first_word("Hello", "Hello"));
35         assert_se(first_word("Hello world", "Hello"));
36         assert_se(first_word("Hello\tworld", "Hello"));
37         assert_se(first_word("Hello\nworld", "Hello"));
38         assert_se(first_word("Hello\rworld", "Hello"));
39         assert_se(first_word("Hello ", "Hello"));
40
41         assert_se(!first_word("Hello", "Hellooo"));
42         assert_se(!first_word("Hello", "xxxxx"));
43         assert_se(!first_word("Hellooo", "Hello"));
44 }
45
46 static void test_parse_boolean(void) {
47         assert_se(parse_boolean("1") == 1);
48         assert_se(parse_boolean("y") == 1);
49         assert_se(parse_boolean("Y") == 1);
50         assert_se(parse_boolean("yes") == 1);
51         assert_se(parse_boolean("YES") == 1);
52         assert_se(parse_boolean("true") == 1);
53         assert_se(parse_boolean("TRUE") == 1);
54         assert_se(parse_boolean("on") == 1);
55         assert_se(parse_boolean("ON") == 1);
56
57         assert_se(parse_boolean("0") == 0);
58         assert_se(parse_boolean("n") == 0);
59         assert_se(parse_boolean("N") == 0);
60         assert_se(parse_boolean("no") == 0);
61         assert_se(parse_boolean("NO") == 0);
62         assert_se(parse_boolean("false") == 0);
63         assert_se(parse_boolean("FALSE") == 0);
64         assert_se(parse_boolean("off") == 0);
65         assert_se(parse_boolean("OFF") == 0);
66
67         assert_se(parse_boolean("garbage") < 0);
68         assert_se(parse_boolean("") < 0);
69 }
70
71 static void test_parse_pid(void) {
72         int r;
73         pid_t pid;
74
75         r = parse_pid("100", &pid);
76         assert_se(r == 0);
77         assert_se(pid == 100);
78
79         r = parse_pid("0x7FFFFFFF", &pid);
80         assert_se(r == 0);
81         assert_se(pid == 2147483647);
82
83         pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
84         r = parse_pid("0", &pid);
85         assert_se(r == -ERANGE);
86         assert_se(pid == 65);
87
88         pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
89         r = parse_pid("-100", &pid);
90         assert_se(r == -ERANGE);
91         assert_se(pid == 65);
92
93         pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
94         r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
95         assert(r == -ERANGE);
96         assert_se(pid == 65);
97 }
98
99 static void test_parse_uid(void) {
100         int r;
101         uid_t uid;
102
103         r = parse_uid("100", &uid);
104         assert_se(r == 0);
105         assert_se(uid == 100);
106 }
107
108 static void test_safe_atolli(void) {
109         int r;
110         long long l;
111
112         r = safe_atolli("12345", &l);
113         assert_se(r == 0);
114         assert_se(l == 12345);
115
116         r = safe_atolli("junk", &l);
117         assert_se(r == -EINVAL);
118 }
119
120 static void test_safe_atod(void) {
121         int r;
122         double d;
123
124         r = safe_atod("0.2244", &d);
125         assert_se(r == 0);
126         assert_se(abs(d - 0.2244) < 0.000001);
127
128         r = safe_atod("junk", &d);
129         assert_se(r == -EINVAL);
130 }
131
132 static void test_strstrip(void) {
133        char *r;
134        char input[] = "   hello, waldo.   ";
135
136        r = strstrip(input);
137        assert_se(streq(r, "hello, waldo."));
138
139 }
140
141 static void test_delete_chars(void) {
142        char *r;
143        char input[] = "   hello, waldo.   abc";
144
145        r = delete_chars(input, WHITESPACE);
146        assert_se(streq(r, "hello,waldo.abc"));
147 }
148
149 static void test_in_charset(void) {
150       assert_se(in_charset("dddaaabbbcccc", "abcd"));
151       assert_se(!in_charset("dddaaabbbcccc", "abc f"));
152 }
153
154 static void test_hexchar(void) {
155         assert_se(hexchar(0xa) == 'a');
156         assert_se(hexchar(0x0) == '0');
157 }
158
159 static void test_unhexchar(void) {
160         assert_se(unhexchar('a') == 0xA);
161         assert_se(unhexchar('A') == 0xA);
162         assert_se(unhexchar('0') == 0x0);
163 }
164
165 static void test_octchar(void) {
166         assert_se(octchar(00) == '0');
167         assert_se(octchar(07) == '7');
168 }
169
170 static void test_unoctchar(void) {
171         assert_se(unoctchar('0') == 00);
172         assert_se(unoctchar('7') == 07);
173 }
174
175 static void test_decchar(void) {
176         assert_se(decchar(0) == '0');
177         assert_se(decchar(9) == '9');
178 }
179
180 static void test_undecchar(void) {
181         assert_se(undecchar('0') == 0);
182         assert_se(undecchar('9') == 9);
183 }
184
185 static void test_foreach_word(void) {
186         char *w, *state;
187         size_t l;
188         int i = 0;
189         const char test[] = "test abc d\te   f   ";
190         const char * const expected[] = {
191                 "test",
192                 "abc",
193                 "d",
194                 "e",
195                 "f",
196                 "",
197                 NULL
198         };
199
200         FOREACH_WORD(w, l, test, state) {
201                 assert_se(strneq(expected[i++], w, l));
202         }
203 }
204
205 static void test_foreach_word_quoted(void) {
206         char *w, *state;
207         size_t l;
208         int i = 0;
209         const char test[] = "test a b c 'd' e '' '' hhh '' '' \"a b c\"";
210         const char * const expected[] = {
211                 "test",
212                 "a",
213                 "b",
214                 "c",
215                 "d",
216                 "e",
217                 "",
218                 "",
219                 "hhh",
220                 "",
221                 "",
222                 "a b c",
223                 NULL
224         };
225
226         printf("<%s>\n", test);
227         FOREACH_WORD_QUOTED(w, l, test, state) {
228                 _cleanup_free_ char *t = NULL;
229
230                 assert_se(t = strndup(w, l));
231                 assert_se(strneq(expected[i++], w, l));
232                 printf("<%s>\n", t);
233         }
234 }
235
236 static void test_default_term_for_tty(void) {
237         puts(default_term_for_tty("/dev/tty23"));
238         puts(default_term_for_tty("/dev/ttyS23"));
239         puts(default_term_for_tty("/dev/tty0"));
240         puts(default_term_for_tty("/dev/pty0"));
241         puts(default_term_for_tty("/dev/pts/0"));
242         puts(default_term_for_tty("/dev/console"));
243         puts(default_term_for_tty("tty23"));
244         puts(default_term_for_tty("ttyS23"));
245         puts(default_term_for_tty("tty0"));
246         puts(default_term_for_tty("pty0"));
247         puts(default_term_for_tty("pts/0"));
248         puts(default_term_for_tty("console"));
249 }
250
251 static void test_memdup_multiply(void) {
252         int org[] = {1, 2, 3};
253         int *dup;
254
255         dup = (int*)memdup_multiply(org, sizeof(int), 3);
256
257         assert_se(dup);
258         assert_se(dup[0] == 1);
259         assert_se(dup[1] == 2);
260         assert_se(dup[2] == 3);
261         free(dup);
262 }
263
264 int main(int argc, char *argv[]) {
265         test_streq_ptr();
266         test_first_word();
267         test_parse_boolean();
268         test_parse_pid();
269         test_parse_uid();
270         test_safe_atolli();
271         test_safe_atod();
272         test_strstrip();
273         test_delete_chars();
274         test_in_charset();
275         test_hexchar();
276         test_unhexchar();
277         test_octchar();
278         test_unoctchar();
279         test_decchar();
280         test_undecchar();
281         test_foreach_word();
282         test_foreach_word_quoted();
283         test_default_term_for_tty();
284         test_memdup_multiply();
285
286         return 0;
287 }