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 2038 If unsure, say N. 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 config TEST_UUID 2048 tristate "Test functions located in the uuid module at runtime" 2049 ··· 2183 This builds the "test_sysctl" module. This driver enables to test the 2184 proc sysctl interfaces available to drivers safely without affecting 2185 production knobs which might alter system functionality. 2186 2187 If unsure, say N. 2188
··· 2037 2038 If unsure, say N. 2039 2040 config TEST_UUID 2041 tristate "Test functions located in the uuid module at runtime" 2042 ··· 2190 This builds the "test_sysctl" module. This driver enables to test the 2191 proc sysctl interfaces available to drivers safely without affecting 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/. 2209 2210 If unsure, say N. 2211
+1 -1
lib/Makefile
··· 80 obj-$(CONFIG_TEST_PRINTF) += test_printf.o 81 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o 82 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o 83 - obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o 84 obj-$(CONFIG_TEST_UUID) += test_uuid.o 85 obj-$(CONFIG_TEST_XARRAY) += test_xarray.o 86 obj-$(CONFIG_TEST_PARMAN) += test_parman.o ··· 339 obj-$(CONFIG_PLDMFW) += pldmfw/ 340 341 # KUnit tests 342 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o 343 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o 344 obj-$(CONFIG_BITS_TEST) += test_bits.o
··· 80 obj-$(CONFIG_TEST_PRINTF) += test_printf.o 81 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o 82 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o 83 obj-$(CONFIG_TEST_UUID) += test_uuid.o 84 obj-$(CONFIG_TEST_XARRAY) += test_xarray.o 85 obj-$(CONFIG_TEST_PARMAN) += test_parman.o ··· 340 obj-$(CONFIG_PLDMFW) += pldmfw/ 341 342 # KUnit tests 343 + obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o 344 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o 345 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o 346 obj-$(CONFIG_BITS_TEST) += test_bits.o
+42 -54
lib/test_bitfield.c lib/bitfield_kunit.c
··· 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 - #include <linux/kernel.h> 9 - #include <linux/module.h> 10 #include <linux/bitfield.h> 11 12 #define CHECK_ENC_GET_U(tp, v, field, res) do { \ ··· 13 u##tp _res; \ 14 \ 15 _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; \ 23 } \ 24 } while (0) 25 ··· 26 __le##tp _res; \ 27 \ 28 _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; \ 37 } \ 38 } while (0) 39 ··· 41 __be##tp _res; \ 42 \ 43 _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; \ 52 } \ 53 } while (0) 54 ··· 57 CHECK_ENC_GET_BE(tp, v, field, res); \ 58 } while (0) 59 60 - static int test_constants(void) 61 { 62 /* 63 * NOTE ··· 90 CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull); 91 CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull); 92 CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull); 93 - 94 - return 0; 95 } 96 97 #define CHECK(tp, mask) do { \ 98 u64 v; \ 99 \ 100 for (v = 0; v < 1 << hweight32(mask); v++) \ 101 - if (tp##_encode_bits(v, mask) != v << __ffs64(mask)) \ 102 - return -EINVAL; \ 103 } while (0) 104 105 - static int test_variables(void) 106 { 107 CHECK(u8, 0x0f); 108 CHECK(u8, 0xf0); ··· 123 CHECK(u64, 0x000000007f000000ull); 124 CHECK(u64, 0x0000000018000000ull); 125 CHECK(u64, 0x0000001f8000000ull); 126 - 127 - return 0; 128 } 129 130 - static int __init test_bitfields(void) 131 { 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 /* these should fail compilation */ 147 CHECK_ENC_GET(16, 16, 0x0f00, 0x1000); 148 u32_encode_bits(7, 0x06000000); 149 150 /* this should at least give a warning */ 151 u16_encode_bits(0, 0x60000); 152 - #endif 153 - 154 - pr_info("tests passed\n"); 155 - 156 - return 0; 157 } 158 - module_init(test_bitfields) 159 160 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); 161 MODULE_LICENSE("GPL");
··· 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 + #include <kunit/test.h> 9 #include <linux/bitfield.h> 10 11 #define CHECK_ENC_GET_U(tp, v, field, res) do { \ ··· 14 u##tp _res; \ 15 \ 16 _res = u##tp##_encode_bits(v, field); \ 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); \ 22 } \ 23 } while (0) 24 ··· 29 __le##tp _res; \ 30 \ 31 _res = le##tp##_encode_bits(v, field); \ 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);\ 39 } \ 40 } while (0) 41 ··· 45 __be##tp _res; \ 46 \ 47 _res = be##tp##_encode_bits(v, field); \ 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);\ 55 } \ 56 } while (0) 57 ··· 62 CHECK_ENC_GET_BE(tp, v, field, res); \ 63 } while (0) 64 65 + static void __init test_bitfields_constants(struct kunit *context) 66 { 67 /* 68 * NOTE ··· 95 CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull); 96 CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull); 97 CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull); 98 } 99 100 #define CHECK(tp, mask) do { \ 101 u64 v; \ 102 \ 103 for (v = 0; v < 1 << hweight32(mask); v++) \ 104 + KUNIT_ASSERT_FALSE(context, \ 105 + tp##_encode_bits(v, mask) != v << __ffs64(mask));\ 106 } while (0) 107 108 + static void __init test_bitfields_variables(struct kunit *context) 109 { 110 CHECK(u8, 0x0f); 111 CHECK(u8, 0xf0); ··· 130 CHECK(u64, 0x000000007f000000ull); 131 CHECK(u64, 0x0000000018000000ull); 132 CHECK(u64, 0x0000001f8000000ull); 133 } 134 135 + 136 + static void __init test_bitfields_compile(struct kunit *context) 137 { 138 /* these should fail compilation */ 139 CHECK_ENC_GET(16, 16, 0x0f00, 0x1000); 140 u32_encode_bits(7, 0x06000000); 141 142 /* this should at least give a warning */ 143 u16_encode_bits(0, 0x60000); 144 } 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); 161 162 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); 163 MODULE_LICENSE("GPL");