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