|
6 | 6 | #include "flb_tests_internal.h" |
7 | 7 |
|
8 | 8 | struct check { |
9 | | - char *tag; |
10 | | - char *match; |
11 | | - int matched; |
| 9 | + const char *tag; |
| 10 | + size_t tag_len; |
| 11 | + const char *match; |
| 12 | + int matched; |
12 | 13 | }; |
13 | 14 |
|
14 | | -struct check route_checks[] = { |
15 | | - |
16 | | - {"file.apache.log", "file.*.log" , FLB_TRUE}, |
17 | | - {"cpu.rpi" , "cpu.rpi" , FLB_TRUE}, |
18 | | - {"cpu.rpi" , "cpu.*" , FLB_TRUE}, |
19 | | - {"cpu.rpi" , "*" , FLB_TRUE}, |
20 | | - {"cpu.rpi" , "*.*" , FLB_TRUE}, |
21 | | - {"cpu.rpi" , "*.rpi" , FLB_TRUE}, |
22 | | - {"cpu.rpi" , "mem.*" , FLB_FALSE}, |
23 | | - {"cpu.rpi" , "*u.r*" , FLB_TRUE}, |
24 | | - {"hoge" , "hogeeeeeee" , FLB_FALSE}, |
25 | | - {"test" , "test" , FLB_TRUE} |
| 15 | +static const struct check wildcard_checks[] = { |
| 16 | + {"file.apache.log", 0, "file.*.log" , FLB_TRUE}, |
| 17 | + {"cpu.rpi" , 0, "cpu.rpi" , FLB_TRUE}, |
| 18 | + {"cpu.rpi" , 0, "cpu.*" , FLB_TRUE}, |
| 19 | + {"cpu.rpi" , 0, "*" , FLB_TRUE}, |
| 20 | + {"cpu.rpi" , 0, "*.*" , FLB_TRUE}, |
| 21 | + {"cpu.rpi" , 0, "*.rpi" , FLB_TRUE}, |
| 22 | + {"cpu.rpi" , 0, "mem.*" , FLB_FALSE}, |
| 23 | + {"cpu.rpi" , 0, "*u.r*" , FLB_TRUE}, |
| 24 | + {"hoge" , 0, "hogeeeeeee" , FLB_FALSE}, |
| 25 | + {"test" , 0, "test" , FLB_TRUE} |
26 | 26 | }; |
27 | 27 |
|
28 | 28 | void test_router_wildcard() |
29 | 29 | { |
30 | | - int i; |
| 30 | + size_t i; |
31 | 31 | int ret; |
32 | | - int len; |
33 | | - int checks = 0; |
34 | | - struct check *c; |
| 32 | + size_t len; |
| 33 | + size_t checks = 0; |
| 34 | + const struct check *c; |
35 | 35 |
|
36 | | - checks = sizeof(route_checks) / sizeof(struct check); |
| 36 | + checks = sizeof(wildcard_checks) / sizeof(wildcard_checks[0]); |
37 | 37 | for (i = 0; i < checks; i++) { |
38 | | - c = &route_checks[i]; |
39 | | - len = strlen(c->tag); |
| 38 | + c = &wildcard_checks[i]; |
| 39 | + len = c->tag_len; |
| 40 | + if (len == 0 && c->tag) { |
| 41 | + len = strlen(c->tag); |
| 42 | + } |
40 | 43 | ret = flb_router_match(c->tag, len, c->match, NULL); |
41 | 44 | TEST_CHECK(ret == c->matched); |
42 | 45 | if (ret != c->matched) { |
43 | 46 | fprintf(stderr, "test %i failed: tag=%s match=%s expected_to_match=%s\n", |
44 | | - i, c->tag, c->match, c->matched ? "YES": "NO"); |
| 47 | + (int) i, c->tag, c->match, c->matched ? "YES": "NO"); |
45 | 48 | } |
46 | 49 | } |
47 | 50 |
|
48 | 51 | ret = flb_router_match("aaaX", 3, "aaa", NULL); |
49 | 52 | TEST_CHECK(ret == FLB_TRUE); |
50 | 53 | } |
51 | 54 |
|
| 55 | +static void print_tag(const char *tag, size_t len) |
| 56 | +{ |
| 57 | + size_t i; |
| 58 | + |
| 59 | + if (tag == NULL) { |
| 60 | + fputs("<NULL>", stderr); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + for (i = 0; i < len; i++) { |
| 65 | + unsigned char c = (unsigned char) tag[i]; |
| 66 | + if (c < 0x20 || c > 0x7e) { |
| 67 | + fprintf(stderr, "\\x%02x", c); |
| 68 | + } |
| 69 | + else { |
| 70 | + fputc(c, stderr); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +static const char raw_tag[] = {'m','e','t','r','i','c','s'}; |
| 76 | +static const char newline_tag[] = {'s','y','s','t','e','m','\n'}; |
| 77 | +static const char repeated_star[] = {'a','b','c','d'}; |
| 78 | +static const char truncated[] = {'a','b','c'}; |
| 79 | +static const char empty_tag[] = {'\0'}; |
| 80 | + |
| 81 | +static const struct check route_checks[] = { |
| 82 | + {raw_tag, sizeof(raw_tag), "metrics", FLB_TRUE}, |
| 83 | + {raw_tag, sizeof(raw_tag), "metrics.*", FLB_FALSE}, |
| 84 | + {newline_tag, sizeof(newline_tag), "system\n", FLB_TRUE}, |
| 85 | + {newline_tag, sizeof(newline_tag), "system", FLB_FALSE}, |
| 86 | + {repeated_star, sizeof(repeated_star), "**d", FLB_TRUE}, |
| 87 | + {repeated_star, sizeof(repeated_star), "*c*", FLB_TRUE}, |
| 88 | + {repeated_star, sizeof(repeated_star), "*e*", FLB_FALSE}, |
| 89 | + {NULL, 0, "", FLB_TRUE}, |
| 90 | + {NULL, 0, "*", FLB_TRUE}, |
| 91 | + {empty_tag, 0, "", FLB_TRUE}, |
| 92 | + {empty_tag, 0, "*", FLB_TRUE}, |
| 93 | + {raw_tag, sizeof(raw_tag), NULL, FLB_FALSE}, |
| 94 | + {truncated, 2, "ab", FLB_TRUE}, |
| 95 | + {truncated, 2, "abc", FLB_FALSE} |
| 96 | +}; |
| 97 | + |
| 98 | +void test_router_edge_cases() |
| 99 | +{ |
| 100 | + size_t i; |
| 101 | + int ret; |
| 102 | + size_t checks = sizeof(route_checks) / sizeof(route_checks[0]); |
| 103 | + |
| 104 | + for (i = 0; i < checks; i++) { |
| 105 | + const struct check *c = &route_checks[i]; |
| 106 | + |
| 107 | + ret = flb_router_match(c->tag, (int) c->tag_len, c->match, NULL); |
| 108 | + TEST_CHECK(ret == c->matched); |
| 109 | + if (ret != c->matched) { |
| 110 | + const char *match = c->match ? c->match : "<NULL>"; |
| 111 | + fprintf(stderr, "edge test %zu failed: tag=", i); |
| 112 | + print_tag(c->tag, c->tag_len); |
| 113 | + fprintf(stderr, " match=%s expected_to_match=%s\n", |
| 114 | + match, c->matched ? "YES" : "NO"); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
52 | 119 | TEST_LIST = { |
53 | 120 | { "wildcard", test_router_wildcard}, |
| 121 | + { "edge_cases", test_router_edge_cases}, |
54 | 122 | { 0 } |
55 | 123 | }; |
0 commit comments