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