summaryrefslogtreecommitdiff
path: root/test/xi2/protocol-xiselectevents.c
blob: 9782cff5c21df3cbb62ad489db9ebbe61233834a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * Copyright © 2009 Red Hat, Inc.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice (including the next
 *  paragraph) shall be included in all copies or substantial portions of the
 *  Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

/*
 * Protocol testing for XISelectEvents request.
 *
 * Test approach:
 *
 * Wrap XISetEventMask to intercept when the server tries to apply the event
 * mask. Ensure that the mask passed in is equivalent to the one supplied by
 * the client. Ensure that invalid devices and invalid masks return errors
 * as appropriate.
 *
 * Tests included:
 * BadValue for num_masks < 0
 * BadWindow for invalid windows
 * BadDevice for non-existing devices
 * BadImplemenation for devices >= 0xFF
 * BadValue if HierarchyChanged bit is set for devices other than
 *          XIAllDevices
 * BadValue for invalid mask bits
 * Sucecss for excessive mask lengths
 *
 */

#include <stdint.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/extensions/XI2proto.h>
#include "inputstr.h"
#include "windowstr.h"
#include "extinit.h" /* for XInputExtensionInit */
#include "scrnintstr.h"
#include "xiselectev.h"

#include "protocol-common.h"
#include <glib.h>

static unsigned char *data[4096 * 16]; /* the request data buffer */

void __wrap_XISetEventMask(DeviceIntPtr dev, WindowPtr win, int len, unsigned char* mask)
{
}

/* dixLookupWindow requires a lot of setup not necessary for this test.
 * Simple wrapper that returns either one of the fake root window or the
 * fake client window. If the requested ID is neither of those wanted,
 * return whatever the real dixLookupWindow does.
 */
int __wrap_dixLookupWindow(WindowPtr *win, XID id, ClientPtr client, Mask access)
{
    if (id == root.drawable.id)
    {
        *win = &root;
        return Success;
    } else if (id == window.drawable.id)
    {
        *win = &window;
        return Success;
    }

    return __real_dixLookupWindow(win, id, client, access);
}


static void request_XISelectEvent(xXISelectEventsReq *req, int error)
{
    char n;
    int i;
    int rc;
    ClientRec client;
    xXIEventMask *mask, *next;

    req->length = (sz_xXISelectEventsReq/4);
    mask = (xXIEventMask*)&req[1];
    for (i = 0; i < req->num_masks; i++)
    {
        req->length += sizeof(xXIEventMask)/4 + mask->mask_len;
        mask = (xXIEventMask*)((char*)&mask[1] + mask->mask_len * 4);
    }

    client = init_client(req->length, req);

    rc = ProcXISelectEvents(&client);
    g_assert(rc == error);

    client.swapped = TRUE;

    mask = (xXIEventMask*)&req[1];
    for (i = 0; i < req->num_masks; i++)
    {
        next = (xXIEventMask*)((char*)&mask[1] + mask->mask_len * 4);
        swaps(&mask->deviceid, n);
        swaps(&mask->mask_len, n);
        mask = next;
    }

    swapl(&req->win, n);
    swaps(&req->length, n);
    swaps(&req->num_masks, n);
    rc = SProcXISelectEvents(&client);
    g_assert(rc == error);
}

