1 // SPDX-License-Identifier: GPL-2.0+
3 * Test cases for API provided by cmdline.c
6 #include <kunit/test.h>
7 #include <linux/kernel.h>
8 #include <linux/random.h>
9 #include <linux/string.h>
11 static const char *cmdline_test_strings[] = {
12 "\"\"", "" , "=" , "\"-", "," , "-," , ",-" , "-" ,
13 "+," , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
16 static const int cmdline_test_values[] = {
17 1, 1, 1, 1, 2, 3, 2, 3,
18 1, 3, 2, 1, 1, 1, 3, 1,
21 static_assert(ARRAY_SIZE(cmdline_test_strings) == ARRAY_SIZE(cmdline_test_values));
23 static const char *cmdline_test_range_strings[] = {
24 "-7" , "--7" , "-1-2" , "7--9",
25 "7-" , "-7--9", "7-9," , "9-7" ,
26 "5-a", "a-5" , "5-8" , ",8-5",
27 "+,1", "-,4" , "-3,0-1,6", "4,-" ,
28 " +2", " -9" , "0-1,-3,6", "- 9" ,
31 static const int cmdline_test_range_values[][16] = {
32 { 1, -7, }, { 0, -0, }, { 4, -1, 0, +1, 2, }, { 0, 7, },
33 { 0, +7, }, { 0, -7, }, { 3, +7, 8, +9, 0, }, { 0, 9, },
34 { 0, +5, }, { 0, -0, }, { 4, +5, 6, +7, 8, }, { 0, 0, },
35 { 0, +0, }, { 0, -0, }, { 4, -3, 0, +1, 6, }, { 1, 4, },
36 { 0, +0, }, { 0, -0, }, { 4, +0, 1, -3, 6, }, { 0, 0, },
39 static_assert(ARRAY_SIZE(cmdline_test_range_strings) == ARRAY_SIZE(cmdline_test_range_values));
41 static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
43 const char *fmt = "Pattern: %s";
48 ret = get_option((char **)&out, &dummy);
50 KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
51 KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
54 static void cmdline_test_noint(struct kunit *test)
59 const char *str = cmdline_test_strings[i];
63 /* Only first and leading '-' will advance the pointer */
64 offset = !!(*str == '-');
65 cmdline_do_one_test(test, str, rc, offset);
66 } while (++i < ARRAY_SIZE(cmdline_test_strings));
69 static void cmdline_test_lead_int(struct kunit *test)
75 const char *str = cmdline_test_strings[i];
76 int rc = cmdline_test_values[i];
79 sprintf(in, "%u%s", get_random_u8(), str);
80 /* Only first '-' after the number will advance the pointer */
81 offset = strlen(in) - strlen(str) + !!(rc == 2);
82 cmdline_do_one_test(test, in, rc, offset);
83 } while (++i < ARRAY_SIZE(cmdline_test_strings));
86 static void cmdline_test_tail_int(struct kunit *test)
92 const char *str = cmdline_test_strings[i];
93 /* When "" or "-" the result will be valid integer */
94 int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
97 sprintf(in, "%s%u", str, get_random_u8());
99 * Only first and leading '-' not followed by integer
100 * will advance the pointer.
102 offset = rc ? strlen(in) : !!(*str == '-');
103 cmdline_do_one_test(test, in, rc, offset);
104 } while (++i < ARRAY_SIZE(cmdline_test_strings));
107 static void cmdline_do_one_range_test(struct kunit *test, const char *in,
108 unsigned int n, const int *e)
114 memset(r, 0, sizeof(r));
115 get_options(in, ARRAY_SIZE(r), r);
116 KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (parsed) expected %d numbers, got %d",
118 for (i = 1; i < ARRAY_SIZE(r); i++)
119 KUNIT_EXPECT_EQ_MSG(test, r[i], e[i], "in test %u at %u", n, i);
121 memset(r, 0, sizeof(r));
122 get_options(in, 0, r);
123 KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (validated) expected %d numbers, got %d",
126 p = memchr_inv(&r[1], 0, sizeof(r) - sizeof(r[0]));
127 KUNIT_EXPECT_PTR_EQ_MSG(test, p, NULL, "in test %u at %u out of bound", n, p - r);
130 static void cmdline_test_range(struct kunit *test)
135 const char *str = cmdline_test_range_strings[i];
136 const int *e = cmdline_test_range_values[i];
138 cmdline_do_one_range_test(test, str, i, e);
139 } while (++i < ARRAY_SIZE(cmdline_test_range_strings));
142 static struct kunit_case cmdline_test_cases[] = {
143 KUNIT_CASE(cmdline_test_noint),
144 KUNIT_CASE(cmdline_test_lead_int),
145 KUNIT_CASE(cmdline_test_tail_int),
146 KUNIT_CASE(cmdline_test_range),
150 static struct kunit_suite cmdline_test_suite = {
152 .test_cases = cmdline_test_cases,
154 kunit_test_suite(cmdline_test_suite);
156 MODULE_LICENSE("GPL");