protocol-xigetclientpointer.c revision 0b0d8713
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 XIGetClientPointer request.
30 */
31#include <stdint.h>
32#include <X11/X.h>
33#include <X11/Xproto.h>
34#include <X11/extensions/XI2proto.h>
35#include "inputstr.h"
36#include "windowstr.h"
37#include "scrnintstr.h"
38#include "xigetclientpointer.h"
39#include "exevents.h"
40
41#include "protocol-common.h"
42#include <glib.h>
43
44struct {
45    int cp_is_set;
46    DeviceIntPtr dev;
47    int win;
48} test_data;
49
50static ClientRec client_window;
51static ClientRec client_request;
52
53int __wrap_dixLookupClient(ClientPtr *pClient, XID rid, ClientPtr client, Mask access)
54{
55    if (rid == ROOT_WINDOW_ID)
56        return BadWindow;
57
58    if (rid == CLIENT_WINDOW_ID)
59    {
60        *pClient = &client_window;
61        return Success;
62    }
63
64    return __real_dixLookupClient(pClient, rid, client, access);
65}
66
67
68static void reply_XIGetClientPointer(ClientPtr client, int len, char *data, void *userdata)
69{
70    xXIGetClientPointerReply *rep = (xXIGetClientPointerReply*)data;
71
72    if (client->swapped)
73    {
74        char n;
75        swapl(&rep->length, n);
76        swaps(&rep->sequenceNumber, n);
77        swaps(&rep->deviceid, n);
78    }
79
80    reply_check_defaults(rep, len, XIGetClientPointer);
81
82    g_assert(rep->set == test_data.cp_is_set);
83    if (rep->set)
84        g_assert(rep->deviceid == test_data.dev->id);
85}
86
87static void request_XIGetClientPointer(ClientPtr client, xXIGetClientPointerReq* req, int error)
88{
89    char n;
90    int rc;
91
92    test_data.win = req->win;
93
94    rc = ProcXIGetClientPointer(&client_request);
95    g_assert(rc == error);
96
97    if (rc == BadWindow)
98        g_assert(client_request.errorValue == req->win);
99
100    client_request.swapped = TRUE;
101    swapl(&req->win, n);
102    swaps(&req->length, n);
103    rc = SProcXIGetClientPointer(&client_request);
104    g_assert(rc == error);
105
106    if (rc == BadWindow)
107        g_assert(client_request.errorValue == req->win);
108
109}
110
111static void test_XIGetClientPointer(void)
112{
113    xXIGetClientPointerReq request;
114
115    request_init(&request, XIGetClientPointer);
116
117    request.win = CLIENT_WINDOW_ID;
118
119
120    reply_handler = reply_XIGetClientPointer;
121
122    client_request = init_client(request.length, &request);
123
124    g_test_message("Testing invalid window");
125    request.win = INVALID_WINDOW_ID;
126    request_XIGetClientPointer(&client_request, &request, BadWindow);
127
128    printf("Testing invalid length\n");
129    client_request.req_len -= 4;
130    request_XIGetClientPointer(&client_request, &request, BadLength);
131    client_request.req_len += 4;
132
133    test_data.cp_is_set = FALSE;
134
135    g_test_message("Testing window None, unset ClientPointer.");
136    request.win = None;
137    request_XIGetClientPointer(&client_request, &request, Success);
138
139    g_test_message("Testing valid window, unset ClientPointer.");
140    request.win = CLIENT_WINDOW_ID;
141    request_XIGetClientPointer(&client_request, &request, Success);
142
143    g_test_message("Testing valid window, set ClientPointer.");
144    client_window.clientPtr = devices.vcp;
145    test_data.dev = devices.vcp;
146    test_data.cp_is_set = TRUE;
147    request.win = CLIENT_WINDOW_ID;
148    request_XIGetClientPointer(&client_request, &request, Success);
149
150    client_window.clientPtr = NULL;
151
152    g_test_message("Testing window None, set ClientPointer.");
153    client_request.clientPtr = devices.vcp;
154    test_data.dev = devices.vcp;
155    test_data.cp_is_set = TRUE;
156    request.win = None;
157    request_XIGetClientPointer(&client_request, &request, Success);
158}
159
160int main(int argc, char** argv)
161{
162    g_test_init(&argc, &argv,NULL);
163    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
164
165    init_simple();
166    client_window = init_client(0, NULL);
167
168
169    g_test_add_func("/xi2/protocol/XIGetClientPointer", test_XIGetClientPointer);
170
171    return g_test_run();
172}
173