1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
8 #include "alloc-util.h"
11 #include "string-util.h"
13 /// Additional includes needed by elogind
14 #include "musl_missing.h"
16 static void test_hexchar(void) {
17 assert_se(hexchar(0xa) == 'a');
18 assert_se(hexchar(0x0) == '0');
21 static void test_unhexchar(void) {
22 assert_se(unhexchar('a') == 0xA);
23 assert_se(unhexchar('A') == 0xA);
24 assert_se(unhexchar('0') == 0x0);
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');
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);
42 static void test_base64char(void) {
43 assert_se(base64char(0) == 'A');
44 assert_se(base64char(26) == 'a');
45 assert_se(base64char(63) == '/');
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);
60 static void test_octchar(void) {
61 assert_se(octchar(00) == '0');
62 assert_se(octchar(07) == '7');
65 static void test_unoctchar(void) {
66 assert_se(unoctchar('0') == 00);
67 assert_se(unoctchar('7') == 07);
70 static void test_decchar(void) {
71 assert_se(decchar(0) == '0');
72 assert_se(decchar(9) == '9');
75 static void test_undecchar(void) {
76 assert_se(undecchar('0') == 0);
77 assert_se(undecchar('9') == 9);
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;
85 assert_se(unhexmem(s, l, &mem, &len) == retval);
92 assert_se(hex = hexmem(mem, len));
93 answer = strndupa(s, l);
94 assert_se(streq(delete_chars(answer, WHITESPACE), hex));
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";
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);
116 /* https://tools.ietf.org/html/rfc4648#section-10 */
117 static void test_base32hexmem(void) {
120 b32 = base32hexmem("", STRLEN(""), true);
122 assert_se(streq(b32, ""));
125 b32 = base32hexmem("f", STRLEN("f"), true);
127 assert_se(streq(b32, "CO======"));
130 b32 = base32hexmem("fo", STRLEN("fo"), true);
132 assert_se(streq(b32, "CPNG===="));
135 b32 = base32hexmem("foo", STRLEN("foo"), true);
137 assert_se(streq(b32, "CPNMU==="));
140 b32 = base32hexmem("foob", STRLEN("foob"), true);
142 assert_se(streq(b32, "CPNMUOG="));
145 b32 = base32hexmem("fooba", STRLEN("fooba"), true);
147 assert_se(streq(b32, "CPNMUOJ1"));
150 b32 = base32hexmem("foobar", STRLEN("foobar"), true);
152 assert_se(streq(b32, "CPNMUOJ1E8======"));
155 b32 = base32hexmem("", STRLEN(""), false);
157 assert_se(streq(b32, ""));
160 b32 = base32hexmem("f", STRLEN("f"), false);
162 assert_se(streq(b32, "CO"));
165 b32 = base32hexmem("fo", STRLEN("fo"), false);
167 assert_se(streq(b32, "CPNG"));
170 b32 = base32hexmem("foo", STRLEN("foo"), false);
172 assert_se(streq(b32, "CPNMU"));
175 b32 = base32hexmem("foob", STRLEN("foob"), false);
177 assert_se(streq(b32, "CPNMUOG"));
180 b32 = base32hexmem("fooba", STRLEN("fooba"), false);
182 assert_se(streq(b32, "CPNMUOJ1"));
185 b32 = base32hexmem("foobar", STRLEN("foobar"), false);
187 assert_se(streq(b32, "CPNMUOJ1E8"));
191 static void test_unbase32hexmem_one(const char *hex, bool padding, int retval, const char *ans) {
192 _cleanup_free_ void *mem = NULL;
195 assert_se(unbase32hexmem(hex, (size_t) -1, padding, &mem, &len) == retval);
199 str = strndupa(mem, len);
200 assert_se(streq(str, ans));
204 static void test_unbase32hexmem(void) {
205 test_unbase32hexmem_one("", true, 0, "");
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");
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);
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);
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);
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);
252 /* https://tools.ietf.org/html/rfc4648#section-10 */
253 static void test_base64mem(void) {
256 assert_se(base64mem("", STRLEN(""), &b64) == 0);
257 assert_se(streq(b64, ""));
260 assert_se(base64mem("f", STRLEN("f"), &b64) == 4);
261 assert_se(streq(b64, "Zg=="));
264 assert_se(base64mem("fo", STRLEN("fo"), &b64) == 4);
265 assert_se(streq(b64, "Zm8="));
268 assert_se(base64mem("foo", STRLEN("foo"), &b64) == 4);
269 assert_se(streq(b64, "Zm9v"));
272 assert_se(base64mem("foob", STRLEN("foob"), &b64) == 8);
273 assert_se(streq(b64, "Zm9vYg=="));
276 assert_se(base64mem("fooba", STRLEN("fooba"), &b64) == 8);
277 assert_se(streq(b64, "Zm9vYmE="));
280 assert_se(base64mem("foobar", STRLEN("foobar"), &b64) == 8);
281 assert_se(streq(b64, "Zm9vYmFy"));
285 static void test_unbase64mem_one(const char *input, const char *output, int ret) {
286 _cleanup_free_ void *buffer = NULL;
289 assert_se(unbase64mem(input, (size_t) -1, &buffer, &size) == ret);
292 assert_se(size == strlen(output));
293 assert_se(memcmp(buffer, output, size) == 0);
294 assert_se(((char*) buffer)[size] == 0);
298 static void test_unbase64mem(void) {
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);
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);
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);
324 static void test_hexdump(void) {
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);
337 for (i = 0; i < ELEMENTSOF(data); i++)
340 hexdump(stdout, data, sizeof(data));
343 int main(int argc, char *argv[]) {
346 test_base32hexchar();
347 test_unbase32hexchar();
356 test_unbase32hexmem();