summaryrefslogtreecommitdiffstats
path: root/drivers/input/tests/input_test.c
blob: 2fa5b725ae0a2a86cc8763e2d1d96b37c7f2a7af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// SPDX-License-Identifier: GPL-2.0
/*
 * KUnit test for the input core.
 *
 * Copyright (c) 2023 Red Hat Inc
 */

#include <linux/delay.h>
#include <linux/input.h>

#include <kunit/test.h>

#define POLL_INTERVAL 100

static int input_test_init(struct kunit *test)
{
	struct input_dev *input_dev;
	int ret;

	input_dev = input_allocate_device();
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, input_dev);

	input_dev->name = "Test input device";
	input_dev->id.bustype = BUS_VIRTUAL;
	input_dev->id.vendor = 1;
	input_dev->id.product = 1;
	input_dev->id.version = 1;
	input_set_capability(input_dev, EV_KEY, BTN_LEFT);
	input_set_capability(input_dev, EV_KEY, BTN_RIGHT);

	ret = input_register_device(input_dev);
	if (ret) {
		input_free_device(input_dev);
		KUNIT_ASSERT_FAILURE(test, "Register device failed: %d", ret);
	}

	test->priv = input_dev;

	return 0;
}

static void input_test_exit(struct kunit *test)
{
	struct input_dev *input_dev = test->priv;

	if (input_dev)
		input_unregister_device(input_dev);
}

static void input_test_poll(struct input_dev *input) { }

static void input_test_polling(struct kunit *test)
{
	struct input_dev *input_dev = test->priv;

	/* Must fail because a poll handler has not been set-up yet */
	KUNIT_ASSERT_EQ(test, input_get_poll_interval(input_dev), -EINVAL);

	KUNIT_ASSERT_EQ(test, input_setup_polling(input_dev, input_test_poll), 0);

	input_set_poll_interval(input_dev, POLL_INTERVAL);

	/* Must succeed because poll handler was set-up and poll interval set */
	KUNIT_ASSERT_EQ(test, input_get_poll_interval(input_dev), POLL_INTERVAL);
}

static void input_test_timestamp(struct kunit *test)
{
	const ktime_t invalid_timestamp = ktime_set(0, 0);
	struct input_dev *input_dev = test->priv;
	ktime_t *timestamp, time;

	timestamp = input_get_timestamp(input_dev);
	time = timestamp[INPUT_CLK_MONO];

	/* The returned timestamp must always be valid */
	KUNIT_ASSERT_EQ(test, ktime_compare(time, invalid_timestamp), 1);

	time = ktime_get();
	input_set_timestamp(input_dev, time);

	timestamp = input_get_timestamp(input_dev);
	/* The timestamp must be the same than set before */
	KUNIT_ASSERT_EQ(test, ktime_compare(timestamp[INPUT_CLK_MONO], time), 0);
}

static void input_test_match_device_id(struct kunit *test)
{
	struct input_dev *input_dev = test->priv;
	struct input_device_id id = { 0 };

	/*
	 * Must match when the input device bus, vendor, product, version
	 * and events capable of handling are the same and fail to match
	 * otherwise.
	 */
	id.flags = INPUT_DEVICE_ID_MATCH_BUS;
	id.bustype = BUS_VIRTUAL;
	KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));

	id.bustype = BUS_I2C;
	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));

	id.flags = INPUT_DEVICE_ID_MATCH_VENDOR;
	id.vendor = 1;
	KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));

	id.vendor = 2;
	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));

	id.flags = INPUT_DEVICE_ID_MATCH_PRODUCT;
	id.product = 1;
	KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));

	id.product = 2;
	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));

	id.flags = INPUT_DEVICE_ID_MATCH_VERSION;
	id.version = 1;
	KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));

	id.version = 2;
	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));

	id.flags = INPUT_DEVICE_ID_MATCH_EVBIT;
	__set_bit(EV_KEY, id.evbit);
	KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));

	__set_bit(EV_ABS, id.evbit);
	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
}

static void input_test_grab(struct kunit *test)
{
	struct input_dev *input_dev = test->priv;
	struct input_handle test_handle;
	struct input_handler handler;
	struct input_handle handle;
	struct input_device_id id;
	int res;

	handler.name = "handler";
	handler.id_table = &id;

	handle.dev = input_get_device(input_dev);
	handle.name = dev_name(&input_dev->dev);
	handle.handler = &handler;
	res = input_grab_device(&handle);
	KUNIT_ASSERT_TRUE(test, res == 0);

	test_handle.dev = input_get_device(input_dev);
	test_handle.name = dev_name(&input_dev->dev);
	test_handle.handler = &handler;
	res = input_grab_device(&test_handle);
	KUNIT_ASSERT_EQ(test, res, -EBUSY);

	input_release_device(&handle);
	input_put_device(input_dev);
	res = input_grab_device(&test_handle);
	KUNIT_ASSERT_TRUE(test, res == 0);
	input_put_device(input_dev);
}

static struct kunit_case input_tests[] = {
	KUNIT_CASE(input_test_polling),
	KUNIT_CASE(input_test_timestamp),
	KUNIT_CASE(input_test_match_device_id),
	KUNIT_CASE(input_test_grab),
	{ /* sentinel */ }
};

static struct kunit_suite input_test_suite = {
	.name = "input_core",
	.init = input_test_init,
	.exit = input_test_exit,
	.test_cases = input_tests,
};

kunit_test_suite(input_test_suite);

MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>");
MODULE_LICENSE("GPL");