^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Extracted fronm glob.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/glob.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* Boot with "glob.verbose=1" to show successful tests, too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static bool verbose = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) module_param(verbose, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct glob_test {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) char const *pat, *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) bool expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static bool __pure __init test(char const *pat, char const *str, bool expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) bool match = glob_match(pat, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) bool success = match == expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Can't get string literals into a particular section, so... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static char const msg_error[] __initconst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) KERN_ERR "glob: \"%s\" vs. \"%s\": %s *** ERROR ***\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static char const msg_ok[] __initconst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) KERN_DEBUG "glob: \"%s\" vs. \"%s\": %s OK\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static char const mismatch[] __initconst = "mismatch";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) char const *message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) message = msg_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) else if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) message = msg_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) printk(message, pat, str, mismatch + 3*match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * The tests are all jammed together in one array to make it simpler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * to place that array in the .init.rodata section. The obvious
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * "array of structures containing char *" has no way to force the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * pointed-to strings to be in a particular section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Anyway, a test consists of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * 1. Expected glob_match result: '1' or '0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * 2. Pattern to match: null-terminated string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * 3. String to match against: null-terminated string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * The list of tests is terminated with a final '\0' instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * a glob_match result character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static char const glob_tests[] __initconst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Some basic tests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "1" "a\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "0" "a\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) "0" "a\0" "aa\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "0" "a\0" "\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "1" "\0" "\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "0" "\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* Simple character class tests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) "1" "[a]\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) "0" "[a]\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) "0" "[!a]\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) "1" "[!a]\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) "1" "[ab]\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) "1" "[ab]\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) "0" "[ab]\0" "c\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) "1" "[!ab]\0" "c\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) "1" "[a-c]\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) "0" "[a-c]\0" "d\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Corner cases in character class parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "1" "[a-c-e-g]\0" "-\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "0" "[a-c-e-g]\0" "d\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "1" "[a-c-e-g]\0" "f\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) "1" "[]a-ceg-ik[]\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) "1" "[]a-ceg-ik[]\0" "]\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) "1" "[]a-ceg-ik[]\0" "[\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) "1" "[]a-ceg-ik[]\0" "h\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) "0" "[]a-ceg-ik[]\0" "f\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) "0" "[!]a-ceg-ik[]\0" "h\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) "0" "[!]a-ceg-ik[]\0" "]\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) "1" "[!]a-ceg-ik[]\0" "f\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Simple wild cards */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "1" "?\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) "0" "?\0" "aa\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) "0" "??\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) "1" "?x?\0" "axb\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) "0" "?x?\0" "abx\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) "0" "?x?\0" "xab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Asterisk wild cards (backtracking) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "0" "*??\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) "1" "*??\0" "ab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "1" "*??\0" "abc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) "1" "*??\0" "abcd\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "0" "??*\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) "1" "??*\0" "ab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) "1" "??*\0" "abc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "1" "??*\0" "abcd\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) "0" "?*?\0" "a\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) "1" "?*?\0" "ab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) "1" "?*?\0" "abc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "1" "?*?\0" "abcd\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) "1" "*b\0" "b\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) "1" "*b\0" "ab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "0" "*b\0" "ba\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) "1" "*b\0" "bb\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "1" "*b\0" "abb\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "1" "*b\0" "bab\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "1" "*bc\0" "abbc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "1" "*bc\0" "bc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "1" "*bc\0" "bbc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "1" "*bc\0" "bcbc\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Multiple asterisks (complex backtracking) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "1" "*ac*\0" "abacadaeafag\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "1" "*ac*ae*ag*\0" "abacadaeafag\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) "0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "1" "*abcd*\0" "abcabcabcabcdefg\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "1" "*ab*cd*\0" "abcabcabcabcdefg\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "0" "*abcd*\0" "abcabcabcabcefg\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) "0" "*ab*cd*\0" "abcabcabcabcefg\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int __init glob_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned successes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) char const *p = glob_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static char const message[] __initconst =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) KERN_INFO "glob: %u self-tests passed, %u failed\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Tests are jammed together in a string. The first byte is '1'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * or '0' to indicate the expected outcome, or '\0' to indicate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * end of the tests. Then come two null-terminated strings: the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * pattern and the string to match it against.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) bool expected = *p++ & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char const *pat = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) p += strlen(p) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) successes += test(pat, p, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) p += strlen(p) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) n -= successes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) printk(message, successes, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* What's the errno for "kernel bug detected"? Guess... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return n ? -ECANCELED : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* We need a dummy exit function to allow unload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void __exit glob_fini(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) module_init(glob_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) module_exit(glob_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_DESCRIPTION("glob(7) matching tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_LICENSE("Dual MIT/GPL");