1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
10 #include "alloc-util.h"
11 #include "hexdecoct.h"
13 #include "string-util.h"
15 /// Additional includes needed by elogind
16 #include "musl_missing.h"
18 static void test_hexchar(void) {
19 assert_se(hexchar(0xa) == 'a');
20 assert_se(hexchar(0x0) == '0');
23 static void test_unhexchar(void) {
24 assert_se(unhexchar('a') == 0xA);
25 assert_se(unhexchar('A') == 0xA);
26 assert_se(unhexchar('0') == 0x0);
29 static void test_base32hexchar(void) {
30 assert_se(base32hexchar(0) == '0');
31 assert_se(base32hexchar(9) == '9');
32 assert_se(base32hexchar(10) == 'A');
33 assert_se(base32hexchar(31) == 'V');
36 static void test_unbase32hexchar(void) {
37 assert_se(unbase32hexchar('0') == 0);
38 assert_se(unbase32hexchar('9') == 9);
39 assert_se(unbase32hexchar('A') == 10);
40 assert_se(unbase32hexchar('V') == 31);
41 assert_se(unbase32hexchar('=') == -EINVAL);
44 static void test_base64char(void) {
45 assert_se(base64char(0) == 'A');
46 assert_se(base64char(26) == 'a');
47 assert_se(base64char(63) == '/');
50 static void test_unbase64char(void) {
51 assert_se(unbase64char('A') == 0);
52 assert_se(unbase64char('Z') == 25);
53 assert_se(unbase64char('a') == 26);
54 assert_se(unbase64char('z') == 51);
55 assert_se(unbase64char('0') == 52);
56 assert_se(unbase64char('9') == 61);
57 assert_se(unbase64char('+') == 62);
58 assert_se(unbase64char('/') == 63);
59 assert_se(unbase64char('=') == -EINVAL);
62 static void test_octchar(void) {
63 assert_se(octchar(00) == '0');
64 assert_se(octchar(07) == '7');
67 static void test_unoctchar(void) {
68 assert_se(unoctchar('0') == 00);
69 assert_se(unoctchar('7') == 07);
72 static void test_decchar(void) {
73 assert_se(decchar(0) == '0');
74 assert_se(decchar(9) == '9');
77 static void test_undecchar(void) {
78 assert_se(undecchar('0') == 0);
79 assert_se(undecchar('9') == 9);
82 static void test_unhexmem_one(const char *s, size_t l, int retval) {
83 _cleanup_free_ char *hex = NULL;
84 _cleanup_free_ void *mem = NULL;
87 assert_se(unhexmem(s, l, &mem, &len) == retval);
94 assert_se(hex = hexmem(mem, len));
95 answer = strndupa(s, l);
96 assert_se(streq(delete_chars(answer, WHITESPACE), hex));
100 static void test_unhexmem(void) {
101 const char *hex = "efa2149213";
102 const char *hex_space = " e f a\n 2\r 14\n\r\t9\t2 \n1\r3 \r\r\t";
103 const char *hex_invalid = "efa214921o";
105 test_unhexmem_one(NULL, 0, 0);
106 test_unhexmem_one("", 0, 0);
107 test_unhexmem_one("", (size_t) -1, 0);
108 test_unhexmem_one(" \n \t\r \t\t \n\n\n", (size_t) -1, 0);
109 test_unhexmem_one(hex_invalid, strlen(hex_invalid), -EINVAL);
110 test_unhexmem_one(hex_invalid, (size_t) - 1, -EINVAL);
111 test_unhexmem_one(hex, strlen(hex) - 1, -EPIPE);
112 test_unhexmem_one(hex, strlen(hex), 0);
113 test_unhexmem_one(hex, (size_t) -1, 0);
114 test_unhexmem_one(hex_space, strlen(hex_space), 0);
115 test_unhexmem_one(hex_space, (size_t) -1, 0);
118 /* https://tools.ietf.org/html/rfc4648#section-10 */
119 static void test_base32hexmem(void) {
122 b32 = base32hexmem("", STRLEN(""), true);
124 assert_se(streq(b32, ""));
127 b32 = base32hexmem("f", STRLEN("f"), true);
129 assert_se(streq(b32, "CO======"));
132 b32 = base32hexmem("fo", STRLEN("fo"), true);
134 assert_se(streq(b32, "CPNG===="));
137 b32 = base32hexmem("foo", STRLEN("foo"), true);
139 assert_se(streq(b32, "CPNMU==="));
142 b32 = base32hexmem("foob", STRLEN("foob"), true);
144 assert_se(streq(b32, "CPNMUOG="));
147 b32 = base32hexmem("fooba", STRLEN("fooba"), true);
149 assert_se(streq(b32, "CPNMUOJ1"));
152 b32 = base32hexmem("foobar", STRLEN("foobar"), true);
154 assert_se(streq(b32, "CPNMUOJ1E8======"));
157 b32 = base32hexmem("", STRLEN(""), false);
159 assert_se(streq(b32, ""));
162 b32 = base32hexmem("f", STRLEN("f"), false);
164 assert_se(streq(b32, "CO"));
167 b32 = base32hexmem("fo", STRLEN("fo"), false);
169 assert_se(streq(b32, "CPNG"));
172 b32 = base32hexmem("foo", STRLEN("foo"), false);
174 assert_se(streq(b32, "CPNMU"));
177 b32 = base32hexmem("foob", STRLEN("foob"), false);
179 assert_se(streq(b32, "CPNMUOG"));
182 b32 = base32hexmem("fooba", STRLEN("fooba"), false);
184 assert_se(streq(b32, "CPNMUOJ1"));
187 b32 = base32hexmem("foobar", STRLEN("foobar"), false);
189 assert_se(streq(b32, "CPNMUOJ1E8"));
193 static void test_unbase32hexmem_one(const char *hex, bool padding, int retval, const char *ans) {
194 _cleanup_free_ void *mem = NULL;
197 assert_se(unbase32hexmem(hex, (size_t) -1, padding, &mem, &len) == retval);
201 str = strndupa(mem, len);
202 assert_se(streq(str, ans));
206 static void test_unbase32hexmem(void) {
207 test_unbase32hexmem_one("", true, 0, "");
209 test_unbase32hexmem_one("CO======", true, 0, "f");
210 test_unbase32hexmem_one("CPNG====", true, 0, "fo");
211 test_unbase32hexmem_one("CPNMU===", true, 0, "foo");
212 test_unbase32hexmem_one("CPNMUOG=", true, 0, "foob");
213 test_unbase32hexmem_one("CPNMUOJ1", true, 0, "fooba");
214 test_unbase32hexmem_one("CPNMUOJ1E8======", true, 0, "foobar");
216 test_unbase32hexmem_one("A", true, -EINVAL, NULL);
217 test_unbase32hexmem_one("A=======", true, -EINVAL, NULL);
218 test_unbase32hexmem_one("AAA=====", true, -EINVAL, NULL);
219 test_unbase32hexmem_one("AAAAAA==", true, -EINVAL, NULL);
220 test_unbase32hexmem_one("AB======", true, -EINVAL, NULL);
221 test_unbase32hexmem_one("AAAB====", true, -EINVAL, NULL);
222 test_unbase32hexmem_one("AAAAB===", true, -EINVAL, NULL);
223 test_unbase32hexmem_one("AAAAAAB=", true, -EINVAL, NULL);
225 test_unbase32hexmem_one("XPNMUOJ1", true, -EINVAL, NULL);
226 test_unbase32hexmem_one("CXNMUOJ1", true, -EINVAL, NULL);
227 test_unbase32hexmem_one("CPXMUOJ1", true, -EINVAL, NULL);
228 test_unbase32hexmem_one("CPNXUOJ1", true, -EINVAL, NULL);
229 test_unbase32hexmem_one("CPNMXOJ1", true, -EINVAL, NULL);
230 test_unbase32hexmem_one("CPNMUXJ1", true, -EINVAL, NULL);
231 test_unbase32hexmem_one("CPNMUOX1", true, -EINVAL, NULL);
232 test_unbase32hexmem_one("CPNMUOJX", true, -EINVAL, NULL);
234 test_unbase32hexmem_one("", false, 0, "");
235 test_unbase32hexmem_one("CO", false, 0, "f");
236 test_unbase32hexmem_one("CPNG", false, 0, "fo");
237 test_unbase32hexmem_one("CPNMU", false, 0, "foo");
238 test_unbase32hexmem_one("CPNMUOG", false, 0, "foob");
239 test_unbase32hexmem_one("CPNMUOJ1", false, 0, "fooba");
240 test_unbase32hexmem_one("CPNMUOJ1E8", false, 0, "foobar");
241 test_unbase32hexmem_one("CPNMUOG=", false, -EINVAL, NULL);
242 test_unbase32hexmem_one("CPNMUOJ1E8======", false, -EINVAL, NULL);
244 test_unbase32hexmem_one("A", false, -EINVAL, NULL);
245 test_unbase32hexmem_one("A", false, -EINVAL, NULL);
246 test_unbase32hexmem_one("AAA", false, -EINVAL, NULL);
247 test_unbase32hexmem_one("AAAAAA", false, -EINVAL, NULL);
248 test_unbase32hexmem_one("AB", false, -EINVAL, NULL);
249 test_unbase32hexmem_one("AAAB", false, -EINVAL, NULL);
250 test_unbase32hexmem_one("AAAAB", false, -EINVAL, NULL);
251 test_unbase32hexmem_one("AAAAAAB", false, -EINVAL, NULL);
254 /* https://tools.ietf.org/html/rfc4648#section-10 */
255 static void test_base64mem(void) {
258 assert_se(base64mem("", STRLEN(""), &b64) == 0);
259 assert_se(streq(b64, ""));
262 assert_se(base64mem("f", STRLEN("f"), &b64) == 4);
263 assert_se(streq(b64, "Zg=="));
266 assert_se(base64mem("fo", STRLEN("fo"), &b64) == 4);
267 assert_se(streq(b64, "Zm8="));
270 assert_se(base64mem("foo", STRLEN("foo"), &b64) == 4);
271 assert_se(streq(b64, "Zm9v"));
274 assert_se(base64mem("foob", STRLEN("foob"), &b64) == 8);
275 assert_se(streq(b64, "Zm9vYg=="));
278 assert_se(base64mem("fooba", STRLEN("fooba"), &b64) == 8);
279 assert_se(streq(b64, "Zm9vYmE="));
282 assert_se(base64mem("foobar", STRLEN("foobar"), &b64) == 8);
283 assert_se(streq(b64, "Zm9vYmFy"));
287 static void test_unbase64mem_one(const char *input, const char *output, int ret) {
288 _cleanup_free_ void *buffer = NULL;
291 assert_se(unbase64mem(input, (size_t) -1, &buffer, &size) == ret);
294 assert_se(size == strlen(output));
295 assert_se(memcmp(buffer, output, size) == 0);
296 assert_se(((char*) buffer)[size] == 0);
300 static void test_unbase64mem(void) {
302 test_unbase64mem_one("", "", 0);
303 test_unbase64mem_one("Zg==", "f", 0);
304 test_unbase64mem_one("Zm8=", "fo", 0);
305 test_unbase64mem_one("Zm9v", "foo", 0);
306 test_unbase64mem_one("Zm9vYg==", "foob", 0);
307 test_unbase64mem_one("Zm9vYmE=", "fooba", 0);
308 test_unbase64mem_one("Zm9vYmFy", "foobar", 0);
310 test_unbase64mem_one(" ", "", 0);
311 test_unbase64mem_one(" \n\r ", "", 0);
312 test_unbase64mem_one(" Zg\n== ", "f", 0);
313 test_unbase64mem_one(" Zm 8=\r", "fo", 0);
314 test_unbase64mem_one(" Zm9\n\r\r\nv ", "foo", 0);
315 test_unbase64mem_one(" Z m9vYg==\n\r", "foob", 0);
316 test_unbase64mem_one(" Zm 9vYmE= ", "fooba", 0);
317 test_unbase64mem_one(" Z m9v YmFy ", "foobar", 0);
319 test_unbase64mem_one("A", NULL, -EPIPE);
320 test_unbase64mem_one("A====", NULL, -EINVAL);
321 test_unbase64mem_one("AAB==", NULL, -EINVAL);
322 test_unbase64mem_one(" A A A B = ", NULL, -EINVAL);
323 test_unbase64mem_one(" Z m 8 = q u u x ", NULL, -ENAMETOOLONG);
326 static void test_hexdump(void) {
330 hexdump(stdout, NULL, 0);
331 hexdump(stdout, "", 0);
332 hexdump(stdout, "", 1);
333 hexdump(stdout, "x", 1);
334 hexdump(stdout, "x", 2);
335 hexdump(stdout, "foobar", 7);
336 hexdump(stdout, "f\nobar", 7);
337 hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
339 for (i = 0; i < ELEMENTSOF(data); i++)
342 hexdump(stdout, data, sizeof(data));
345 int main(int argc, char *argv[]) {
348 test_base32hexchar();
349 test_unbase32hexchar();
358 test_unbase32hexmem();