| 1 | |
| 2 | |
| 3 | * Test cases for glob functions.
|
| 4 |
|
| 5 |
|
| 6 | #include <kunit/test.h>
|
| 7 | #include <linux/glob.h>
|
| 8 | #include <linux/module.h>
|
| 9 |
|
| 10 | |
| 11 | * struct glob_test_case - Test case for glob matching.
|
| 12 | * @pat: Pattern to match.
|
| 13 | * @str: String to match against.
|
| 14 | * @expected: Expected glob_match result, true if matched.
|
| 15 |
|
| 16 | struct glob_test_case {
|
| 17 | const char *pat;
|
| 18 | const char *str;
|
| 19 | bool expected;
|
| 20 | };
|
| 21 |
|
| 22 | static const struct glob_test_case glob_test_cases[] = {
|
| 23 |
|
| 24 | { .pat = "a", .str = "a", .expected = true },
|
| 25 | { .pat = "a", .str = "b", .expected = false },
|
| 26 | { .pat = "a", .str = "aa", .expected = false },
|
| 27 | { .pat = "a", .str = "", .expected = false },
|
| 28 | { .pat = "", .str = "", .expected = true },
|
| 29 | { .pat = "", .str = "a", .expected = false },
|
| 30 |
|
| 31 | { .pat = "[a]", .str = "a", .expected = true },
|
| 32 | { .pat = "[a]", .str = "b", .expected = false },
|
| 33 | { .pat = "[!a]", .str = "a", .expected = false },
|
| 34 | { .pat = "[!a]", .str = "b", .expected = true },
|
| 35 | { .pat = "[ab]", .str = "a", .expected = true },
|
| 36 | { .pat = "[ab]", .str = "b", .expected = true },
|
| 37 | { .pat = "[ab]", .str = "c", .expected = false },
|
| 38 | { .pat = "[!ab]", .str = "c", .expected = true },
|
| 39 | { .pat = "[a-c]", .str = "b", .expected = true },
|
| 40 | { .pat = "[a-c]", .str = "d", .expected = false },
|
| 41 |
|
| 42 | { .pat = "[a-c-e-g]", .str = "-", .expected = true },
|
| 43 | { .pat = "[a-c-e-g]", .str = "d", .expected = false },
|
| 44 | { .pat = "[a-c-e-g]", .str = "f", .expected = true },
|
| 45 | { .pat = "[]a-ceg-ik[]", .str = "a", .expected = true },
|
| 46 | { .pat = "[]a-ceg-ik[]", .str = "]", .expected = true },
|
| 47 | { .pat = "[]a-ceg-ik[]", .str = "[", .expected = true },
|
| 48 | { .pat = "[]a-ceg-ik[]", .str = "h", .expected = true },
|
| 49 | { .pat = "[]a-ceg-ik[]", .str = "f", .expected = false },
|
| 50 | { .pat = "[!]a-ceg-ik[]", .str = "h", .expected = false },
|
| 51 | { .pat = "[!]a-ceg-ik[]", .str = "]", .expected = false },
|
| 52 | { .pat = "[!]a-ceg-ik[]", .str = "f", .expected = true },
|
| 53 |
|
| 54 | { .pat = "?", .str = "a", .expected = true },
|
| 55 | { .pat = "?", .str = "aa", .expected = false },
|
| 56 | { .pat = "??", .str = "a", .expected = false },
|
| 57 | { .pat = "?x?", .str = "axb", .expected = true },
|
| 58 | { .pat = "?x?", .str = "abx", .expected = false },
|
| 59 | { .pat = "?x?", .str = "xab", .expected = false },
|
| 60 |
|
| 61 | { .pat = "*??", .str = "a", .expected = false },
|
| 62 | { .pat = "*??", .str = "ab", .expected = true },
|
| 63 | { .pat = "*??", .str = "abc", .expected = true },
|
| 64 | { .pat = "*??", .str = "abcd", .expected = true },
|
| 65 | { .pat = "??*", .str = "a", .expected = false },
|
| 66 | { .pat = "??*", .str = "ab", .expected = true },
|
| 67 | { .pat = "??*", .str = "abc", .expected = true },
|
| 68 | { .pat = "??*", .str = "abcd", .expected = true },
|
| 69 | { .pat = "?*?", .str = "a", .expected = false },
|
| 70 | { .pat = "?*?", .str = "ab", .expected = true },
|
| 71 | { .pat = "?*?", .str = "abc", .expected = true },
|
| 72 | { .pat = "?*?", .str = "abcd", .expected = true },
|
| 73 | { .pat = "*b", .str = "b", .expected = true },
|
| 74 | { .pat = "*b", .str = "ab", .expected = true },
|
| 75 | { .pat = "*b", .str = "ba", .expected = false },
|
| 76 | { .pat = "*b", .str = "bb", .expected = true },
|
| 77 | { .pat = "*b", .str = "abb", .expected = true },
|
| 78 | { .pat = "*b", .str = "bab", .expected = true },
|
| 79 | { .pat = "*bc", .str = "abbc", .expected = true },
|
| 80 | { .pat = "*bc", .str = "bc", .expected = true },
|
| 81 | { .pat = "*bc", .str = "bbc", .expected = true },
|
| 82 | { .pat = "*bc", .str = "bcbc", .expected = true },
|
| 83 |
|
| 84 | { .pat = "*ac*", .str = "abacadaeafag", .expected = true },
|
| 85 | { .pat = "*ac*ae*ag*", .str = "abacadaeafag", .expected = true },
|
| 86 | { .pat = "*a*b*[bc]*[ef]*g*", .str = "abacadaeafag", .expected = true },
|
| 87 | { .pat = "*a*b*[ef]*[cd]*g*", .str = "abacadaeafag", .expected = false },
|
| 88 | { .pat = "*abcd*", .str = "abcabcabcabcdefg", .expected = true },
|
| 89 | { .pat = "*ab*cd*", .str = "abcabcabcabcdefg", .expected = true },
|
| 90 | { .pat = "*abcd*abcdef*", .str = "abcabcdabcdeabcdefg", .expected = true },
|
| 91 | { .pat = "*abcd*", .str = "abcabcabcabcefg", .expected = false },
|
| 92 | { .pat = "*ab*cd*", .str = "abcabcabcabcefg", .expected = false },
|
| 93 | };
|
| 94 |
|
| 95 | static void glob_case_to_desc(const struct glob_test_case *t, char *desc)
|
| 96 | {
|
| 97 | snprintf(desc, KUNIT_PARAM_DESC_SIZE, "pat:\"%s\" str:\"%s\"", t->pat, t->str);
|
| 98 | }
|
| 99 |
|
| 100 | KUNIT_ARRAY_PARAM(glob, glob_test_cases, glob_case_to_desc);
|
| 101 |
|
| 102 | static void glob_test_match(struct kunit *test)
|
| 103 | {
|
| 104 | const struct glob_test_case *params = test->param_value;
|
| 105 |
|
| 106 | KUNIT_EXPECT_EQ_MSG(test,
|
| 107 | glob_match(params->pat, params->str),
|
| 108 | params->expected,
|
| 109 | "Pattern: \"%s\", String: \"%s\", Expected: %d",
|
| 110 | params->pat, params->str, params->expected);
|
| 111 | }
|
| 112 |
|
| 113 | static struct kunit_case glob_kunit_test_cases[] = {
|
| 114 | KUNIT_CASE_PARAM(glob_test_match, glob_gen_params),
|
| 115 | {}
|
| 116 | };
|
| 117 |
|
| 118 | static struct kunit_suite glob_test_suite = {
|
| 119 | .name = "glob",
|
| 120 | .test_cases = glob_kunit_test_cases,
|
| 121 | };
|
| 122 |
|
| 123 | kunit_test_suite(glob_test_suite);
|
| 124 | MODULE_DESCRIPTION("Test cases for glob functions");
|
| 125 | MODULE_LICENSE("Dual MIT/GPL");
|