Skip to contentSkip to conversation

lib/crc16_kunit.c: add KUnit tests for crc16

PATCH Superseded Vinicius Peixoto 2 participants 3 messages Oct 3, 2024 – Oct 4, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/crc16_kunit.c: add KUnit tests for crc16

Add Kunit tests for the kernel's implementation of the standard CRC-16 algorithm (<linux/crc16.h>). The test data consists of 100 randomly-generated test cases, validated against a naive CRC-16 implementation.

This test follows roughly the same logic as lib/crc32test.c, but without the performance measurements.

---
Hi all,

This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).

We noticed there were tests for CRC32 in lib/crc32test.c and thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).

We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)

Link to v1: https://lore.kernel.org/linux-kselftest/[email protected]/

Changes in v2 (suggested by David Laight):
- Use the PRNG from include/linux/prandom.h to generate pseudorandom data/test cases instead of having them hardcoded as large static arrays
- Add a naive CRC16 implementation used to validate the kernel's implementation (instead of having the test case results be hard-coded)

[1] https://lkcamp.dev/about

---
 lib/Kconfig.debug |   9 +++
 lib/Makefile      |   1 +
 lib/crc16_kunit.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 175 insertions(+)
lib/Kconfig.debug +9 -0
index 7315f643817ae1021f1e4b3dd27b424f49e3f761..f9617e3054948ce43090f524dc67650e9549cee8 100644
@@ -2850,6 +2850,15 @@ config USERCOPY_KUNIT_TEST
 	  on the copy_to/from_user infrastructure, making sure basic
 	  user/kernel boundary testing is working.
 
+config CRC16_KUNIT_TEST
+	tristate "KUnit tests for CRC16"
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	select CRC16
+	help
+	  Enable this option to run unit tests for the kernel's CRC16
+	  implementation (<linux/crc16.h>).
+
 config TEST_UDELAY
 	tristate "udelay test driver"
 	help
lib/Makefile +1 -0
index 773adf88af41665b2419202e5427e0513c6becae..1faed6414a85fd366b4966a00e8ba231d7546e14 100644
@@ -389,6 +389,7 @@ CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
 obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
 obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
+obj-$(CONFIG_CRC16_KUNIT_TEST) += crc16_kunit.o
 
 obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
 
lib/crc16_kunit.c new file +165 -0

165 lines not on this page · raw message

---
base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
change-id: 20241003-crc16-kunit-127a4dc2b72c

Best regards,

Signature · 1 line
Vinicius Peixoto <[email protected]>
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Vinicius,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 9852d85ec9d492ebef56dc5f229416c925758edc]

url:    https://github.com/intel-lab-lkp/linux/commits/Vinicius-Peixoto/lib-crc16_kunit-c-add-KUnit-tests-for-crc16/20241004-050248
base:   9852d85ec9d492ebef56dc5f229416c925758edc
patch link:    https://lore.kernel.org/r/20241003-crc16-kunit-v2-1-5fe74b113e1e%40lkcamp.dev
patch subject: [PATCH v2] lib/crc16_kunit.c: add KUnit tests for crc16
config: parisc-randconfig-r071-20241005 (https://download.01.org/0day-ci/archive/20241005/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241005/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> lib/crc16_kunit.c:29: warning: Excess struct member 'crc16' description in 'crc16_test'

lib/crc16_kunit.c:96: warning: Function parameter or struct member 'test' not described in 'crc16_test_empty' lib/crc16_kunit.c:111: warning: Function parameter or struct member 'test' not described in 'crc16_test_correctness' lib/crc16_kunit.c:132: warning: Function parameter or struct member 'test' not described in 'crc16_test_combine'

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [m]:
- RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]

vim +29 lib/crc16_kunit.c

    17	
    18	/**
    19	 * struct crc16_test - CRC16 test data
    20	 * @crc: initial input value to CRC16
    21	 * @start: Start index within the data buffer
    22	 * @length: Length of the data
    23	 * @crc16: Expected CRC16 value for the test
    24	 */
    25	static struct crc16_test {
    26		u16 crc;
    27		u16 start;
    28		u16 length;
  > 29	} tests[CRC16_KUNIT_TEST_SIZE];
    30	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
Quoted 19 lines · kernel test robot

Hi Vinicius,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 9852d85ec9d492ebef56dc5f229416c925758edc]

url:    https://github.com/intel-lab-lkp/linux/commits/Vinicius-Peixoto/lib-crc16_kunit-c-add-KUnit-tests-for-crc16/20241004-050248
base:   9852d85ec9d492ebef56dc5f229416c925758edc
patch link:    https://lore.kernel.org/r/20241003-crc16-kunit-v2-1-5fe74b113e1e%40lkcamp.dev
patch subject: [PATCH v2] lib/crc16_kunit.c: add KUnit tests for crc16
config: parisc-randconfig-r071-20241005 (https://download.01.org/0day-ci/archive/20241005/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241005/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 7 lines

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

lib/crc16_kunit.c:29: warning: Excess struct member 'crc16' description in 'crc16_test'

lib/crc16_kunit.c:96: warning: Function parameter or struct member 'test' not described in 'crc16_test_empty' lib/crc16_kunit.c:111: warning: Function parameter or struct member 'test' not described in 'crc16_test_correctness' lib/crc16_kunit.c:132: warning: Function parameter or struct member 'test' not described in 'crc16_test_combine'

Ah, oops, I forgot to update the function documentation for v2. I will fix these warnings in v3.

Quoted 24 lines

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [m]:
- RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]

vim +29 lib/crc16_kunit.c

     17	
     18	/**
     19	 * struct crc16_test - CRC16 test data
     20	 * @crc: initial input value to CRC16
     21	 * @start: Start index within the data buffer
     22	 * @length: Length of the data
     23	 * @crc16: Expected CRC16 value for the test
     24	 */
     25	static struct crc16_test {
     26		u16 crc;
     27		u16 start;
     28		u16 length;
Quoted 1 line

29 } tests[CRC16_KUNIT_TEST_SIZE];

     30