Skip to content

Commit d62f8c9

Browse files
tamirdkees
authored andcommitted
scanf: break kunit into test cases
Use `suite_init` and move some tests into `scanf_test_cases`. This gives us nicer output in the event of a failure. Reviewed-by: David Gow <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Tested-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 97c1f30 commit d62f8c9

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

lib/tests/scanf_kunit.c

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,11 @@ static void numbers_list_hh(struct kunit *test, const char *delim)
434434
numbers_list_8(signed char, "0x%hhx", delim, "hhi", check_char);
435435
}
436436

437-
static void numbers_list(struct kunit *test, const char *delim)
437+
static void numbers_list(struct kunit *test)
438438
{
439+
const char * const *param = test->param_value;
440+
const char *delim = *param;
441+
439442
numbers_list_ll(test, delim);
440443
numbers_list_l(test, delim);
441444
numbers_list_d(test, delim);
@@ -506,8 +509,11 @@ static void numbers_list_field_width_hh(struct kunit *test, const char *delim)
506509
* List of numbers separated by delim. Each field width specifier is the
507510
* maximum possible digits for the given type and base.
508511
*/
509-
static void numbers_list_field_width_typemax(struct kunit *test, const char *delim)
512+
static void numbers_list_field_width_typemax(struct kunit *test)
510513
{
514+
const char * const *param = test->param_value;
515+
const char *delim = *param;
516+
511517
numbers_list_field_width_ll(test, delim);
512518
numbers_list_field_width_l(test, delim);
513519
numbers_list_field_width_d(test, delim);
@@ -569,8 +575,11 @@ static void numbers_list_field_width_val_hh(struct kunit *test, const char *deli
569575
* List of numbers separated by delim. Each field width specifier is the
570576
* exact length of the corresponding value digits in the string being scanned.
571577
*/
572-
static void numbers_list_field_width_val_width(struct kunit *test, const char *delim)
578+
static void numbers_list_field_width_val_width(struct kunit *test)
573579
{
580+
const char * const *param = test->param_value;
581+
const char *delim = *param;
582+
574583
numbers_list_field_width_val_ll(test, delim);
575584
numbers_list_field_width_val_l(test, delim);
576585
numbers_list_field_width_val_d(test, delim);
@@ -586,7 +595,12 @@ static void numbers_list_field_width_val_width(struct kunit *test, const char *d
586595
*/
587596
static void numbers_slice(struct kunit *test)
588597
{
589-
numbers_list_field_width_val_width(test, "");
598+
const char *delim = "";
599+
600+
KUNIT_ASSERT_PTR_EQ(test, test->param_value, NULL);
601+
test->param_value = &delim;
602+
603+
numbers_list_field_width_val_width(test);
590604
}
591605

592606
#define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn) \
@@ -737,62 +751,60 @@ static const char * const number_delimiters[] = {
737751
" ", ":", ",", "-", "/",
738752
};
739753

740-
static void test_numbers(struct kunit *test)
754+
static void number_delimiter_param_desc(const char * const *param,
755+
char *desc)
741756
{
742-
int i;
757+
snprintf(desc, KUNIT_PARAM_DESC_SIZE, "\"%s\"", *param);
758+
}
743759

744-
/* String containing only one number. */
745-
numbers_simple(test);
760+
KUNIT_ARRAY_PARAM(number_delimiters, number_delimiters, number_delimiter_param_desc);
746761

762+
static struct kunit_case scanf_test_cases[] = {
763+
KUNIT_CASE(numbers_simple),
747764
/* String with multiple numbers separated by delimiter. */
748-
for (i = 0; i < ARRAY_SIZE(number_delimiters); i++) {
749-
numbers_list(test, number_delimiters[i]);
750-
751-
/* Field width may be longer than actual field digits. */
752-
numbers_list_field_width_typemax(test, number_delimiters[i]);
753-
754-
/* Each field width exactly length of actual field digits. */
755-
numbers_list_field_width_val_width(test, number_delimiters[i]);
756-
}
757-
765+
KUNIT_CASE_PARAM(numbers_list, number_delimiters_gen_params),
766+
/* Field width may be longer than actual field digits. */
767+
KUNIT_CASE_PARAM(numbers_list_field_width_typemax, number_delimiters_gen_params),
768+
/* Each field width exactly length of actual field digits. */
769+
KUNIT_CASE_PARAM(numbers_list_field_width_val_width, number_delimiters_gen_params),
758770
/* Slice continuous sequence of digits using field widths. */
759-
numbers_slice(test);
771+
KUNIT_CASE(numbers_slice),
772+
KUNIT_CASE(numbers_prefix_overflow),
760773

761-
numbers_prefix_overflow(test);
762-
}
774+
KUNIT_CASE(test_simple_strtoull),
775+
KUNIT_CASE(test_simple_strtoll),
776+
KUNIT_CASE(test_simple_strtoul),
777+
KUNIT_CASE(test_simple_strtol),
778+
{}
779+
};
763780

764-
static void scanf_test(struct kunit *test)
781+
static int scanf_suite_init(struct kunit_suite *suite)
765782
{
766783
test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
767784
if (!test_buffer)
768-
return;
785+
return -ENOMEM;
769786

770787
fmt_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
771788
if (!fmt_buffer) {
772789
kfree(test_buffer);
773-
return;
790+
return -ENOMEM;
774791
}
775792

776793
prandom_seed_state(&rnd_state, 3141592653589793238ULL);
777794

778-
test_numbers(test);
779-
780-
test_simple_strtoull(test);
781-
test_simple_strtoll(test);
782-
test_simple_strtoul(test);
783-
test_simple_strtol(test);
795+
return 0;
796+
}
784797

798+
static void scanf_suite_exit(struct kunit_suite *suite)
799+
{
785800
kfree(fmt_buffer);
786801
kfree(test_buffer);
787802
}
788803

789-
static struct kunit_case scanf_test_cases[] = {
790-
KUNIT_CASE(scanf_test),
791-
{}
792-
};
793-
794804
static struct kunit_suite scanf_test_suite = {
795805
.name = "scanf",
806+
.suite_init = scanf_suite_init,
807+
.suite_exit = scanf_suite_exit,
796808
.test_cases = scanf_test_cases,
797809
};
798810

0 commit comments

Comments
 (0)