| 1 |
#ifdef HAVE_CONFIG_H |
| 2 |
#include <config.h> |
| 3 |
#endif |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include "OVAL/oval_glob_to_regex.h" |
| 9 |
#define COUNT 18 |
| 10 |
|
| 11 |
int test_glob_to_regex(const char *glob, const char *regex, int noescape); |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
const char *globs[] = { |
| 16 |
"list.?", |
| 17 |
"project.*", |
| 18 |
"*old", |
| 19 |
"type*.[ch]", |
| 20 |
"*.*", |
| 21 |
"*", |
| 22 |
"?", |
| 23 |
"\\*", |
| 24 |
"\\?", |
| 25 |
"\\[hello\\]", |
| 26 |
"x[[:digit:]]\\*", |
| 27 |
"", |
| 28 |
"~/files/*.txt", |
| 29 |
"\\", |
| 30 |
"[ab", |
| 31 |
".*.conf", |
| 32 |
"docs/?b", |
| 33 |
"xy/??z", |
| 34 |
}; |
| 35 |
const char *regexes[] = { |
| 36 |
"^list\\.[^/]$", |
| 37 |
"^project\\.[^/]*$", |
| 38 |
"^(?=[^.])[^/]*old$", |
| 39 |
"^type[^/]*\\.[ch]$", |
| 40 |
"^(?=[^.])[^/]*\\.[^/]*$", |
| 41 |
"^(?=[^.])[^/]*$", |
| 42 |
"^[^./]$", |
| 43 |
"^\\*$", |
| 44 |
"^\\?$", |
| 45 |
"^\\[hello\\]$", |
| 46 |
"^x[[:digit:]]\\*$", |
| 47 |
"^$", |
| 48 |
"^~/files/(?=[^.])[^/]*\\.txt$", |
| 49 |
"^\\\\$", |
| 50 |
NULL, |
| 51 |
"^\\.[^/]*\\.conf$", |
| 52 |
"^docs/[^./]b$", |
| 53 |
"^xy/[^./][^/]z$" |
| 54 |
}; |
| 55 |
const char *noescape_regexes[] = { |
| 56 |
"^list\\.[^/]$", |
| 57 |
"^project\\.[^/]*$", |
| 58 |
"^(?=[^.])[^/]*old$", |
| 59 |
"^type[^/]*\\.[ch]$", |
| 60 |
"^(?=[^.])[^/]*\\.[^/]*$", |
| 61 |
"^(?=[^.])[^/]*$", |
| 62 |
"^[^./]$", |
| 63 |
"^\\\\[^/]*$", |
| 64 |
"^\\\\[^/]$", |
| 65 |
"^\\\\[hello\\\\]$", |
| 66 |
"^x[[:digit:]]\\\\[^/]*$", |
| 67 |
"^$", |
| 68 |
"^~/files/(?=[^.])[^/]*\\.txt$", |
| 69 |
"^\\\\$", |
| 70 |
NULL, |
| 71 |
"^\\.[^/]*\\.conf$", |
| 72 |
"^docs/[^./]b$", |
| 73 |
"^xy/[^./][^/]z$" |
| 74 |
}; |
| 75 |
|
| 76 |
int test_glob_to_regex(const char *glob, const char *regex, int noescape) |
| 77 |
{ |
| 78 |
char *result; |
| 79 |
int retval; |
| 80 |
result = oval_glob_to_regex(glob, noescape); |
| 81 |
if (regex == NULL) { |
| 82 |
if (result == NULL) { |
| 83 |
printf("\tPASS\t%s\tNULL\tNULL\n", glob); |
| 84 |
retval = 1; |
| 85 |
} else { |
| 86 |
printf("\tFAIL\t%s\t%s\tNULL\n", glob, result); |
| 87 |
retval = 0; |
| 88 |
} |
| 89 |
} else { |
| 90 |
if (result == NULL) { |
| 91 |
printf("\tFAIL\t%s\tNULL\t%s\n", glob, regex); |
| 92 |
retval = 0; |
| 93 |
} else if (strcmp(regex, result) == 0) { |
| 94 |
printf("\tPASS\t%s\t%s\t%s\n", glob, result, regex); |
| 95 |
retval = 1; |
| 96 |
} else { |
| 97 |
printf("\tFAIL\t%s\t%s\t%s\n", glob, result, regex); |
| 98 |
retval = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
free(result); |
| 102 |
return retval; |
| 103 |
} |
| 104 |
|
| 105 |
int main (int argc, char *argv[]) |
| 106 |
{ |
| 107 |
int i, retval = 0; |
| 108 |
|
| 109 |
if (argc == 1) { |
| 110 |
printf("glob_noescape=false:\n"); |
| 111 |
printf("Result\tInput\tOutput\tExpected\n"); |
| 112 |
for (i = 0; i < COUNT; i++) { |
| 113 |
if(!test_glob_to_regex(globs[i], regexes[i], 0)) { |
| 114 |
retval = 1; |
| 115 |
} |
| 116 |
} |
| 117 |
printf("glob_noescape=true:\n"); |
| 118 |
printf("Result\tInput\tOutput\tExpected\n"); |
| 119 |
for (i = 0; i < COUNT; i++) { |
| 120 |
if(!test_glob_to_regex(globs[i], noescape_regexes[i], 1)) { |
| 121 |
retval = 1; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return retval; |
| 127 |
} |