1706f2543Smrg/**
2706f2543Smrg * Copyright © 2009 Red Hat, Inc.
3706f2543Smrg *
4706f2543Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
5706f2543Smrg *  copy of this software and associated documentation files (the "Software"),
6706f2543Smrg *  to deal in the Software without restriction, including without limitation
7706f2543Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
8706f2543Smrg *  and/or sell copies of the Software, and to permit persons to whom the
9706f2543Smrg *  Software is furnished to do so, subject to the following conditions:
10706f2543Smrg *
11706f2543Smrg *  The above copyright notice and this permission notice (including the next
12706f2543Smrg *  paragraph) shall be included in all copies or substantial portions of the
13706f2543Smrg *  Software.
14706f2543Smrg *
15706f2543Smrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16706f2543Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17706f2543Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18706f2543Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19706f2543Smrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20706f2543Smrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21706f2543Smrg *  DEALINGS IN THE SOFTWARE.
22706f2543Smrg */
23706f2543Smrg
24706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
25706f2543Smrg#include <dix-config.h>
26706f2543Smrg#endif
27706f2543Smrg
28706f2543Smrg/*
29706f2543Smrg * Protocol testing for XIQueryVersion request and reply.
30706f2543Smrg *
31706f2543Smrg * Test approach:
32706f2543Smrg * Wrap WriteToClient to intercept the server's reply.
33706f2543Smrg * Repeatedly test a client/server version combination, compare version in
34706f2543Smrg * reply with versions given. Version must be equal to either
35706f2543Smrg * server version or client version, whichever is smaller.
36706f2543Smrg * Client version less than 2 must return BadValue.
37706f2543Smrg */
38706f2543Smrg
39706f2543Smrg#include <stdint.h>
40706f2543Smrg#include <X11/X.h>
41706f2543Smrg#include <X11/Xproto.h>
42706f2543Smrg#include <X11/extensions/XI2proto.h>
43706f2543Smrg#include "inputstr.h"
44706f2543Smrg#include "extinit.h" /* for XInputExtensionInit */
45706f2543Smrg#include "scrnintstr.h"
46706f2543Smrg#include "xiqueryversion.h"
47706f2543Smrg
48706f2543Smrg#include "protocol-common.h"
49706f2543Smrg#include <glib.h>
50706f2543Smrg
51706f2543Smrgextern XExtensionVersion XIVersion;
52706f2543Smrg
53706f2543Smrgstruct test_data {
54706f2543Smrg    int major_client;
55706f2543Smrg    int minor_client;
56706f2543Smrg    int major_server;
57706f2543Smrg    int minor_server;
58706f2543Smrg};
59706f2543Smrg
60706f2543Smrgstatic void reply_XIQueryVersion(ClientPtr client, int len, char* data, void *userdata)
61706f2543Smrg{
62706f2543Smrg    xXIQueryVersionReply *rep = (xXIQueryVersionReply*)data;
63706f2543Smrg    struct test_data *versions = (struct test_data*)userdata;
64706f2543Smrg    unsigned int sver, cver, ver;
65706f2543Smrg
66706f2543Smrg    if (client->swapped)
67706f2543Smrg    {
68706f2543Smrg        char n;
69706f2543Smrg        swapl(&rep->length, n);
70706f2543Smrg        swaps(&rep->sequenceNumber, n);
71706f2543Smrg        swaps(&rep->major_version, n);
72706f2543Smrg        swaps(&rep->minor_version, n);
73706f2543Smrg    }
74706f2543Smrg
75706f2543Smrg    reply_check_defaults(rep, len, XIQueryVersion);
76706f2543Smrg
77706f2543Smrg    g_assert(rep->length == 0);
78706f2543Smrg
79706f2543Smrg    sver = versions->major_server * 1000 + versions->minor_server;
80706f2543Smrg    cver = versions->major_client * 1000 + versions->minor_client;
81706f2543Smrg    ver = rep->major_version * 1000 + rep->minor_version;
82706f2543Smrg
83706f2543Smrg    g_assert(ver >= 2000);
84706f2543Smrg    g_assert((sver > cver) ? ver == cver : ver == sver);
85706f2543Smrg}
86706f2543Smrg
87706f2543Smrg/**
88706f2543Smrg * Run a single test with server version smaj.smin and client
89706f2543Smrg * version cmaj.cmin. Verify that return code is equal to 'error'.
90706f2543Smrg *
91706f2543Smrg * Test is run normal, then for a swapped client.
92706f2543Smrg */
93706f2543Smrgstatic void request_XIQueryVersion(int smaj, int smin, int cmaj, int cmin, int error)
94706f2543Smrg{
95706f2543Smrg    char n;
96706f2543Smrg    int rc;
97706f2543Smrg    struct test_data versions;
98706f2543Smrg    xXIQueryVersionReq request;
99706f2543Smrg    ClientRec client;
100706f2543Smrg
101706f2543Smrg    request_init(&request, XIQueryVersion);
102706f2543Smrg    client = init_client(request.length, &request);
103706f2543Smrg    userdata = (void*)&versions;
104706f2543Smrg
105706f2543Smrg    /* Change the server to support smaj.smin */
106706f2543Smrg    XIVersion.major_version = smaj;
107706f2543Smrg    XIVersion.minor_version = smin;
108706f2543Smrg
109706f2543Smrg    /* remember versions we send and expect */
110706f2543Smrg    versions.major_client = cmaj;
111706f2543Smrg    versions.minor_client = cmin;
112706f2543Smrg    versions.major_server = XIVersion.major_version;
113706f2543Smrg    versions.minor_server = XIVersion.minor_version;
114706f2543Smrg
115706f2543Smrg    request.major_version = versions.major_client;
116706f2543Smrg    request.minor_version = versions.minor_client;
117706f2543Smrg    rc = ProcXIQueryVersion(&client);
118706f2543Smrg    g_assert(rc == error);
119706f2543Smrg
120706f2543Smrg    client.swapped = TRUE;
121706f2543Smrg
122706f2543Smrg    swaps(&request.length, n);
123706f2543Smrg    swaps(&request.major_version, n);
124706f2543Smrg    swaps(&request.minor_version, n);
125706f2543Smrg
126706f2543Smrg    rc = SProcXIQueryVersion(&client);
127706f2543Smrg    g_assert(rc == error);
128706f2543Smrg}
129706f2543Smrg
130706f2543Smrg/* Client version less than 2.0 must return BadValue, all other combinations
131706f2543Smrg * Success */
132706f2543Smrgstatic void test_XIQueryVersion(void)
133706f2543Smrg{
134706f2543Smrg    reply_handler = reply_XIQueryVersion;
135706f2543Smrg
136706f2543Smrg    g_test_message("Server version 2.0 - client versions [1..3].0");
137706f2543Smrg    /* some simple tests to catch common errors quickly */
138706f2543Smrg    request_XIQueryVersion(2, 0, 1, 0, BadValue);
139706f2543Smrg    request_XIQueryVersion(2, 0, 2, 0, Success);
140706f2543Smrg    request_XIQueryVersion(2, 0, 3, 0, Success);
141706f2543Smrg
142706f2543Smrg    g_test_message("Server version 3.0 - client versions [1..3].0");
143706f2543Smrg    request_XIQueryVersion(3, 0, 1, 0, BadValue);
144706f2543Smrg    request_XIQueryVersion(3, 0, 2, 0, Success);
145706f2543Smrg    request_XIQueryVersion(3, 0, 3, 0, Success);
146706f2543Smrg
147706f2543Smrg    g_test_message("Server version 2.0 - client versions [1..3].[1..3]");
148706f2543Smrg    request_XIQueryVersion(2, 0, 1, 1, BadValue);
149706f2543Smrg    request_XIQueryVersion(2, 0, 2, 2, Success);
150706f2543Smrg    request_XIQueryVersion(2, 0, 3, 3, Success);
151706f2543Smrg
152706f2543Smrg    g_test_message("Server version 2.2 - client versions [1..3].0");
153706f2543Smrg    request_XIQueryVersion(2, 2, 1, 0, BadValue);
154706f2543Smrg    request_XIQueryVersion(2, 2, 2, 0, Success);
155706f2543Smrg    request_XIQueryVersion(2, 2, 3, 0, Success);
156706f2543Smrg
157706f2543Smrg#if 0
158706f2543Smrg    /* this one takes a while */
159706f2543Smrg    unsigned int cmin, cmaj, smin, smaj;
160706f2543Smrg
161706f2543Smrg    g_test_message("Testing all combinations.");
162706f2543Smrg    for (smaj = 2; smaj <= 0xFFFF; smaj++)
163706f2543Smrg        for (smin = 0; smin <= 0xFFFF; smin++)
164706f2543Smrg            for (cmin = 0; cmin <= 0xFFFF; cmin++)
165706f2543Smrg                for (cmaj = 0; cmaj <= 0xFFFF; cmaj++)
166706f2543Smrg                {
167706f2543Smrg                    int error = (cmaj < 2) ? BadValue : Success;
168706f2543Smrg                    request_XIQueryVersion(smaj, smin, cmaj, cmin, error);
169706f2543Smrg                }
170706f2543Smrg
171706f2543Smrg#endif
172706f2543Smrg
173706f2543Smrg    reply_handler = NULL;
174706f2543Smrg}
175706f2543Smrg
176706f2543Smrgint main(int argc, char** argv)
177706f2543Smrg{
178706f2543Smrg    g_test_init(&argc, &argv,NULL);
179706f2543Smrg    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
180706f2543Smrg
181706f2543Smrg    init_simple();
182706f2543Smrg
183706f2543Smrg    g_test_add_func("/xi2/protocol/XIQueryVersion", test_XIQueryVersion);
184706f2543Smrg
185706f2543Smrg    return g_test_run();
186706f2543Smrg}
187