0001
0002
0003
0004
0005
0006 #include <kunit/test.h>
0007 #include <linux/kernel.h>
0008 #include <linux/random.h>
0009 #include <linux/string.h>
0010
0011 static const char *cmdline_test_strings[] = {
0012 "\"\"", "" , "=" , "\"-", "," , "-," , ",-" , "-" ,
0013 "+," , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
0014 };
0015
0016 static const int cmdline_test_values[] = {
0017 1, 1, 1, 1, 2, 3, 2, 3,
0018 1, 3, 2, 1, 1, 1, 3, 1,
0019 };
0020
0021 static_assert(ARRAY_SIZE(cmdline_test_strings) == ARRAY_SIZE(cmdline_test_values));
0022
0023 static const char *cmdline_test_range_strings[] = {
0024 "-7" , "--7" , "-1-2" , "7--9",
0025 "7-" , "-7--9", "7-9," , "9-7" ,
0026 "5-a", "a-5" , "5-8" , ",8-5",
0027 "+,1", "-,4" , "-3,0-1,6", "4,-" ,
0028 " +2", " -9" , "0-1,-3,6", "- 9" ,
0029 };
0030
0031 static const int cmdline_test_range_values[][16] = {
0032 { 1, -7, }, { 0, -0, }, { 4, -1, 0, +1, 2, }, { 0, 7, },
0033 { 0, +7, }, { 0, -7, }, { 3, +7, 8, +9, 0, }, { 0, 9, },
0034 { 0, +5, }, { 0, -0, }, { 4, +5, 6, +7, 8, }, { 0, 0, },
0035 { 0, +0, }, { 0, -0, }, { 4, -3, 0, +1, 6, }, { 1, 4, },
0036 { 0, +0, }, { 0, -0, }, { 4, +0, 1, -3, 6, }, { 0, 0, },
0037 };
0038
0039 static_assert(ARRAY_SIZE(cmdline_test_range_strings) == ARRAY_SIZE(cmdline_test_range_values));
0040
0041 static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
0042 {
0043 const char *fmt = "Pattern: %s";
0044 const char *out = in;
0045 int dummy;
0046 int ret;
0047
0048 ret = get_option((char **)&out, &dummy);
0049
0050 KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
0051 KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
0052 }
0053
0054 static void cmdline_test_noint(struct kunit *test)
0055 {
0056 unsigned int i = 0;
0057
0058 do {
0059 const char *str = cmdline_test_strings[i];
0060 int rc = 0;
0061 int offset;
0062
0063
0064 offset = !!(*str == '-');
0065 cmdline_do_one_test(test, str, rc, offset);
0066 } while (++i < ARRAY_SIZE(cmdline_test_strings));
0067 }
0068
0069 static void cmdline_test_lead_int(struct kunit *test)
0070 {
0071 unsigned int i = 0;
0072 char in[32];
0073
0074 do {
0075 const char *str = cmdline_test_strings[i];
0076 int rc = cmdline_test_values[i];
0077 int offset;
0078
0079 sprintf(in, "%u%s", get_random_int() % 256, str);
0080
0081 offset = strlen(in) - strlen(str) + !!(rc == 2);
0082 cmdline_do_one_test(test, in, rc, offset);
0083 } while (++i < ARRAY_SIZE(cmdline_test_strings));
0084 }
0085
0086 static void cmdline_test_tail_int(struct kunit *test)
0087 {
0088 unsigned int i = 0;
0089 char in[32];
0090
0091 do {
0092 const char *str = cmdline_test_strings[i];
0093
0094 int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
0095 int offset;
0096
0097 sprintf(in, "%s%u", str, get_random_int() % 256);
0098
0099
0100
0101
0102 offset = rc ? strlen(in) : !!(*str == '-');
0103 cmdline_do_one_test(test, in, rc, offset);
0104 } while (++i < ARRAY_SIZE(cmdline_test_strings));
0105 }
0106
0107 static void cmdline_do_one_range_test(struct kunit *test, const char *in,
0108 unsigned int n, const int *e)
0109 {
0110 unsigned int i;
0111 int r[16];
0112 int *p;
0113
0114 memset(r, 0, sizeof(r));
0115 get_options(in, ARRAY_SIZE(r), r);
0116 KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (parsed) expected %d numbers, got %d",
0117 n, e[0], r[0]);
0118 for (i = 1; i < ARRAY_SIZE(r); i++)
0119 KUNIT_EXPECT_EQ_MSG(test, r[i], e[i], "in test %u at %u", n, i);
0120
0121 memset(r, 0, sizeof(r));
0122 get_options(in, 0, r);
0123 KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (validated) expected %d numbers, got %d",
0124 n, e[0], r[0]);
0125
0126 p = memchr_inv(&r[1], 0, sizeof(r) - sizeof(r[0]));
0127 KUNIT_EXPECT_PTR_EQ_MSG(test, p, NULL, "in test %u at %u out of bound", n, p - r);
0128 }
0129
0130 static void cmdline_test_range(struct kunit *test)
0131 {
0132 unsigned int i = 0;
0133
0134 do {
0135 const char *str = cmdline_test_range_strings[i];
0136 const int *e = cmdline_test_range_values[i];
0137
0138 cmdline_do_one_range_test(test, str, i, e);
0139 } while (++i < ARRAY_SIZE(cmdline_test_range_strings));
0140 }
0141
0142 static struct kunit_case cmdline_test_cases[] = {
0143 KUNIT_CASE(cmdline_test_noint),
0144 KUNIT_CASE(cmdline_test_lead_int),
0145 KUNIT_CASE(cmdline_test_tail_int),
0146 KUNIT_CASE(cmdline_test_range),
0147 {}
0148 };
0149
0150 static struct kunit_suite cmdline_test_suite = {
0151 .name = "cmdline",
0152 .test_cases = cmdline_test_cases,
0153 };
0154 kunit_test_suite(cmdline_test_suite);
0155
0156 MODULE_LICENSE("GPL");