2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 #include <linux/input.h>
30 #include <libevdev/libevdev-uinput.h>
32 #include "test-common.h"
33 #define UINPUT_NODE "/dev/uinput"
35 START_TEST(test_uinput_create_device)
37 struct libevdev *dev, *dev2;
38 struct libevdev_uinput *uidev;
40 unsigned int type, code;
45 ck_assert(dev != NULL);
46 libevdev_set_name(dev, TEST_DEVICE_NAME);
47 libevdev_enable_event_type(dev, EV_SYN);
48 libevdev_enable_event_type(dev, EV_REL);
49 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
50 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
52 rc = libevdev_uinput_create_from_device(dev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev);
53 ck_assert_int_eq(rc, 0);
54 ck_assert(uidev != NULL);
56 uinput_fd = libevdev_uinput_get_fd(uidev);
57 ck_assert_int_gt(uinput_fd, -1);
59 devnode = libevdev_uinput_get_devnode(uidev);
60 ck_assert(devnode != NULL);
62 fd = open(devnode, O_RDONLY);
63 ck_assert_int_gt(fd, -1);
64 rc = libevdev_new_from_fd(fd, &dev2);
65 ck_assert_int_eq(rc, 0);
67 for (type = 0; type < EV_MAX; type++) {
68 int max = libevdev_get_event_type_max(type);
72 for (code = 0; code < max; code++) {
73 ck_assert_int_eq(libevdev_has_event_code(dev, type, code),
74 libevdev_has_event_code(dev2, type, code));
80 libevdev_uinput_destroy(uidev);
83 /* uinput fd is managed, so make sure it did get closed */
84 ck_assert_int_eq(close(uinput_fd), -1);
85 ck_assert_int_eq(errno, EBADF);
90 START_TEST(test_uinput_create_device_invalid)
93 struct libevdev_uinput *uidev = NULL;
97 ck_assert(dev != NULL);
98 libevdev_set_name(dev, TEST_DEVICE_NAME);
99 libevdev_enable_event_type(dev, EV_SYN);
100 libevdev_enable_event_type(dev, EV_REL);
101 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
102 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
104 rc = libevdev_uinput_create_from_device(dev, -1, &uidev);
105 ck_assert_int_eq(rc, -EBADF);
106 ck_assert(uidev == NULL);
112 START_TEST(test_uinput_create_device_from_fd)
114 struct libevdev *dev, *dev2;
115 struct libevdev_uinput *uidev;
117 unsigned int type, code;
121 dev = libevdev_new();
122 ck_assert(dev != NULL);
123 libevdev_set_name(dev, TEST_DEVICE_NAME);
124 libevdev_enable_event_type(dev, EV_SYN);
125 libevdev_enable_event_type(dev, EV_REL);
126 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
127 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
129 fd = open(UINPUT_NODE, O_RDWR);
130 ck_assert_int_gt(fd, -1);
132 rc = libevdev_uinput_create_from_device(dev, fd, &uidev);
133 ck_assert_int_eq(rc, 0);
134 ck_assert(uidev != NULL);
136 ck_assert_int_eq(libevdev_uinput_get_fd(uidev), fd);
138 devnode = libevdev_uinput_get_devnode(uidev);
139 ck_assert(devnode != NULL);
141 fd2 = open(devnode, O_RDONLY);
142 ck_assert_int_gt(fd2, -1);
143 rc = libevdev_new_from_fd(fd2, &dev2);
144 ck_assert_int_eq(rc, 0);
146 for (type = 0; type < EV_MAX; type++) {
147 int max = libevdev_get_event_type_max(type);
151 for (code = 0; code < max; code++) {
152 ck_assert_int_eq(libevdev_has_event_code(dev, type, code),
153 libevdev_has_event_code(dev2, type, code));
159 libevdev_uinput_destroy(uidev);
165 START_TEST(test_uinput_check_syspath_time)
167 struct libevdev *dev;
168 struct libevdev_uinput *uidev, *uidev2;
169 const char *syspath, *syspath2;
173 dev = libevdev_new();
174 ck_assert(dev != NULL);
175 libevdev_set_name(dev, TEST_DEVICE_NAME);
176 libevdev_enable_event_type(dev, EV_SYN);
177 libevdev_enable_event_type(dev, EV_REL);
178 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
179 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
181 fd = open(UINPUT_NODE, O_RDWR);
182 ck_assert_int_gt(fd, -1);
183 fd2 = open(UINPUT_NODE, O_RDWR);
184 ck_assert_int_gt(fd2, -1);
186 rc = libevdev_uinput_create_from_device(dev, fd, &uidev);
187 ck_assert_int_eq(rc, 0);
189 /* sleep for 1.5 seconds. sysfs resolution is 1 second, so
190 creating both devices without delay means
191 libevdev_uinput_get_syspath can't actually differ between
192 them. By waiting, we get different ctime for uidev and uidev2,
193 and exercise that part of the code.
197 /* create a second one to test the syspath time filtering code */
198 rc = libevdev_uinput_create_from_device(dev, fd2, &uidev2);
199 ck_assert_int_eq(rc, 0);
201 syspath = libevdev_uinput_get_syspath(uidev);
202 ck_assert(syspath != NULL);
204 /* get syspath twice returns same pointer */
205 syspath2 = libevdev_uinput_get_syspath(uidev);
206 ck_assert(syspath == syspath2);
208 /* second dev has different syspath */
209 syspath2 = libevdev_uinput_get_syspath(uidev2);
210 ck_assert(strcmp(syspath, syspath2) != 0);
213 libevdev_uinput_destroy(uidev);
214 libevdev_uinput_destroy(uidev2);
221 START_TEST(test_uinput_check_syspath_name)
223 struct libevdev *dev;
224 struct libevdev_uinput *uidev, *uidev2;
225 const char *syspath, *syspath2;
229 dev = libevdev_new();
230 ck_assert(dev != NULL);
231 libevdev_set_name(dev, TEST_DEVICE_NAME);
232 libevdev_enable_event_type(dev, EV_SYN);
233 libevdev_enable_event_type(dev, EV_REL);
234 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
235 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
237 fd = open(UINPUT_NODE, O_RDWR);
238 ck_assert_int_gt(fd, -1);
239 fd2 = open(UINPUT_NODE, O_RDWR);
240 ck_assert_int_gt(fd2, -1);
242 rc = libevdev_uinput_create_from_device(dev, fd, &uidev);
243 ck_assert_int_eq(rc, 0);
245 /* create a second one to stress the syspath filtering code */
246 libevdev_set_name(dev, TEST_DEVICE_NAME " 2");
247 rc = libevdev_uinput_create_from_device(dev, fd2, &uidev2);
248 ck_assert_int_eq(rc, 0);
250 syspath = libevdev_uinput_get_syspath(uidev);
251 ck_assert(syspath != NULL);
253 /* get syspath twice returns same pointer */
254 syspath2 = libevdev_uinput_get_syspath(uidev);
255 ck_assert(syspath == syspath2);
257 /* second dev has different syspath */
258 syspath2 = libevdev_uinput_get_syspath(uidev2);
259 ck_assert(strcmp(syspath, syspath2) != 0);
262 libevdev_uinput_destroy(uidev);
263 libevdev_uinput_destroy(uidev2);
270 START_TEST(test_uinput_events)
272 struct libevdev *dev;
273 struct libevdev_uinput *uidev;
278 const int nevents = 5;
279 struct input_event events[] = { {{0, 0}, EV_REL, REL_X, 1},
280 {{0, 0}, EV_REL, REL_Y, -1},
281 {{0, 0}, EV_SYN, SYN_REPORT, 0},
282 {{0, 0}, EV_KEY, BTN_LEFT, 1},
283 {{0, 0}, EV_SYN, SYN_REPORT, 0}};
284 struct input_event events_read[nevents];
286 dev = libevdev_new();
287 ck_assert(dev != NULL);
288 libevdev_set_name(dev, TEST_DEVICE_NAME);
289 libevdev_enable_event_type(dev, EV_SYN);
290 libevdev_enable_event_type(dev, EV_REL);
291 libevdev_enable_event_type(dev, EV_KEY);
292 libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
293 libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
294 libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT, NULL);
296 fd = open(UINPUT_NODE, O_RDWR);
297 ck_assert_int_gt(fd, -1);
299 rc = libevdev_uinput_create_from_device(dev, fd, &uidev);
300 ck_assert_int_eq(rc, 0);
301 ck_assert(uidev != NULL);
303 devnode = libevdev_uinput_get_devnode(uidev);
304 ck_assert(devnode != NULL);
306 fd2 = open(devnode, O_RDONLY);
308 for (i = 0; i < nevents; i++)
309 libevdev_uinput_write_event(uidev, events[i].type, events[i].code, events[i].value);
311 rc = read(fd2, events_read, sizeof(events_read));
312 ck_assert_int_eq(rc, sizeof(events_read));
314 for (i = 0; i < nevents; i++) {
315 ck_assert_int_eq(events[i].type, events_read[i].type);
316 ck_assert_int_eq(events[i].code, events_read[i].code);
317 ck_assert_int_eq(events[i].value, events_read[i].value);
321 libevdev_uinput_destroy(uidev);
330 Suite *s = suite_create("libevdev uinput device tests");
332 TCase *tc = tcase_create("device creation");
333 tcase_add_test(tc, test_uinput_create_device);
334 tcase_add_test(tc, test_uinput_create_device_invalid);
335 tcase_add_test(tc, test_uinput_create_device_from_fd);
336 tcase_add_test(tc, test_uinput_check_syspath_time);
337 tcase_add_test(tc, test_uinput_check_syspath_name);
338 suite_add_tcase(s, tc);
340 tc = tcase_create("device events");
341 tcase_add_test(tc, test_uinput_events);
342 suite_add_tcase(s, tc);