Debian Codegraph @

test/xi2/protocol-xigetselectedevents.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 XIGetSelectedEvents request.
33 *
34 * Tests include:
35 * BadWindow on wrong window.
36 * Zero-length masks if no masks are set.
37 * Valid masks for valid devices.
38 * Masks set on non-existent devices are not returned.
39 *
40 * Note that this test is not connected to the XISelectEvents request.
41 */
42 #include <stdint.h>
43 #include <X11/X.h>
44 #include <X11/Xproto.h>
45 #include <X11/extensions/XI2proto.h>
46 #include "inputstr.h"
47 #include "windowstr.h"
48 #include "extinit.h" /* for XInputExtensionInit */
49 #include "scrnintstr.h"
50 #include "xiselectev.h"
51 #include "exevents.h"
52
53 #include "protocol-common.h"
54
55 DECLARE_WRAP_FUNCTION(WriteToClient, void, ClientPtr client, int len, void *data);
56 DECLARE_WRAP_FUNCTION(AddResource, Bool, XID id, RESTYPE type, void *value);
57
58 static void reply_XIGetSelectedEvents(ClientPtr client, int len, void *data);
59 static void reply_XIGetSelectedEvents_data(ClientPtr client, int len, void *data);
60
61 static struct {
62 int num_masks_expected;
63 unsigned char mask[MAXDEVICES][XI2LASTEVENT]; /* intentionally bigger */
64 int mask_len;
65 } test_data;
66
67 extern ClientRec client_window;
68
69 /* AddResource is called from XISetSEventMask, we don't need this */
70 static Bool
71 override_AddResource(XID id, RESTYPE type, void *value)
72 {
73 return TRUE;
74 }
75
76 static void
77 reply_XIGetSelectedEvents(ClientPtr client, int len, void *data)
78 {
79 xXIGetSelectedEventsReply *reply = (xXIGetSelectedEventsReply *) data;
80 xXIGetSelectedEventsReply rep = *reply; /* copy so swapping doesn't touch the real reply */
81
82 assert(len < 0xffff); /* suspicious size, swapping bug */
83
84 if (client->swapped) {
85 swapl(&rep.length);
86 swaps(&rep.sequenceNumber);
87 swaps(&rep.num_masks);
88 }
89
90 reply_check_defaults(&rep, len, XIGetSelectedEvents);
91
92 assert(rep.num_masks == test_data.num_masks_expected);
93
94 wrapped_WriteToClient = reply_XIGetSelectedEvents_data;
95 }
96
97 static void
98 reply_XIGetSelectedEvents_data(ClientPtr client, int len, void *data)
99 {
100 int i;
101 xXIEventMask *mask;
102 unsigned char *bitmask;
103
104 assert(len < 0xffff); /* suspicious size, swapping bug */
105
106 mask = (xXIEventMask *) data;
107 for (i = 0; i < test_data.num_masks_expected; i++) {
108 if (client->swapped) {
109 swaps(&mask->deviceid);
110 swaps(&mask->mask_len);
111 }
112
113 assert(mask->deviceid < 6);
114 assert(mask->mask_len <= (((XI2LASTEVENT + 8) / 8) + 3) / 4);
115
116 bitmask = (unsigned char *) &mask[1];
117 assert(memcmp(bitmask,
118 test_data.mask[mask->deviceid], mask->mask_len * 4) == 0);
119
120 mask =
121 (xXIEventMask *) ((char *) mask + mask->mask_len * 4 +
122 sizeof(xXIEventMask));
123 }
124
125 }
126
127 static void
128 request_XIGetSelectedEvents(xXIGetSelectedEventsReq * req, int error)
129 {
130 int rc;
131 ClientRec client;
132
133 client = init_client(req->length, req);
134
135 wrapped_WriteToClient = reply_XIGetSelectedEvents;
136
137 rc = ProcXIGetSelectedEvents(&client);
138 assert(rc == error);
139
140 wrapped_WriteToClient = reply_XIGetSelectedEvents;
141 client.swapped = TRUE;
142 swapl(&req->win);
143 swaps(&req->length);
144 rc = SProcXIGetSelectedEvents(&client);
145 assert(rc == error);
146 }
147
148 static void
149 test_XIGetSelectedEvents(void)
150 {
151 int i, j;
152 xXIGetSelectedEventsReq request;
153 ClientRec client;
154 unsigned char *mask;
155 DeviceIntRec dev;
156
157 wrapped_AddResource = override_AddResource;
158
159 init_simple();
160 client = init_client(0, NULL);
161
162 request_init(&request, XIGetSelectedEvents);
163
164 dbg("Testing for BadWindow on invalid window.\n");
165 request.win = None;
166 request_XIGetSelectedEvents(&request, BadWindow);
167
168 dbg("Testing for zero-length (unset) masks.\n");
169 /* No masks set yet */
170 test_data.num_masks_expected = 0;
171 request.win = ROOT_WINDOW_ID;
172 request_XIGetSelectedEvents(&request, Success);
173
174 request.win = CLIENT_WINDOW_ID;
175 request_XIGetSelectedEvents(&request, Success);
176
177 memset(test_data.mask, 0, sizeof(test_data.mask));
178
179 dbg("Testing for valid masks\n");
180 memset(&dev, 0, sizeof(dev)); /* dev->id is enough for XISetEventMask */
181 request.win = ROOT_WINDOW_ID;
182
183 /* devices 6 - MAXDEVICES don't exist, they mustn't be included in the
184 * reply even if a mask is set */
185 for (j = 0; j < MAXDEVICES; j++) {
186 test_data.num_masks_expected = min(j + 1, devices.num_devices + 2);
187 dev.id = j;
188 mask = test_data.mask[j];
189 /* bits one-by-one */
190 for (i = 0; i < XI2LASTEVENT; i++) {
191 SetBit(mask, i);
192 XISetEventMask(&dev, &root, &client, (i + 8) / 8, mask);
193 request_XIGetSelectedEvents(&request, Success);
194 ClearBit(mask, i);
195 }
196
197 /* all valid mask bits */
198 for (i = 0; i < XI2LASTEVENT; i++) {
199 SetBit(mask, i);
200 XISetEventMask(&dev, &root, &client, (i + 8) / 8, mask);
201 request_XIGetSelectedEvents(&request, Success);
202 }
203 }
204
205 dbg("Testing removing all masks\n");
206 /* Unset all masks one-by-one */
207 for (j = MAXDEVICES - 1; j >= 0; j--) {
208 if (j < devices.num_devices + 2)
209 test_data.num_masks_expected--;
210
211 mask = test_data.mask[j];
212 memset(mask, 0, XI2LASTEVENT);
213
214 dev.id = j;
215 XISetEventMask(&dev, &root, &client, 0, NULL);
216
217 request_XIGetSelectedEvents(&request, Success);
218 }
219 }
220
221 const testfunc_t*
222 protocol_xigetselectedevents_test(void)
223 {
224 static const testfunc_t testfuncs[] = {
225 test_XIGetSelectedEvents,
226 NULL,
227 };
228 return testfuncs;
229
230 return 0;
231 }