tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
183
184
185
186
187
/*
 * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 /*
  *  Create a virtual device (mouse), send events to /dev/uinput
  *  and check that the events are well received in /dev/input/eventX
  */
 
#include <linux/input.h>
 
#include "input_helper.h"
#include "test.h"
#include "safe_macros.h"
#include "lapi/fcntl.h"
 
#define NB_TEST 20
 
static void setup(void);
static void send_events(void);
static int verify_data(struct input_event *iev, int nb);
static int check_events(void);
static void cleanup(void);
 
static int fd;
static int fd2;
 
char *TCID = "input01";
 
int main(int ac, char **av)
{
   int lc;
   int pid;
 
   tst_parse_opts(ac, av, NULL, NULL);
 
   setup();
 
   for (lc = 0; TEST_LOOPING(lc); ++lc) {
       pid = tst_fork();
 
       switch (pid) {
       case 0:
           send_events();
           exit(0);
       case -1:
           tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
       default:
           if (check_events())
               tst_resm(TFAIL, "Wrong data read from eventX");
           else
               tst_resm(TPASS, "Data received from eventX");
       break;
       }
 
       SAFE_WAITPID(NULL, pid, NULL, 0);
   }
 
   cleanup();
   tst_exit();
}
 
static void setup(void)
{
   tst_require_root();
 
   fd = open_uinput();
   setup_mouse_events(fd);
   create_device(fd);
 
   fd2 = open_device();
}
 
static void send_events(void)
{
   int nb;
 
   for (nb = 0; nb < NB_TEST; ++nb) {
       send_rel_move(fd, 10, 1);
       usleep(1000);
   }
}
 
static int check_events(void)
{
   int nb, rd;
   unsigned int i;
   struct input_event iev[64];
 
   nb = 0;
 
   while (nb < NB_TEST * 3) {
       rd = read(fd2, iev, sizeof(iev));
 
       if (rd < 0)
           tst_brkm(TBROK | TERRNO, cleanup, "read()");
 
       if (rd == 0 || rd % sizeof(struct input_event)) {
           tst_resm(TINFO, "read() returned unexpected %i", rd);
           return 1;
       }
 
       for (i = 0; i < rd / sizeof(struct input_event); i++) {
           if (verify_data(&iev[i], nb++))
               return 1;
       }
   }
 
   return 0;
}
 
static int verify_data(struct input_event *iev, int nb)
{
   if (nb % 3 == 0) {
       if (iev->type != EV_REL) {
           tst_resm(TINFO,
                    "%i: Unexpected event type %i expected %i",
                    nb, iev->type, EV_REL);
           return 1;
       }
 
       if (iev->code != REL_X)
           return 1;
 
       if (iev->value != 10)
           return 1;
 
       return 0;
   }
 
   if (nb % 3 == 1) {
       if (iev->type != EV_REL) {
           tst_resm(TINFO,
                    "%i: Unexpected event type %i expected %i",
                    nb, iev->type, EV_REL);
           return 1;
       }
 
       if (iev->code != REL_Y)
           return 1;
 
       if (iev->value != 1)
           return 1;
 
       return 0;
   }
 
   if (nb % 3 == 2) {
       if (iev->type != EV_SYN) {
           tst_resm(TINFO,
                    "%i: Unexpected event type %i expected %i",
                    nb, iev->type, EV_SYN);
           return 1;
       }
 
       if (iev->code != 0)
           return 1;
 
       if (iev->value != 0)
           return 1;
 
       return 0;
   }
   return 1;
}
 
static void cleanup(void)
{
   if (fd2 > 0 && close(fd2))
       tst_resm(TWARN | TERRNO, "close(fd2)");
 
   destroy_device(fd);
}