[PATCH 5/5] test: add some XIPassiveGrab protocol testing.

Peter Hutterer peter.hutterer at who-t.net
Thu Apr 14 20:21:00 PDT 2011


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 test/xi2/Makefile.am                    |    4 +
 test/xi2/protocol-xipassivegrabdevice.c |  238 +++++++++++++++++++++++++++++++
 2 files changed, 242 insertions(+), 0 deletions(-)
 create mode 100644 test/xi2/protocol-xipassivegrabdevice.c

diff --git a/test/xi2/Makefile.am b/test/xi2/Makefile.am
index 1b9bfe0..c6165e0 100644
--- a/test/xi2/Makefile.am
+++ b/test/xi2/Makefile.am
@@ -8,6 +8,7 @@ noinst_PROGRAMS =  \
 	protocol-xigetselectedevents \
         protocol-xisetclientpointer \
         protocol-xigetclientpointer \
+        protocol-xipassivegrabdevice \
         protocol-xiquerypointer \
         protocol-xiwarppointer \
         protocol-eventconvert
@@ -30,6 +31,7 @@ protocol_xigetselectedevents_LDADD=$(TEST_LDADD)
 protocol_xisetclientpointer_LDADD=$(TEST_LDADD)
 protocol_xigetclientpointer_LDADD=$(TEST_LDADD)
 protocol_xiquerypointer_LDADD=$(TEST_LDADD)
+protocol_xipassivegrabdevice_LDADD=$(TEST_LDADD)
 protocol_xiwarppointer_LDADD=$(TEST_LDADD)
 protocol_eventconvert_LDADD=$(TEST_LDADD)
 
@@ -39,6 +41,7 @@ protocol_xiselectevents_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,dixLookupWindow -Wl,-wra
 protocol_xigetselectedevents_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,WriteToClient -Wl,-wrap,dixLookupWindow -Wl,-wrap,AddResource
 protocol_xisetclientpointer_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,dixLookupClient
 protocol_xigetclientpointer_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,WriteToClient -Wl,-wrap,dixLookupClient
+protocol_xipassivegrabdevice_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,GrabButton -Wl,-wrap,dixLookupWindow -Wl,-wrap,WriteToClient
 protocol_xiquerypointer_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,WriteToClient -Wl,-wrap,dixLookupWindow
 protocol_xiwarppointer_LDFLAGS=$(AM_LDFLAGS) -Wl,-wrap,WriteToClient -Wl,-wrap,dixLookupWindow
 
@@ -49,6 +52,7 @@ protocol_xigetselectedevents_SOURCES=$(COMMON_SOURCES) protocol-xigetselectedeve
 protocol_xisetclientpointer_SOURCES=$(COMMON_SOURCES) protocol-xisetclientpointer.c
 protocol_xigetclientpointer_SOURCES=$(COMMON_SOURCES) protocol-xigetclientpointer.c
 protocol_xiquerypointer_SOURCES=$(COMMON_SOURCES) protocol-xiquerypointer.c
+protocol_xipassivegrabdevice_SOURCES=$(COMMON_SOURCES) protocol-xipassivegrabdevice.c
 protocol_xiwarppointer_SOURCES=$(COMMON_SOURCES) protocol-xiwarppointer.c
 endif
 endif
