chiark / gitweb /
2f6f39f15ec60151e0593dd3583e673037dcb3a4
[elogind.git] / src / test / test-hexdecoct.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7
8 #include "alloc-util.h"
9 #include "hexdecoct.h"
10 #include "macro.h"
11 #include "string-util.h"
12
13 /// Additional includes needed by elogind
14 #include "musl_missing.h"
15
16 static void test_hexchar(void) {
17         assert_se(hexchar(0xa) == 'a');
18         assert_se(hexchar(0x0) == '0');
19 }
20
21 static void test_unhexchar(void) {
22         assert_se(unhexchar('a') == 0xA);
23         assert_se(unhexchar('A') == 0xA);
24         assert_se(unhexchar('0') == 0x0);
25 }
26
27 static void test_base32hexchar(void) {
28         assert_se(base32hexchar(0) == '0');
29         assert_se(base32hexchar(9) == '9');
30         assert_se(base32hexchar(10) == 'A');
31         assert_se(base32hexchar(31) == 'V');
32 }
33
34 static void test_unbase32hexchar(void) {
35         assert_se(unbase32hexchar('0') == 0);
36         assert_se(unbase32hexchar('9') == 9);
37         assert_se(unbase32hexchar('A') == 10);
38         assert_se(unbase32hexchar('V') == 31);
39         assert_se(unbase32hexchar('=') == -EINVAL);
40 }
41
42 static void test_base64char(void) {
43         assert_se(base64char(0) == 'A');
44         assert_se(base64char(26) == 'a');
45         assert_se(base64char(63) == '/');
46 }
47
48 static void test_unbase64char(void) {
49         assert_se(unbase64char('A') == 0);
50         assert_se(unbase64char('Z') == 25);
51         assert_se(unbase64char('a') == 26);
52         assert_se(unbase64char('z') == 51);
53         assert_se(unbase64char('0') == 52);
54         assert_se(unbase64char('9') == 61);
55         assert_se(unbase64char('+') == 62);
56         assert_se(unbase64char('/') == 63);
57         assert_se(unbase64char('=') == -EINVAL);
58 }
59
60 static void test_octchar(void) {
61         assert_se(octchar(00) == '0');
62         assert_se(octchar(07) == '7');
63 }
64
65 static void test_unoctchar(void) {
66         assert_se(unoctchar('0') == 00);
67         assert_se(unoctchar('7') == 07);
68 }
69
70 static void test_decchar(void) {
71         assert_se(decchar(0) == '0');
72         assert_se(decchar(9) == '9');
73 }
74
75 static void test_undecchar(void) {
76         assert_se(undecchar('0') == 0);
77         assert_se(undecchar('9') == 9);
78 }
79
80 static void test_unhexmem_one(const char *s, size_t l, int retval) {
81         _cleanup_free_ char *hex = NULL;
82         _cleanup_free_ void *mem = NULL;
83         size_t len;
84
85         assert_se(unhexmem(s, l, &mem, &len) == retval);
86         if (retval == 0) {
87                 char *answer;
88
89                 if (l == (size_t) -1)
90                         l = strlen(s);
91
92                 assert_se(hex = hexmem(mem, len));
93                 answer = strndupa(s, l);
94                 assert_se(streq(delete_chars(answer, WHITESPACE), hex));
95         }
96 }
97
98 static void test_unhexmem(void) {
99         const char *hex = "efa2149213";
100         const char *hex_space = "  e f   a\n 2\r  14\n\r\t9\t2 \n1\r3 \r\r\t";
101         const char *hex_invalid = "efa214921o";
102
103         test_unhexmem_one(NULL, 0, 0);
104         test_unhexmem_one("", 0, 0);
105         test_unhexmem_one("", (size_t) -1, 0);
106         test_unhexmem_one("   \n \t\r   \t\t \n\n\n", (size_t) -1, 0);
107         test_unhexmem_one(hex_invalid, strlen(hex_invalid), -EINVAL);
108         test_unhexmem_one(hex_invalid, (size_t) - 1, -EINVAL);
109         test_unhexmem_one(hex, strlen(hex) - 1, -EPIPE);
110         test_unhexmem_one(hex, strlen(hex), 0);
111         test_unhexmem_one(hex, (size_t) -1, 0);
112         test_unhexmem_one(hex_space, strlen(hex_space), 0);
113         test_unhexmem_one(hex_space, (size_t) -1, 0);
114 }
115
116 /* https://tools.ietf.org/html/rfc4648#section-10 */
117 static void test_base32hexmem(void) {
118         char *b32;
119
120         b32 = base32hexmem("", STRLEN(""), true);
121         assert_se(b32);
122         assert_se(streq(b32, ""));
123         free(b32);
124
125         b32 = base32hexmem("f", STRLEN("f"), true);
126         assert_se(b32);
127         assert_se(streq(b32, "CO======"));
128         free(b32);
129
130         b32 = base32hexmem("fo", STRLEN("fo"), true);
131         assert_se(b32);
132         assert_se(streq(b32, "CPNG===="));
133         free(b32);
134
135         b32 = base32hexmem("foo", STRLEN("foo"), true);
136         assert_se(b32);
137         assert_se(streq(b32, "CPNMU==="));
138         free(b32);
139
140         b32 = base32hexmem("foob", STRLEN("foob"), true);
141         assert_se(b32);
142         assert_se(streq(b32, "CPNMUOG="));
143         free(b32);
144
145         b32 = base32hexmem("fooba", STRLEN("fooba"), true);
146         assert_se(b32);
147         assert_se(streq(b32, "CPNMUOJ1"));
148         free(b32);
149
150         b32 = base32hexmem("foobar", STRLEN("foobar"), true);
151         assert_se(b32);
152         assert_se(streq(b32, "CPNMUOJ1E8======"));
153         free(b32);
154
155         b32 = base32hexmem("", STRLEN(""), false);
156         assert_se(b32);
157         assert_se(streq(b32, ""));
158         free(b32);
159
160         b32 = base32hexmem("f", STRLEN("f"), false);
161         assert_se(b32);
162         assert_se(streq(b32, "CO"));
163         free(b32);
164
165         b32 = base32hexmem("fo", STRLEN("fo"), false);
166         assert_se(b32);
167         assert_se(streq(b32, "CPNG"));
168         free(b32);
169
170         b32 = base32hexmem("foo", STRLEN("foo"), false);
171         assert_se(b32);
172         assert_se(streq(b32, "CPNMU"));
173         free(b32);
174
175         b32 = base32hexmem("foob", STRLEN("foob"), false);
176         assert_se(b32);
177         assert_se(streq(b32, "CPNMUOG"));
178         free(b32);
179
180         b32 = base32hexmem("fooba", STRLEN("fooba"), false);
181         assert_se(b32);
182         assert_se(streq(b32, "CPNMUOJ1"));
183         free(b32);
184
185         b32 = base32hexmem("foobar", STRLEN("foobar"), false);
186         assert_se(b32);
187         assert_se(streq(b32, "CPNMUOJ1E8"));
188         free(b32);
189 }
190
191 static void test_unbase32hexmem_one(const char *hex, bool padding, int retval, const char *ans) {
192         _cleanup_free_ void *mem = NULL;
193         size_t len;
194
195         assert_se(unbase32hexmem(hex, (size_t) -1, padding, &mem, &len) == retval);
196         if (retval == 0) {
197                 char *str;
198
199                 str = strndupa(mem, len);
200                 assert_se(streq(str, ans));
201         }
202 }
203
204 static void test_unbase32hexmem(void) {
205         test_unbase32hexmem_one("", true, 0, "");
206
207         test_unbase32hexmem_one("CO======", true, 0, "f");
208         test_unbase32hexmem_one("CPNG====", true, 0, "fo");
209         test_unbase32hexmem_one("CPNMU===", true, 0, "foo");
210         test_unbase32hexmem_one("CPNMUOG=", true, 0, "foob");
211         test_unbase32hexmem_one("CPNMUOJ1", true, 0, "fooba");
212         test_unbase32hexmem_one("CPNMUOJ1E8======", true, 0, "foobar");
213
214         test_unbase32hexmem_one("A", true, -EINVAL, NULL);
215         test_unbase32hexmem_one("A=======", true, -EINVAL, NULL);
216         test_unbase32hexmem_one("AAA=====", true, -EINVAL, NULL);
217         test_unbase32hexmem_one("AAAAAA==", true, -EINVAL, NULL);
218         test_unbase32hexmem_one("AB======", true, -EINVAL, NULL);
219         test_unbase32hexmem_one("AAAB====", true, -EINVAL, NULL);
220         test_unbase32hexmem_one("AAAAB===", true, -EINVAL, NULL);
221         test_unbase32hexmem_one("AAAAAAB=", true, -EINVAL, NULL);
222
223         test_unbase32hexmem_one("XPNMUOJ1", true, -EINVAL, NULL);
224         test_unbase32hexmem_one("CXNMUOJ1", true, -EINVAL, NULL);
225         test_unbase32hexmem_one("CPXMUOJ1", true, -EINVAL, NULL);
226         test_unbase32hexmem_one("CPNXUOJ1", true, -EINVAL, NULL);
227         test_unbase32hexmem_one("CPNMXOJ1", true, -EINVAL, NULL);
228         test_unbase32hexmem_one("CPNMUXJ1", true, -EINVAL, NULL);
229         test_unbase32hexmem_one("CPNMUOX1", true, -EINVAL, NULL);
230         test_unbase32hexmem_one("CPNMUOJX", true, -EINVAL, NULL);
231
232         test_unbase32hexmem_one("", false, 0, "");
233         test_unbase32hexmem_one("CO", false, 0, "f");
234         test_unbase32hexmem_one("CPNG", false, 0, "fo");
235         test_unbase32hexmem_one("CPNMU", false, 0, "foo");
236         test_unbase32hexmem_one("CPNMUOG", false, 0, "foob");
237         test_unbase32hexmem_one("CPNMUOJ1", false, 0, "fooba");
238         test_unbase32hexmem_one("CPNMUOJ1E8", false, 0, "foobar");
239         test_unbase32hexmem_one("CPNMUOG=", false, -EINVAL, NULL);
240         test_unbase32hexmem_one("CPNMUOJ1E8======", false, -EINVAL, NULL);
241
242         test_unbase32hexmem_one("A", false, -EINVAL, NULL);
243         test_unbase32hexmem_one("A", false, -EINVAL, NULL);
244         test_unbase32hexmem_one("AAA", false, -EINVAL, NULL);
245         test_unbase32hexmem_one("AAAAAA", false, -EINVAL, NULL);
246         test_unbase32hexmem_one("AB", false, -EINVAL, NULL);
247         test_unbase32hexmem_one("AAAB", false, -EINVAL, NULL);
248         test_unbase32hexmem_one("AAAAB", false, -EINVAL, NULL);
249         test_unbase32hexmem_one("AAAAAAB", false, -EINVAL, NULL);
250 }
251
252 /* https://tools.ietf.org/html/rfc4648#section-10 */
253 static void test_base64mem(void) {
254         char *b64;
255
256         assert_se(base64mem("", STRLEN(""), &b64) == 0);
257         assert_se(streq(b64, ""));
258         free(b64);
259
260         assert_se(base64mem("f", STRLEN("f"), &b64) == 4);
261         assert_se(streq(b64, "Zg=="));
262         free(b64);
263
264         assert_se(base64mem("fo", STRLEN("fo"), &b64) == 4);
265         assert_se(streq(b64, "Zm8="));
266         free(b64);
267
268         assert_se(base64mem("foo", STRLEN("foo"), &b64) == 4);
269         assert_se(streq(b64, "Zm9v"));
270         free(b64);
271
272         assert_se(base64mem("foob", STRLEN("foob"), &b64) == 8);
273         assert_se(streq(b64, "Zm9vYg=="));
274         free(b64);
275
276         assert_se(base64mem("fooba", STRLEN("fooba"), &b64) == 8);
277         assert_se(streq(b64, "Zm9vYmE="));
278         free(b64);
279
280         assert_se(base64mem("foobar", STRLEN("foobar"), &b64) == 8);
281         assert_se(streq(b64, "Zm9vYmFy"));
282         free(b64);
283 }
284
285 static void test_unbase64mem_one(const char *input, const char *output, int ret) {
286         _cleanup_free_ void *buffer = NULL;
287         size_t size = 0;
288
289         assert_se(unbase64mem(input, (size_t) -1, &buffer, &size) == ret);
290
291         if (ret >= 0) {
292                 assert_se(size == strlen(output));
293                 assert_se(memcmp(buffer, output, size) == 0);
294                 assert_se(((char*) buffer)[size] == 0);
295         }
296 }
297
298 static void test_unbase64mem(void) {
299
300         test_unbase64mem_one("", "", 0);
301         test_unbase64mem_one("Zg==", "f", 0);
302         test_unbase64mem_one("Zm8=", "fo", 0);
303         test_unbase64mem_one("Zm9v", "foo", 0);
304         test_unbase64mem_one("Zm9vYg==", "foob", 0);
305         test_unbase64mem_one("Zm9vYmE=", "fooba", 0);
306         test_unbase64mem_one("Zm9vYmFy", "foobar", 0);
307
308         test_unbase64mem_one(" ", "", 0);
309         test_unbase64mem_one(" \n\r ", "", 0);
310         test_unbase64mem_one("    Zg\n==       ", "f", 0);
311         test_unbase64mem_one(" Zm 8=\r", "fo", 0);
312         test_unbase64mem_one("  Zm9\n\r\r\nv   ", "foo", 0);
313         test_unbase64mem_one(" Z m9vYg==\n\r", "foob", 0);
314         test_unbase64mem_one(" Zm 9vYmE=   ", "fooba", 0);
315         test_unbase64mem_one("   Z m9v    YmFy   ", "foobar", 0);
316
317         test_unbase64mem_one("A", NULL, -EPIPE);
318         test_unbase64mem_one("A====", NULL, -EINVAL);
319         test_unbase64mem_one("AAB==", NULL, -EINVAL);
320         test_unbase64mem_one(" A A A B = ", NULL, -EINVAL);
321         test_unbase64mem_one(" Z m 8 = q u u x ", NULL, -ENAMETOOLONG);
322 }
323
324 static void test_hexdump(void) {
325         uint8_t data[146];
326         unsigned i;
327
328         hexdump(stdout, NULL, 0);
329         hexdump(stdout, "", 0);
330         hexdump(stdout, "", 1);
331         hexdump(stdout, "x", 1);
332         hexdump(stdout, "x", 2);
333         hexdump(stdout, "foobar", 7);
334         hexdump(stdout, "f\nobar", 7);
335         hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
336
337         for (i = 0; i < ELEMENTSOF(data); i++)
338                 data[i] = i*2;
339
340         hexdump(stdout, data, sizeof(data));
341 }
342
343 int main(int argc, char *argv[]) {
344         test_hexchar();
345         test_unhexchar();
346         test_base32hexchar();
347         test_unbase32hexchar();
348         test_base64char();
349         test_unbase64char();
350         test_octchar();
351         test_unoctchar();
352         test_decchar();
353         test_undecchar();
354         test_unhexmem();
355         test_base32hexmem();
356         test_unbase32hexmem();
357         test_base64mem();
358         test_unbase64mem();
359         test_hexdump();
360
361         return 0;
362 }