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