blob: a72a2c16066ef7cb2efa1e8c1636f4ff8a8a672b [file] [log] [blame]
Andy Shevchenko75468612020-12-15 20:43:34 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Test cases for API provided by cmdline.c
4 */
5
6#include <kunit/test.h>
7#include <linux/kernel.h>
8#include <linux/random.h>
9#include <linux/string.h>
10
11static const char *cmdline_test_strings[] = {
12 "\"\"", "" , "=" , "\"-", "," , "-," , ",-" , "-" ,
13 "+," , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
14};
15
16static const int cmdline_test_values[] = {
17 1, 1, 1, 1, 2, 3, 2, 3,
18 1, 3, 2, 1, 1, 1, 3, 1,
19};
20
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +020021static_assert(ARRAY_SIZE(cmdline_test_strings) == ARRAY_SIZE(cmdline_test_values));
22
23static 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" ,
29};
30
31static 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, },
37};
38
39static_assert(ARRAY_SIZE(cmdline_test_range_strings) == ARRAY_SIZE(cmdline_test_range_values));
40
Andy Shevchenko75468612020-12-15 20:43:34 -080041static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
42{
43 const char *fmt = "Pattern: %s";
44 const char *out = in;
45 int dummy;
46 int ret;
47
48 ret = get_option((char **)&out, &dummy);
49
50 KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
51 KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
52}
53
54static void cmdline_test_noint(struct kunit *test)
55{
56 unsigned int i = 0;
57
58 do {
59 const char *str = cmdline_test_strings[i];
60 int rc = 0;
61 int offset;
62
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));
67}
68
69static void cmdline_test_lead_int(struct kunit *test)
70{
71 unsigned int i = 0;
72 char in[32];
73
74 do {
75 const char *str = cmdline_test_strings[i];
76 int rc = cmdline_test_values[i];
77 int offset;
78
79 sprintf(in, "%u%s", get_random_int() % 256, 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));
84}
85
86static void cmdline_test_tail_int(struct kunit *test)
87{
88 unsigned int i = 0;
89 char in[32];
90
91 do {
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;
95 int offset;
96
97 sprintf(in, "%s%u", str, get_random_int() % 256);
98 /*
99 * Only first and leading '-' not followed by integer
100 * will advance the pointer.
101 */
102 offset = rc ? strlen(in) : !!(*str == '-');
103 cmdline_do_one_test(test, in, rc, offset);
104 } while (++i < ARRAY_SIZE(cmdline_test_strings));
105}
106
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +0200107static void cmdline_do_one_range_test(struct kunit *test, const char *in,
108 unsigned int n, const int *e)
109{
110 unsigned int i;
111 int r[16];
Andy Shevchenko0ea09082021-01-22 15:56:52 +0200112 int *p;
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +0200113
114 memset(r, 0, sizeof(r));
115 get_options(in, ARRAY_SIZE(r), r);
Andy Shevchenko0ea09082021-01-22 15:56:52 +0200116 KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (parsed) expected %d numbers, got %d",
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +0200117 n, e[0], r[0]);
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);
Andy Shevchenko0ea09082021-01-22 15:56:52 +0200120
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",
124 n, e[0], r[0]);
125
126 p = memchr_inv(&r[1], 0, sizeof(r) - sizeof(r[0]));
David Gow255ede32021-05-13 12:32:04 -0700127 KUNIT_EXPECT_PTR_EQ_MSG(test, p, NULL, "in test %u at %u out of bound", n, p - r);
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +0200128}
129
130static void cmdline_test_range(struct kunit *test)
131{
132 unsigned int i = 0;
133
134 do {
135 const char *str = cmdline_test_range_strings[i];
136 const int *e = cmdline_test_range_values[i];
137
138 cmdline_do_one_range_test(test, str, i, e);
139 } while (++i < ARRAY_SIZE(cmdline_test_range_strings));
140}
141
Andy Shevchenko75468612020-12-15 20:43:34 -0800142static 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),
Andy Shevchenkod3c7cfc2021-01-22 14:38:48 +0200146 KUNIT_CASE(cmdline_test_range),
Andy Shevchenko75468612020-12-15 20:43:34 -0800147 {}
148};
149
150static struct kunit_suite cmdline_test_suite = {
151 .name = "cmdline",
152 .test_cases = cmdline_test_cases,
153};
154kunit_test_suite(cmdline_test_suite);
155
156MODULE_LICENSE("GPL");