e37a5c2f267b452e6ae1807eeabd015fc722ef90
[ossec-hids.git] / src / tests / test_os_regex.c
1 /* Copyright (C) 2014 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include <check.h>
11 #include <stdlib.h>
12 #include "../os_regex/os_regex.h"
13 #include "../os_regex/os_regex_internal.h"
14
15 Suite *test_suite(void);
16
17 START_TEST(test_success_match1)
18 {
19
20     int i;
21     const char *tests[][3] = {
22         {"abc", "abcd", ""},
23         {"abcd", "abcd", ""},
24         {"a", "a", ""},
25         {"a", "aa", ""},
26         {"^a", "ab", ""},
27         {"test", "testa", ""},
28         {"test", "testest", ""},
29         {"lalaila", "lalalalaila", ""},
30         {"abc|cde", "cde", ""},
31         {"^aa|ee|ii|oo|uu", "dfgdsii", ""},
32         {"Abc", "abc", ""},
33         {"ZBE", "zbe", ""},
34         {"ABC", "ABc", ""},
35         {"^A", "a", ""},
36         {"a|E", "abcdef", ""},
37         {"daniel", "daniel", ""},
38         {"DANIeL", "daNIel", ""},
39         {"^abc ", "abc ", ""},
40         {"ddd|eee|fff|ggg|ggg|hhh|iii", "iii", ""},
41         {"kwo|fe|fw|wfW|edW|dwDF|WdW|dw|d|^la", "la", ""},
42         {"^a", "a", ""},
43         {"^ab$", "ab", ""},
44         {"c$", "c", ""},
45         {"c$", "lalalalac", ""},
46         {"^bin$|^shell$", "bin", ""},
47         {"^bin$|^shell$", "shell", ""},
48         {"^bin$|^shell$|^ftp$", "shell", ""},
49         {"^bin$|^shell$|^ftp$", "ftp", ""},
50         {NULL, NULL, NULL}
51     };
52
53     for(i=0; tests[i][0] != NULL ; i++) {
54         ck_assert_msg(OS_Match2(tests[i][0],tests[i][1]),
55                       "%s should have OS_Match2 true with %s: Ref: %s",
56                       tests[i][0], tests[i][1], tests[i][1]);
57     }
58 }
59 END_TEST
60
61 START_TEST(test_fail_match1)
62 {
63
64     int i;
65     const char *tests[][3] = {
66         {"abc", "abb", ""},
67         {"^ab", " ab", ""},
68         {"test", "tes", ""},
69         {"abcd", "abc", ""},
70         {"abbb", "abb", ""},
71         {"abbbbbbbb", "abbbbbbb", ""},
72         {"a|b|c| ", "def", ""},
73         {"lala$", "lalalalalal", ""},
74         {"^ab$", "abc", ""},
75         {"zzzz$", "zzzzzzzzzzzz ", ""},
76         {"^bin$|^shell$", "bina", ""},
77         {"^bin$|^shell$", "shella", ""},
78         {"^bin$|^shell$", "ashell", ""},
79         {NULL, NULL, NULL}
80     };
81
82     for(i=0; tests[i][0] != NULL ; i++) {
83         ck_assert_msg(!OS_Match2(tests[i][0],tests[i][1]),
84                       "%s should have OS_Match2 false with %s: Ref: %s",
85                       tests[i][0], tests[i][1], tests[i][2]);
86     }
87 }
88 END_TEST
89
90 START_TEST(test_success_regex1)
91 {
92
93     int i;
94     /*
95      * Please note that all strings are \ escaped
96      */
97     const char *tests[][3] = {
98         {"abc", "abcd", ""},
99         {"abcd", "abcd", ""},
100         {"a", "a", ""},
101         {"a", "aa", ""},
102         {"^a", "ab", ""},
103         {"test", "testa", ""},
104         {"test", "testest", ""},
105         {"lalaila", "lalalalaila", ""},
106         {"abc|cde", "cde", ""},
107         {"^aa|ee|ii|oo|uu", "dfgdsii", ""},
108         {"Abc", "abc", ""},
109         {"ZBE", "zbe", ""},
110         {"ABC", "ABc", ""},
111         {"^A", "a", ""},
112         {"a|E", "abcdef", ""},
113         {"daniel", "daniel", ""},
114         {"DANIeL", "daNIel", ""},
115         {"^abc ", "abc ", ""},
116         {"ddd|eee|fff|ggg|ggg|hhh|iii", "iii", ""},
117         {"kwo|fe|fw|wfW|edW|dwDF|WdW|dw|d|^la", "la", ""},
118         {"^a", "a", ""},
119         {"^ab$", "ab", ""},
120         {"c$", "c", ""},
121         {"c$", "lalalalac", ""},
122         {"^bin$|^shell$", "bin", ""},
123         {"^bin$|^shell$", "shell", ""},
124         {"^bin$|^shell$|^ftp$", "shell", ""},
125         {"^bin$|^shell$|^ftp$", "ftp", ""},
126         {"\\s+123", "  123", ""},
127         {"\\s*123", "123", ""},
128         {"\\s123", " 123", ""},
129         {"\\w+\\s+\\w+", "a 1", ""},
130         {"\\w+\\d+\\w+\\s+", "ab12fb12fd12 ", ""},
131         {"^\\s*\\w\\s*\\w+", "a   l a  a", ""},
132         {"\\w+\\s+\\w+\\d+\\s$", "a aa11 ", ""},
133         {"^su\\S*: BAD su", "su: BAD SU dcid to root on /dev/ttyp0", ""},
134         {"^su\\s*: BAD su", "su: BAD SU dcid to root on /dev/ttyp0", ""},
135         {"^abc\\sabc", "abc abcd", ""},
136         {"^abc\\s\\s*abc", "abc abcd", ""},
137         {"^\\s+\\sl", "     lala", ""},
138         {"^\\s*\\sl", "     lala", ""},
139         {"^\\s\\s+l", "     lala", ""},
140         {"^\\s+\\s l", "     lala", ""},
141         {"^\\s*\\s lal\\w$", "  lala", ""},
142         {"test123test\\d+$", "test123test123", ""},
143         {"^kernel: \\S+ \\.+ SRC=\\S+ DST=\\S+ \\.+ PROTO=\\w+ SPT=\\d+ DPT=\\d+ ", "kernel: IPTABLE IN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:03:93:db:2e:b4:08:00 SRC=10.4.11.40 DST=255.255.255.255 LEN=180 TOS=0x00 PREC=0x00 TTL=64 ID=4753 PROTO=UDP SPT=49320 DPT=2222 LEN=160", ""},
144         {"test (\\w+)la", "test abclala", ""},
145         {"(\\w+) (\\w+)", "wofl wofl", ""},
146         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:469:3] ICMP PING NMAP [Classification: Attempted Information Leak] [Priority: 2]: {ICMP} 10.4.12.26 -> 10.4.10.231", ""},
147         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:408:5] ICMP Echo Reply [Classification: Misc Activity] [Priority: 3]: {ICMP} 10.4.10.231 -> 10.4.12.26", ""},
148         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:1420:11] SNMP trap tcp [Classification: Attempted Information Leak] [Priority: 2]: {TCP} 10.4.12.26:37020 -> 10.4.10.231:162", ""},
149         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:1420:11] SNMP trap tcp [Classification: Attempted Information Leak] [Priority: 2]: {TCP} 10.4.12.26:37021 -> 10.4.10.231:162", ""},
150         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:590:12] RPC portmap ypserv request UDP [Classification: Decode of an RPC Query] [Priority: 2]: {UDP} 10.4.11.94:669 -> 10.4.3.20:111", ""},
151         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:590:12] RPC portmap ypserv request UDP [Classification: Decode of an RPC Query] [Priority: 2]: {UDP} 10.4.11.94:670 -> 10.4.3.20:111", ""},
152         {"^\\S+ [(\\d+:\\d+:\\d+)] \\.+ (\\d+.\\d+.\\d+.\\d+)\\p*\\d* -> (\\d+.\\d+.\\d+.\\d+)\\p*", "snort: [1:1421:11] SNMP AgentX/tcp request [Classification: Attempted Information Leak] [Priority: 2]: {TCP} 10.4.12.26:37020 -> 10.4.10.231:705", ""},
153         {NULL,NULL,NULL}
154     };
155
156     for(i=0; tests[i][0] != NULL ; i++) {
157         ck_assert_msg(OS_Regex(tests[i][0],tests[i][1]),
158                       "%s should have OS_Regex true with %s: Ref: %s",
159                       tests[i][0], tests[i][1], tests[i][2]);
160     }
161 }
162 END_TEST
163
164 START_TEST(test_fail_regex1)
165 {
166
167     int i;
168     /*
169      * Please note that all strings are \ escaped
170      */
171     const char *tests[][3] = {
172         {"abc", "abb", ""},
173         {"^ab", " ab", ""},
174         {"test", "tes", ""},
175         {"abcd", "abc", ""},
176         {"abbb", "abb", ""},
177         {"abbbbbbbb", "abbbbbbb", ""},
178         {"a|b|c| ", "def", ""},
179         {"lala$", "lalalalalal", ""},
180         {"^ab$", "abc", ""},
181         {"zzzz$", "zzzzzzzzzzzz ", ""},
182         {"^bin$|^shell$", "bina", ""},
183         {"^bin$|^shell$", "shella", ""},
184         {"^bin$|^shell$", "ashell", ""},
185         {"\\w+\\s+\\w+\\d+\\s$", "a aa11  ", ""},
186         {"^\\s+\\s     l", "     lala", ""},
187         {"test123test\\d+", "test123test", ""},
188         {"test123test\\d+$", "test123test", ""},
189         {"(lalala", "lalala", ""},
190         {"test123(\\d)", "test123a", ""},
191         {"\\(test)", "test", ""},
192         {"(\\w+)(\\d+)", "1 1", ""},
193         {NULL,NULL,NULL},
194     };
195
196     for(i=0; tests[i][0] != NULL ; i++) {
197         ck_assert_msg(!OS_Regex(tests[i][0],tests[i][1]),
198                       "%s should have OS_Regex false with %s: Ref: %s",
199                       tests[i][0], tests[i][1], tests[i][2]);
200     }
201 }
202 END_TEST
203
204 START_TEST(test_success_wordmatch)
205 {
206     int i;
207
208     /*
209      * Please note that all strings are \ escaped
210      */
211     const char *tests[][2] = {
212             { "test", "this is a test" },
213             { "test", "thistestiswithoutspaces" },
214             { "test|not", "test" },
215             { "test|not", "not" },
216             { "^test", "test on start" },
217             {NULL,NULL},
218        };
219
220     for(i=0; tests[i][0] != NULL ; i++) {
221         ck_assert_msg(OS_WordMatch(tests[i][0],tests[i][1]),
222         "%s should match positive with %s by OS_WordMatch",
223         tests[i][0], tests[i][1]);
224     }
225
226 }
227 END_TEST
228
229 START_TEST(test_fail_wordmatch)
230 {
231     int i;
232
233     /*
234      * Please note that all strings are \ escaped
235      */
236     const char *tests[][2] = {
237             { "-test", "this is a test" },
238             { "", "test" },
239             { "test|not", "negative" },
240             { "test", "" },
241             { "^test", "starttest" },
242             {NULL,NULL},
243        };
244
245     for(i=0; tests[i][0] != NULL ; i++) {
246         ck_assert_msg(!OS_WordMatch(tests[i][0],tests[i][1]),
247         "%s should not match positive with %s by OS_WordMatch",
248         tests[i][0], tests[i][1]);
249     }
250
251 }
252 END_TEST
253
254 START_TEST(test_success_strisnum)
255 {
256     int i;
257
258     /*
259      * Please note that all strings are \ escaped
260      */
261     const char *tests[] = {
262             "1",
263             "0123",
264             NULL,
265        };
266
267     for(i=0; tests[i] != NULL ; i++) {
268         ck_assert_msg(OS_StrIsNum(tests[i]),
269         "%s should match positive by OS_StrIsNum",
270         tests[i]);
271     }
272
273 }
274 END_TEST
275
276 START_TEST(test_fail_strisnum)
277 {
278     int i;
279
280     /*
281      * Please note that all strings are \ escaped
282      */
283     const char *tests[] = {
284             "test",
285             "1234e",
286             "-1",
287             "+1",
288             NULL,
289        };
290
291     for(i=0; tests[i] != NULL ; i++) {
292         ck_assert_msg(!OS_StrIsNum(tests[i]),
293         "%s should not match positive by OS_StrIsNum",
294         tests[i]);
295     }
296
297 }
298 END_TEST
299
300 START_TEST(test_strhowclosedmatch)
301 {
302     int i;
303
304     /*
305      * Please note that all strings are \ escaped
306      */
307     const char *tests[][3] = {
308             { "test", "test1234", "4" },
309             { "test1234", "test", "4" },
310             { "test", "test", "4" },
311             { "test", "", "0" },
312             { "", "test", "0" },
313             {NULL,NULL,NULL},
314        };
315
316     for(i=0; tests[i][0] != NULL ; i++) {
317         ck_assert_uint_eq(OS_StrHowClosedMatch(tests[i][0],tests[i][1])
318                 , (unsigned) atoi(tests[i][2]));
319     }
320
321 }
322 END_TEST
323
324 START_TEST(test_strbreak)
325 {
326     int i;
327
328     /*
329      * Please note that all strings are \ escaped
330      */
331     const char *tests[][15] = {
332             { "X", "testX1234", "4", "test", "1234", NULL},
333             { "X", "XtestX1234X", "4", "", "test", "1234", "", NULL},
334             { "Y", "testX1234", "4", "testX1234", NULL},
335             { "X", "testXX1234", "4", "test", "", "1234", NULL},
336             { "X", "testX1234", "1", "testX1234", NULL},
337             { "X", "testX1234X5678", "2", "test", "1234X5678", NULL},
338             { "X", "testX1234", "0", NULL},
339             {NULL},
340        };
341
342     for(i=0; tests[i][0] != NULL; i++) {
343         char **result = OS_StrBreak(tests[i][0][0], tests[i][1], (unsigned) atoi(tests[i][2]));
344
345         int j = 3;
346         if(tests[i][j] == NULL)
347         {
348             ck_assert_ptr_eq(result, NULL);
349             continue;
350         }
351
352         int k;
353         for(k = 0; tests[i][j] != NULL; j++, k++)
354         {
355             ck_assert_ptr_ne(result[k], NULL);
356             ck_assert_str_eq(result[k], tests[i][j]);
357         }
358         ck_assert_ptr_eq(result[k], NULL);
359
360         k=0;
361         while(result[k])
362             free(result[k++]);
363         free(result);
364     }
365
366 }
367 END_TEST
368
369 START_TEST(test_regexextraction)
370 {
371
372     int i;
373     /*
374      * Please note that all strings are \ escaped
375      */
376     const char *tests[][15] = {
377         { "123(\\w+\\s+)abc", "123sdf    abc", "sdf    ", NULL},
378         { "123(\\w+\\s+)abc", "abc123sdf    abc", "sdf    ", NULL},
379         { "123 (\\d+.\\d.\\d.\\d\\d*\\d*)", "123 45.6.5.567", "45.6.5.567", NULL},
380         { "from (\\S*\\d+.\\d+.\\d+.\\d\\d*\\d*)", "sshd[21576]: Illegal user web14 from ::ffff:212.227.60.55", "::ffff:212.227.60.55", NULL},
381         { "^sshd[\\d+]: Accepted \\S+ for (\\S+) from (\\S+) port ", "sshd[21405]: Accepted password for root from 192.1.1.1 port 6023", "root", "192.1.1.1", NULL},
382         { ": \\((\\S+)@(\\S+)\\) [", "pure-ftpd: (?@enigma.lab.ossec.net) [INFO] New connection from enigma.lab.ossec.net", "?", "enigma.lab.ossec.net", NULL},
383         {NULL,NULL,NULL}
384     };
385
386     for(i=0; tests[i][0] != NULL; i++) {
387         OSRegex reg;
388         ck_assert_int_eq(OSRegex_Compile(tests[i][0], &reg, OS_RETURN_SUBSTRING), 1);
389         ck_assert_ptr_ne(OSRegex_Execute(tests[i][1], &reg), NULL);
390
391
392
393         char **result = reg.sub_strings;
394
395         int j;
396         int k;
397         for(j = 2, k = 0; tests[i][j] != NULL; j++, k++)
398         {
399             ck_assert_ptr_ne(result[k], NULL);
400             ck_assert_str_eq(result[k], tests[i][j]);
401         }
402         ck_assert_ptr_eq(result[k], NULL);
403
404         OSRegex_FreePattern(&reg);
405     }
406 }
407 END_TEST
408
409 START_TEST(test_hostnamemap)
410 {
411     unsigned char test = 0;
412
413     while(1)
414     {
415         if((test >= 48 && test <= 57) // 0-9
416                 || (test >= 65 && test <= 90) // A-Z
417                 || (test >= 97 && test <= 122) // a-z
418                 || test == '(' || test == ')' || test == '-'
419                 || test == '.' || test == '@' || test == '/'
420                 || test == '_')
421         {
422             ck_assert_msg(isValidChar(test) == 1, "char %d should be a valid hostname char", test);
423         }
424         else
425         {
426             ck_assert_msg(isValidChar(test) != 1, "char %d should not be a valid hostname char", test);
427         }
428
429
430
431         if(test == 255)
432         {
433             break;
434         }
435         test++;
436     }
437
438 }
439 END_TEST
440
441 START_TEST(test_caseinsensitivecharmap)
442 {
443     unsigned char test = 0;
444
445     while(1)
446     {
447         if(test >= 65 && test <= 90) // A-Z
448         {
449             ck_assert_msg(charmap[test] == test+32, "char %d should resolve to lowercase version %d and not to %d", test, test+32, charmap[test]);
450         }
451         else
452         {
453             ck_assert_msg(charmap[test] == test, "char %d should resolve to itself and not to %d", test, charmap[test]);
454         }
455
456
457
458         if(test == 255)
459         {
460             break;
461         }
462         test++;
463     }
464
465 }
466 END_TEST
467
468 START_TEST(test_regexmap_digit)
469 {
470     unsigned char test = 0;
471
472     while(1)
473     {
474         if(test >= '0' && test <= '9')
475         {
476             ck_assert_msg(regexmap[1][test] == 1, "char %d should match", test);
477         }
478         else
479         {
480             ck_assert_msg(regexmap[1][test] != 1, "char %d should not match", test);
481         }
482
483
484         if(test == 255)
485         {
486             break;
487         }
488         test++;
489     }
490
491 }
492 END_TEST
493
494 START_TEST(test_regexmap_word)
495 {
496     unsigned char test = 0;
497
498     while(1)
499     {
500         if((test >= 'a' && test <= 'z')
501                 || (test >= 'A' && test <= 'Z')
502                 || (test >= '0' && test <= '9')
503                 || test == '-' || test == '@'
504                 || test == '_')
505         {
506             ck_assert_msg(regexmap[2][test] == 1, "char %d should match", test);
507         }
508         else
509         {
510             ck_assert_msg(regexmap[2][test] != 1, "char %d should not match", test);
511         }
512
513
514         if(test == 255)
515         {
516             break;
517         }
518         test++;
519     }
520
521 }
522 END_TEST
523
524 START_TEST(test_regexmap_space)
525 {
526     unsigned char test = 0;
527
528     while(1)
529     {
530         if(test == ' ')
531         {
532             ck_assert_msg(regexmap[3][test] == 1, "char %d should match", test);
533         }
534         else
535         {
536             ck_assert_msg(regexmap[3][test] != 1, "char %d should not match", test);
537         }
538
539
540         if(test == 255)
541         {
542             break;
543         }
544         test++;
545     }
546
547 }
548 END_TEST
549
550 START_TEST(test_regexmap_punctuation)
551 {
552     unsigned char test = 0;
553
554     while(1)
555     {
556         if(test == '<' || test == '>' || test == '!' || test == '?'
557                 || test == '"' || test == '\'' || test == '#'
558                 || test == '$' || test == '%' || test == '&'
559                 || test == '(' || test == ')' || test == '+'
560                 || test == '*' || test == ',' || test == '-'
561                 || test == '-' || test == ':' || test == '|'
562                 || test == '.' || test == ';' || test == '='
563                 || test == '[' || test == ']' || test == '{'
564                 || test == '}')
565         {
566             ck_assert_msg(regexmap[4][test] == 1, "char %d should match", test);
567         }
568         else
569         {
570             ck_assert_msg(regexmap[4][test] != 1, "char %d should not match", test);
571         }
572
573
574         if(test == 255)
575         {
576             break;
577         }
578         test++;
579     }
580
581 }
582 END_TEST
583
584 START_TEST(test_regexmap_lparenthesis)
585 {
586     unsigned char test = 0;
587
588     while(1)
589     {
590         if(test == '(')
591         {
592             ck_assert_msg(regexmap[5][test] == 1, "char %d should match", test);
593         }
594         else
595         {
596             ck_assert_msg(regexmap[5][test] != 1, "char %d should not match", test);
597         }
598
599
600         if(test == 255)
601         {
602             break;
603         }
604         test++;
605     }
606
607 }
608 END_TEST
609
610 START_TEST(test_regexmap_rparenthesis)
611 {
612     unsigned char test = 0;
613
614     while(1)
615     {
616         if(test == ')')
617         {
618             ck_assert_msg(regexmap[6][test] == 1, "char %d should match", test);
619         }
620         else
621         {
622             ck_assert_msg(regexmap[6][test] != 1, "char %d should not match", test);
623         }
624
625
626         if(test == 255)
627         {
628             break;
629         }
630         test++;
631     }
632
633 }
634 END_TEST
635
636 START_TEST(test_regexmap_backslash)
637 {
638     unsigned char test = 0;
639
640     while(1)
641     {
642         if(test == '\\')
643         {
644             ck_assert_msg(regexmap[7][test] == 1, "char %d should match", test);
645         }
646         else
647         {
648             ck_assert_msg(regexmap[7][test] != 1, "char %d should not match", test);
649         }
650
651
652         if(test == 255)
653         {
654             break;
655         }
656         test++;
657     }
658
659 }
660 END_TEST
661
662
663 START_TEST(test_regexmap_nondigit)
664 {
665     unsigned char test = 0;
666
667     while(1)
668     {
669         if(!(test >= '0' && test <= '9'))
670         {
671             ck_assert_msg(regexmap[8][test] == 1, "char %d should match", test);
672         }
673         else
674         {
675             ck_assert_msg(regexmap[8][test] != 1, "char %d should not match", test);
676         }
677
678
679         if(test == 255)
680         {
681             break;
682         }
683         test++;
684     }
685
686 }
687 END_TEST
688
689 START_TEST(test_regexmap_nonword)
690 {
691     unsigned char test = 0;
692
693     while(1)
694     {
695         if(!((test >= 'a' && test <= 'z')
696                 || (test >= 'A' && test <= 'Z')
697                 || (test >= '0' && test <= '9')
698                 || test == '_' || test == 127))
699         {
700             ck_assert_msg(regexmap[9][test] == 1, "char %d should match", test);
701         }
702         else
703         {
704             ck_assert_msg(regexmap[9][test] != 1, "char %d should not match", test);
705         }
706
707
708         if(test == 255)
709         {
710             break;
711         }
712         test++;
713     }
714
715 }
716 END_TEST
717
718 START_TEST(test_regexmap_nonspace)
719 {
720     unsigned char test = 0;
721
722     while(1)
723     {
724         if(test != ' ')
725         {
726             ck_assert_msg(regexmap[10][test] == 1, "char %d should match", test);
727         }
728         else
729         {
730             ck_assert_msg(regexmap[10][test] != 1, "char %d should not match", test);
731         }
732
733
734         if(test == 255)
735         {
736             break;
737         }
738         test++;
739     }
740
741 }
742 END_TEST
743
744 START_TEST(test_regexmap_all)
745 {
746     unsigned char test = 0;
747
748     while(1)
749     {
750         ck_assert_msg(regexmap[11][test] == 1, "char %d should match", test);
751
752
753         if(test == 255)
754         {
755             break;
756         }
757         test++;
758     }
759
760 }
761 END_TEST
762
763 START_TEST(test_regexmap_tab)
764 {
765     unsigned char test = 0;
766
767     while(1)
768     {
769         if(test == '\t')
770         {
771             ck_assert_msg(regexmap[12][test] == 1, "char %d should match", test);
772         }
773         else
774         {
775             ck_assert_msg(regexmap[12][test] != 1, "char %d should not match", test);
776         }
777
778
779         if(test == 255)
780         {
781             break;
782         }
783         test++;
784     }
785
786 }
787 END_TEST
788
789 START_TEST(test_regexmap_dollar)
790 {
791     unsigned char test = 0;
792
793     while(1)
794     {
795         if(test == '$')
796         {
797             ck_assert_msg(regexmap[13][test] == 1, "char %d should match", test);
798         }
799         else
800         {
801             ck_assert_msg(regexmap[13][test] != 1, "char %d should not match", test);
802         }
803
804
805         if(test == 255)
806         {
807             break;
808         }
809         test++;
810     }
811
812 }
813 END_TEST
814
815 START_TEST(test_regexmap_or)
816 {
817     unsigned char test = 0;
818
819     while(1)
820     {
821         if(test == '|')
822         {
823             ck_assert_msg(regexmap[14][test] == 1, "char %d should match", test);
824         }
825         else
826         {
827             ck_assert_msg(regexmap[14][test] != 1, "char %d should not match", test);
828         }
829
830
831         if(test == 255)
832         {
833             break;
834         }
835         test++;
836     }
837
838 }
839 END_TEST
840
841 START_TEST(test_regexmap_lt)
842 {
843     unsigned char test = 0;
844
845     while(1)
846     {
847         if(test == '<')
848         {
849             ck_assert_msg(regexmap[15][test] == 1, "char %d should match", test);
850         }
851         else
852         {
853             ck_assert_msg(regexmap[15][test] != 1, "char %d should not match", test);
854         }
855
856
857         if(test == 255)
858         {
859             break;
860         }
861         test++;
862     }
863
864 }
865 END_TEST
866
867 START_TEST(test_success_strstartswith)
868 {
869     int i;
870
871     /*
872      * Please note that all strings are \ escaped
873      */
874     const char *tests[][2] = {
875             { "test1234", "test" },
876             { "test", "test" },
877             { "test", "" },
878             { "", "" },
879             {NULL,NULL},
880        };
881
882     for(i=0; tests[i][0] != NULL ; i++) {
883         ck_assert_msg(OS_StrStartsWith(tests[i][0],tests[i][1]),
884         "%s should match positive with %s by OS_StrStartsWith",
885         tests[i][0], tests[i][1]);
886     }
887
888 }
889 END_TEST
890
891 START_TEST(test_fail_strstartswith)
892 {
893     int i;
894
895     /*
896      * Please note that all strings are \ escaped
897      */
898     const char *tests[][2] = {
899             { "test", "test1234" },
900             { "", "test" },
901             {NULL,NULL},
902        };
903
904     for(i=0; tests[i][0] != NULL ; i++) {
905         ck_assert_msg(!OS_StrStartsWith(tests[i][0],tests[i][1]),
906         "%s should not match positive with %s by OS_StrStartsWith",
907         tests[i][0], tests[i][1]);
908     }
909
910 }
911 END_TEST
912
913 Suite *test_suite(void)
914 {
915     Suite *s = suite_create("os_regex");
916
917     /* Core test case */
918     TCase *tc_match = tcase_create("Match");
919     TCase *tc_regex = tcase_create("Regex");
920     TCase *tc_wordmatch = tcase_create("WordMatch");
921     TCase *tc_strisnum = tcase_create("StrIsNum");
922     TCase *tc_strhowclosedmatch = tcase_create("StrHowClosedMatch");
923     TCase *tc_strbreak = tcase_create("StrBreak");
924     TCase *tc_regexextraction = tcase_create("RegexExtraction");
925     TCase *tc_hostnamemap = tcase_create("HostnameMap");
926     TCase *tc_caseinsensitivecharmap = tcase_create("CaseInsensitiveCharmap");
927     TCase *tc_regexmap = tcase_create("RegexMap");
928     TCase *tc_strstartswith = tcase_create("StrStartsWith");
929
930     tcase_add_test(tc_match, test_success_match1);
931     tcase_add_test(tc_match, test_fail_match1);
932
933     tcase_add_test(tc_regex, test_success_regex1);
934     tcase_add_test(tc_regex, test_fail_regex1);
935
936     tcase_add_test(tc_wordmatch, test_success_wordmatch);
937     tcase_add_test(tc_wordmatch, test_fail_wordmatch);
938
939     tcase_add_test(tc_strisnum, test_success_strisnum);
940     tcase_add_test(tc_strisnum, test_fail_strisnum);
941
942     tcase_add_test(tc_strhowclosedmatch, test_strhowclosedmatch);
943
944     tcase_add_test(tc_strbreak, test_strbreak);
945
946     tcase_add_test(tc_regexextraction, test_regexextraction);
947
948     tcase_add_test(tc_hostnamemap, test_hostnamemap);
949
950     tcase_add_test(tc_caseinsensitivecharmap, test_caseinsensitivecharmap);
951
952     tcase_add_test(tc_regexmap, test_regexmap_digit);
953     tcase_add_test(tc_regexmap, test_regexmap_word);
954     tcase_add_test(tc_regexmap, test_regexmap_space);
955     tcase_add_test(tc_regexmap, test_regexmap_punctuation);
956     tcase_add_test(tc_regexmap, test_regexmap_lparenthesis);
957     tcase_add_test(tc_regexmap, test_regexmap_rparenthesis);
958     tcase_add_test(tc_regexmap, test_regexmap_backslash);
959     tcase_add_test(tc_regexmap, test_regexmap_nondigit);
960     tcase_add_test(tc_regexmap, test_regexmap_nonword);
961     tcase_add_test(tc_regexmap, test_regexmap_nonspace);
962     tcase_add_test(tc_regexmap, test_regexmap_all);
963     tcase_add_test(tc_regexmap, test_regexmap_tab);
964     tcase_add_test(tc_regexmap, test_regexmap_dollar);
965     tcase_add_test(tc_regexmap, test_regexmap_or);
966     tcase_add_test(tc_regexmap, test_regexmap_lt);
967
968     tcase_add_test(tc_strstartswith, test_success_strstartswith);
969     tcase_add_test(tc_strstartswith, test_fail_strstartswith);
970
971     suite_add_tcase(s, tc_match);
972     suite_add_tcase(s, tc_regex);
973     suite_add_tcase(s, tc_wordmatch);
974     suite_add_tcase(s, tc_strisnum);
975     suite_add_tcase(s, tc_strhowclosedmatch);
976     suite_add_tcase(s, tc_strbreak);
977     suite_add_tcase(s, tc_regexextraction);
978     suite_add_tcase(s, tc_hostnamemap);
979     suite_add_tcase(s, tc_caseinsensitivecharmap);
980     suite_add_tcase(s, tc_regexmap);
981     suite_add_tcase(s, tc_strstartswith);
982
983     return (s);
984 }
985
986 int main(void)
987 {
988     Suite *s = test_suite();
989     SRunner *sr = srunner_create(s);
990     srunner_run_all(sr, CK_NORMAL);
991     int number_failed = srunner_ntests_failed(sr);
992     srunner_free(sr);
993
994     return ((number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
995 }