1*d6e8b64aSMedicine Yeh #include <assert.h>
2*d6e8b64aSMedicine Yeh #include <stdlib.h>
3*d6e8b64aSMedicine Yeh #include <stdint.h>
4*d6e8b64aSMedicine Yeh #include <stdio.h>
5*d6e8b64aSMedicine Yeh
6*d6e8b64aSMedicine Yeh #ifndef SYSCONFDIR
7*d6e8b64aSMedicine Yeh // Bypass compilation error due to -DSYSCONFDIR not provided
8*d6e8b64aSMedicine Yeh #define SYSCONFDIR
9*d6e8b64aSMedicine Yeh #endif
10*d6e8b64aSMedicine Yeh
11*d6e8b64aSMedicine Yeh #include "config.c"
12*d6e8b64aSMedicine Yeh
13*d6e8b64aSMedicine Yeh struct test_parse_size_unit {
14*d6e8b64aSMedicine Yeh const char *test_str;
15*d6e8b64aSMedicine Yeh size_t expected_size;
16*d6e8b64aSMedicine Yeh int expected_rc;
17*d6e8b64aSMedicine Yeh };
18*d6e8b64aSMedicine Yeh
test_config_parse_bytesize(void)19*d6e8b64aSMedicine Yeh void test_config_parse_bytesize(void)
20*d6e8b64aSMedicine Yeh {
21*d6e8b64aSMedicine Yeh const struct test_parse_size_unit test_data[] = {
22*d6e8b64aSMedicine Yeh { NULL, 0, -1 },
23*d6e8b64aSMedicine Yeh { "", 0, -1 },
24*d6e8b64aSMedicine Yeh { "0", 0, -1 },
25*d6e8b64aSMedicine Yeh { "1", 1, 0 },
26*d6e8b64aSMedicine Yeh { "4k", 4ul * 1024ul, 0 },
27*d6e8b64aSMedicine Yeh { "6M", (6ul << 20), 0 },
28*d6e8b64aSMedicine Yeh { "4095M", (4095ul << 20), 0 },
29*d6e8b64aSMedicine Yeh { "2G", (2ul << 30), 0 },
30*d6e8b64aSMedicine Yeh { "8M\n", (8ul << 20), 0 }, /* Suffix ignored */
31*d6e8b64aSMedicine Yeh { " 10k", 10ul * 1024ul, 0 }, /* Leading spaces trimmed */
32*d6e8b64aSMedicine Yeh { "10k ", 10ul * 1024ul, 0 }, /* Trailing spaces trimmed */
33*d6e8b64aSMedicine Yeh { "\r\t10k \r\t", 10ul * 1024ul, 0 }, /* Spaces trimmed */
34*d6e8b64aSMedicine Yeh { " 10 kB ", 10ul * 1024ul, 0 }, /* Spaces trimmed */
35*d6e8b64aSMedicine Yeh { "11G", 0, -1 }, /* Overflow */
36*d6e8b64aSMedicine Yeh { "4294967296", 0, -1 }, /* Overflow */
37*d6e8b64aSMedicine Yeh { "4096M", 0, -1 }, /* Overflow */
38*d6e8b64aSMedicine Yeh { "65535G", 0, -1 }, /* Overflow */
39*d6e8b64aSMedicine Yeh { "xyz", 0, -1 }, /* Invalid */
40*d6e8b64aSMedicine Yeh { "000", 0, -1 }, /* Invalid */
41*d6e8b64aSMedicine Yeh { "0.1", 0, -1 }, /* Invalid */
42*d6e8b64aSMedicine Yeh { "9T", 0, -1 }, /* Invalid suffix */
43*d6e8b64aSMedicine Yeh };
44*d6e8b64aSMedicine Yeh const size_t num_tests =
45*d6e8b64aSMedicine Yeh sizeof(test_data) / sizeof(struct test_parse_size_unit);
46*d6e8b64aSMedicine Yeh size_t size;
47*d6e8b64aSMedicine Yeh size_t i;
48*d6e8b64aSMedicine Yeh int rc;
49*d6e8b64aSMedicine Yeh
50*d6e8b64aSMedicine Yeh for (i = 0; i < num_tests; i++) {
51*d6e8b64aSMedicine Yeh rc = config_parse_bytesize(test_data[i].test_str, &size);
52*d6e8b64aSMedicine Yeh
53*d6e8b64aSMedicine Yeh if (rc == -1 && rc != test_data[i].expected_rc) {
54*d6e8b64aSMedicine Yeh warn("[%zu] Str %s expected rc %d, got rc %d\n", i,
55*d6e8b64aSMedicine Yeh test_data[i].test_str, test_data[i].expected_rc,
56*d6e8b64aSMedicine Yeh rc);
57*d6e8b64aSMedicine Yeh } else if (rc == 0 && test_data[i].expected_size != size) {
58*d6e8b64aSMedicine Yeh warn("[%zu] Str %s expected size %zu, got size %zu\n",
59*d6e8b64aSMedicine Yeh i, test_data[i].test_str,
60*d6e8b64aSMedicine Yeh test_data[i].expected_size, size);
61*d6e8b64aSMedicine Yeh }
62*d6e8b64aSMedicine Yeh assert(rc == test_data[i].expected_rc);
63*d6e8b64aSMedicine Yeh if (rc == 0) {
64*d6e8b64aSMedicine Yeh assert(size == test_data[i].expected_size);
65*d6e8b64aSMedicine Yeh }
66*d6e8b64aSMedicine Yeh }
67*d6e8b64aSMedicine Yeh }
68*d6e8b64aSMedicine Yeh
main(void)69*d6e8b64aSMedicine Yeh int main(void)
70*d6e8b64aSMedicine Yeh {
71*d6e8b64aSMedicine Yeh test_config_parse_bytesize();
72*d6e8b64aSMedicine Yeh return EXIT_SUCCESS;
73*d6e8b64aSMedicine Yeh }
74