Skip to content

Commit 4d9593e

Browse files
committed
tests: internal: router: add edge cases for matching rules
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 7e2a887 commit 4d9593e

File tree

1 file changed

+91
-23
lines changed

1 file changed

+91
-23
lines changed

tests/internal/router.c

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,118 @@
66
#include "flb_tests_internal.h"
77

88
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;
1213
};
1314

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}
2626
};
2727

2828
void test_router_wildcard()
2929
{
30-
int i;
30+
size_t i;
3131
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;
3535

36-
checks = sizeof(route_checks) / sizeof(struct check);
36+
checks = sizeof(wildcard_checks) / sizeof(wildcard_checks[0]);
3737
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+
}
4043
ret = flb_router_match(c->tag, len, c->match, NULL);
4144
TEST_CHECK(ret == c->matched);
4245
if (ret != c->matched) {
4346
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");
4548
}
4649
}
4750

4851
ret = flb_router_match("aaaX", 3, "aaa", NULL);
4952
TEST_CHECK(ret == FLB_TRUE);
5053
}
5154

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+
52119
TEST_LIST = {
53120
{ "wildcard", test_router_wildcard},
121+
{ "edge_cases", test_router_edge_cases},
54122
{ 0 }
55123
};

0 commit comments

Comments
 (0)