xref: /linux/drivers/hid/hid-uclogic-core-test.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
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 
59 static void fake_work(struct work_struct *work)
60 {
61 
62 }
63 
64 static void hid_test_uclogic_exec_event_hook_test(struct kunit *test)
65 {
66 	struct uclogic_params p = {0, };
67 	struct uclogic_raw_event_hook *filter;
68 	bool res;
69 	int n;
70 
71 	/* Initialize the list of events to hook */
72 	p.event_hooks = kunit_kzalloc(test, sizeof(*p.event_hooks), GFP_KERNEL);
73 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p.event_hooks);
74 	INIT_LIST_HEAD(&p.event_hooks->list);
75 
76 	for (n = 0; n < ARRAY_SIZE(hook_events); n++) {
77 		filter = kunit_kzalloc(test, sizeof(*filter), GFP_KERNEL);
78 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter);
79 
80 		filter->size = hook_events[n].size;
81 		filter->event = kunit_kzalloc(test, filter->size, GFP_KERNEL);
82 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter->event);
83 		memcpy(filter->event, &hook_events[n].event[0], filter->size);
84 
85 		INIT_WORK(&filter->work, fake_work);
86 
87 		list_add_tail(&filter->list, &p.event_hooks->list);
88 	}
89 
90 	/* Test uclogic_exec_event_hook() */
91 	for (n = 0; n < ARRAY_SIZE(test_events); n++) {
92 		res = uclogic_exec_event_hook(&p, &test_events[n].event[0],
93 					      test_events[n].size);
94 		KUNIT_ASSERT_EQ(test, res, test_events[n].expected);
95 	}
96 }
97 
98 static struct kunit_case hid_uclogic_core_test_cases[] = {
99 	KUNIT_CASE(hid_test_uclogic_exec_event_hook_test),
100 	{}
101 };
102 
103 static struct kunit_suite hid_uclogic_core_test_suite = {
104 	.name = "hid_uclogic_core_test",
105 	.test_cases = hid_uclogic_core_test_cases,
106 };
107 
108 kunit_test_suite(hid_uclogic_core_test_suite);
109 
110 MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver");
111 MODULE_LICENSE("GPL");
112 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");
113