utils.c 17 KB

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