Debian Codegraph @

test/utils.c

raw
1 /*
2 * Copyright © 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14
15 #include "test.h"
16 #include "utils.h"
17 #include "utils-numbers.h"
18 #include "utils-paths.h"
19 #include "test/utils-text.h"
20
21 static void
22 test_string_functions(void)
23 {
24 char buffer[10];
25
26 assert(!snprintf_safe(buffer, 0, "foo"));
27 assert(!snprintf_safe(buffer, 1, "foo"));
28 assert(!snprintf_safe(buffer, 3, "foo"));
29
30 assert(snprintf_safe(buffer, 10, "foo"));
31 assert(streq(buffer, "foo"));
32
33 assert(!snprintf_safe(buffer, 10, "%s", "1234567890"));
34 assert(snprintf_safe(buffer, 10, "%s", "123456789"));
35
36 assert(streq_null("foo", "foo"));
37 assert(!streq_null("foobar", "foo"));
38 assert(!streq_null("foobar", NULL));
39 assert(!streq_null(NULL, "foobar"));
40 assert(streq_null(NULL, NULL));
41
42 const char text[] =
43 "123; // abc\n"
44 " // def\n"
45 "456 // ghi // jkl\n"
46 "// mno\n"
47 "//\n"
48 "ok; // pqr\n"
49 "foo\n";
50
51 char *out;
52 out = strip_lines(text, ARRAY_SIZE(text), "//");
53 assert_streq_not_null(
54 "strip_lines",
55 "123; \n"
56 "456 \n"
57 "ok; \n"
58 "foo\n",
59 out
60 );
61 free(out);
62
63 out = uncomment(text, ARRAY_SIZE(text), "//");
64 assert_streq_not_null(
65 "uncomment",
66 "123; abc\n"
67 " def\n"
68 "456 ghi // jkl\n"
69 " mno\n"
70 "\n"
71 "ok; pqr\n"
72 "foo\n",
73 out
74 );
75 free(out);
76 }
77
78 static void
79 test_path_functions(void)
80 {
81 /* Absolute paths */
82 assert(!is_absolute_path(""));
83 #ifdef _WIN32
84 assert(!is_absolute_path("path\\test"));
85 assert(is_absolute_path("c:\\test"));
86 assert(!is_absolute_path("c:test"));
87 assert(is_absolute_path("c:\\"));
88 assert(is_absolute_path("c:/"));
89 assert(!is_absolute_path("c:"));
90 assert(is_absolute_path("\\\\foo"));
91 assert(is_absolute_path("\\\\?\\foo"));
92 assert(is_absolute_path("\\\\?\\UNC\\foo"));
93 assert(is_absolute_path("/foo"));
94 assert(is_absolute_path("\\foo"));
95 #else
96 assert(!is_absolute_path("test/path"));
97 assert(is_absolute_path("/test" ));
98 assert(is_absolute_path("/" ));
99 #endif
100 }
101
102 static uint32_t
103 rand_uint32(void)
104 {
105 /* First decide how many bits we’ll actually use (1-32) */
106 int bits = 1 + (rand() % 32);
107
108 /* Generate a number with that many bits */
109 uint32_t result = 0;
110 while (bits > 0) {
111 int bits_this_round = bits > 16 ? 16 : bits;
112 result = (result << bits_this_round)
113 | (rand() & ((1U << bits_this_round) - 1));
114 bits -= bits_this_round;
115 }
116
117 return result;
118 }
119
120 static uint64_t
121 rand_uint64(void)
122 {
123 /* First decide how many bits we’ll actually use (1-64) */
124 int bits = 1 + (rand() % 64);
125
126 /* Generate a number with that many bits */
127 uint64_t result = 0;
128 while (bits > 0) {
129 int bits_this_round = bits > 16 ? 16 : bits;
130 result = (result << bits_this_round)
131 | (rand() & ((1U << bits_this_round) - 1));
132 bits -= bits_this_round;
133 }
134
135 return result;
136 }
137
138 /* NOLINTBEGIN(google-readability-function-size) */
139 static void
140 test_number_parsers(void)
141 {
142 /* Check the claim that it always works on normal strings using SIZE_MAX and
143 * that it always stops on the first NULL byte */
144 {
145 static const struct {
146 const char* input;
147 struct { int count; uint64_t val; } dec;
148 struct { int count; uint64_t val; } hex;
149 } tests[] = {
150 {
151 .input = "",
152 .dec = { 0, UINT64_C(0) },
153 .hex = { 0, UINT64_C(0) }
154 },
155 {
156 .input = "\0""123",
157 .dec = { 0, UINT64_C(0) },
158 .hex = { 0, UINT64_C(0) }
159 },
160 {
161 .input = "/",
162 .dec = { 0, UINT64_C(0) },
163 .hex = { 0, UINT64_C(0) }
164 },
165 {
166 .input = ";",
167 .dec = { 0, UINT64_C(0) },
168 .hex = { 0, UINT64_C(0) }
169 },
170 {
171 .input = "x",
172 .dec = { 0, UINT64_C(0) },
173 .hex = { 0, UINT64_C(0) }
174 },
175 {
176 .input = "/1",
177 .dec = { 0, UINT64_C(0) },
178 .hex = { 0, UINT64_C(0) }
179 },
180 {
181 .input = ";1",
182 .dec = { 0, UINT64_C(0) },
183 .hex = { 0, UINT64_C(0) }
184 },
185 {
186 .input = "x1",
187 .dec = { 0, UINT64_C(0) },
188 .hex = { 0, UINT64_C(0) }
189 },
190 {
191 .input = "0",
192 .dec = { 1, UINT64_C(0) },
193 .hex = { 1, UINT64_C(0) }
194 },
195 {
196 .input = "1",
197 .dec = { 1, UINT64_C(1) },
198 .hex = { 1, UINT64_C(1) }
199 },
200 {
201 .input = "123",
202 .dec = { 3, UINT64_C(123) },
203 .hex = { 3, UINT64_C(0x123)}
204 },
205 {
206 .input = "123x",
207 .dec = { 3, UINT64_C(123) },
208 .hex = { 3, UINT64_C(0x123)}
209 },
210 {
211 .input = "123""\0""456",
212 .dec = { 3, UINT64_C(123) },
213 .hex = { 3, UINT64_C(0x123)}
214 },
215 {
216 .input = "18446744073709551615",
217 .dec = { 20, UINT64_MAX },
218 .hex = { -1, UINT64_C(0x1844674407370955)}
219 },
220 {
221 .input = "18446744073709551616",
222 .dec = { -1, UINT64_C(1844674407370955161)},
223 .hex = { -1, UINT64_C(0x1844674407370955) }
224 },
225 {
226 .input = "99999999999999999999",
227 .dec = { -1, UINT64_C(9999999999999999999)},
228 .hex = { -1, UINT64_C(0x9999999999999999) }
229 },
230 {
231 .input = "184467440737095516150",
232 .dec = { -1, UINT64_MAX },
233 .hex = { -1, UINT64_C(0x1844674407370955)}
234 },
235 {
236 .input = "00000000000000000",
237 .dec = { 17, UINT64_C(0) },
238 .hex = { 17, UINT64_C(0) }
239 },
240 {
241 .input = "00000000000000001",
242 .dec = { 17, UINT64_C(1) },
243 .hex = { 17, UINT64_C(1) }
244 },
245 {
246 .input = "ffffffffffffffff",
247 .dec = { 0 , 0 },
248 .hex = { 16, UINT64_MAX}
249 },
250 {
251 .input = "ffffffffffffffff0",
252 .dec = { 0 , 0 },
253 .hex = { -1, UINT64_MAX}
254 },
255 {
256 .input = "10000000000000000",
257 .dec = { 17, UINT64_C(10000000000000000) },
258 .hex = { -1, UINT64_C(0x1000000000000000)}
259 },
260 {
261 .input = "fffffffffffffffff",
262 .dec = { 0 , 0 },
263 .hex = { -1, UINT64_MAX}
264 },
265
266 };
267 for (size_t k = 0; k < ARRAY_SIZE(tests); k++) {
268 const size_t len = strlen(tests[k].input);
269 /* Try different lengths */
270 const struct {const char* label; size_t len;} sizes[] = {
271 { .label = "buffer" , .len = len },
272 { .label = "string" , .len = len + 1 },
273 { .label = "SIZE_MAX", .len = SIZE_MAX}
274 };
275 for (unsigned int s = 0; s < ARRAY_SIZE(sizes); s++) {
276 int count;
277 /* Decimal */
278 uint64_t dec = 0;
279 count =
280 parse_dec_to_uint64_t(tests[k].input, sizes[s].len, &dec);
281 assert_printf(count == tests[k].dec.count,
282 "Dec %s #%zu \"%s\" (%zu), "
283 "expected: %d, got: %d\n",
284 sizes[s].label, k, tests[k].input, sizes[s].len,
285 tests[k].dec.count, count);
286 assert_printf(dec == tests[k].dec.val,
287 "Dec %s #%zu \"%s\", "
288 "expected: %"PRIu64", got: %"PRIu64"\n",
289 sizes[s].label, k, tests[k].input,
290 tests[k].dec.val, dec);
291 /* Hexadecimal */
292 uint64_t hex = 0;
293 count =
294 parse_hex_to_uint64_t(tests[k].input, sizes[s].len, &hex);
295 assert_printf(count == tests[k].hex.count,
296 "Hex %s #%zu \"%s\" (%zu), "
297 "expected: %d, got: %d\n",
298 sizes[s].label, k, tests[k].input, sizes[s].len,
299 tests[k].hex.count, count);
300 assert_printf(hex == tests[k].hex.val,
301 "Hex %s #%zu \"%s\", "
302 "expected: %#"PRIx64", got: %#"PRIx64"\n",
303 sizes[s].label, k, tests[k].input,
304 tests[k].hex.val, hex);
305 }
306 }
307 }
308
309 #define PRIuint64_t PRIx64
310 #define PRIuint32_t PRIx32
311 #define test_parse_to(type, format, input, count, expected) do { \
312 type n = 0; \
313 int r = parse_##format##_to_##type(input, ARRAY_SIZE(input), &n);\
314 assert_printf(r == (count), \
315 "Buffer: expected count: %d, " \
316 "got: %d (value: %#"PRI##type", string: %.*s)\n", \
317 count, r, n, (int) ARRAY_SIZE(input), (input)); \
318 assert_printf(n == (expected), \
319 "Buffer: expected value: %#"PRI##type", got: " \
320 "%#"PRI##type"\n", expected, n); \
321 } while (0)
322
323 /* Test syntax variants */
324 const uint64_t values[] = {
325 UINT64_C(0),
326 UINT64_C(1),
327 UINT64_C(10),
328 UINT64_C(0xA),
329 UINT64_C(0xF),
330 UINT64_C(123),
331 UINT32_MAX / 10,
332 UINT32_MAX / 10 + 9,
333 UINT32_MAX >> 4,
334 UINT32_MAX >> 4 | 0xf,
335 UINT32_MAX - 1,
336 UINT32_MAX,
337 UINT32_MAX + UINT64_C(1),
338 UINT64_C(9999999999999999999),
339 UINT64_MAX / 10,
340 UINT64_MAX / 10 + 9,
341 UINT64_MAX >> 4,
342 UINT64_MAX >> 4 | 0xf,
343 UINT64_MAX - 1,
344 UINT64_MAX,
345 };
346 char buffer[30] = {0};
347 for (size_t k = 0; k < ARRAY_SIZE(values); k++) {
348 int count;
349 /* Basic: decimal */
350 count = snprintf(buffer, sizeof(buffer), "%"PRIu32, (uint32_t) values[k]);
351 assert(count > 0);
352 test_parse_to(uint32_t, dec, buffer, count, (uint32_t) values[k]);
353 count = snprintf(buffer, sizeof(buffer), "%"PRIu64, values[k]);
354 assert(count > 0);
355 test_parse_to(uint64_t, dec, buffer, count, values[k]);
356 /* Basic: hex lower case */
357 count = snprintf(buffer, sizeof(buffer), "%"PRIx32, (uint32_t) values[k]);
358 assert(count > 0);
359 test_parse_to(uint32_t, hex, buffer, count, (uint32_t) values[k]);
360 count = snprintf(buffer, sizeof(buffer), "%"PRIx64, values[k]);
361 assert(count > 0);
362 test_parse_to(uint64_t, hex, buffer, count, values[k]);
363 /* Basic: hex upper case */
364 count = snprintf(buffer, sizeof(buffer), "%"PRIX32, (uint32_t) values[k]);
365 assert(count > 0);
366 test_parse_to(uint32_t, hex, buffer, count, (uint32_t) values[k]);
367 count = snprintf(buffer, sizeof(buffer), "%"PRIX64, values[k]);
368 assert(count > 0);
369 test_parse_to(uint64_t, hex, buffer, count, values[k]);
370 /* Prefix with some zeroes */
371 for (int z = 0; z < 10 ; z++) {
372 /* Decimal */
373 count = snprintf(buffer, sizeof(buffer), "%0*"PRIu32,
374 z, (uint32_t) values[k]);
375 assert(count > 0);
376 test_parse_to(uint32_t, dec, buffer, count, (uint32_t) values[k]);
377 count = snprintf(buffer, sizeof(buffer), "%0*"PRIu64,
378 z, values[k]);
379 assert(count > 0);
380 test_parse_to(uint64_t, dec, buffer, count, values[k]);
381 /* Hexadecimal */
382 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIx32,
383 z, 0, (uint32_t) values[k]);
384 assert(count > 0);
385 test_parse_to(uint32_t, hex, buffer, count, (uint32_t) values[k]);
386 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIx64,
387 z, 0, values[k]);
388 assert(count > 0);
389 test_parse_to(uint64_t, hex, buffer, count, values[k]);
390 /* Append some garbage */
391 for (int c = 0; c < 0x100 ; c++) {
392 if (c < '0' || c > '9') {
393 /* Decimal */
394 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIu32"%c",
395 z, 0, (uint32_t) values[k], (char) c);
396 assert(count > 0);
397 test_parse_to(uint32_t, dec, buffer, count - 1, (uint32_t) values[k]);
398 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIu64"%c",
399 z, 0, values[k], (char) c);
400 assert(count > 0);
401 test_parse_to(uint64_t, dec, buffer, count - 1, values[k]);
402 }
403 if (!is_xdigit((char) c)) {
404 /* Hexadecimal */
405 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIx32"%c",
406 z, 0, (uint32_t) values[k], (char) c);
407 assert(count > 0);
408 test_parse_to(uint32_t, hex, buffer, count - 1, (uint32_t) values[k]);
409 count = snprintf(buffer, sizeof(buffer), "%0*u%"PRIx64"%c",
410 z, 0, values[k], (char) c);
411 assert(count > 0);
412 test_parse_to(uint64_t, hex, buffer, count - 1, values[k]);
413 }
414 }
415 }
416 }
417
418 /* Random */
419 for (unsigned int k = 0; k < 10000; k++) {
420 const uint32_t x32 = rand_uint32();
421 const uint64_t x64 = rand_uint64();
422 int count;
423 /* Hex: Lower case */
424 count = snprintf(buffer, sizeof(buffer), "%"PRIx32, x32);
425 assert(count > 0);
426 test_parse_to(uint32_t, hex, buffer, count, x32);
427 count = snprintf(buffer, sizeof(buffer), "%"PRIx64, x64);
428 assert(count > 0);
429 test_parse_to(uint64_t, hex, buffer, count, x64);
430 /* Hex: Upper case (32 bits) */
431 count = snprintf(buffer, sizeof(buffer), "%"PRIX32, x32);
432 assert(count > 0);
433 test_parse_to(uint32_t, hex, buffer, count, x32);
434 /* Hex: some garbage after */
435 buffer[count] = (char) (((unsigned int) rand()) % '0');
436 test_parse_to(uint32_t, hex, buffer, count, x32);
437 /* Hex: Upper case (64 bits) */
438 count = snprintf(buffer, sizeof(buffer), "%"PRIX64, x64);
439 assert(count > 0);
440 test_parse_to(uint64_t, hex, buffer, count, x64);
441 /* Hex: some garbage after */
442 buffer[count] = (char) (((unsigned int) rand()) % '0');
443 test_parse_to(uint64_t, hex, buffer, count, x64);
444 /* Decimal (32 bits) */
445 count = snprintf(buffer, sizeof(buffer), "%"PRIu32, x32);
446 assert(count > 0);
447 test_parse_to(uint32_t, dec, buffer, count, x32);
448 /* Decimal: some garbage after */
449 buffer[count] = (char) (((unsigned int) rand()) % '0');
450 test_parse_to(uint32_t, dec, buffer, count, x32);
451 /* Decimal (64 bits) */
452 count = snprintf(buffer, sizeof(buffer), "%"PRIu64, x64);
453 assert(count > 0);
454 test_parse_to(uint64_t, dec, buffer, count, x64);
455 buffer[count] = (char) (((unsigned int) rand()) % '0');
456 test_parse_to(uint64_t, dec, buffer, count, x64);
457 }
458 }
459 /* NOLINTEND(google-readability-function-size) */
460
461 /* CLI positional arguments:
462 * 1. Seed for the pseudo-random generator:
463 * - Leave it unset or set it to “-” to use current time.
464 * - Use an integer to set it explicitly.
465 */
466 int
467 main(int argc, char *argv[])
468 {
469 test_init();
470
471 /* Initialize pseudo-random generator with program arg or current time */
472 unsigned int seed;
473 if (argc >= 2 && !streq(argv[1], "-")) {
474 seed = (unsigned int) atoi(argv[1]);
475 } else {
476 seed = (unsigned int) time(NULL);
477 }
478 fprintf(stderr, "Seed for the pseudo-random generator: %u\n", seed);
479 srand(seed);
480
481 test_string_functions();
482 test_path_functions();
483 test_number_parsers();
484
485 return EXIT_SUCCESS;
486 }