diff --git a/test/xi2/protocol-xipassivegrabdevice.c b/test/xi2/protocol-xipassivegrabdevice.c
new file mode 100644
index 0000000..36742d6
--- /dev/null
+++ b/test/xi2/protocol-xipassivegrabdevice.c
@@ -0,0 +1,238 @@
+/**
+ * Copyright © 2011 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 XIPassiveGrab request.
+ */
+#include <stdint.h>
+#include <X11/X.h>
+#include <X11/Xproto.h>
+#include <X11/extensions/XI2proto.h>
+#include "inputstr.h"
+#include "windowstr.h"
+#include "scrnintstr.h"
+#include "xipassivegrab.h"
+#include "exevents.h"
+
+#include "protocol-common.h"
+#include <glib.h>
+
+static ClientRec client_request;
+#define N_MODS 7
+static uint32_t modifiers[N_MODS] = {1, 2, 3, 4, 5, 6, 7};
+
+struct test_data {
+    int num_modifiers;
+} testdata;
+
+int __wrap_GrabButton(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
+                      int button, GrabParameters *param, GrabType grabtype,
+                      GrabMask *mask);
+static void reply_XIPassiveGrabDevice_data(ClientPtr client, int len, char *data, void *userdata);
+
+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);
+}
+
+int __wrap_GrabButton(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
+                      int button, GrabParameters *param, GrabType grabtype,
+                      GrabMask *mask)
+{
+    /* Fail every odd modifier */
+    if (param->modifiers % 2)
+        return BadAccess;
+
+    return Success;
+}
+
+static void reply_XIPassiveGrabDevice(ClientPtr client, int len, char *data, void *userdata)
+{
+    xXIPassiveGrabDeviceReply *rep = (xXIPassiveGrabDeviceReply*)data;
+
+    if (client->swapped)
+    {
+        char n;
+        swaps(&rep->sequenceNumber, n);
+        swapl(&rep->length, n);
+        swaps(&rep->num_modifiers, n);
+
+        testdata.num_modifiers = rep->num_modifiers;
+    }
+
+    reply_check_defaults(rep, len, XIPassiveGrabDevice);
+
+    /* ProcXIPassiveGrabDevice sends the data in two batches, let the second
+     * handler handle the modifier data */
+    if (rep->num_modifiers > 0)
+        reply_handler = reply_XIPassiveGrabDevice_data;
+}
+
+static void reply_XIPassiveGrabDevice_data(ClientPtr client, int len, char *data, void *userdata)
+{
+    int i;
+    int n;
+
+    xXIGrabModifierInfo *mods = (xXIGrabModifierInfo*)data;
+
+    for (i = 0; i < testdata.num_modifiers; i++, mods++)
+    {
+        if (client->swapped)
+            swapl(&mods->modifiers, n);
+
+        /* 1 - 7 is the range we use for the global modifiers array
+         * above */
+        g_assert(mods->modifiers > 0);
+        g_assert(mods->modifiers <= 7);
+        g_assert(mods->modifiers % 2 == 1); /* because we fail odd ones */
+        g_assert(mods->status != Success);
+        g_assert(mods->pad0 == 0);
+        g_assert(mods->pad1 == 0);
+    }
+
+    reply_handler = reply_XIPassiveGrabDevice;
+}
+
+static void request_XIPassiveGrabDevice(ClientPtr client, xXIPassiveGrabDeviceReq* req, int error, int errval)
+{
+    char n;
+    int rc;
+    int modifiers;
+
+    rc = ProcXIPassiveGrabDevice(&client_request);
+    g_assert(rc == error);
+
+    if (rc != Success)
+        g_assert(client_request.errorValue == errval);
+
+    client_request.swapped = TRUE;
+    swaps(&req->length, n);
+    swapl(&req->time, n);
+    swapl(&req->grab_window, n);
+    swapl(&req->cursor, n);
+    swapl(&req->detail, n);
+    swaps(&req->deviceid, n);
+    modifiers = req->num_modifiers;
+    swaps(&req->num_modifiers, n);
+    swaps(&req->mask_len, n);
+
+    while(modifiers--)
+    {
+        CARD32 *mod = ((CARD32*)(req + 1)) + modifiers;
+        swapl(mod, n);
+    }
+
+    rc = SProcXIPassiveGrabDevice(&client_request);
+    g_assert(rc == error);
+
+    if (rc != Success)
+        g_assert(client_request.errorValue == errval);
+}
+
+static unsigned char *data[4096]; /* the request buffer */
+static void test_XIPassiveGrabDevice(void)
+{
+    int i;
+    xXIPassiveGrabDeviceReq *request = (xXIPassiveGrabDeviceReq*)data;
+    unsigned char *mask;
+
+    request_init(request, XIPassiveGrabDevice);
+
+    request->grab_window = CLIENT_WINDOW_ID;
+
+    reply_handler = reply_XIPassiveGrabDevice;
+    client_request = init_client(request->length, request);
+
+    g_test_message("Testing invalid device");
+    request->deviceid = 12;
+    request_XIPassiveGrabDevice(&client_request, request, BadDevice, request->deviceid);
+
+    request->deviceid = XIAllMasterDevices;
+
+    g_test_message("Testing invalid grab types");
+    for (i = XIGrabtypeFocusIn + 1; i < 0xFF; i++)
+    {
+        request->grab_type = i;
+        request_XIPassiveGrabDevice(&client_request, request, BadValue, request->grab_type);
+    }
+
+    g_test_message("Testing invalid grab type + detail combinations");
+    request->grab_type = XIGrabtypeEnter;
+    request->detail = 1;
+    request_XIPassiveGrabDevice(&client_request, request, BadValue, request->detail);
+
+    request->grab_type = XIGrabtypeFocusIn;
+    request_XIPassiveGrabDevice(&client_request, request, BadValue, request->detail);
+
+    request->detail = 0;
+
+    g_test_message("Testing invalid masks");
+    mask = (unsigned char*)&request[1];
+
+    request->mask_len = bytes_to_int32(XI2LASTEVENT + 1);
+    request->length += request->mask_len;
+    SetBit(mask, XI2LASTEVENT + 1);
+    request_XIPassiveGrabDevice(&client_request, request, BadValue, XI2LASTEVENT + 1);
+
+    ClearBit(mask, XI2LASTEVENT + 1);
+
+    /* tested all special cases now, test a few valid cases */
+
+    /* no modifiers */
+    request->deviceid = XIAllDevices;
+    request->grab_type = XIGrabtypeButton;
+    request->detail = XIAnyButton;
+    request_XIPassiveGrabDevice(&client_request, request, Success, 0);
+
+    /* some modifiers */
+    request->num_modifiers = N_MODS;
+    request->length += N_MODS;
+    memcpy((uint32_t*)(request + 1) + request->mask_len, modifiers, sizeof(modifiers));
+    request_XIPassiveGrabDevice(&client_request, request, Success, 0);
+}
+
+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/XIPassiveGrab", test_XIPassiveGrabDevice);
+
+    return g_test_run();
+}
-- 
1.7.4.2



More information about the xorg-devel mailing list