1 /*
2  * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19  /*
20   *  Create a virtual device (mouse), send events to /dev/uinput
21   *  and Check that events not advertised in the input device bits
22   *  are filtered.
23   */
24 
25 #include <linux/input.h>
26 #include <linux/uinput.h>
27 
28 #include "test.h"
29 #include "safe_macros.h"
30 #include "lapi/fcntl.h"
31 #include "input_helper.h"
32 
33 #define X_VALUE 10
34 #define Y_VALUE 10
35 
36 #define NB_TEST 20
37 
38 static void setup(void);
39 static void send_events(void);
40 static void cleanup(void);
41 
42 static int fd;
43 static int fd2;
44 
45 char *TCID = "input05";
46 
47 int main(int ac, char **av)
48 {
49 	int lc;
50 	int pid;
51 
52 	tst_parse_opts(ac, av, NULL, NULL);
53 
54 	setup();
55 
56 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
57 		pid = tst_fork();
58 
59 		switch (pid) {
60 		case 0:
61 			send_events();
62 			exit(0);
63 		case -1:
64 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
65 		default:
66 			if (no_events_queued(fd2, 1))
67 				tst_resm(TPASS, "No data received in eventX");
68 			else
69 				tst_resm(TFAIL, "Data received in eventX");
70 		break;
71 		}
72 
73 		SAFE_WAITPID(NULL, pid, NULL, 0);
74 	}
75 
76 	cleanup();
77 	tst_exit();
78 }
79 
80 static void setup(void)
81 {
82 	tst_require_root();
83 
84 	fd = open_uinput();
85 
86 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY);
87 	SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_LEFT);
88 
89 	create_device(fd);
90 
91 	fd2 = open_device();
92 }
93 
94 static void send_events(void)
95 {
96 	int nb;
97 
98 	for (nb = 0; nb < NB_TEST; ++nb) {
99 		send_rel_move(fd, X_VALUE, Y_VALUE);
100 		usleep(1000);
101 	}
102 }
103 
104 static void cleanup(void)
105 {
106 	destroy_device(fd);
107 }
108