lib: kunit: add bitfield test conversion to KUnit

This adds the conversion of the runtime tests of test_bitfield,
from `lib/test_bitfield.c` to KUnit tests.

Code Style Documentation: [0]

Signed-off-by: Vitor Massaru Iha <vitor@massaru.org>
Link: [0] https://lore.kernel.org/linux-kselftest/20200620054944.167330-1-davidgow@google.com/T/#u
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by Vitor Massaru Iha and committed by Shuah Khan d2585f51 a82763e6

+59 -62
+16 -7
lib/Kconfig.debug
··· 2037 2037 2038 2038 If unsure, say N. 2039 2039 2040 - config TEST_BITFIELD 2041 - tristate "Test bitfield functions at runtime" 2042 - help 2043 - Enable this option to test the bitfield functions at boot. 2044 - 2045 - If unsure, say N. 2046 - 2047 2040 config TEST_UUID 2048 2041 tristate "Test functions located in the uuid module at runtime" 2049 2042 ··· 2183 2190 This builds the "test_sysctl" module. This driver enables to test the 2184 2191 proc sysctl interfaces available to drivers safely without affecting 2185 2192 production knobs which might alter system functionality. 2193 + 2194 + If unsure, say N. 2195 + 2196 + config BITFIELD_KUNIT 2197 + tristate "KUnit test bitfield functions at runtime" 2198 + depends on KUNIT 2199 + help 2200 + Enable this option to test the bitfield functions at boot. 2201 + 2202 + KUnit tests run during boot and output the results to the debug log 2203 + in TAP format (http://testanything.org/). Only useful for kernel devs 2204 + running the KUnit test harness, and not intended for inclusion into a 2205 + production build. 2206 + 2207 + For more information on KUnit and unit tests in general please refer 2208 + to the KUnit documentation in Documentation/dev-tools/kunit/. 2186 2209 2187 2210 If unsure, say N. 2188 2211
+1 -1
lib/Makefile
··· 80 80 obj-$(CONFIG_TEST_PRINTF) += test_printf.o 81 81 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o 82 82 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o 83 - obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o 84 83 obj-$(CONFIG_TEST_UUID) += test_uuid.o 85 84 obj-$(CONFIG_TEST_XARRAY) += test_xarray.o 86 85 obj-$(CONFIG_TEST_PARMAN) += test_parman.o ··· 339 340 obj-$(CONFIG_PLDMFW) += pldmfw/ 340 341 341 342 # KUnit tests 343 + obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o 342 344 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o 343 345 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o 344 346 obj-$(CONFIG_BITS_TEST) += test_bits.o
+42 -54
lib/test_bitfield.c lib/bitfield_kunit.c
··· 5 5 6 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 7 8 - #include <linux/kernel.h> 9 - #include <linux/module.h> 8 + #include <kunit/test.h> 10 9 #include <linux/bitfield.h> 11 10 12 11 #define CHECK_ENC_GET_U(tp, v, field, res) do { \ ··· 13 14 u##tp _res; \ 14 15 \ 15 16 _res = u##tp##_encode_bits(v, field); \ 16 - if (_res != res) { \ 17 - pr_warn("u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n",\ 18 - (u64)_res); \ 19 - return -EINVAL; \ 20 - } \ 21 - if (u##tp##_get_bits(_res, field) != v) \ 22 - return -EINVAL; \ 17 + KUNIT_ASSERT_FALSE_MSG(context, _res != res, \ 18 + "u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n", \ 19 + (u64)_res); \ 20 + KUNIT_ASSERT_FALSE(context, \ 21 + u##tp##_get_bits(_res, field) != v); \ 23 22 } \ 24 23 } while (0) 25 24 ··· 26 29 __le##tp _res; \ 27 30 \ 28 31 _res = le##tp##_encode_bits(v, field); \ 29 - if (_res != cpu_to_le##tp(res)) { \ 30 - pr_warn("le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\ 31 - (u64)le##tp##_to_cpu(_res), \ 32 - (u64)(res)); \ 33 - return -EINVAL; \ 34 - } \ 35 - if (le##tp##_get_bits(_res, field) != v) \ 36 - return -EINVAL; \ 32 + KUNIT_ASSERT_FALSE_MSG(context, \ 33 + _res != cpu_to_le##tp(res), \ 34 + "le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\ 35 + (u64)le##tp##_to_cpu(_res), \ 36 + (u64)(res)); \ 37 + KUNIT_ASSERT_FALSE(context, \ 38 + le##tp##_get_bits(_res, field) != v);\ 37 39 } \ 38 40 } while (0) 39 41 ··· 41 45 __be##tp _res; \ 42 46 \ 43 47 _res = be##tp##_encode_bits(v, field); \ 44 - if (_res != cpu_to_be##tp(res)) { \ 45 - pr_warn("be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\ 46 - (u64)be##tp##_to_cpu(_res), \ 47 - (u64)(res)); \ 48 - return -EINVAL; \ 49 - } \ 50 - if (be##tp##_get_bits(_res, field) != v) \ 51 - return -EINVAL; \ 48 + KUNIT_ASSERT_FALSE_MSG(context, \ 49 + _res != cpu_to_be##tp(res), \ 50 + "be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \ 51 + (u64)be##tp##_to_cpu(_res), \ 52 + (u64)(res)); \ 53 + KUNIT_ASSERT_FALSE(context, \ 54 + be##tp##_get_bits(_res, field) != v);\ 52 55 } \ 53 56 } while (0) 54 57 ··· 57 62 CHECK_ENC_GET_BE(tp, v, field, res); \ 58 63 } while (0) 59 64 60 - static int test_constants(void) 65 + static void __init test_bitfields_constants(struct kunit *context) 61 66 { 62 67 /* 63 68 * NOTE ··· 90 95 CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull); 91 96 CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull); 92 97 CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull); 93 - 94 - return 0; 95 98 } 96 99 97 100 #define CHECK(tp, mask) do { \ 98 101 u64 v; \ 99 102 \ 100 103 for (v = 0; v < 1 << hweight32(mask); v++) \ 101 - if (tp##_encode_bits(v, mask) != v << __ffs64(mask)) \ 102 - return -EINVAL; \ 104 + KUNIT_ASSERT_FALSE(context, \ 105 + tp##_encode_bits(v, mask) != v << __ffs64(mask));\ 103 106 } while (0) 104 107 105 - static int test_variables(void) 108 + static void __init test_bitfields_variables(struct kunit *context) 106 109 { 107 110 CHECK(u8, 0x0f); 108 111 CHECK(u8, 0xf0); ··· 123 130 CHECK(u64, 0x000000007f000000ull); 124 131 CHECK(u64, 0x0000000018000000ull); 125 132 CHECK(u64, 0x0000001f8000000ull); 126 - 127 - return 0; 128 133 } 129 134 130 - static int __init test_bitfields(void) 135 + 136 + static void __init test_bitfields_compile(struct kunit *context) 131 137 { 132 - int ret = test_constants(); 133 - 134 - if (ret) { 135 - pr_warn("constant tests failed!\n"); 136 - return ret; 137 - } 138 - 139 - ret = test_variables(); 140 - if (ret) { 141 - pr_warn("variable tests failed!\n"); 142 - return ret; 143 - } 144 - 145 - #ifdef TEST_BITFIELD_COMPILE 146 138 /* these should fail compilation */ 147 139 CHECK_ENC_GET(16, 16, 0x0f00, 0x1000); 148 140 u32_encode_bits(7, 0x06000000); 149 141 150 142 /* this should at least give a warning */ 151 143 u16_encode_bits(0, 0x60000); 152 - #endif 153 - 154 - pr_info("tests passed\n"); 155 - 156 - return 0; 157 144 } 158 - module_init(test_bitfields) 145 + 146 + static struct kunit_case __refdata bitfields_test_cases[] = { 147 + KUNIT_CASE(test_bitfields_constants), 148 + KUNIT_CASE(test_bitfields_variables), 149 + #ifdef TEST_BITFIELD_COMPILE 150 + KUNIT_CASE(test_bitfields_compile), 151 + #endif 152 + {} 153 + }; 154 + 155 + static struct kunit_suite bitfields_test_suite = { 156 + .name = "bitfields", 157 + .test_cases = bitfields_test_cases, 158 + }; 159 + 160 + kunit_test_suites(&bitfields_test_suite); 159 161 160 162 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); 161 163 MODULE_LICENSE("GPL");