mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/
synced 2025-04-19 20:58:31 +09:00
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 <davidgow@google.com> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Tested-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20250307-scanf-kunit-convert-v9-4-b98820fa39ff@gmail.com Signed-off-by: Kees Cook <kees@kernel.org>
This commit is contained in:
parent
97c1f302f2
commit
d62f8c9547
@ -434,8 +434,11 @@ static void numbers_list_hh(struct kunit *test, const char *delim)
|
||||
numbers_list_8(signed char, "0x%hhx", delim, "hhi", check_char);
|
||||
}
|
||||
|
||||
static void numbers_list(struct kunit *test, const char *delim)
|
||||
static void numbers_list(struct kunit *test)
|
||||
{
|
||||
const char * const *param = test->param_value;
|
||||
const char *delim = *param;
|
||||
|
||||
numbers_list_ll(test, delim);
|
||||
numbers_list_l(test, delim);
|
||||
numbers_list_d(test, delim);
|
||||
@ -506,8 +509,11 @@ static void numbers_list_field_width_hh(struct kunit *test, const char *delim)
|
||||
* List of numbers separated by delim. Each field width specifier is the
|
||||
* maximum possible digits for the given type and base.
|
||||
*/
|
||||
static void numbers_list_field_width_typemax(struct kunit *test, const char *delim)
|
||||
static void numbers_list_field_width_typemax(struct kunit *test)
|
||||
{
|
||||
const char * const *param = test->param_value;
|
||||
const char *delim = *param;
|
||||
|
||||
numbers_list_field_width_ll(test, delim);
|
||||
numbers_list_field_width_l(test, delim);
|
||||
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
|
||||
* List of numbers separated by delim. Each field width specifier is the
|
||||
* exact length of the corresponding value digits in the string being scanned.
|
||||
*/
|
||||
static void numbers_list_field_width_val_width(struct kunit *test, const char *delim)
|
||||
static void numbers_list_field_width_val_width(struct kunit *test)
|
||||
{
|
||||
const char * const *param = test->param_value;
|
||||
const char *delim = *param;
|
||||
|
||||
numbers_list_field_width_val_ll(test, delim);
|
||||
numbers_list_field_width_val_l(test, delim);
|
||||
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
|
||||
*/
|
||||
static void numbers_slice(struct kunit *test)
|
||||
{
|
||||
numbers_list_field_width_val_width(test, "");
|
||||
const char *delim = "";
|
||||
|
||||
KUNIT_ASSERT_PTR_EQ(test, test->param_value, NULL);
|
||||
test->param_value = &delim;
|
||||
|
||||
numbers_list_field_width_val_width(test);
|
||||
}
|
||||
|
||||
#define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn) \
|
||||
@ -737,62 +751,60 @@ static const char * const number_delimiters[] = {
|
||||
" ", ":", ",", "-", "/",
|
||||
};
|
||||
|
||||
static void test_numbers(struct kunit *test)
|
||||
static void number_delimiter_param_desc(const char * const *param,
|
||||
char *desc)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* String containing only one number. */
|
||||
numbers_simple(test);
|
||||
|
||||
/* String with multiple numbers separated by delimiter. */
|
||||
for (i = 0; i < ARRAY_SIZE(number_delimiters); i++) {
|
||||
numbers_list(test, number_delimiters[i]);
|
||||
|
||||
/* Field width may be longer than actual field digits. */
|
||||
numbers_list_field_width_typemax(test, number_delimiters[i]);
|
||||
|
||||
/* Each field width exactly length of actual field digits. */
|
||||
numbers_list_field_width_val_width(test, number_delimiters[i]);
|
||||
}
|
||||
|
||||
/* Slice continuous sequence of digits using field widths. */
|
||||
numbers_slice(test);
|
||||
|
||||
numbers_prefix_overflow(test);
|
||||
snprintf(desc, KUNIT_PARAM_DESC_SIZE, "\"%s\"", *param);
|
||||
}
|
||||
|
||||
static void scanf_test(struct kunit *test)
|
||||
KUNIT_ARRAY_PARAM(number_delimiters, number_delimiters, number_delimiter_param_desc);
|
||||
|
||||
static struct kunit_case scanf_test_cases[] = {
|
||||
KUNIT_CASE(numbers_simple),
|
||||
/* String with multiple numbers separated by delimiter. */
|
||||
KUNIT_CASE_PARAM(numbers_list, number_delimiters_gen_params),
|
||||
/* Field width may be longer than actual field digits. */
|
||||
KUNIT_CASE_PARAM(numbers_list_field_width_typemax, number_delimiters_gen_params),
|
||||
/* Each field width exactly length of actual field digits. */
|
||||
KUNIT_CASE_PARAM(numbers_list_field_width_val_width, number_delimiters_gen_params),
|
||||
/* Slice continuous sequence of digits using field widths. */
|
||||
KUNIT_CASE(numbers_slice),
|
||||
KUNIT_CASE(numbers_prefix_overflow),
|
||||
|
||||
KUNIT_CASE(test_simple_strtoull),
|
||||
KUNIT_CASE(test_simple_strtoll),
|
||||
KUNIT_CASE(test_simple_strtoul),
|
||||
KUNIT_CASE(test_simple_strtol),
|
||||
{}
|
||||
};
|
||||
|
||||
static int scanf_suite_init(struct kunit_suite *suite)
|
||||
{
|
||||
test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
|
||||
if (!test_buffer)
|
||||
return;
|
||||
return -ENOMEM;
|
||||
|
||||
fmt_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
|
||||
if (!fmt_buffer) {
|
||||
kfree(test_buffer);
|
||||
return;
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
prandom_seed_state(&rnd_state, 3141592653589793238ULL);
|
||||
|
||||
test_numbers(test);
|
||||
|
||||
test_simple_strtoull(test);
|
||||
test_simple_strtoll(test);
|
||||
test_simple_strtoul(test);
|
||||
test_simple_strtol(test);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void scanf_suite_exit(struct kunit_suite *suite)
|
||||
{
|
||||
kfree(fmt_buffer);
|
||||
kfree(test_buffer);
|
||||
}
|
||||
|
||||
static struct kunit_case scanf_test_cases[] = {
|
||||
KUNIT_CASE(scanf_test),
|
||||
{}
|
||||
};
|
||||
|
||||
static struct kunit_suite scanf_test_suite = {
|
||||
.name = "scanf",
|
||||
.suite_init = scanf_suite_init,
|
||||
.suite_exit = scanf_suite_exit,
|
||||
.test_cases = scanf_test_cases,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user