string: Convert helpers selftest to KUnit
Convert test-string_helpers.c to KUnit so it can be easily run with everything else. Failure reporting doesn't need to be open-coded in most places, for example, forcing a failure in the expected output for upper/lower testing looks like this: [12:18:43] # test_upper_lower: EXPECTATION FAILED at lib/string_helpers_kunit.c:579 [12:18:43] Expected dst == strings_upper[i].out, but [12:18:43] dst == "ABCDEFGH1234567890TEST" [12:18:43] strings_upper[i].out == "ABCDEFGH1234567890TeST" [12:18:43] [FAILED] test_upper_lower Currently passes without problems: $ ./tools/testing/kunit/kunit.py run string_helpers ... [12:23:55] Starting KUnit Kernel (1/1)... [12:23:55] ============================================================ [12:23:55] =============== string_helpers (3 subtests) ================ [12:23:55] [PASSED] test_get_size [12:23:55] [PASSED] test_upper_lower [12:23:55] [PASSED] test_unescape [12:23:55] ================= [PASSED] string_helpers ================== [12:23:55] ============================================================ [12:23:55] Testing complete. Ran 3 tests: passed: 3 [12:23:55] Elapsed time: 6.709s total, 0.001s configuring, 6.591s building, 0.066s running Link: https://lore.kernel.org/r/20240301202732.2688342-2-keescook@chromium.org Signed-off-by: Kees Cook <keescook@chromium.org>
This commit is contained in:
parent
29d8568849
commit
fb57550fcb
@ -8978,7 +8978,7 @@ F: include/linux/string_helpers.h
|
||||
F: lib/string.c
|
||||
F: lib/string_kunit.c
|
||||
F: lib/string_helpers.c
|
||||
F: lib/test-string_helpers.c
|
||||
F: lib/string_helpers_kunit.c
|
||||
F: scripts/coccinelle/api/string_choices.cocci
|
||||
|
||||
GENERIC UIO DRIVER FOR PCI DEVICES
|
||||
|
@ -2357,8 +2357,10 @@ config STRING_KUNIT_TEST
|
||||
depends on KUNIT
|
||||
default KUNIT_ALL_TESTS
|
||||
|
||||
config TEST_STRING_HELPERS
|
||||
tristate "Test functions located in the string_helpers module at runtime"
|
||||
config STRING_HELPERS_KUNIT_TEST
|
||||
tristate "KUnit test string helpers at runtime" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT
|
||||
default KUNIT_ALL_TESTS
|
||||
|
||||
config TEST_KSTRTOX
|
||||
tristate "Test kstrto*() family of functions at runtime"
|
||||
|
@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
|
||||
generic-radix-tree.o bitmap-str.o
|
||||
obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o
|
||||
obj-y += string_helpers.o
|
||||
obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
|
||||
obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
|
||||
obj-y += hexdump.o
|
||||
obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
|
||||
obj-y += kstrtox.o
|
||||
|
@ -1,35 +1,25 @@
|
||||
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
|
||||
/*
|
||||
* Test cases for lib/string_helpers.c module.
|
||||
*/
|
||||
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
||||
|
||||
#include <kunit/test.h>
|
||||
#include <linux/array_size.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/random.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/string_helpers.h>
|
||||
|
||||
static __init bool test_string_check_buf(const char *name, unsigned int flags,
|
||||
char *in, size_t p,
|
||||
char *out_real, size_t q_real,
|
||||
char *out_test, size_t q_test)
|
||||
static void test_string_check_buf(struct kunit *test,
|
||||
const char *name, unsigned int flags,
|
||||
char *in, size_t p,
|
||||
char *out_real, size_t q_real,
|
||||
char *out_test, size_t q_test)
|
||||
{
|
||||
if (q_real == q_test && !memcmp(out_test, out_real, q_test))
|
||||
return true;
|
||||
|
||||
pr_warn("Test '%s' failed: flags = %#x\n", name, flags);
|
||||
|
||||
print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
|
||||
in, p, true);
|
||||
print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
|
||||
out_test, q_test, true);
|
||||
print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
|
||||
out_real, q_real, true);
|
||||
|
||||
return false;
|
||||
KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name);
|
||||
KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test,
|
||||
"name:%s", name);
|
||||
}
|
||||
|
||||
struct test_string {
|
||||
@ -38,7 +28,7 @@ struct test_string {
|
||||
unsigned int flags;
|
||||
};
|
||||
|
||||
static const struct test_string strings[] __initconst = {
|
||||
static const struct test_string strings[] = {
|
||||
{
|
||||
.in = "\\f\\ \\n\\r\\t\\v",
|
||||
.out = "\f\\ \n\r\t\v",
|
||||
@ -61,17 +51,19 @@ static const struct test_string strings[] __initconst = {
|
||||
},
|
||||
};
|
||||
|
||||
static void __init test_string_unescape(const char *name, unsigned int flags,
|
||||
bool inplace)
|
||||
static void test_string_unescape(struct kunit *test,
|
||||
const char *name, unsigned int flags,
|
||||
bool inplace)
|
||||
{
|
||||
int q_real = 256;
|
||||
char *in = kmalloc(q_real, GFP_KERNEL);
|
||||
char *out_test = kmalloc(q_real, GFP_KERNEL);
|
||||
char *out_real = kmalloc(q_real, GFP_KERNEL);
|
||||
char *in = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
||||
char *out_test = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
||||
char *out_real = kunit_kzalloc(test, q_real, GFP_KERNEL);
|
||||
int i, p = 0, q_test = 0;
|
||||
|
||||
if (!in || !out_test || !out_real)
|
||||
goto out;
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(strings); i++) {
|
||||
const char *s = strings[i].in;
|
||||
@ -104,12 +96,8 @@ static void __init test_string_unescape(const char *name, unsigned int flags,
|
||||
q_real = string_unescape(in, out_real, q_real, flags);
|
||||
}
|
||||
|
||||
test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
|
||||
test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real,
|
||||
out_test, q_test);
|
||||
out:
|
||||
kfree(out_real);
|
||||
kfree(out_test);
|
||||
kfree(in);
|
||||
}
|
||||
|
||||
struct test_string_1 {
|
||||
@ -124,7 +112,7 @@ struct test_string_2 {
|
||||
};
|
||||
|
||||
#define TEST_STRING_2_DICT_0 NULL
|
||||
static const struct test_string_2 escape0[] __initconst = {{
|
||||
static const struct test_string_2 escape0[] = {{
|
||||
.in = "\f\\ \n\r\t\v",
|
||||
.s1 = {{
|
||||
.out = "\\f\\ \\n\\r\\t\\v",
|
||||
@ -222,7 +210,7 @@ static const struct test_string_2 escape0[] __initconst = {{
|
||||
}};
|
||||
|
||||
#define TEST_STRING_2_DICT_1 "b\\ \t\r\xCF"
|
||||
static const struct test_string_2 escape1[] __initconst = {{
|
||||
static const struct test_string_2 escape1[] = {{
|
||||
.in = "\f\\ \n\r\t\v",
|
||||
.s1 = {{
|
||||
.out = "\f\\134\\040\n\\015\\011\v",
|
||||
@ -359,7 +347,7 @@ static const struct test_string_2 escape1[] __initconst = {{
|
||||
/* terminator */
|
||||
}};
|
||||
|
||||
static const struct test_string strings_upper[] __initconst = {
|
||||
static const struct test_string strings_upper[] = {
|
||||
{
|
||||
.in = "abcdefgh1234567890test",
|
||||
.out = "ABCDEFGH1234567890TEST",
|
||||
@ -370,7 +358,7 @@ static const struct test_string strings_upper[] __initconst = {
|
||||
},
|
||||
};
|
||||
|
||||
static const struct test_string strings_lower[] __initconst = {
|
||||
static const struct test_string strings_lower[] = {
|
||||
{
|
||||
.in = "ABCDEFGH1234567890TEST",
|
||||
.out = "abcdefgh1234567890test",
|
||||
@ -381,8 +369,8 @@ static const struct test_string strings_lower[] __initconst = {
|
||||
},
|
||||
};
|
||||
|
||||
static __init const char *test_string_find_match(const struct test_string_2 *s2,
|
||||
unsigned int flags)
|
||||
static const char *test_string_find_match(const struct test_string_2 *s2,
|
||||
unsigned int flags)
|
||||
{
|
||||
const struct test_string_1 *s1 = s2->s1;
|
||||
unsigned int i;
|
||||
@ -403,31 +391,31 @@ static __init const char *test_string_find_match(const struct test_string_2 *s2,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static __init void
|
||||
test_string_escape_overflow(const char *in, int p, unsigned int flags, const char *esc,
|
||||
static void
|
||||
test_string_escape_overflow(struct kunit *test,
|
||||
const char *in, int p, unsigned int flags, const char *esc,
|
||||
int q_test, const char *name)
|
||||
{
|
||||
int q_real;
|
||||
|
||||
q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
|
||||
if (q_real != q_test)
|
||||
pr_warn("Test '%s' failed: flags = %#x, osz = 0, expected %d, got %d\n",
|
||||
name, flags, q_test, q_real);
|
||||
KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s: flags:%#x", name, flags);
|
||||
}
|
||||
|
||||
static __init void test_string_escape(const char *name,
|
||||
const struct test_string_2 *s2,
|
||||
unsigned int flags, const char *esc)
|
||||
static void test_string_escape(struct kunit *test, const char *name,
|
||||
const struct test_string_2 *s2,
|
||||
unsigned int flags, const char *esc)
|
||||
{
|
||||
size_t out_size = 512;
|
||||
char *out_test = kmalloc(out_size, GFP_KERNEL);
|
||||
char *out_real = kmalloc(out_size, GFP_KERNEL);
|
||||
char *in = kmalloc(256, GFP_KERNEL);
|
||||
char *out_test = kunit_kzalloc(test, out_size, GFP_KERNEL);
|
||||
char *out_real = kunit_kzalloc(test, out_size, GFP_KERNEL);
|
||||
char *in = kunit_kzalloc(test, 256, GFP_KERNEL);
|
||||
int p = 0, q_test = 0;
|
||||
int q_real;
|
||||
|
||||
if (!out_test || !out_real || !in)
|
||||
goto out;
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
|
||||
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
|
||||
|
||||
for (; s2->in; s2++) {
|
||||
const char *out;
|
||||
@ -463,45 +451,35 @@ static __init void test_string_escape(const char *name,
|
||||
|
||||
q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);
|
||||
|
||||
test_string_check_buf(name, flags, in, p, out_real, q_real, out_test,
|
||||
test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_test,
|
||||
q_test);
|
||||
|
||||
test_string_escape_overflow(in, p, flags, esc, q_test, name);
|
||||
|
||||
out:
|
||||
kfree(in);
|
||||
kfree(out_real);
|
||||
kfree(out_test);
|
||||
test_string_escape_overflow(test, in, p, flags, esc, q_test, name);
|
||||
}
|
||||
|
||||
#define string_get_size_maxbuf 16
|
||||
#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
|
||||
do { \
|
||||
BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
|
||||
BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
|
||||
__test_string_get_size((size), (blk_size), (exp_result10), \
|
||||
(exp_result2)); \
|
||||
#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
|
||||
do { \
|
||||
BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
|
||||
BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
|
||||
__test_string_get_size(test, (size), (blk_size), (exp_result10), \
|
||||
(exp_result2)); \
|
||||
} while (0)
|
||||
|
||||
|
||||
static __init void test_string_get_size_check(const char *units,
|
||||
const char *exp,
|
||||
char *res,
|
||||
const u64 size,
|
||||
const u64 blk_size)
|
||||
static void test_string_get_size_check(struct kunit *test,
|
||||
const char *units,
|
||||
const char *exp,
|
||||
char *res,
|
||||
const u64 size,
|
||||
const u64 blk_size)
|
||||
{
|
||||
if (!memcmp(res, exp, strlen(exp) + 1))
|
||||
return;
|
||||
|
||||
res[string_get_size_maxbuf - 1] = '\0';
|
||||
|
||||
pr_warn("Test 'test_string_get_size' failed!\n");
|
||||
pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
|
||||
KUNIT_EXPECT_MEMEQ_MSG(test, res, exp, strlen(exp) + 1,
|
||||
"string_get_size(size = %llu, blk_size = %llu, units = %s)",
|
||||
size, blk_size, units);
|
||||
pr_warn("expected: '%s', got '%s'\n", exp, res);
|
||||
}
|
||||
|
||||
static __init void __strchrcut(char *dst, const char *src, const char *cut)
|
||||
static void __strchrcut(char *dst, const char *src, const char *cut)
|
||||
{
|
||||
const char *from = src;
|
||||
size_t len;
|
||||
@ -515,11 +493,12 @@ static __init void __strchrcut(char *dst, const char *src, const char *cut)
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
static __init void __test_string_get_size_one(const u64 size, const u64 blk_size,
|
||||
const char *exp_result10,
|
||||
const char *exp_result2,
|
||||
enum string_size_units units,
|
||||
const char *cut)
|
||||
static void __test_string_get_size_one(struct kunit *test,
|
||||
const u64 size, const u64 blk_size,
|
||||
const char *exp_result10,
|
||||
const char *exp_result2,
|
||||
enum string_size_units units,
|
||||
const char *cut)
|
||||
{
|
||||
char buf10[string_get_size_maxbuf];
|
||||
char buf2[string_get_size_maxbuf];
|
||||
@ -537,13 +516,14 @@ static __init void __test_string_get_size_one(const u64 size, const u64 blk_size
|
||||
string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(buf10));
|
||||
string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2));
|
||||
|
||||
test_string_get_size_check(prefix10, exp10, buf10, size, blk_size);
|
||||
test_string_get_size_check(prefix2, exp2, buf2, size, blk_size);
|
||||
test_string_get_size_check(test, prefix10, exp10, buf10, size, blk_size);
|
||||
test_string_get_size_check(test, prefix2, exp2, buf2, size, blk_size);
|
||||
}
|
||||
|
||||
static __init void __test_string_get_size(const u64 size, const u64 blk_size,
|
||||
const char *exp_result10,
|
||||
const char *exp_result2)
|
||||
static void __test_string_get_size(struct kunit *test,
|
||||
const u64 size, const u64 blk_size,
|
||||
const char *exp_result10,
|
||||
const char *exp_result2)
|
||||
{
|
||||
struct {
|
||||
enum string_size_units units;
|
||||
@ -557,12 +537,13 @@ static __init void __test_string_get_size(const u64 size, const u64 blk_size,
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(get_size_test_cases); i++)
|
||||
__test_string_get_size_one(size, blk_size, exp_result10, exp_result2,
|
||||
__test_string_get_size_one(test, size, blk_size,
|
||||
exp_result10, exp_result2,
|
||||
get_size_test_cases[i].units,
|
||||
get_size_test_cases[i].cut);
|
||||
}
|
||||
|
||||
static __init void test_string_get_size(void)
|
||||
static void test_get_size(struct kunit *test)
|
||||
{
|
||||
/* small values */
|
||||
test_string_get_size_one(0, 512, "0 B", "0 B");
|
||||
@ -582,7 +563,7 @@ static __init void test_string_get_size(void)
|
||||
test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
|
||||
}
|
||||
|
||||
static void __init test_string_upper_lower(void)
|
||||
static void test_upper_lower(struct kunit *test)
|
||||
{
|
||||
char *dst;
|
||||
int i;
|
||||
@ -592,16 +573,10 @@ static void __init test_string_upper_lower(void)
|
||||
int len = strlen(strings_upper[i].in) + 1;
|
||||
|
||||
dst = kmalloc(len, GFP_KERNEL);
|
||||
if (!dst)
|
||||
return;
|
||||
KUNIT_ASSERT_NOT_NULL(test, dst);
|
||||
|
||||
string_upper(dst, s);
|
||||
if (memcmp(dst, strings_upper[i].out, len)) {
|
||||
pr_warn("Test 'string_upper' failed : expected %s, got %s!\n",
|
||||
strings_upper[i].out, dst);
|
||||
kfree(dst);
|
||||
return;
|
||||
}
|
||||
KUNIT_EXPECT_STREQ(test, dst, strings_upper[i].out);
|
||||
kfree(dst);
|
||||
}
|
||||
|
||||
@ -610,45 +585,44 @@ static void __init test_string_upper_lower(void)
|
||||
int len = strlen(strings_lower[i].in) + 1;
|
||||
|
||||
dst = kmalloc(len, GFP_KERNEL);
|
||||
if (!dst)
|
||||
return;
|
||||
KUNIT_ASSERT_NOT_NULL(test, dst);
|
||||
|
||||
string_lower(dst, s);
|
||||
if (memcmp(dst, strings_lower[i].out, len)) {
|
||||
pr_warn("Test 'string_lower failed : : expected %s, got %s!\n",
|
||||
strings_lower[i].out, dst);
|
||||
kfree(dst);
|
||||
return;
|
||||
}
|
||||
KUNIT_EXPECT_STREQ(test, dst, strings_lower[i].out);
|
||||
kfree(dst);
|
||||
}
|
||||
}
|
||||
|
||||
static int __init test_string_helpers_init(void)
|
||||
static void test_unescape(struct kunit *test)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
pr_info("Running tests...\n");
|
||||
for (i = 0; i < UNESCAPE_ALL_MASK + 1; i++)
|
||||
test_string_unescape("unescape", i, false);
|
||||
test_string_unescape("unescape inplace",
|
||||
test_string_unescape(test, "unescape", i, false);
|
||||
test_string_unescape(test, "unescape inplace",
|
||||
get_random_u32_below(UNESCAPE_ALL_MASK + 1), true);
|
||||
|
||||
/* Without dictionary */
|
||||
for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
|
||||
test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
|
||||
test_string_escape(test, "escape 0", escape0, i, TEST_STRING_2_DICT_0);
|
||||
|
||||
/* With dictionary */
|
||||
for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
|
||||
test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
|
||||
|
||||
/* Test string_get_size() */
|
||||
test_string_get_size();
|
||||
|
||||
/* Test string upper(), string_lower() */
|
||||
test_string_upper_lower();
|
||||
|
||||
return -EINVAL;
|
||||
test_string_escape(test, "escape 1", escape1, i, TEST_STRING_2_DICT_1);
|
||||
}
|
||||
module_init(test_string_helpers_init);
|
||||
|
||||
static struct kunit_case string_helpers_test_cases[] = {
|
||||
KUNIT_CASE(test_get_size),
|
||||
KUNIT_CASE(test_upper_lower),
|
||||
KUNIT_CASE(test_unescape),
|
||||
{}
|
||||
};
|
||||
|
||||
static struct kunit_suite string_helpers_test_suite = {
|
||||
.name = "string_helpers",
|
||||
.test_cases = string_helpers_test_cases,
|
||||
};
|
||||
|
||||
kunit_test_suites(&string_helpers_test_suite);
|
||||
|
||||
MODULE_LICENSE("Dual BSD/GPL");
|
Loading…
Reference in New Issue
Block a user