Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+103 -127
+1 -1
MAINTAINERS
··· 8978 8978 F: lib/string.c 8979 8979 F: lib/string_kunit.c 8980 8980 F: lib/string_helpers.c 8981 - F: lib/test-string_helpers.c 8981 + F: lib/string_helpers_kunit.c 8982 8982 F: scripts/coccinelle/api/string_choices.cocci 8983 8983 8984 8984 GENERIC UIO DRIVER FOR PCI DEVICES
+4 -2
lib/Kconfig.debug
··· 2357 2357 depends on KUNIT 2358 2358 default KUNIT_ALL_TESTS 2359 2359 2360 - config TEST_STRING_HELPERS 2361 - tristate "Test functions located in the string_helpers module at runtime" 2360 + config STRING_HELPERS_KUNIT_TEST 2361 + tristate "KUnit test string helpers at runtime" if !KUNIT_ALL_TESTS 2362 + depends on KUNIT 2363 + default KUNIT_ALL_TESTS 2362 2364 2363 2365 config TEST_KSTRTOX 2364 2366 tristate "Test kstrto*() family of functions at runtime"
+1 -1
lib/Makefile
··· 51 51 generic-radix-tree.o bitmap-str.o 52 52 obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o 53 53 obj-y += string_helpers.o 54 - obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 54 + obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o 55 55 obj-y += hexdump.o 56 56 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o 57 57 obj-y += kstrtox.o
+97 -123
lib/test-string_helpers.c lib/string_helpers_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 1 2 /* 2 3 * Test cases for lib/string_helpers.c module. 3 4 */ 4 5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 5 6 7 + #include <kunit/test.h> 6 8 #include <linux/array_size.h> 7 - #include <linux/init.h> 8 9 #include <linux/kernel.h> 9 - #include <linux/slab.h> 10 - #include <linux/module.h> 11 10 #include <linux/random.h> 12 11 #include <linux/string.h> 13 12 #include <linux/string_helpers.h> 14 13 15 - static __init bool test_string_check_buf(const char *name, unsigned int flags, 16 - char *in, size_t p, 17 - char *out_real, size_t q_real, 18 - char *out_test, size_t q_test) 14 + static void test_string_check_buf(struct kunit *test, 15 + const char *name, unsigned int flags, 16 + char *in, size_t p, 17 + char *out_real, size_t q_real, 18 + char *out_test, size_t q_test) 19 19 { 20 - if (q_real == q_test && !memcmp(out_test, out_real, q_test)) 21 - return true; 22 - 23 - pr_warn("Test '%s' failed: flags = %#x\n", name, flags); 24 - 25 - print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1, 26 - in, p, true); 27 - print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1, 28 - out_test, q_test, true); 29 - print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1, 30 - out_real, q_real, true); 31 - 32 - return false; 20 + KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name); 21 + KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test, 22 + "name:%s", name); 33 23 } 34 24 35 25 struct test_string { ··· 28 38 unsigned int flags; 29 39 }; 30 40 31 - static const struct test_string strings[] __initconst = { 41 + static const struct test_string strings[] = { 32 42 { 33 43 .in = "\\f\\ \\n\\r\\t\\v", 34 44 .out = "\f\\ \n\r\t\v", ··· 51 61 }, 52 62 }; 53 63 54 - static void __init test_string_unescape(const char *name, unsigned int flags, 55 - bool inplace) 64 + static void test_string_unescape(struct kunit *test, 65 + const char *name, unsigned int flags, 66 + bool inplace) 56 67 { 57 68 int q_real = 256; 58 - char *in = kmalloc(q_real, GFP_KERNEL); 59 - char *out_test = kmalloc(q_real, GFP_KERNEL); 60 - char *out_real = kmalloc(q_real, GFP_KERNEL); 69 + char *in = kunit_kzalloc(test, q_real, GFP_KERNEL); 70 + char *out_test = kunit_kzalloc(test, q_real, GFP_KERNEL); 71 + char *out_real = kunit_kzalloc(test, q_real, GFP_KERNEL); 61 72 int i, p = 0, q_test = 0; 62 73 63 - if (!in || !out_test || !out_real) 64 - goto out; 74 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in); 75 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test); 76 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real); 65 77 66 78 for (i = 0; i < ARRAY_SIZE(strings); i++) { 67 79 const char *s = strings[i].in; ··· 96 104 q_real = string_unescape(in, out_real, q_real, flags); 97 105 } 98 106 99 - test_string_check_buf(name, flags, in, p - 1, out_real, q_real, 107 + test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real, 100 108 out_test, q_test); 101 - out: 102 - kfree(out_real); 103 - kfree(out_test); 104 - kfree(in); 105 109 } 106 110 107 111 struct test_string_1 { ··· 112 124 }; 113 125 114 126 #define TEST_STRING_2_DICT_0 NULL 115 - static const struct test_string_2 escape0[] __initconst = {{ 127 + static const struct test_string_2 escape0[] = {{ 116 128 .in = "\f\\ \n\r\t\v", 117 129 .s1 = {{ 118 130 .out = "\\f\\ \\n\\r\\t\\v", ··· 210 222 }}; 211 223 212 224 #define TEST_STRING_2_DICT_1 "b\\ \t\r\xCF" 213 - static const struct test_string_2 escape1[] __initconst = {{ 225 + static const struct test_string_2 escape1[] = {{ 214 226 .in = "\f\\ \n\r\t\v", 215 227 .s1 = {{ 216 228 .out = "\f\\134\\040\n\\015\\011\v", ··· 347 359 /* terminator */ 348 360 }}; 349 361 350 - static const struct test_string strings_upper[] __initconst = { 362 + static const struct test_string strings_upper[] = { 351 363 { 352 364 .in = "abcdefgh1234567890test", 353 365 .out = "ABCDEFGH1234567890TEST", ··· 358 370 }, 359 371 }; 360 372 361 - static const struct test_string strings_lower[] __initconst = { 373 + static const struct test_string strings_lower[] = { 362 374 { 363 375 .in = "ABCDEFGH1234567890TEST", 364 376 .out = "abcdefgh1234567890test", ··· 369 381 }, 370 382 }; 371 383 372 - static __init const char *test_string_find_match(const struct test_string_2 *s2, 373 - unsigned int flags) 384 + static const char *test_string_find_match(const struct test_string_2 *s2, 385 + unsigned int flags) 374 386 { 375 387 const struct test_string_1 *s1 = s2->s1; 376 388 unsigned int i; ··· 391 403 return NULL; 392 404 } 393 405 394 - static __init void 395 - test_string_escape_overflow(const char *in, int p, unsigned int flags, const char *esc, 406 + static void 407 + test_string_escape_overflow(struct kunit *test, 408 + const char *in, int p, unsigned int flags, const char *esc, 396 409 int q_test, const char *name) 397 410 { 398 411 int q_real; 399 412 400 413 q_real = string_escape_mem(in, p, NULL, 0, flags, esc); 401 - if (q_real != q_test) 402 - pr_warn("Test '%s' failed: flags = %#x, osz = 0, expected %d, got %d\n", 403 - name, flags, q_test, q_real); 414 + KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s: flags:%#x", name, flags); 404 415 } 405 416 406 - static __init void test_string_escape(const char *name, 407 - const struct test_string_2 *s2, 408 - unsigned int flags, const char *esc) 417 + static void test_string_escape(struct kunit *test, const char *name, 418 + const struct test_string_2 *s2, 419 + unsigned int flags, const char *esc) 409 420 { 410 421 size_t out_size = 512; 411 - char *out_test = kmalloc(out_size, GFP_KERNEL); 412 - char *out_real = kmalloc(out_size, GFP_KERNEL); 413 - char *in = kmalloc(256, GFP_KERNEL); 422 + char *out_test = kunit_kzalloc(test, out_size, GFP_KERNEL); 423 + char *out_real = kunit_kzalloc(test, out_size, GFP_KERNEL); 424 + char *in = kunit_kzalloc(test, 256, GFP_KERNEL); 414 425 int p = 0, q_test = 0; 415 426 int q_real; 416 427 417 - if (!out_test || !out_real || !in) 418 - goto out; 428 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test); 429 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real); 430 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in); 419 431 420 432 for (; s2->in; s2++) { 421 433 const char *out; ··· 451 463 452 464 q_real = string_escape_mem(in, p, out_real, out_size, flags, esc); 453 465 454 - test_string_check_buf(name, flags, in, p, out_real, q_real, out_test, 466 + test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_test, 455 467 q_test); 456 468 457 - test_string_escape_overflow(in, p, flags, esc, q_test, name); 458 - 459 - out: 460 - kfree(in); 461 - kfree(out_real); 462 - kfree(out_test); 469 + test_string_escape_overflow(test, in, p, flags, esc, q_test, name); 463 470 } 464 471 465 472 #define string_get_size_maxbuf 16 466 - #define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \ 467 - do { \ 468 - BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \ 469 - BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \ 470 - __test_string_get_size((size), (blk_size), (exp_result10), \ 471 - (exp_result2)); \ 473 + #define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \ 474 + do { \ 475 + BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \ 476 + BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \ 477 + __test_string_get_size(test, (size), (blk_size), (exp_result10), \ 478 + (exp_result2)); \ 472 479 } while (0) 473 480 474 481 475 - static __init void test_string_get_size_check(const char *units, 476 - const char *exp, 477 - char *res, 478 - const u64 size, 479 - const u64 blk_size) 482 + static void test_string_get_size_check(struct kunit *test, 483 + const char *units, 484 + const char *exp, 485 + char *res, 486 + const u64 size, 487 + const u64 blk_size) 480 488 { 481 - if (!memcmp(res, exp, strlen(exp) + 1)) 482 - return; 483 - 484 - res[string_get_size_maxbuf - 1] = '\0'; 485 - 486 - pr_warn("Test 'test_string_get_size' failed!\n"); 487 - pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n", 489 + KUNIT_EXPECT_MEMEQ_MSG(test, res, exp, strlen(exp) + 1, 490 + "string_get_size(size = %llu, blk_size = %llu, units = %s)", 488 491 size, blk_size, units); 489 - pr_warn("expected: '%s', got '%s'\n", exp, res); 490 492 } 491 493 492 - static __init void __strchrcut(char *dst, const char *src, const char *cut) 494 + static void __strchrcut(char *dst, const char *src, const char *cut) 493 495 { 494 496 const char *from = src; 495 497 size_t len; ··· 493 515 *dst = '\0'; 494 516 } 495 517 496 - static __init void __test_string_get_size_one(const u64 size, const u64 blk_size, 497 - const char *exp_result10, 498 - const char *exp_result2, 499 - enum string_size_units units, 500 - const char *cut) 518 + static void __test_string_get_size_one(struct kunit *test, 519 + const u64 size, const u64 blk_size, 520 + const char *exp_result10, 521 + const char *exp_result2, 522 + enum string_size_units units, 523 + const char *cut) 501 524 { 502 525 char buf10[string_get_size_maxbuf]; 503 526 char buf2[string_get_size_maxbuf]; ··· 516 537 string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(buf10)); 517 538 string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2)); 518 539 519 - test_string_get_size_check(prefix10, exp10, buf10, size, blk_size); 520 - test_string_get_size_check(prefix2, exp2, buf2, size, blk_size); 540 + test_string_get_size_check(test, prefix10, exp10, buf10, size, blk_size); 541 + test_string_get_size_check(test, prefix2, exp2, buf2, size, blk_size); 521 542 } 522 543 523 - static __init void __test_string_get_size(const u64 size, const u64 blk_size, 524 - const char *exp_result10, 525 - const char *exp_result2) 544 + static void __test_string_get_size(struct kunit *test, 545 + const u64 size, const u64 blk_size, 546 + const char *exp_result10, 547 + const char *exp_result2) 526 548 { 527 549 struct { 528 550 enum string_size_units units; ··· 537 557 int i; 538 558 539 559 for (i = 0; i < ARRAY_SIZE(get_size_test_cases); i++) 540 - __test_string_get_size_one(size, blk_size, exp_result10, exp_result2, 560 + __test_string_get_size_one(test, size, blk_size, 561 + exp_result10, exp_result2, 541 562 get_size_test_cases[i].units, 542 563 get_size_test_cases[i].cut); 543 564 } 544 565 545 - static __init void test_string_get_size(void) 566 + static void test_get_size(struct kunit *test) 546 567 { 547 568 /* small values */ 548 569 test_string_get_size_one(0, 512, "0 B", "0 B"); ··· 563 582 test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB"); 564 583 } 565 584 566 - static void __init test_string_upper_lower(void) 585 + static void test_upper_lower(struct kunit *test) 567 586 { 568 587 char *dst; 569 588 int i; ··· 573 592 int len = strlen(strings_upper[i].in) + 1; 574 593 575 594 dst = kmalloc(len, GFP_KERNEL); 576 - if (!dst) 577 - return; 595 + KUNIT_ASSERT_NOT_NULL(test, dst); 578 596 579 597 string_upper(dst, s); 580 - if (memcmp(dst, strings_upper[i].out, len)) { 581 - pr_warn("Test 'string_upper' failed : expected %s, got %s!\n", 582 - strings_upper[i].out, dst); 583 - kfree(dst); 584 - return; 585 - } 598 + KUNIT_EXPECT_STREQ(test, dst, strings_upper[i].out); 586 599 kfree(dst); 587 600 } 588 601 ··· 585 610 int len = strlen(strings_lower[i].in) + 1; 586 611 587 612 dst = kmalloc(len, GFP_KERNEL); 588 - if (!dst) 589 - return; 613 + KUNIT_ASSERT_NOT_NULL(test, dst); 590 614 591 615 string_lower(dst, s); 592 - if (memcmp(dst, strings_lower[i].out, len)) { 593 - pr_warn("Test 'string_lower failed : : expected %s, got %s!\n", 594 - strings_lower[i].out, dst); 595 - kfree(dst); 596 - return; 597 - } 616 + KUNIT_EXPECT_STREQ(test, dst, strings_lower[i].out); 598 617 kfree(dst); 599 618 } 600 619 } 601 620 602 - static int __init test_string_helpers_init(void) 621 + static void test_unescape(struct kunit *test) 603 622 { 604 623 unsigned int i; 605 624 606 - pr_info("Running tests...\n"); 607 625 for (i = 0; i < UNESCAPE_ALL_MASK + 1; i++) 608 - test_string_unescape("unescape", i, false); 609 - test_string_unescape("unescape inplace", 626 + test_string_unescape(test, "unescape", i, false); 627 + test_string_unescape(test, "unescape inplace", 610 628 get_random_u32_below(UNESCAPE_ALL_MASK + 1), true); 611 629 612 630 /* Without dictionary */ 613 631 for (i = 0; i < ESCAPE_ALL_MASK + 1; i++) 614 - test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0); 632 + test_string_escape(test, "escape 0", escape0, i, TEST_STRING_2_DICT_0); 615 633 616 634 /* With dictionary */ 617 635 for (i = 0; i < ESCAPE_ALL_MASK + 1; i++) 618 - test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1); 619 - 620 - /* Test string_get_size() */ 621 - test_string_get_size(); 622 - 623 - /* Test string upper(), string_lower() */ 624 - test_string_upper_lower(); 625 - 626 - return -EINVAL; 636 + test_string_escape(test, "escape 1", escape1, i, TEST_STRING_2_DICT_1); 627 637 } 628 - module_init(test_string_helpers_init); 638 + 639 + static struct kunit_case string_helpers_test_cases[] = { 640 + KUNIT_CASE(test_get_size), 641 + KUNIT_CASE(test_upper_lower), 642 + KUNIT_CASE(test_unescape), 643 + {} 644 + }; 645 + 646 + static struct kunit_suite string_helpers_test_suite = { 647 + .name = "string_helpers", 648 + .test_cases = string_helpers_test_cases, 649 + }; 650 + 651 + kunit_test_suites(&string_helpers_test_suite); 652 + 629 653 MODULE_LICENSE("Dual BSD/GPL");