Skip to content

Commit 2d167aa

Browse files
JoseExpositoJiri Kosina
authored andcommitted
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 <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 76e645b commit 2d167aa

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

drivers/hid/.kunitconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_USB=y
3+
CONFIG_USB_HID=y
4+
CONFIG_HID_UCLOGIC=y
5+
CONFIG_HID_KUNIT_TEST=y

drivers/hid/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,22 @@ config HID_MCP2221
13061306
To compile this driver as a module, choose M here: the module
13071307
will be called hid-mcp2221.ko.
13081308

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+
13091325
endmenu
13101326

13111327
endif # HID

drivers/hid/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ obj-$(CONFIG_HID_WIIMOTE) += hid-wiimote.o
144144
obj-$(CONFIG_HID_SENSOR_HUB) += hid-sensor-hub.o
145145
obj-$(CONFIG_HID_SENSOR_CUSTOM_SENSOR) += hid-sensor-custom.o
146146

147+
obj-$(CONFIG_HID_KUNIT_TEST) += hid-uclogic-rdesc.o \
148+
hid-uclogic-rdesc-test.o
149+
147150
obj-$(CONFIG_USB_HID) += usbhid/
148151
obj-$(CONFIG_USB_MOUSE) += usbhid/
149152
obj-$(CONFIG_USB_KBD) += usbhid/

drivers/hid/hid-uclogic-rdesc-test.c

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 <[email protected]>
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 <[email protected]>");

0 commit comments

Comments
 (0)