Skip to contentSkip to conversation

lib/tests: extend cmdline KUnit with next_arg() tests

PATCH Applied Shuvam Pandey 2 participants 3 messages Mar 16

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tests: extend cmdline KUnit with next_arg() tests

The cmdline KUnit suite covers get_option() and get_options(), but it does not exercise next_arg().

Extend the suite with one test for a quoted value containing spaces and one regression test for a bare quote token after a normal parameter.

The regression test covers the bare quote token path fixed by commit 9847f21225c4 ("lib/cmdline: avoid page fault in next_arg").

---
 lib/tests/cmdline_kunit.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
lib/tests/cmdline_kunit.c +38 -0
index c1602f797637..b5c7aba65f43 100644
@@ -139,11 +139,49 @@ static void cmdline_test_range(struct kunit *test)
 	} while (++i < ARRAY_SIZE(cmdline_test_range_strings));
 }
 
+static void cmdline_test_next_arg_quoted_value(struct kunit *test)
+{
+	char in[] = "foo=\"bar baz\" qux=1";
+	char *next, *param, *val;
+
+	next = next_arg(in, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "foo");
+	KUNIT_ASSERT_NOT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, val, "bar baz");
+	KUNIT_EXPECT_STREQ(test, next, "qux=1");
+
+	next = next_arg(next, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "qux");
+	KUNIT_ASSERT_NOT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, val, "1");
+	KUNIT_EXPECT_STREQ(test, next, "");
+}
+
+static void cmdline_test_next_arg_bare_quote_regression(struct kunit *test)
+{
+	char in[] = "foo=bar \"";
+	char *next, *param, *val;
+
+	next = next_arg(in, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "foo");
+	KUNIT_ASSERT_NOT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, val, "bar");
+	KUNIT_EXPECT_STREQ(test, next, "\"");
+
+	/* This hits the i == 0 quoted-token case fixed by 9847f21225c4. */
+	next = next_arg(next, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "");
+	KUNIT_EXPECT_PTR_EQ(test, val, NULL);
+	KUNIT_EXPECT_STREQ(test, next, "");
+}
+
 static struct kunit_case cmdline_test_cases[] = {
 	KUNIT_CASE(cmdline_test_noint),
 	KUNIT_CASE(cmdline_test_lead_int),
 	KUNIT_CASE(cmdline_test_tail_int),
 	KUNIT_CASE(cmdline_test_range),
+	KUNIT_CASE(cmdline_test_next_arg_quoted_value),
+	KUNIT_CASE(cmdline_test_next_arg_bare_quote_regression),
 	{}
 };
 
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/1 lib/tests: extend cmdline next_arg() coverage with mixed tokens

The cmdline KUnit suite now covers quoted values and the bare quote regression path in next_arg(), but it still does not exercise a mixed sequence containing an empty value, a bare token, and a quoted value with '='.

Andy Shevchenko suggested covering a sequence such as "bbb= jjj kkk=\"a=b\"". Add that case so the suite checks all three forms in one parse stream.

Validated with the arm64 cmdline KUnit suite and a W=1 rebuild of lib/tests/cmdline_kunit.o.

---
 lib/tests/cmdline_kunit.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
lib/tests/cmdline_kunit.c +24 -0
index b5c7aba65f43..af44ae9e92c3 100644
@@ -175,6 +175,29 @@ static void cmdline_test_next_arg_bare_quote_regression(struct kunit *test)
 	KUNIT_EXPECT_STREQ(test, next, "");
 }
 
+static void cmdline_test_next_arg_mixed_tokens(struct kunit *test)
+{
+	char in[] = "bbb= jjj kkk=\"a=b\"";
+	char *next, *param, *val;
+
+	next = next_arg(in, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "bbb");
+	KUNIT_ASSERT_NOT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, val, "");
+	KUNIT_EXPECT_STREQ(test, next, "jjj kkk=\"a=b\"");
+
+	next = next_arg(next, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "jjj");
+	KUNIT_EXPECT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, next, "kkk=\"a=b\"");
+
+	next = next_arg(next, &param, &val);
+	KUNIT_EXPECT_STREQ(test, param, "kkk");
+	KUNIT_ASSERT_NOT_NULL(test, val);
+	KUNIT_EXPECT_STREQ(test, val, "a=b");
+	KUNIT_EXPECT_STREQ(test, next, "");
+}
+
 static struct kunit_case cmdline_test_cases[] = {
 	KUNIT_CASE(cmdline_test_noint),
 	KUNIT_CASE(cmdline_test_lead_int),
@@ -182,6 +205,7 @@ static struct kunit_case cmdline_test_cases[] = {
 	KUNIT_CASE(cmdline_test_range),
 	KUNIT_CASE(cmdline_test_next_arg_quoted_value),
 	KUNIT_CASE(cmdline_test_next_arg_bare_quote_regression),
+	KUNIT_CASE(cmdline_test_next_arg_mixed_tokens),
 	{}
 };
 
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH] lib/tests: extend cmdline next_arg() coverage with mixed tokens

Quoted 11 lines · Shuvam Pandey

The cmdline KUnit suite now covers quoted values and the bare quote regression path in next_arg(), but it still does not exercise a mixed sequence containing an empty value, a bare token, and a quoted value with '='.

Andy Shevchenko suggested covering a sequence such as "bbb= jjj kkk=\"a=b\"". Add that case so the suite checks all three forms in one parse stream.

Validated with the arm64 cmdline KUnit suite and a W=1 rebuild of lib/tests/cmdline_kunit.o.

Thanks!

Andrew, I believe it can be folded into the original patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko