globtest.c 4.2 KB

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