| 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 XIQueryVersion request and reply. |
| 33 |
* |
| 34 |
* Test approach: |
| 35 |
* Wrap WriteToClient to intercept the server's reply. |
| 36 |
* Repeatedly test a client/server version combination, compare version in |
| 37 |
* reply with versions given. Version must be equal to either |
| 38 |
* server version or client version, whichever is smaller. |
| 39 |
* Client version less than 2 must return BadValue. |
| 40 |
*/ |
| 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 "extinit.h" /* for XInputExtensionInit */ |
| 48 |
#include "scrnintstr.h" |
| 49 |
#include "xiqueryversion.h" |
| 50 |
#include "protocol-common.h" |
| 51 |
#include "exglobals.h" |
| 52 |
|
| 53 |
DECLARE_WRAP_FUNCTION(WriteToClient, void, ClientPtr client, int len, void *data); |
| 54 |
|
| 55 |
extern XExtensionVersion XIVersion; |
| 56 |
|
| 57 |
static struct test_data { |
| 58 |
int major_client; |
| 59 |
int minor_client; |
| 60 |
int major_server; |
| 61 |
int minor_server; |
| 62 |
int major_expected; |
| 63 |
int minor_expected; |
| 64 |
} versions; |
| 65 |
|
| 66 |
|
| 67 |
extern ClientRec client_window; |
| 68 |
|
| 69 |
static void |
| 70 |
reply_XIQueryVersion(ClientPtr client, int len, void *data) |
| 71 |
{ |
| 72 |
xXIQueryVersionReply *reply = (xXIQueryVersionReply *) data; |
| 73 |
xXIQueryVersionReply rep = *reply; /* copy so swapping doesn't touch the real reply */ |
| 74 |
|
| 75 |
unsigned int sver, cver, ver; |
| 76 |
|
| 77 |
assert(len < 0xffff); /* suspicious size, swapping bug */ |
| 78 |
|
| 79 |
if (client->swapped) { |
| 80 |
swapl(&rep.length); |
| 81 |
swaps(&rep.sequenceNumber); |
| 82 |
swaps(&rep.major_version); |
| 83 |
swaps(&rep.minor_version); |
| 84 |
} |
| 85 |
|
| 86 |
reply_check_defaults(&rep, len, XIQueryVersion); |
| 87 |
|
| 88 |
assert(rep.length == 0); |
| 89 |
|
| 90 |
sver = versions.major_server * 1000 + versions.minor_server; |
| 91 |
cver = versions.major_client * 1000 + versions.minor_client; |
| 92 |
ver = rep.major_version * 1000 + rep.minor_version; |
| 93 |
|
| 94 |
assert(ver >= 2000); |
| 95 |
assert((sver > cver) ? ver == cver : ver == sver); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
reply_XIQueryVersion_multiple(ClientPtr client, int len, void *data) |
| 100 |
{ |
| 101 |
xXIQueryVersionReply *reply = (xXIQueryVersionReply *) data; |
| 102 |
xXIQueryVersionReply rep = *reply; /* copy so swapping doesn't touch the real reply */ |
| 103 |
|
| 104 |
reply_check_defaults(&rep, len, XIQueryVersion); |
| 105 |
assert(rep.length == 0); |
| 106 |
|
| 107 |
assert(versions.major_expected == rep.major_version); |
| 108 |
assert(versions.minor_expected == rep.minor_version); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Run a single test with server version smaj.smin and client |
| 113 |
* version cmaj.cmin. Verify that return code is equal to 'error'. |
| 114 |
* |
| 115 |
* Test is run normal, then for a swapped client. |
| 116 |
*/ |
| 117 |
static void |
| 118 |
request_XIQueryVersion(int smaj, int smin, int cmaj, int cmin, int error) |
| 119 |
{ |
| 120 |
int rc; |
| 121 |
xXIQueryVersionReq request; |
| 122 |
ClientRec client; |
| 123 |
|
| 124 |
request_init(&request, XIQueryVersion); |
| 125 |
client = init_client(request.length, &request); |
| 126 |
|
| 127 |
/* Change the server to support smaj.smin */ |
| 128 |
XIVersion.major_version = smaj; |
| 129 |
XIVersion.minor_version = smin; |
| 130 |
|
| 131 |
/* remember versions we send and expect */ |
| 132 |
versions.major_client = cmaj; |
| 133 |
versions.minor_client = cmin; |
| 134 |
versions.major_server = XIVersion.major_version; |
| 135 |
versions.minor_server = XIVersion.minor_version; |
| 136 |
|
| 137 |
request.major_version = versions.major_client; |
| 138 |
request.minor_version = versions.minor_client; |
| 139 |
rc = ProcXIQueryVersion(&client); |
| 140 |
assert(rc == error); |
| 141 |
|
| 142 |
client = init_client(request.length, &request); |
| 143 |
client.swapped = TRUE; |
| 144 |
|
| 145 |
swaps(&request.length); |
| 146 |
swaps(&request.major_version); |
| 147 |
swaps(&request.minor_version); |
| 148 |
|
| 149 |
rc = SProcXIQueryVersion(&client); |
| 150 |
assert(rc == error); |
| 151 |
} |
| 152 |
|
| 153 |
/* Client version less than 2.0 must return BadValue, all other combinations |
| 154 |
* Success */ |
| 155 |
static void |
| 156 |
test_XIQueryVersion(void) |
| 157 |
{ |
| 158 |
init_simple(); |
| 159 |
|
| 160 |
wrapped_WriteToClient = reply_XIQueryVersion; |
| 161 |
|
| 162 |
dbg("Server version 2.0 - client versions [1..3].0\n"); |
| 163 |
/* some simple tests to catch common errors quickly */ |
| 164 |
request_XIQueryVersion(2, 0, 1, 0, BadValue); |
| 165 |
request_XIQueryVersion(2, 0, 2, 0, Success); |
| 166 |
request_XIQueryVersion(2, 0, 3, 0, Success); |
| 167 |
|
| 168 |
dbg("Server version 3.0 - client versions [1..3].0\n"); |
| 169 |
request_XIQueryVersion(3, 0, 1, 0, BadValue); |
| 170 |
request_XIQueryVersion(3, 0, 2, 0, Success); |
| 171 |
request_XIQueryVersion(3, 0, 3, 0, Success); |
| 172 |
|
| 173 |
dbg("Server version 2.0 - client versions [1..3].[1..3]\n"); |
| 174 |
request_XIQueryVersion(2, 0, 1, 1, BadValue); |
| 175 |
request_XIQueryVersion(2, 0, 2, 2, Success); |
| 176 |
request_XIQueryVersion(2, 0, 3, 3, Success); |
| 177 |
|
| 178 |
dbg("Server version 2.2 - client versions [1..3].0\n"); |
| 179 |
request_XIQueryVersion(2, 2, 1, 0, BadValue); |
| 180 |
request_XIQueryVersion(2, 2, 2, 0, Success); |
| 181 |
request_XIQueryVersion(2, 2, 3, 0, Success); |
| 182 |
|
| 183 |
#if 0 |
| 184 |
/* this one takes a while */ |
| 185 |
unsigned int cmin, cmaj, smin, smaj; |
| 186 |
|
| 187 |
dbg("Testing all combinations.\n"); |
| 188 |
for (smaj = 2; smaj <= 0xFFFF; smaj++) |
| 189 |
for (smin = 0; smin <= 0xFFFF; smin++) |
| 190 |
for (cmin = 0; cmin <= 0xFFFF; cmin++) |
| 191 |
for (cmaj = 0; cmaj <= 0xFFFF; cmaj++) { |
| 192 |
int error = (cmaj < 2) ? BadValue : Success; |
| 193 |
|
| 194 |
request_XIQueryVersion(smaj, smin, cmaj, cmin, error); |
| 195 |
} |
| 196 |
|
| 197 |
#endif |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
static void |
| 202 |
test_XIQueryVersion_multiple(void) |
| 203 |
{ |
| 204 |
xXIQueryVersionReq request; |
| 205 |
ClientRec client; |
| 206 |
XIClientPtr pXIClient; |
| 207 |
int rc; |
| 208 |
|
| 209 |
init_simple(); |
| 210 |
|
| 211 |
request_init(&request, XIQueryVersion); |
| 212 |
client = init_client(request.length, &request); |
| 213 |
|
| 214 |
/* Change the server to support 2.2 */ |
| 215 |
XIVersion.major_version = 2; |
| 216 |
XIVersion.minor_version = 2; |
| 217 |
|
| 218 |
wrapped_WriteToClient = reply_XIQueryVersion_multiple; |
| 219 |
|
| 220 |
/* run 1 */ |
| 221 |
|
| 222 |
/* client is lower than server, nonexpected */ |
| 223 |
versions.major_expected = request.major_version = 2; |
| 224 |
versions.minor_expected = request.minor_version = 1; |
| 225 |
rc = ProcXIQueryVersion(&client); |
| 226 |
assert(rc == Success); |
| 227 |
|
| 228 |
/* client is higher than server, no change */ |
| 229 |
request.major_version = 2; |
| 230 |
request.minor_version = 3; |
| 231 |
rc = ProcXIQueryVersion(&client); |
| 232 |
assert(rc == Success); |
| 233 |
|
| 234 |
/* client tries to set higher version, stays same */ |
| 235 |
request.major_version = 2; |
| 236 |
request.minor_version = 2; |
| 237 |
rc = ProcXIQueryVersion(&client); |
| 238 |
assert(rc == Success); |
| 239 |
|
| 240 |
/* client tries to set lower version, no change */ |
| 241 |
request.major_version = 2; |
| 242 |
request.minor_version = 0; |
| 243 |
rc = ProcXIQueryVersion(&client); |
| 244 |
assert(rc == BadValue); |
| 245 |
|
| 246 |
/* run 2 */ |
| 247 |
client = init_client(request.length, &request); |
| 248 |
XIVersion.major_version = 2; |
| 249 |
XIVersion.minor_version = 3; |
| 250 |
|
| 251 |
versions.major_expected = request.major_version = 2; |
| 252 |
versions.minor_expected = request.minor_version = 2; |
| 253 |
rc = ProcXIQueryVersion(&client); |
| 254 |
assert(rc == Success); |
| 255 |
|
| 256 |
/* client bumps version from 2.2 to 2.3 */ |
| 257 |
request.major_version = 2; |
| 258 |
versions.minor_expected = request.minor_version = 3; |
| 259 |
rc = ProcXIQueryVersion(&client); |
| 260 |
assert(rc == Success); |
| 261 |
|
| 262 |
/* real version is changed, too! */ |
| 263 |
pXIClient = dixLookupPrivate(&client.devPrivates, XIClientPrivateKey); |
| 264 |
assert(pXIClient->minor_version == 3); |
| 265 |
|
| 266 |
/* client tries to set lower version, no change */ |
| 267 |
request.major_version = 2; |
| 268 |
request.minor_version = 1; |
| 269 |
rc = ProcXIQueryVersion(&client); |
| 270 |
assert(rc == BadValue); |
| 271 |
|
| 272 |
/* run 3 */ |
| 273 |
client = init_client(request.length, &request); |
| 274 |
XIVersion.major_version = 2; |
| 275 |
XIVersion.minor_version = 3; |
| 276 |
|
| 277 |
versions.major_expected = request.major_version = 2; |
| 278 |
versions.minor_expected = request.minor_version = 3; |
| 279 |
rc = ProcXIQueryVersion(&client); |
| 280 |
assert(rc == Success); |
| 281 |
|
| 282 |
request.major_version = 2; |
| 283 |
versions.minor_expected = request.minor_version = 2; |
| 284 |
rc = ProcXIQueryVersion(&client); |
| 285 |
assert(rc == Success); |
| 286 |
|
| 287 |
/* but real client version must not be lowered */ |
| 288 |
pXIClient = dixLookupPrivate(&client.devPrivates, XIClientPrivateKey); |
| 289 |
assert(pXIClient->minor_version == 3); |
| 290 |
|
| 291 |
request.major_version = 2; |
| 292 |
request.minor_version = 1; |
| 293 |
rc = ProcXIQueryVersion(&client); |
| 294 |
assert(rc == BadValue); |
| 295 |
} |
| 296 |
|
| 297 |
const testfunc_t* |
| 298 |
protocol_xiqueryversion_test(void) |
| 299 |
{ |
| 300 |
static const testfunc_t testfuncs[] = { |
| 301 |
test_XIQueryVersion, |
| 302 |
test_XIQueryVersion_multiple, |
| 303 |
NULL, |
| 304 |
}; |
| 305 |
|
| 306 |
return testfuncs; |
| 307 |
} |