Debian Codegraph @

t_iwildmatch.c

raw symbols SCIP
1 /* Unit test for KI-53: iwildmatch() must be case-insensitive on BOTH the text
2 * and the pattern. The old code folded only the text, so an upper-case pattern
3 * (e.g. a "hosts deny" token *.BADDOMAIN.COM) failed to match a lower-case host
4 * -> access-control fail-open. Exits 0 if all cases match, 1 otherwise. */
5
6 #include <stdio.h>
7
8 extern int iwildmatch(const char *pattern, const char *text);
9
10 static const struct { const char *p, *t; int exp; } cases[] = {
11 { "abc", "ABC", 1 }, /* text folded (always worked) */
12 { "ABC", "abc", 1 }, /* pattern folded (the fix) */
13 { "ABC", "abd", 0 },
14 { "*.EXAMPLE.COM", "foo.example.com", 1 },
15 { "*.example.com", "FOO.EXAMPLE.COM", 1 },
16 { "*.BADDOMAIN.COM", "x.baddomain.com", 1 }, /* the access-control case */
17 { "[A-Z]bc", "abc", 1 }, /* upper-case range folds */
18 { "[ABC]xy", "bxy", 1 }, /* upper-case class-member folds */
19 { "x[A-Z]z", "xyz", 1 }, /* range mid-pattern */
20 { "[a-z]BC", "abc", 1 }, /* mixed: class + literal fold */
21 { "[\\A]bc", "abc", 1 }, /* escaped class member folds */
22 { "[A-Z]bc", "5bc", 0 }, /* negative: digit not in range */
23 };
24
25 int main(int argc, char *argv[])
26 {
27 (void)argc; (void)argv;
28 int i, fails = 0, n = (int)(sizeof cases / sizeof cases[0]);
29 for (i = 0; i < n; i++) {
30 int r = iwildmatch(cases[i].p, cases[i].t);
31 if (r != cases[i].exp) {
32 printf("FAIL: iwildmatch(%s, %s) = %d, expected %d\n",
33 cases[i].p, cases[i].t, r, cases[i].exp);
34 fails++;
35 }
36 }
37 if (fails) {
38 printf("iwildmatch: %d case(s) wrong -- the pattern is not folded "
39 "(asymmetric case handling)\n", fails);
40 return 1;
41 }
42 printf("iwildmatch: case-insensitive on both text and pattern\n");
43 return 0;
44 }