| 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 |
#include <stdint.h> |
| 32 |
#include <X11/X.h> |
| 33 |
#include <X11/Xproto.h> |
| 34 |
#include <X11/extensions/XI2proto.h> |
| 35 |
#include <X11/Xatom.h> |
| 36 |
#include "inputstr.h" |
| 37 |
#include "extinit.h" |
| 38 |
#include "exglobals.h" |
| 39 |
#include "scrnintstr.h" |
| 40 |
#include "xkbsrv.h" |
| 41 |
|
| 42 |
#include "xiquerydevice.h" |
| 43 |
|
| 44 |
#include "protocol-common.h" |
| 45 |
|
| 46 |
DECLARE_WRAP_FUNCTION(WriteToClient, void, ClientPtr client, int len, void *data); |
| 47 |
/* |
| 48 |
* Protocol testing for XIQueryDevice request and reply. |
| 49 |
* |
| 50 |
* Test approach: |
| 51 |
* Wrap WriteToClient to intercept server's reply. ProcXIQueryDevice returns |
| 52 |
* data in two batches, once for the request, once for the trailing data |
| 53 |
* with the device information. |
| 54 |
* Repeatedly test with varying deviceids and check against data in reply. |
| 55 |
*/ |
| 56 |
|
| 57 |
static struct test_data { |
| 58 |
int which_device; |
| 59 |
int num_devices_in_reply; |
| 60 |
} test_data; |
| 61 |
|
| 62 |
extern ClientRec client_window; |
| 63 |
|
| 64 |
static void reply_XIQueryDevice_data(ClientPtr client, int len, void *data); |
| 65 |
|
| 66 |
/* reply handling for the first bytes that constitute the reply */ |
| 67 |
static void |
| 68 |
reply_XIQueryDevice(ClientPtr client, int len, void *data) |
| 69 |
{ |
| 70 |
xXIQueryDeviceReply *reply = (xXIQueryDeviceReply *) data; |
| 71 |
xXIQueryDeviceReply rep = *reply; /* copy so swapping doesn't touch the real reply */ |
| 72 |
|
| 73 |
assert(len < 0xffff); /* suspicious size, swapping bug */ |
| 74 |
|
| 75 |
if (client->swapped) { |
| 76 |
swapl(&rep.length); |
| 77 |
swaps(&rep.sequenceNumber); |
| 78 |
swaps(&rep.num_devices); |
| 79 |
} |
| 80 |
|
| 81 |
reply_check_defaults(&rep, len, XIQueryDevice); |
| 82 |
|
| 83 |
if (test_data.which_device == XIAllDevices) |
| 84 |
assert(rep.num_devices == devices.num_devices); |
| 85 |
else if (test_data.which_device == XIAllMasterDevices) |
| 86 |
assert(rep.num_devices == devices.num_master_devices); |
| 87 |
else |
| 88 |
assert(rep.num_devices == 1); |
| 89 |
|
| 90 |
test_data.num_devices_in_reply = rep.num_devices; |
| 91 |
|
| 92 |
wrapped_WriteToClient = reply_XIQueryDevice_data; |
| 93 |
} |
| 94 |
|
| 95 |
/* reply handling for the trailing bytes that constitute the device info */ |
| 96 |
static void |
| 97 |
reply_XIQueryDevice_data(ClientPtr client, int len, void *data) |
| 98 |
{ |
| 99 |
int i, j; |
| 100 |
|
| 101 |
DeviceIntPtr dev; |
| 102 |
xXIDeviceInfo *info = (xXIDeviceInfo *) data; |
| 103 |
xXIAnyInfo *any; |
| 104 |
|
| 105 |
assert(len < 0xffff); /* suspicious size, swapping bug */ |
| 106 |
|
| 107 |
for (i = 0; i < test_data.num_devices_in_reply; i++) { |
| 108 |
if (client->swapped) { |
| 109 |
swaps(&info->deviceid); |
| 110 |
swaps(&info->attachment); |
| 111 |
swaps(&info->use); |
| 112 |
swaps(&info->num_classes); |
| 113 |
swaps(&info->name_len); |
| 114 |
} |
| 115 |
|
| 116 |
if (test_data.which_device > XIAllMasterDevices) |
| 117 |
assert(info->deviceid == test_data.which_device); |
| 118 |
|
| 119 |
assert(info->deviceid >= 2); /* 0 and 1 is reserved */ |
| 120 |
|
| 121 |
switch (info->deviceid) { |
| 122 |
case 2: /* VCP */ |
| 123 |
dev = devices.vcp; |
| 124 |
assert(info->use == XIMasterPointer); |
| 125 |
assert(info->attachment == devices.vck->id); |
| 126 |
assert(info->num_classes == 3); /* 2 axes + button */ |
| 127 |
break; |
| 128 |
case 3: /* VCK */ |
| 129 |
dev = devices.vck; |
| 130 |
assert(info->use == XIMasterKeyboard); |
| 131 |
assert(info->attachment == devices.vcp->id); |
| 132 |
assert(info->num_classes == 1); |
| 133 |
break; |
| 134 |
case 4: /* mouse */ |
| 135 |
dev = devices.mouse; |
| 136 |
assert(info->use == XISlavePointer); |
| 137 |
assert(info->attachment == devices.vcp->id); |
| 138 |
assert(info->num_classes == 7); /* 4 axes + button + 2 scroll */ |
| 139 |
break; |
| 140 |
case 5: /* keyboard */ |
| 141 |
dev = devices.kbd; |
| 142 |
assert(info->use == XISlaveKeyboard); |
| 143 |
assert(info->attachment == devices.vck->id); |
| 144 |
assert(info->num_classes == 1); |
| 145 |
break; |
| 146 |
|
| 147 |
default: |
| 148 |
/* We shouldn't get here */ |
| 149 |
assert(0); |
| 150 |
break; |
| 151 |
} |
| 152 |
assert(info->enabled == dev->enabled); |
| 153 |
assert(info->name_len == strlen(dev->name)); |
| 154 |
assert(strncmp((char *) &info[1], dev->name, info->name_len) == 0); |
| 155 |
|
| 156 |
any = |
| 157 |
(xXIAnyInfo *) ((char *) &info[1] + ((info->name_len + 3) / 4) * 4); |
| 158 |
for (j = 0; j < info->num_classes; j++) { |
| 159 |
if (client->swapped) { |
| 160 |
swaps(&any->type); |
| 161 |
swaps(&any->length); |
| 162 |
swaps(&any->sourceid); |
| 163 |
} |
| 164 |
|
| 165 |
switch (info->deviceid) { |
| 166 |
case 3: /* VCK and kbd have the same properties */ |
| 167 |
case 5: |
| 168 |
{ |
| 169 |
int k; |
| 170 |
xXIKeyInfo *ki = (xXIKeyInfo *) any; |
| 171 |
XkbDescPtr xkb = devices.vck->key->xkbInfo->desc; |
| 172 |
uint32_t *kc; |
| 173 |
|
| 174 |
if (client->swapped) |
| 175 |
swaps(&ki->num_keycodes); |
| 176 |
|
| 177 |
assert(any->type == XIKeyClass); |
| 178 |
assert(ki->num_keycodes == |
| 179 |
(xkb->max_key_code - xkb->min_key_code + 1)); |
| 180 |
assert(any->length == (2 + ki->num_keycodes)); |
| 181 |
|
| 182 |
kc = (uint32_t *) &ki[1]; |
| 183 |
for (k = 0; k < ki->num_keycodes; k++, kc++) { |
| 184 |
if (client->swapped) |
| 185 |
swapl(kc); |
| 186 |
|
| 187 |
assert(*kc >= xkb->min_key_code); |
| 188 |
assert(*kc <= xkb->max_key_code); |
| 189 |
} |
| 190 |
break; |
| 191 |
} |
| 192 |
case 4: |
| 193 |
{ |
| 194 |
assert(any->type == XIButtonClass || |
| 195 |
any->type == XIValuatorClass || |
| 196 |
any->type == XIScrollClass); |
| 197 |
|
| 198 |
if (any->type == XIScrollClass) { |
| 199 |
xXIScrollInfo *si = (xXIScrollInfo *) any; |
| 200 |
|
| 201 |
if (client->swapped) { |
| 202 |
swaps(&si->number); |
| 203 |
swaps(&si->scroll_type); |
| 204 |
swapl(&si->increment.integral); |
| 205 |
swapl(&si->increment.frac); |
| 206 |
} |
| 207 |
assert(si->length == 6); |
| 208 |
assert(si->number == 2 || si->number == 3); |
| 209 |
if (si->number == 2) { |
| 210 |
assert(si->scroll_type == XIScrollTypeVertical); |
| 211 |
assert(!si->flags); |
| 212 |
} |
| 213 |
if (si->number == 3) { |
| 214 |
assert(si->scroll_type == XIScrollTypeHorizontal); |
| 215 |
assert(si->flags & XIScrollFlagPreferred); |
| 216 |
assert(!(si->flags & ~XIScrollFlagPreferred)); |
| 217 |
} |
| 218 |
|
| 219 |
assert(si->increment.integral == si->number); |
| 220 |
/* protocol-common.c sets up increments of 2.4 and 3.5 */ |
| 221 |
assert(si->increment.frac > 0.3 * (1ULL << 32)); |
| 222 |
assert(si->increment.frac < 0.6 * (1ULL << 32)); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
/* fall through */ |
| 227 |
case 2: /* VCP and mouse have the same properties except for scroll */ |
| 228 |
{ |
| 229 |
if (info->deviceid == 2) /* VCP */ |
| 230 |
assert(any->type == XIButtonClass || |
| 231 |
any->type == XIValuatorClass); |
| 232 |
|
| 233 |
if (any->type == XIButtonClass) { |
| 234 |
int l; |
| 235 |
xXIButtonInfo *bi = (xXIButtonInfo *) any; |
| 236 |
|
| 237 |
if (client->swapped) |
| 238 |
swaps(&bi->num_buttons); |
| 239 |
|
| 240 |
assert(bi->num_buttons == devices.vcp->button->numButtons); |
| 241 |
|
| 242 |
l = 2 + bi->num_buttons + |
| 243 |
bytes_to_int32(bits_to_bytes(bi->num_buttons)); |
| 244 |
assert(bi->length == l); |
| 245 |
} |
| 246 |
else if (any->type == XIValuatorClass) { |
| 247 |
xXIValuatorInfo *vi = (xXIValuatorInfo *) any; |
| 248 |
|
| 249 |
if (client->swapped) { |
| 250 |
swaps(&vi->number); |
| 251 |
swapl(&vi->label); |
| 252 |
swapl(&vi->min.integral); |
| 253 |
swapl(&vi->min.frac); |
| 254 |
swapl(&vi->max.integral); |
| 255 |
swapl(&vi->max.frac); |
| 256 |
swapl(&vi->resolution); |
| 257 |
} |
| 258 |
|
| 259 |
assert(vi->length == 11); |
| 260 |
assert(vi->number >= 0); |
| 261 |
assert(vi->number < 4); |
| 262 |
if (info->deviceid == 2) /* VCP */ |
| 263 |
assert(vi->number < 2); |
| 264 |
|
| 265 |
assert(vi->mode == XIModeRelative); |
| 266 |
/* device was set up as relative, so standard |
| 267 |
* values here. */ |
| 268 |
assert(vi->min.integral == -1); |
| 269 |
assert(vi->min.frac == 0); |
| 270 |
assert(vi->max.integral == -1); |
| 271 |
assert(vi->max.frac == 0); |
| 272 |
assert(vi->resolution == 0); |
| 273 |
} |
| 274 |
} |
| 275 |
break; |
| 276 |
} |
| 277 |
any = (xXIAnyInfo *) (((char *) any) + any->length * 4); |
| 278 |
} |
| 279 |
|
| 280 |
info = (xXIDeviceInfo *) any; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
static void |
| 285 |
request_XIQueryDevice(struct test_data *querydata, int deviceid, int error) |
| 286 |
{ |
| 287 |
int rc; |
| 288 |
ClientRec client; |
| 289 |
xXIQueryDeviceReq request; |
| 290 |
|
| 291 |
request_init(&request, XIQueryDevice); |
| 292 |
client = init_client(request.length, &request); |
| 293 |
wrapped_WriteToClient = reply_XIQueryDevice; |
| 294 |
|
| 295 |
querydata->which_device = deviceid; |
| 296 |
|
| 297 |
request.deviceid = deviceid; |
| 298 |
rc = ProcXIQueryDevice(&client); |
| 299 |
assert(rc == error); |
| 300 |
|
| 301 |
if (rc != Success) |
| 302 |
assert(client.errorValue == deviceid); |
| 303 |
|
| 304 |
wrapped_WriteToClient = reply_XIQueryDevice; |
| 305 |
|
| 306 |
client.swapped = TRUE; |
| 307 |
swaps(&request.length); |
| 308 |
swaps(&request.deviceid); |
| 309 |
rc = SProcXIQueryDevice(&client); |
| 310 |
assert(rc == error); |
| 311 |
|
| 312 |
if (rc != Success) |
| 313 |
assert(client.errorValue == deviceid); |
| 314 |
} |
| 315 |
|
| 316 |
static void |
| 317 |
test_XIQueryDevice(void) |
| 318 |
{ |
| 319 |
int i; |
| 320 |
xXIQueryDeviceReq request; |
| 321 |
|
| 322 |
init_simple(); |
| 323 |
|
| 324 |
wrapped_WriteToClient = reply_XIQueryDevice; |
| 325 |
request_init(&request, XIQueryDevice); |
| 326 |
|
| 327 |
dbg("Testing XIAllDevices.\n"); |
| 328 |
request_XIQueryDevice(&test_data, XIAllDevices, Success); |
| 329 |
dbg("Testing XIAllMasterDevices.\n"); |
| 330 |
request_XIQueryDevice(&test_data, XIAllMasterDevices, Success); |
| 331 |
|
| 332 |
dbg("Testing existing device ids.\n"); |
| 333 |
for (i = 2; i < 6; i++) |
| 334 |
request_XIQueryDevice(&test_data, i, Success); |
| 335 |
|
| 336 |
dbg("Testing non-existing device ids.\n"); |
| 337 |
for (i = 6; i <= 0xFFFF; i++) |
| 338 |
request_XIQueryDevice(&test_data, i, BadDevice); |
| 339 |
} |
| 340 |
|
| 341 |
const testfunc_t* |
| 342 |
protocol_xiquerydevice_test(void) |
| 343 |
{ |
| 344 |
static const testfunc_t testfuncs[] = { |
| 345 |
test_XIQueryDevice, |
| 346 |
NULL, |
| 347 |
}; |
| 348 |
|
| 349 |
return testfuncs; |
| 350 |
} |