Debian Codegraph @

test/xi2/protocol-xipassivegrabdevice.c

raw
1 /**
2 * Copyright © 2011 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 XIPassiveGrab request.
33 */
34 #include <stdint.h>
35 #include <X11/X.h>
36 #include <X11/Xproto.h>
37 #include <X11/extensions/XI2proto.h>
38 #include "inputstr.h"
39 #include "windowstr.h"
40 #include "scrnintstr.h"
41 #include "xipassivegrab.h"
42 #include "exevents.h"
43 #include "exglobals.h"
44
45 #include "protocol-common.h"
46
47 DECLARE_WRAP_FUNCTION(WriteToClient, void, ClientPtr client, int len, void *data);
48 DECLARE_WRAP_FUNCTION(GrabButton, int,
49 ClientPtr client, DeviceIntPtr dev,
50 DeviceIntPtr modifier_device, int button,
51 GrabParameters *param, enum InputLevel grabtype,
52 GrabMask *mask);
53
54 extern ClientRec client_window;
55 static ClientRec client_request;
56
57 #define N_MODS 7
58 static uint32_t modifiers[N_MODS] = { 1, 2, 3, 4, 5, 6, 7 };
59
60 static struct test_data {
61 int num_modifiers;
62 } testdata;
63
64
65 static void reply_XIPassiveGrabDevice_data(ClientPtr client, int len,
66 void *data);
67
68 static int
69 override_GrabButton(ClientPtr client, DeviceIntPtr dev,
70 DeviceIntPtr modifier_device, int button,
71 GrabParameters *param, enum InputLevel grabtype,
72 GrabMask *mask)
73 {
74 /* Fail every odd modifier */
75 if (param->modifiers % 2)
76 return BadAccess;
77
78 return Success;
79 }
80
81 static void
82 reply_XIPassiveGrabDevice(ClientPtr client, int len, void *data)
83 {
84 xXIPassiveGrabDeviceReply *reply = (xXIPassiveGrabDeviceReply *) data;
85 xXIPassiveGrabDeviceReply rep = *reply; /* copy so swapping doesn't touch the real reply */
86
87 assert(len < 0xffff); /* suspicious size, swapping bug */
88
89 if (client->swapped) {
90 swaps(&rep.sequenceNumber);
91 swapl(&rep.length);
92 swaps(&rep.num_modifiers);
93
94 testdata.num_modifiers = rep.num_modifiers;
95 }
96
97 reply_check_defaults(&rep, len, XIPassiveGrabDevice);
98
99 /* ProcXIPassiveGrabDevice sends the data in two batches, let the second
100 * handler handle the modifier data */
101 if (rep.num_modifiers > 0)
102 wrapped_WriteToClient = reply_XIPassiveGrabDevice_data;
103 }
104
105 static void
106 reply_XIPassiveGrabDevice_data(ClientPtr client, int len, void *data)
107 {
108 int i;
109
110 xXIGrabModifierInfo *mods = (xXIGrabModifierInfo *) data;
111
112 assert(len < 0xffff); /* suspicious size, swapping bug */
113
114 for (i = 0; i < testdata.num_modifiers; i++, mods++) {
115 if (client->swapped)
116 swapl(&mods->modifiers);
117
118 /* 1 - 7 is the range we use for the global modifiers array
119 * above */
120 assert(mods->modifiers > 0);
121 assert(mods->modifiers <= 7);
122 assert(mods->modifiers % 2 == 1); /* because we fail odd ones */
123 assert(mods->status != Success);
124 assert(mods->pad0 == 0);
125 assert(mods->pad1 == 0);
126 }
127
128 wrapped_WriteToClient = reply_XIPassiveGrabDevice;
129 }
130
131 static void
132 request_XIPassiveGrabDevice(ClientPtr client, xXIPassiveGrabDeviceReq * req,
133 int error, int errval)
134 {
135 int rc;
136 int local_modifiers;
137 int mask_len;
138
139 client_request.req_len = req->length;
140 rc = ProcXIPassiveGrabDevice(&client_request);
141 assert(rc == error);
142
143 if (rc != Success)
144 assert(client_request.errorValue == errval);
145
146 client_request.swapped = TRUE;
147 swaps(&req->length);
148 swapl(&req->time);
149 swapl(&req->grab_window);
150 swapl(&req->cursor);
151 swapl(&req->detail);
152 swaps(&req->deviceid);
153 local_modifiers = req->num_modifiers;
154 swaps(&req->num_modifiers);
155 mask_len = req->mask_len;
156 swaps(&req->mask_len);
157
158 while (local_modifiers--) {
159 CARD32 *mod = (CARD32 *) (req + 1) + mask_len + local_modifiers;
160
161 swapl(mod);
162 }
163
164 rc = SProcXIPassiveGrabDevice(&client_request);
165 assert(rc == error);
166
167 if (rc != Success)
168 assert(client_request.errorValue == errval);
169 }
170
171 static unsigned char *data[4096]; /* the request buffer */
172 static void
173 test_XIPassiveGrabDevice(void)
174 {
175 int i;
176 xXIPassiveGrabDeviceReq *request = (xXIPassiveGrabDeviceReq *) data;
177 unsigned char *mask;
178
179 wrapped_GrabButton = override_GrabButton;
180
181 init_simple();
182
183 request_init(request, XIPassiveGrabDevice);
184
185 request->grab_window = CLIENT_WINDOW_ID;
186
187 wrapped_WriteToClient = reply_XIPassiveGrabDevice;
188 client_request = init_client(request->length, request);
189
190 dbg("Testing invalid device\n");
191 request->deviceid = 12;
192 request_XIPassiveGrabDevice(&client_request, request, BadDevice,
193 request->deviceid);
194
195 dbg("Testing invalid length\n");
196 request->length -= 2;
197 request_XIPassiveGrabDevice(&client_request, request, BadLength,
198 client_request.errorValue);
199 /* re-init request since swapped length test leaves some values swapped */
200 request_init(request, XIPassiveGrabDevice);
201 request->grab_window = CLIENT_WINDOW_ID;
202 request->deviceid = XIAllMasterDevices;
203
204 dbg("Testing invalid grab types\n");
205 for (i = XIGrabtypeGestureSwipeBegin + 1; i < 0xFF; i++) {
206 request->grab_type = i;
207 request_XIPassiveGrabDevice(&client_request, request, BadValue,
208 request->grab_type);
209 }
210
211 dbg("Testing invalid grab type + detail combinations\n");
212 request->grab_type = XIGrabtypeEnter;
213 request->detail = 1;
214 request_XIPassiveGrabDevice(&client_request, request, BadValue,
215 request->detail);
216
217 request->grab_type = XIGrabtypeFocusIn;
218 request_XIPassiveGrabDevice(&client_request, request, BadValue,
219 request->detail);
220
221 request->detail = 0;
222
223 dbg("Testing invalid masks\n");
224 mask = (unsigned char *) &request[1];
225
226 request->mask_len = bytes_to_int32(XI2LASTEVENT + 1);
227 request->length += request->mask_len;
228 SetBit(mask, XI2LASTEVENT + 1);
229 request_XIPassiveGrabDevice(&client_request, request, BadValue,
230 XI2LASTEVENT + 1);
231
232 ClearBit(mask, XI2LASTEVENT + 1);
233
234 /* tested all special cases now, test a few valid cases */
235
236 /* no modifiers */
237 request->deviceid = XIAllDevices;
238 request->grab_type = XIGrabtypeButton;
239 request->detail = XIAnyButton;
240 request_XIPassiveGrabDevice(&client_request, request, Success, 0);
241
242 /* Set a few random masks to make sure we handle modifiers correctly */
243 SetBit(mask, XI_ButtonPress);
244 SetBit(mask, XI_KeyPress);
245 SetBit(mask, XI_Enter);
246
247 /* some modifiers */
248 request->num_modifiers = N_MODS;
249 request->length += N_MODS;
250 memcpy((uint32_t *) (request + 1) + request->mask_len, modifiers,
251 sizeof(modifiers));
252 request_XIPassiveGrabDevice(&client_request, request, Success, 0);
253 }
254
255 const testfunc_t*
256 protocol_xipassivegrabdevice_test(void)
257 {
258 static const testfunc_t testfuncs[] = {
259 test_XIPassiveGrabDevice,
260 NULL,
261 };
262
263 return testfuncs;
264 }