static void request_XISelectEvents_masks(xXISelectEventsReq *req)
{
    int i, j;
    xXIEventMask *mask;
    int nmasks = (XI_LASTEVENT + 7)/8;
    unsigned char *bits;

    mask = (xXIEventMask*)&req[1];
    req->win = ROOT_WINDOW_ID;

    /* if a clients submits more than 100 masks, consider it insane and untested */
    for (i = 1; i <= 1000; i++)
    {
        req->num_masks = i;
        mask->deviceid = XIAllDevices;

        /* Test 0:
         * mask_len is 0 -> Success
         */
        mask->mask_len = 0;
        request_XISelectEvent(req, Success);

        /* Test 1:
         * mask may be larger than needed for XI_LASTEVENT.
         * Test setting each valid mask bit, while leaving unneeded bits 0.
         * -> Success
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4 * 10;
        memset(bits, 0, mask->mask_len * 4);
        for (j = 0; j <= XI_LASTEVENT; j++)
        {
            SetBit(bits, j);
            request_XISelectEvent(req, Success);
            ClearBit(bits, j);
        }

        /* Test 2:
         * mask may be larger than needed for XI_LASTEVENT.
         * Test setting all valid mask bits, while leaving unneeded bits 0.
         * -> Success
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4 * 10;
        memset(bits, 0, mask->mask_len * 4);

        for (j = 0; j <= XI_LASTEVENT; j++)
        {
            SetBit(bits, j);
            request_XISelectEvent(req, Success);
        }

        /* Test 3:
         * mask is larger than needed for XI_LASTEVENT. If any unneeded bit
         * is set -> BadValue
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4 * 10;
        memset(bits, 0, mask->mask_len * 4);

        for (j = XI_LASTEVENT + 1; j < mask->mask_len * 4; j++)
        {
            SetBit(bits, j);
            request_XISelectEvent(req, BadValue);
            ClearBit(bits, j);
        }

        /* Test 4:
         * Mask len is a sensible length, only valid bits are set -> Success
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4;
        memset(bits, 0, mask->mask_len * 4);
        for (j = 0; j <= XI_LASTEVENT; j++)
        {
            SetBit(bits, j);
            request_XISelectEvent(req, Success);
        }

        /* Test 5:
         * HierarchyChanged bit is BadValue for devices other than
         * XIAllDevices
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4;
        memset(bits, 0, mask->mask_len * 4);
        SetBit(bits, XI_HierarchyChanged);
        mask->deviceid = XIAllDevices;
        request_XISelectEvent(req, Success);
        for (j = 1; j < devices.num_devices; j++)
        {
            mask->deviceid = j;
            request_XISelectEvent(req, BadValue);
        }

        /* Test 6:
         * All bits set minus hierarchy changed bit -> Success
         */
        bits = (unsigned char*)&mask[1];
        mask->mask_len = (nmasks + 3)/4;
        memset(bits, 0, mask->mask_len * 4);
        for (j = 0; j <= XI_LASTEVENT; j++)
            SetBit(bits, j);
        ClearBit(bits, XI_HierarchyChanged);
        for (j = 1; j < 6; j++)
        {
            mask->deviceid = j;
            request_XISelectEvent(req, Success);
        }

        mask = (xXIEventMask*)((char*)mask + sizeof(xXIEventMask) + mask->mask_len * 4);
    }
}

static void test_XISelectEvents(void)
{
    int i;
    xXIEventMask *mask;
    xXISelectEventsReq *req;
    req = (xXISelectEventsReq*)data;

    request_init(req, XISelectEvents);

    g_test_message("Testing for BadValue on zero-length masks");
    /* zero masks are BadValue, regardless of the window */
    req->num_masks = 0;

    req->win = None;
    request_XISelectEvent(req, BadValue);

    req->win = ROOT_WINDOW_ID;
    request_XISelectEvent(req, BadValue);

    req->win = CLIENT_WINDOW_ID;
    request_XISelectEvent(req, BadValue);

    g_test_message("Testing for BadWindow.");
    /* None window is BadWindow, regardless of the masks.
     * We don't actually need to set the masks here, BadWindow must occur
     * before checking the masks.
     */
    req->win = None;
    req->num_masks = 1;
    request_XISelectEvent(req, BadWindow);

    req->num_masks = 2;
    request_XISelectEvent(req, BadWindow);

    req->num_masks = 0xFF;
    request_XISelectEvent(req, BadWindow);

    /* request size is 3, so 0xFFFC is the highest num_mask that doesn't
     * overflow req->length */
    req->num_masks = 0xFFFC;
    request_XISelectEvent(req, BadWindow);

    g_test_message("Triggering num_masks/length overflow");
    /* Integer overflow - req->length can't hold that much */
    req->num_masks = 0xFFFF;
    request_XISelectEvent(req, BadLength);

    req->win = ROOT_WINDOW_ID;
    req->num_masks = 1;

    g_test_message("Triggering bogus mask length error");
    mask = (xXIEventMask*)&req[1];
    mask->deviceid = 0;
    mask->mask_len = 0xFFFF;
    request_XISelectEvent(req, BadLength);

    /* testing various device ids */
    g_test_message("Testing existing device ids.");
    for (i = 0; i < 6; i++)
    {
        mask = (xXIEventMask*)&req[1];
        mask->deviceid = i;
        mask->mask_len = 1;
        req->win = ROOT_WINDOW_ID;
        req->num_masks = 1;
        request_XISelectEvent(req, Success);
    }

    g_test_message("Testing non-existing device ids.");
    for (i = 6; i <= 0xFFFF; i++)
    {
        req->win = ROOT_WINDOW_ID;
        req->num_masks = 1;
        mask = (xXIEventMask*)&req[1];
        mask->deviceid = i;
        mask->mask_len = 1;
        request_XISelectEvent(req, BadDevice);
    }

    request_XISelectEvents_masks(req);
}

int main(int argc, char** argv)
{
    g_test_init(&argc, &argv,NULL);
    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");

    init_simple();

    g_test_add_func("/xi2/protocol/XISelectEvents", test_XISelectEvents);

    return g_test_run();
}