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