9c45495d3ac4b4aef0256c0619a5f75162ef7eac
[ircu2.10.12-pk.git] / ircd / test / ircd_match_t.c
1 /*
2  * ircd_match_t.c - test cases for irc glob matching
3  */
4
5 #include "ircd_log.h"
6 #include "match.h"
7
8 #include <errno.h>    /* errno */
9 #include <fcntl.h>    /* O_RDONLY */
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/mman.h> /* mmap(), munmap() */
13 #include <unistd.h>   /* sysconf() */
14
15 #if !defined(MAP_ANONYMOUS)
16 # if defined(MAP_ANON)
17 #  define MAP_ANONYMOUS MAP_ANON
18 # else
19 #  error I do not know how to request an anonymous mmap from your OS.
20 # endif
21 #endif
22
23 struct match_test {
24   const char *glob;
25   const char *should_match;
26   const char *shouldnt_match;
27 };
28
29 const struct match_test match_tests[] = {
30   { "\\*",
31     "*\0",
32     "a\0*PeacefuL*\0" },
33   { "*a*",
34     "a\0pizza\0abe\0brack\0",
35     "b\0" },
36   { "?",
37     "*\0a\0?\0",
38     "*PeacefuL*\0pizza\0???\0" },
39   { "abc",
40     "abc\0",
41     "abcd\0cabc\0" },
42   { "*abc",
43     "abc\0fooabc\0ababc\0",
44     "abra\0abcd\0" },
45   { "\\?",
46     "?\0",
47     "a\0" },
48   { "*\\\\[*!~*",
49     "har\\[dy!~boy\0",
50     "dark\\s|de!pimp\0joe\\[mama\0" },
51   { NULL, NULL, NULL }
52 };
53
54 int test_match(const char glob[], const char name[])
55 {
56   static unsigned int page_size;
57   static char *pages;
58   char *test_glob;
59   char *test_name;
60   size_t length;
61   int res;
62
63   /* If we have not yet set up our test mappings, do so. */
64   if (!page_size)
65   {
66     int dev_zero_fd;
67
68     page_size = sysconf(_SC_PAGE_SIZE);
69     if (page_size == 0 || page_size == (unsigned int)-1)
70     {
71       fprintf(stderr, "sysconf(_SC_PAGE_SIZE) failed: %s\n", strerror(errno));
72       assert(0);
73     }
74     dev_zero_fd = open("/dev/zero", O_RDONLY);
75     /* If dev_zero_fd == -1 (failed), we may still be able to mmap anonymously. */
76     pages = mmap(NULL, 4 * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, dev_zero_fd, 0);
77     if (pages == MAP_FAILED)
78     {
79       /* Try using fd == -1 for MAP_ANONYMOUS, which BSD systems require. */
80       pages = mmap(NULL, 4 * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
81     }
82     if (pages == MAP_FAILED)
83     {
84       fprintf(stderr, "Unable to map pages: %s\n", strerror(errno));
85       assert(0);
86     }
87     if (dev_zero_fd >= 0)
88     {
89       close(dev_zero_fd);
90       dev_zero_fd = -1;
91     }
92     res = munmap(pages + page_size * 1, page_size);
93     if (res < 0)
94     {
95       fprintf(stderr, "Unable to unmap page 2/4: %s\n", strerror(errno));
96       /* Dysfunctional OSes */
97     }
98     munmap(pages + page_size * 3, page_size);
99     if (res < 0)
100     {
101       fprintf(stderr, "Unable to unmap page 4/4: %s\n", strerror(errno));
102     }
103   }
104
105   /* Copy the strings to the end of their respective pages. */
106   length = strlen(glob) + 1;
107   test_glob = pages + page_size * 1 - length;
108   memcpy(test_glob, glob, length);
109   length = strlen(name) + 1;
110   test_name = pages + page_size * 3 - length;
111   memcpy(test_name, name, length);
112
113   /* Perform the test. */
114   return match(test_glob, test_name);
115 }
116
117 void do_match_test(const struct match_test *test)
118 {
119   const char *candidate;
120   unsigned int matched, not_matched;
121   int res;
122
123   for (candidate = test->should_match, matched = 0;
124        *candidate;
125        candidate += strlen(candidate) + 1, ++matched) {
126     res = test_match(test->glob, candidate);
127     if (res != 0) {
128       fprintf(stderr, "\"%s\" failed to match \"%s\".\n", test->glob, candidate);
129       assert(0);
130     }
131   }
132
133   for (candidate = test->shouldnt_match, not_matched = 0;
134        *candidate;
135        candidate += strlen(candidate) + 1, ++not_matched) {
136     res = test_match(test->glob, candidate);
137     if (res == 0) {
138       fprintf(stderr, "\"%s\" incorrectly matched \"%s\".\n", test->glob, candidate);
139       assert(0);
140     }
141   }
142
143   printf("Passed: %s (%u matches, %u non-matches)\n",
144          test->glob, matched, not_matched);
145 }
146
147 int main(int argc, char *argv[])
148 {
149   const struct match_test *match;
150   for (match = match_tests; match->glob; ++match)
151     do_match_test(match);
152   return 0;
153 }