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-params.h"
11
12 #define MAX_EVENT_SIZE 12
13
14 struct uclogic_raw_event_hook_test {
15 u8 event[MAX_EVENT_SIZE];
16 size_t size;
17 bool expected;
18 };
19
20 static struct uclogic_raw_event_hook_test hook_events[] = {
21 {
22 .event = { 0xA1, 0xB2, 0xC3, 0xD4 },
23 .size = 4,
24 },
25 {
26 .event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
27 .size = 6,
28 },
29 };
30
31 static struct uclogic_raw_event_hook_test test_events[] = {
32 {
33 .event = { 0xA1, 0xB2, 0xC3, 0xD4 },
34 .size = 4,
35 .expected = true,
36 },
37 {
38 .event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
39 .size = 6,
40 .expected = true,
41 },
42 {
43 .event = { 0xA1, 0xB2, 0xC3 },
44 .size = 3,
45 .expected = false,
46 },
47 {
48 .event = { 0xA1, 0xB2, 0xC3, 0xD4, 0x00 },
49 .size = 5,
50 .expected = false,
51 },
52 {
53 .event = { 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x1F },
54 .size = 6,
55 .expected = false,
56 },
57 };
58
hid_test_uclogic_exec_event_hook_test(struct kunit * test)59 static void hid_test_uclogic_exec_event_hook_test(struct kunit *test)
60 {
61 struct uclogic_params p = {0, };
62 struct uclogic_raw_event_hook *filter;
63 bool res;
64 int n;
65
66 /* Initialize the list of events to hook */
67 p.event_hooks = kunit_kzalloc(test, sizeof(*p.event_hooks), GFP_KERNEL);
68 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p.event_hooks);
69 INIT_LIST_HEAD(&p.event_hooks->list);
70
71 for (n = 0; n < ARRAY_SIZE(hook_events); n++) {
72 filter = kunit_kzalloc(test, sizeof(*filter), GFP_KERNEL);
73 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter);
74
75 filter->size = hook_events[n].size;
76 filter->event = kunit_kzalloc(test, filter->size, GFP_KERNEL);
77 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter->event);
78 memcpy(filter->event, &hook_events[n].event[0], filter->size);
79
80 list_add_tail(&filter->list, &p.event_hooks->list);
81 }
82
83 /* Test uclogic_exec_event_hook() */
84 for (n = 0; n < ARRAY_SIZE(test_events); n++) {
85 res = uclogic_exec_event_hook(&p, &test_events[n].event[0],
86 test_events[n].size);
87 KUNIT_ASSERT_EQ(test, res, test_events[n].expected);
88 }
89 }
90
91 static struct kunit_case hid_uclogic_core_test_cases[] = {
92 KUNIT_CASE(hid_test_uclogic_exec_event_hook_test),
93 {}
94 };
95
96 static struct kunit_suite hid_uclogic_core_test_suite = {
97 .name = "hid_uclogic_core_test",
98 .test_cases = hid_uclogic_core_test_cases,
99 };
100
101 kunit_test_suite(hid_uclogic_core_test_suite);
102
103 MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver");
104 MODULE_LICENSE("GPL");
105 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");
106