Debian Codegraph @

test/xi2/protocol-xiselectevents.c

raw
1 /**
2 * Copyright © 2009 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /* Test relies on assert() */
25 #undef NDEBUG
26
27 #ifdef HAVE_DIX_CONFIG_H
28 #include <dix-config.h>
29 #endif
30
31 /*
32 * Protocol testing for XISelectEvents request.
33 *
34 * Test approach:
35 *
36 * Wrap XISetEventMask to intercept when the server tries to apply the event
37 * mask. Ensure that the mask passed in is equivalent to the one supplied by
38 * the client. Ensure that invalid devices and invalid masks return errors
39 * as appropriate.
40 *
41 * Tests included:
42 * BadValue for num_masks < 0
43 * BadWindow for invalid windows
44 * BadDevice for non-existing devices
45 * BadImplemenation for devices >= 0xFF
46 * BadValue if HierarchyChanged bit is set for devices other than
47 * XIAllDevices
48 * BadValue for invalid mask bits
49 * Sucecss for excessive mask lengths
50 *
51 */
52
53 #include <stdint.h>
54 #include <X11/X.h>
55 #include <X11/Xproto.h>
56 #include <X11/extensions/XI2proto.h>
57 #include "inputstr.h"
58 #include "windowstr.h"
59 #include "extinit.h" /* for XInputExtensionInit */
60 #include "scrnintstr.h"
61 #include "exglobals.h"
62 #include "xiselectev.h"
63
64 #include "protocol-common.h"
65
66 DECLARE_WRAP_FUNCTION(XISetEventMask, int, DeviceIntPtr dev,
67 WindowPtr win, ClientPtr client,
68 int len, unsigned char *mask);
69
70
71 static unsigned char *data[4096 * 20]; /* the request data buffer */
72
73 extern ClientRec client_window;
74
75 static int
76 override_XISetEventMask(DeviceIntPtr dev, WindowPtr win, ClientPtr client,
77 int len, unsigned char *mask)
78 {
79 return Success;
80 }
81
82 static void
83 request_XISelectEvent(xXISelectEventsReq * req, int error)
84 {
85 int i;
86 int rc;
87 ClientRec client;
88 xXIEventMask *mask, *next;
89
90 req->length = (sz_xXISelectEventsReq / 4);
91 mask = (xXIEventMask *) &req[1];
92 for (i = 0; i < req->num_masks; i++) {
93 req->length += sizeof(xXIEventMask) / 4 + mask->mask_len;
94 mask = (xXIEventMask *) ((char *) &mask[1] + mask->mask_len * 4);
95 }
96
97 client = init_client(req->length, req);
98
99 rc = ProcXISelectEvents(&client);
100 assert(rc == error);
101
102 client.swapped = TRUE;
103
104 mask = (xXIEventMask *) &req[1];
105 for (i = 0; i < req->num_masks; i++) {
106 next = (xXIEventMask *) ((char *) &mask[1] + mask->mask_len * 4);
107 swaps(&mask->deviceid);
108 swaps(&mask->mask_len);
109 mask = next;
110 }
111
112 swapl(&req->win);
113 swaps(&req->length);
114 swaps(&req->num_masks);
115 rc = SProcXISelectEvents(&client);
116 assert(rc == error);
117 }
118
119 static void
120 _set_bit(unsigned char *bits, int bit)
121 {
122 SetBit(bits, bit);
123 if (bit >= XI_TouchBegin && bit <= XI_TouchOwnership) {
124 SetBit(bits, XI_TouchBegin);
125 SetBit(bits, XI_TouchUpdate);
126 SetBit(bits, XI_TouchEnd);
127 }
128 if (bit >= XI_GesturePinchBegin && bit <= XI_GesturePinchEnd) {
129 SetBit(bits, XI_GesturePinchBegin);
130 SetBit(bits, XI_GesturePinchUpdate);
131 SetBit(bits, XI_GesturePinchEnd);
132 }
133 if (bit >= XI_GestureSwipeBegin && bit <= XI_GestureSwipeEnd) {
134 SetBit(bits, XI_GestureSwipeBegin);
135 SetBit(bits, XI_GestureSwipeUpdate);
136 SetBit(bits, XI_GestureSwipeEnd);
137 }
138 }
139
140 static void
141 _clear_bit(unsigned char *bits, int bit)
142 {
143 ClearBit(bits, bit);
144 if (bit >= XI_TouchBegin && bit <= XI_TouchOwnership) {
145 ClearBit(bits, XI_TouchBegin);
146 ClearBit(bits, XI_TouchUpdate);
147 ClearBit(bits, XI_TouchEnd);
148 }
149 if (bit >= XI_GesturePinchBegin && bit <= XI_GesturePinchEnd) {
150 ClearBit(bits, XI_GesturePinchBegin);
151 ClearBit(bits, XI_GesturePinchUpdate);
152 ClearBit(bits, XI_GesturePinchEnd);
153 }
154 if (bit >= XI_GestureSwipeBegin && bit <= XI_GestureSwipeEnd) {
155 ClearBit(bits, XI_GestureSwipeBegin);
156 ClearBit(bits, XI_GestureSwipeUpdate);
157 ClearBit(bits, XI_GestureSwipeEnd);
158 }
159 }
160
161 static void
162 request_XISelectEvents_masks(xXISelectEventsReq * req)
163 {
164 int i, j;
165 xXIEventMask *mask;
166 int nmasks = XI2MASKSIZE;
167 unsigned char *bits;
168
169 mask = (xXIEventMask *) &req[1];
170 req->win = ROOT_WINDOW_ID;
171
172 /* if a clients submits more than 100 masks, consider it insane and untested */
173 for (i = 1; i <= 1000; i += 33) {
174 req->num_masks = i;
175 mask->deviceid = XIAllDevices;
176
177 /* Test 0:
178 * mask_len is 0 -> Success
179 */
180 mask->mask_len = 0;
181 request_XISelectEvent(req, Success);
182
183 /* Test 1:
184 * mask may be larger than needed for XI2LASTEVENT.
185 * Test setting each valid mask bit, while leaving unneeded bits 0.
186 * -> Success
187 */
188 bits = (unsigned char *) &mask[1];
189 mask->mask_len = (nmasks + 3) / 4 * 10;
190 memset(bits, 0, mask->mask_len * 4);
191 for (j = 0; j <= XI2LASTEVENT; j++) {
192 _set_bit(bits, j);
193 request_XISelectEvent(req, Success);
194 _clear_bit(bits, j);
195 }
196
197 /* Test 2:
198 * mask may be larger than needed for XI2LASTEVENT.
199 * Test setting all valid mask bits, while leaving unneeded bits 0.
200 * -> Success
201 */
202 bits = (unsigned char *) &mask[1];
203 mask->mask_len = (nmasks + 3) / 4 * 10;
204 memset(bits, 0, mask->mask_len * 4);
205
206 for (j = 0; j <= XI2LASTEVENT; j++) {
207 _set_bit(bits, j);
208 request_XISelectEvent(req, Success);
209 }
210
211 /* Test 3:
212 * mask is larger than needed for XI2LASTEVENT. If any unneeded bit
213 * is set -> BadValue
214 */
215 bits = (unsigned char *) &mask[1];
216 mask->mask_len = (nmasks + 3) / 4 * 10;
217 memset(bits, 0, mask->mask_len * 4);
218
219 for (j = XI2LASTEVENT + 1; j < mask->mask_len * 4; j++) {
220 _set_bit(bits, j);
221 request_XISelectEvent(req, BadValue);
222 _clear_bit(bits, j);
223 }
224
225 /* Test 4:
226 * Mask len is a sensible length, only valid bits are set -> Success
227 */
228 bits = (unsigned char *) &mask[1];
229 mask->mask_len = (nmasks + 3) / 4;
230 memset(bits, 0, mask->mask_len * 4);
231 for (j = 0; j <= XI2LASTEVENT; j++) {
232 _set_bit(bits, j);
233 request_XISelectEvent(req, Success);
234 }
235
236 /* Test 5:
237 * Mask len is 1 and XI_GestureSwipeEnd is set outside the mask.
238 * That bit should be ignored -> Success
239 */
240 bits = (unsigned char *) &mask[1];
241 mask->mask_len = 1;
242 memset(bits, 0, 5);
243 SetBit(bits, XI_ButtonPress); // does not matter which one
244 SetBit(bits, XI_GestureSwipeEnd);
245 request_XISelectEvent(req, Success);
246
247 /* Test 6:
248 * HierarchyChanged bit is BadValue for devices other than
249 * XIAllDevices
250 */
251 bits = (unsigned char *) &mask[1];
252 mask->mask_len = (nmasks + 3) / 4;
253 memset(bits, 0, mask->mask_len * 4);
254 SetBit(bits, XI_HierarchyChanged);
255 mask->deviceid = XIAllDevices;
256 request_XISelectEvent(req, Success);
257 for (j = 1; j < devices.num_devices; j++) {
258 mask->deviceid = j;
259 request_XISelectEvent(req, BadValue);
260 }
261
262 /* Test 7:
263 * All bits set minus hierarchy changed bit -> Success
264 */
265 bits = (unsigned char *) &mask[1];
266 mask->mask_len = (nmasks + 3) / 4;
267 memset(bits, 0, mask->mask_len * 4);
268 for (j = 0; j <= XI2LASTEVENT; j++)
269 _set_bit(bits, j);
270 _clear_bit(bits, XI_HierarchyChanged);
271 for (j = 1; j < 6; j++) {
272 mask->deviceid = j;
273 request_XISelectEvent(req, Success);
274 }
275
276 mask =
277 (xXIEventMask *) ((char *) mask + sizeof(xXIEventMask) +
278 mask->mask_len * 4);
279 }
280 }
281
282 static void
283 test_XISelectEvents(void)
284 {
285 int i;
286 xXIEventMask *mask;
287 xXISelectEventsReq *req;
288
289 wrapped_XISetEventMask = override_XISetEventMask;
290
291 init_simple();
292
293 req = (xXISelectEventsReq *) data;
294
295 request_init(req, XISelectEvents);
296
297 dbg("Testing for BadValue on zero-length masks\n");
298 /* zero masks are BadValue, regardless of the window */
299 req->num_masks = 0;
300
301 req->win = None;
302 request_XISelectEvent(req, BadValue);
303
304 req->win = ROOT_WINDOW_ID;
305 request_XISelectEvent(req, BadValue);
306
307 req->win = CLIENT_WINDOW_ID;
308 request_XISelectEvent(req, BadValue);
309
310 dbg("Testing for BadWindow.\n");
311 /* None window is BadWindow, regardless of the masks.
312 * We don't actually need to set the masks here, BadWindow must occur
313 * before checking the masks.
314 */
315 req->win = None;
316 req->num_masks = 1;
317 request_XISelectEvent(req, BadWindow);
318
319 req->num_masks = 2;
320 request_XISelectEvent(req, BadWindow);
321
322 req->num_masks = 0xFF;
323 request_XISelectEvent(req, BadWindow);
324
325 /* request size is 3, so 0xFFFC is the highest num_mask that doesn't
326 * overflow req->length */
327 req->num_masks = 0xFFFC;
328 request_XISelectEvent(req, BadWindow);
329
330 dbg("Triggering num_masks/length overflow\n");
331 req->win = ROOT_WINDOW_ID;
332 /* Integer overflow - req->length can't hold that much */
333 req->num_masks = 0xFFFF;
334 request_XISelectEvent(req, BadLength);
335
336 req->win = ROOT_WINDOW_ID;
337 req->num_masks = 1;
338
339 dbg("Triggering bogus mask length error\n");
340 mask = (xXIEventMask *) &req[1];
341 mask->deviceid = 0;
342 mask->mask_len = 0xFFFF;
343 request_XISelectEvent(req, BadLength);
344
345 /* testing various device ids */
346 dbg("Testing existing device ids.\n");
347 for (i = 0; i < 6; i++) {
348 mask = (xXIEventMask *) &req[1];
349 mask->deviceid = i;
350 mask->mask_len = 1;
351 req->win = ROOT_WINDOW_ID;
352 req->num_masks = 1;
353 request_XISelectEvent(req, Success);
354 }
355
356 dbg("Testing non-existing device ids.\n");
357 for (i = 6; i <= 0xFFFF; i++) {
358 req->win = ROOT_WINDOW_ID;
359 req->num_masks = 1;
360 mask = (xXIEventMask *) &req[1];
361 mask->deviceid = i;
362 mask->mask_len = 1;
363 request_XISelectEvent(req, BadDevice);
364 }
365
366 request_XISelectEvents_masks(req);
367 }
368
369 const testfunc_t*
370 protocol_xiselectevents_test(void)
371 {
372 static const testfunc_t testfuncs[] = {
373 test_XISelectEvents,
374 NULL,
375 };
376
377 return testfuncs;
378 }