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