2 * Extracted fronm glob.c
5 #include <linux/module.h>
6 #include <linux/moduleparam.h>
7 #include <linux/glob.h>
8 #include <linux/printk.h>
10 /* Boot with "glob.verbose=1" to show successful tests, too */
11 static bool verbose
= false;
12 module_param(verbose
, bool, 0);
15 char const *pat
, *str
;
19 static bool __pure __init
test(char const *pat
, char const *str
, bool expected
)
21 bool match
= glob_match(pat
, str
);
22 bool success
= match
== expected
;
24 /* Can't get string literals into a particular section, so... */
25 static char const msg_error
[] __initconst
=
26 KERN_ERR
"glob: \"%s\" vs. \"%s\": %s *** ERROR ***\n";
27 static char const msg_ok
[] __initconst
=
28 KERN_DEBUG
"glob: \"%s\" vs. \"%s\": %s OK\n";
29 static char const mismatch
[] __initconst
= "mismatch";
39 printk(message
, pat
, str
, mismatch
+ 3*match
);
44 * The tests are all jammed together in one array to make it simpler
45 * to place that array in the .init.rodata section. The obvious
46 * "array of structures containing char *" has no way to force the
47 * pointed-to strings to be in a particular section.
49 * Anyway, a test consists of:
50 * 1. Expected glob_match result: '1' or '0'.
51 * 2. Pattern to match: null-terminated string
52 * 3. String to match against: null-terminated string
54 * The list of tests is terminated with a final '\0' instead of
55 * a glob_match result character.
57 static char const glob_tests
[] __initconst
=
58 /* Some basic tests */
65 /* Simple character class tests */
76 /* Corner cases in character class parsing */
77 "1" "[a-c-e-g]\0" "-\0"
78 "0" "[a-c-e-g]\0" "d\0"
79 "1" "[a-c-e-g]\0" "f\0"
80 "1" "[]a-ceg-ik[]\0" "a\0"
81 "1" "[]a-ceg-ik[]\0" "]\0"
82 "1" "[]a-ceg-ik[]\0" "[\0"
83 "1" "[]a-ceg-ik[]\0" "h\0"
84 "0" "[]a-ceg-ik[]\0" "f\0"
85 "0" "[!]a-ceg-ik[]\0" "h\0"
86 "0" "[!]a-ceg-ik[]\0" "]\0"
87 "1" "[!]a-ceg-ik[]\0" "f\0"
88 /* Simple wild cards */
95 /* Asterisk wild cards (backtracking) */
118 /* Multiple asterisks (complex backtracking) */
119 "1" "*ac*\0" "abacadaeafag\0"
120 "1" "*ac*ae*ag*\0" "abacadaeafag\0"
121 "1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0"
122 "0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0"
123 "1" "*abcd*\0" "abcabcabcabcdefg\0"
124 "1" "*ab*cd*\0" "abcabcabcabcdefg\0"
125 "1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0"
126 "0" "*abcd*\0" "abcabcabcabcefg\0"
127 "0" "*ab*cd*\0" "abcabcabcabcefg\0";
129 static int __init
glob_init(void)
131 unsigned successes
= 0;
133 char const *p
= glob_tests
;
134 static char const message
[] __initconst
=
135 KERN_INFO
"glob: %u self-tests passed, %u failed\n";
138 * Tests are jammed together in a string. The first byte is '1'
139 * or '0' to indicate the expected outcome, or '\0' to indicate the
140 * end of the tests. Then come two null-terminated strings: the
141 * pattern and the string to match it against.
144 bool expected
= *p
++ & 1;
148 successes
+= test(pat
, p
, expected
);
154 printk(message
, successes
, n
);
156 /* What's the errno for "kernel bug detected"? Guess... */
157 return n
? -ECANCELED
: 0;
160 /* We need a dummy exit function to allow unload */
161 static void __exit
glob_fini(void) { }
163 module_init(glob_init
);
164 module_exit(glob_fini
);
166 MODULE_DESCRIPTION("glob(7) matching tests");
167 MODULE_LICENSE("Dual MIT/GPL");