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 XIGetClientPointer request.
30706f2543Smrg */
31706f2543Smrg#include <stdint.h>
32706f2543Smrg#include <X11/X.h>
33706f2543Smrg#include <X11/Xproto.h>
34706f2543Smrg#include <X11/extensions/XI2proto.h>
35706f2543Smrg#include "inputstr.h"
36706f2543Smrg#include "windowstr.h"
37706f2543Smrg#include "scrnintstr.h"
38706f2543Smrg#include "xigetclientpointer.h"
39706f2543Smrg#include "exevents.h"
40706f2543Smrg
41706f2543Smrg#include "protocol-common.h"
42706f2543Smrg#include <glib.h>
43706f2543Smrg
44706f2543Smrgstruct {
45706f2543Smrg    int cp_is_set;
46706f2543Smrg    DeviceIntPtr dev;
47706f2543Smrg    int win;
48706f2543Smrg} test_data;
49706f2543Smrg
50706f2543Smrgstatic ClientRec client_window;
51706f2543Smrgstatic ClientRec client_request;
52706f2543Smrg
53706f2543Smrgint __wrap_dixLookupClient(ClientPtr *pClient, XID rid, ClientPtr client, Mask access)
54706f2543Smrg{
55706f2543Smrg    if (rid == ROOT_WINDOW_ID)
56706f2543Smrg        return BadWindow;
57706f2543Smrg
58706f2543Smrg    if (rid == CLIENT_WINDOW_ID)
59706f2543Smrg    {
60706f2543Smrg        *pClient = &client_window;
61706f2543Smrg        return Success;
62706f2543Smrg    }
63706f2543Smrg
64706f2543Smrg    return __real_dixLookupClient(pClient, rid, client, access);
65706f2543Smrg}
66706f2543Smrg
67706f2543Smrg
68706f2543Smrgstatic void reply_XIGetClientPointer(ClientPtr client, int len, char *data, void *userdata)
69706f2543Smrg{
70706f2543Smrg    xXIGetClientPointerReply *rep = (xXIGetClientPointerReply*)data;
71706f2543Smrg
72706f2543Smrg    if (client->swapped)
73706f2543Smrg    {
74706f2543Smrg        char n;
75706f2543Smrg        swapl(&rep->length, n);
76706f2543Smrg        swaps(&rep->sequenceNumber, n);
77706f2543Smrg        swaps(&rep->deviceid, n);
78706f2543Smrg    }
79706f2543Smrg
80706f2543Smrg    reply_check_defaults(rep, len, XIGetClientPointer);
81706f2543Smrg
82706f2543Smrg    g_assert(rep->set == test_data.cp_is_set);
83706f2543Smrg    if (rep->set)
84706f2543Smrg        g_assert(rep->deviceid == test_data.dev->id);
85706f2543Smrg}
86706f2543Smrg
87706f2543Smrgstatic void request_XIGetClientPointer(ClientPtr client, xXIGetClientPointerReq* req, int error)
88706f2543Smrg{
89706f2543Smrg    char n;
90706f2543Smrg    int rc;
91706f2543Smrg
92706f2543Smrg    test_data.win = req->win;
93706f2543Smrg
94706f2543Smrg    rc = ProcXIGetClientPointer(&client_request);
95706f2543Smrg    g_assert(rc == error);
96706f2543Smrg
97706f2543Smrg    if (rc == BadWindow)
98706f2543Smrg        g_assert(client_request.errorValue == req->win);
99706f2543Smrg
100706f2543Smrg    client_request.swapped = TRUE;
101706f2543Smrg    swapl(&req->win, n);
102706f2543Smrg    swaps(&req->length, n);
103706f2543Smrg    rc = SProcXIGetClientPointer(&client_request);
104706f2543Smrg    g_assert(rc == error);
105706f2543Smrg
106706f2543Smrg    if (rc == BadWindow)
107706f2543Smrg        g_assert(client_request.errorValue == req->win);
108706f2543Smrg
109706f2543Smrg}
110706f2543Smrg
111706f2543Smrgstatic void test_XIGetClientPointer(void)
112706f2543Smrg{
113706f2543Smrg    xXIGetClientPointerReq request;
114706f2543Smrg
115706f2543Smrg    request_init(&request, XIGetClientPointer);
116706f2543Smrg
117706f2543Smrg    request.win = CLIENT_WINDOW_ID;
118706f2543Smrg
119706f2543Smrg
120706f2543Smrg    reply_handler = reply_XIGetClientPointer;
121706f2543Smrg
122706f2543Smrg    client_request = init_client(request.length, &request);
123706f2543Smrg
124706f2543Smrg    g_test_message("Testing invalid window");
125706f2543Smrg    request.win = INVALID_WINDOW_ID;
126706f2543Smrg    request_XIGetClientPointer(&client_request, &request, BadWindow);
127706f2543Smrg
128706f2543Smrg    printf("Testing invalid length\n");
129706f2543Smrg    client_request.req_len -= 4;
130706f2543Smrg    request_XIGetClientPointer(&client_request, &request, BadLength);
131706f2543Smrg    client_request.req_len += 4;
132706f2543Smrg
133706f2543Smrg    test_data.cp_is_set = FALSE;
134706f2543Smrg
135706f2543Smrg    g_test_message("Testing window None, unset ClientPointer.");
136706f2543Smrg    request.win = None;
137706f2543Smrg    request_XIGetClientPointer(&client_request, &request, Success);
138706f2543Smrg
139706f2543Smrg    g_test_message("Testing valid window, unset ClientPointer.");
140706f2543Smrg    request.win = CLIENT_WINDOW_ID;
141706f2543Smrg    request_XIGetClientPointer(&client_request, &request, Success);
142706f2543Smrg
143706f2543Smrg    g_test_message("Testing valid window, set ClientPointer.");
144706f2543Smrg    client_window.clientPtr = devices.vcp;
145706f2543Smrg    test_data.dev = devices.vcp;
146706f2543Smrg    test_data.cp_is_set = TRUE;
147706f2543Smrg    request.win = CLIENT_WINDOW_ID;
148706f2543Smrg    request_XIGetClientPointer(&client_request, &request, Success);
149706f2543Smrg
150706f2543Smrg    client_window.clientPtr = NULL;
151706f2543Smrg
152706f2543Smrg    g_test_message("Testing window None, set ClientPointer.");
153706f2543Smrg    client_request.clientPtr = devices.vcp;
154706f2543Smrg    test_data.dev = devices.vcp;
155706f2543Smrg    test_data.cp_is_set = TRUE;
156706f2543Smrg    request.win = None;
157706f2543Smrg    request_XIGetClientPointer(&client_request, &request, Success);
158706f2543Smrg}
159706f2543Smrg
160706f2543Smrgint main(int argc, char** argv)
161706f2543Smrg{
162706f2543Smrg    g_test_init(&argc, &argv,NULL);
163706f2543Smrg    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
164706f2543Smrg
165706f2543Smrg    init_simple();
166706f2543Smrg    client_window = init_client(0, NULL);
167706f2543Smrg
168706f2543Smrg
169706f2543Smrg    g_test_add_func("/xi2/protocol/XIGetClientPointer", test_XIGetClientPointer);
170706f2543Smrg
171706f2543Smrg    return g_test_run();
172706f2543Smrg}
173