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

HID: uclogic: Add KUnit tests for uclogic_rdesc_template_apply()

The uclogic_rdesc_template_apply() function is used by the driver to
generate HID descriptors from templates.

In order to avoid regressions in future patches, add KUnit tests to
test the function.

To run the tests:

$ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/hid \
--kconfig_add CONFIG_VIRTIO_UML=y \
--kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

Signed-off-by: José Expósito <jose.exposito89@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

José Expósito and committed by
Jiri Kosina
2d167aab 76e645be

+207
+5
drivers/hid/.kunitconfig
··· 1 + CONFIG_KUNIT=y 2 + CONFIG_USB=y 3 + CONFIG_USB_HID=y 4 + CONFIG_HID_UCLOGIC=y 5 + CONFIG_HID_KUNIT_TEST=y
+16
drivers/hid/Kconfig
··· 1306 1306 To compile this driver as a module, choose M here: the module 1307 1307 will be called hid-mcp2221.ko. 1308 1308 1309 + config HID_KUNIT_TEST 1310 + bool "KUnit tests for HID" if !KUNIT_ALL_TESTS 1311 + depends on KUNIT=y 1312 + depends on HID_UCLOGIC 1313 + default KUNIT_ALL_TESTS 1314 + help 1315 + This builds unit tests for HID. This option is not useful for 1316 + distributions or general kernels, but only for kernel 1317 + developers working on HID and associated drivers. 1318 + 1319 + For more information on KUnit and unit tests in general, 1320 + please refer to the KUnit documentation in 1321 + Documentation/dev-tools/kunit/. 1322 + 1323 + If in doubt, say "N". 1324 + 1309 1325 endmenu 1310 1326 1311 1327 endif # HID
+3
drivers/hid/Makefile
··· 144 144 obj-$(CONFIG_HID_SENSOR_HUB) += hid-sensor-hub.o 145 145 obj-$(CONFIG_HID_SENSOR_CUSTOM_SENSOR) += hid-sensor-custom.o 146 146 147 + obj-$(CONFIG_HID_KUNIT_TEST) += hid-uclogic-rdesc.o \ 148 + hid-uclogic-rdesc-test.o 149 + 147 150 obj-$(CONFIG_USB_HID) += usbhid/ 148 151 obj-$(CONFIG_USB_MOUSE) += usbhid/ 149 152 obj-$(CONFIG_USB_KBD) += usbhid/
+183
drivers/hid/hid-uclogic-rdesc-test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + 3 + /* 4 + * HID driver for UC-Logic devices not fully compliant with HID standard 5 + * 6 + * Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com> 7 + */ 8 + 9 + #include <kunit/test.h> 10 + #include "./hid-uclogic-rdesc.h" 11 + 12 + struct uclogic_template_case { 13 + const char *name; 14 + const __u8 *template; 15 + size_t template_size; 16 + const s32 *param_list; 17 + size_t param_num; 18 + const __u8 *expected; 19 + }; 20 + 21 + static const s32 params_pen_all[UCLOGIC_RDESC_PH_ID_NUM] = { 22 + [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA, 23 + [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB, 24 + [UCLOGIC_RDESC_PEN_PH_ID_Y_LM] = 0xCC, 25 + [UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0xDD, 26 + [UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] = 0xEE, 27 + }; 28 + 29 + static const s32 params_pen_some[] = { 30 + [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA, 31 + [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB, 32 + }; 33 + 34 + static const __u8 template_empty[] = { }; 35 + static const __u8 template_small[] = { 0x00 }; 36 + static const __u8 template_no_ph[] = { 0xAA, 0xFE, 0xAA, 0xED, 0x1D }; 37 + 38 + static const __u8 template_pen_ph_end[] = { 39 + 0xAA, 0xBB, UCLOGIC_RDESC_PEN_PH_HEAD 40 + }; 41 + 42 + static const __u8 template_pen_all_params[] = { 43 + UCLOGIC_RDESC_PEN_PH(X_LM), 44 + 0x47, UCLOGIC_RDESC_PEN_PH(X_PM), 45 + 0x27, UCLOGIC_RDESC_PEN_PH(Y_LM), 46 + UCLOGIC_RDESC_PEN_PH(Y_PM), 47 + 0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM), 48 + }; 49 + 50 + static const __u8 expected_pen_all_params[] = { 51 + 0xAA, 0x00, 0x00, 0x00, 52 + 0x47, 0xBB, 0x00, 0x00, 0x00, 53 + 0x27, 0xCC, 0x00, 0x00, 0x00, 54 + 0xDD, 0x00, 0x00, 0x00, 55 + 0x00, 0xEE, 0x00, 0x00, 0x00, 56 + }; 57 + 58 + static const __u8 template_pen_some_params[] = { 59 + 0x01, 0x02, 60 + UCLOGIC_RDESC_PEN_PH(X_LM), 61 + 0x03, UCLOGIC_RDESC_PEN_PH(X_PM), 62 + 0x04, 0x05, 63 + }; 64 + 65 + static const __u8 expected_pen_some_params[] = { 66 + 0x01, 0x02, 67 + 0xAA, 0x00, 0x00, 0x00, 68 + 0x03, 0xBB, 0x00, 0x00, 0x00, 69 + 0x04, 0x05, 70 + }; 71 + 72 + static const __u8 template_params_none[] = { 73 + 0x27, UCLOGIC_RDESC_PEN_PH(Y_LM), 74 + UCLOGIC_RDESC_PEN_PH(Y_PM), 75 + 0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM), 76 + }; 77 + 78 + static struct uclogic_template_case uclogic_template_cases[] = { 79 + { 80 + .name = "Empty template", 81 + .template = template_empty, 82 + .template_size = sizeof(template_empty), 83 + .param_list = params_pen_all, 84 + .param_num = ARRAY_SIZE(params_pen_all), 85 + .expected = template_empty, 86 + }, 87 + { 88 + .name = "Template smaller than the placeholder", 89 + .template = template_small, 90 + .template_size = sizeof(template_small), 91 + .param_list = params_pen_all, 92 + .param_num = ARRAY_SIZE(params_pen_all), 93 + .expected = template_small, 94 + }, 95 + { 96 + .name = "No placeholder", 97 + .template = template_no_ph, 98 + .template_size = sizeof(template_no_ph), 99 + .param_list = params_pen_all, 100 + .param_num = ARRAY_SIZE(params_pen_all), 101 + .expected = template_no_ph, 102 + }, 103 + { 104 + .name = "Pen placeholder at the end, without ID", 105 + .template = template_pen_ph_end, 106 + .template_size = sizeof(template_pen_ph_end), 107 + .param_list = params_pen_all, 108 + .param_num = ARRAY_SIZE(params_pen_all), 109 + .expected = template_pen_ph_end, 110 + }, 111 + { 112 + .name = "All params present in the pen template", 113 + .template = template_pen_all_params, 114 + .template_size = sizeof(template_pen_all_params), 115 + .param_list = params_pen_all, 116 + .param_num = ARRAY_SIZE(params_pen_all), 117 + .expected = expected_pen_all_params, 118 + }, 119 + { 120 + .name = "Some params present in the pen template (complete param list)", 121 + .template = template_pen_some_params, 122 + .template_size = sizeof(template_pen_some_params), 123 + .param_list = params_pen_all, 124 + .param_num = ARRAY_SIZE(params_pen_all), 125 + .expected = expected_pen_some_params, 126 + }, 127 + { 128 + .name = "Some params present in the pen template (incomplete param list)", 129 + .template = template_pen_some_params, 130 + .template_size = sizeof(template_pen_some_params), 131 + .param_list = params_pen_some, 132 + .param_num = ARRAY_SIZE(params_pen_some), 133 + .expected = expected_pen_some_params, 134 + }, 135 + { 136 + .name = "No params present in the template", 137 + .template = template_params_none, 138 + .template_size = sizeof(template_params_none), 139 + .param_list = params_pen_some, 140 + .param_num = ARRAY_SIZE(params_pen_some), 141 + .expected = template_params_none, 142 + }, 143 + }; 144 + 145 + static void uclogic_template_case_desc(struct uclogic_template_case *t, 146 + char *desc) 147 + { 148 + strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE); 149 + } 150 + 151 + KUNIT_ARRAY_PARAM(uclogic_template, uclogic_template_cases, 152 + uclogic_template_case_desc); 153 + 154 + static void uclogic_template_test(struct kunit *test) 155 + { 156 + __u8 *res; 157 + const struct uclogic_template_case *params = test->param_value; 158 + 159 + res = uclogic_rdesc_template_apply(params->template, 160 + params->template_size, 161 + params->param_list, 162 + params->param_num); 163 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); 164 + KUNIT_EXPECT_EQ(test, 0, 165 + memcmp(res, params->expected, params->template_size)); 166 + kfree(res); 167 + } 168 + 169 + static struct kunit_case hid_uclogic_rdesc_test_cases[] = { 170 + KUNIT_CASE_PARAM(uclogic_template_test, uclogic_template_gen_params), 171 + {} 172 + }; 173 + 174 + static struct kunit_suite hid_uclogic_rdesc_test_suite = { 175 + .name = "hid-uclogic-rdesc-test", 176 + .test_cases = hid_uclogic_rdesc_test_cases, 177 + }; 178 + 179 + kunit_test_suite(hid_uclogic_rdesc_test_suite); 180 + 181 + MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver"); 182 + MODULE_LICENSE("GPL"); 183 + MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");