[PATCH 1/2] Add a test/ directory for a in-server test-suite.

Peter Hutterer peter.hutterer at who-t.net
Sun Apr 19 17:55:48 PDT 2009


---
 configure.ac           |    4 +
 test/Makefile.am       |   43 ++++++++++++
 test/README            |   38 +++++++++++
 test/libxservertest.c  |   63 +++++++++++++++++
 test/xkb.c             |  174 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/xserver-testing.h |   74 ++++++++++++++++++++
 6 files changed, 396 insertions(+), 0 deletions(-)
 create mode 100644 test/Makefile.am
 create mode 100644 test/README
 create mode 100644 test/libxservertest.c
 create mode 100644 test/xkb.c
 create mode 100644 test/xserver-testing.h

diff --git a/configure.ac b/configure.ac
index 0331730..1f3024c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -79,6 +79,9 @@ AC_SYS_LARGEFILE
 XORG_PROG_RAWCPP
 AC_PATH_PROG(SED,sed)
 
+# for the test suite
+AC_CHECK_TOOL(OBJCOPY, objcopy)
+
 dnl Check for dtrace program (needed to build Xserver dtrace probes)
 dnl Also checks for <sys/sdt.h>, since some Linux distros have an 
 dnl ISDN trace program named dtrace
@@ -1973,5 +1976,6 @@ hw/kdrive/fbdev/Makefile
 hw/kdrive/linux/Makefile
 hw/kdrive/sdl/Makefile
 hw/kdrive/src/Makefile
+test/Makefile
 xorg-server.pc
 ])
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..2accb99
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,43 @@
+check_PROGRAMS = xkb
+check_LTLIBRARIES = libxservertest.la libxservertest_t.la
+
+TESTS=$(check_PROGRAMS)
+
+# This makefile is not happy if you have concurrent makes going on.
+AM_MAKEFLAGS = -j1 
+
+AM_CFLAGS = $(DIX_CFLAGS) @XORG_CFLAGS@
+INCLUDES = @XORG_INCS@
+
+libxservertest_la_SOURCES= libxservertest.c
+libxservertest_la_LIBADD = \
+            $(XSERVER_LIBS) \
+            $(top_builddir)/hw/xfree86/loader/libloader.la \
+            $(top_builddir)/hw/xfree86/os-support/libxorgos.la \
+            $(top_builddir)/hw/xfree86/common/libcommon.la \
+            $(top_builddir)/hw/xfree86/parser/libxf86config.la \
+            $(top_builddir)/hw/xfree86/dixmods/libdixmods.la \
+            $(top_builddir)/hw/xfree86/modes/libxf86modes.la \
+            $(top_builddir)/hw/xfree86/ramdac/libramdac.la \
+            $(top_builddir)/hw/xfree86/ddc/libddc.la \
+            $(top_builddir)/hw/xfree86/i2c/libi2c.la \
+            $(top_builddir)/hw/xfree86/dixmods/libxorgxkb.la \
+            $(top_builddir)/hw/xfree86/libxorg.la \
+            $(top_builddir)/mi/libmi.la \
+            $(top_builddir)/os/libos.la \
+            @XORG_LIBS@
+
+libxservertest_t.c:
+	touch $@
+
+# libxservertest.la defines main, so we need to get rid of it somehow.
+# we do this by copying libxservertest.a into libxservertest_t.a and removing the
+# symbol during the copy process.
+libxservertest_t.la: libxservertest.la
+	$(OBJCOPY) --strip-symbol=main .libs/libxservertest.a .libs/libxservertest_t.a
+
+AM_LDFLAGS =.libs/libxservertest_t.a $(XORG_SYS_LIBS) $(XSERVER_SYS_LIBS)
+
+all:
+	echo "Run 'make check' to run the test suite"
+
diff --git a/test/README b/test/README
new file mode 100644
index 0000000..a6fab98
--- /dev/null
+++ b/test/README
@@ -0,0 +1,38 @@
+                        X server test suite
+
+This suite contains a set of tests to verify the behaviour of functions used
+internally to the server.
+
+How it works:
+Through some automake abuse, we link the test programs with the same static
+libraries as the Xorg binary. The test suites can then call various functions
+and verify their behaviour - without the need to start the server or connect
+clients.
+
+This testing only works for functions that do not rely on a particular state
+of the X server. Unless the test suite replicates the expected state, which
+may be difficult.
+
+How to run the tests:
+Run "make check" the test directory. This will compile the tests and execute
+them in the order specified in the TESTS variable in test/Makefile.am.
+
+Each set of tests related to a subsystem are available as a binary that can be
+executed directly. For example, run "xkb" to perform some xkb-related tests.
+
+How to add a new test:
+At this stage the test suite is very simple. These instructions may change
+frequently.
+
+To add a new test set:
+1. Add the new subsystem name to check_PROGRAMS in Makefile.am, e.g. "dix"
+2. Add (and git-add) the new file "dix.c"
+3. Add a simple main() method that returns 0 by default.
+4. Proceed with the next section.
+
+To add a new test to an existing set:
+1. Add a static int mytest(void) that returns 0 on succes.
+2. Add an explanation of the test using test_explain() and test_expected().
+3. Perform verifications using the test_test() macro.
+4. Add a test_call() entry to main() to call your new test.
+5. Run "make check" to compile and run all tests.
diff --git a/test/libxservertest.c b/test/libxservertest.c
new file mode 100644
index 0000000..ac30416
--- /dev/null
+++ b/test/libxservertest.c
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ *
+ *  Authors: Peter Hutterer
+ *
+ */
+
+/**
+ * @file libxservertest.c
+ *
+ * This file contains various utility functions to be used in the test
+ * programs.
+ */
+
+#include "xserver-testing.h"
+
+#include <stdio.h>
+
+static int verbosity = 0;
+
+void test_init(int argc, char **argv)
+{
+    verbosity = TEST_VERBOSITY_ERROR;
+}
+
+
+void test_explain(char *msg)
+{
+    if (verbosity < TEST_VERBOSITY_INFO)
+        return;
+
+    printf("---------------------------------------------\n");
+    printf("TESTING %s:%s()\n\n", __FILE__, __func__);
+    printf("%s\n", msg);
+}
+
+void test_expected(char *msg)
+{
+    if (verbosity < TEST_VERBOSITY_INFO)
+        return;
+
+    printf("EXPECTED RESULT: %s\n", msg);
+}
+
diff --git a/test/xkb.c b/test/xkb.c
new file mode 100644
index 0000000..8855762
--- /dev/null
+++ b/test/xkb.c
@@ -0,0 +1,174 @@
+/**
+ * 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
+
+#include <xkb-config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <math.h>
+#include <X11/X.h>
+#include <X11/Xproto.h>
+#include <X11/keysym.h>
+#include <X11/Xatom.h>
+#include "misc.h"
+#include "inputstr.h"
+#include "opaque.h"
+#include "property.h"
+#define	XKBSRV_NEED_FILE_FUNCS
+#include <xkbsrv.h>
+#include "../xkb/xkbgeom.h"
+#include <X11/extensions/XKMformat.h>
+#include "xkbfile.h"
+#include "../xkb/xkb.h"
+
+#include "xserver-testing.h"
+
+static int xkb_get_rules_test(void)
+{
+    XkbRMLVOSet rmlvo = { NULL};
+    XkbGetRulesDflts(&rmlvo);
+
+    test_explain("Initialize an empty XkbRMLVOSet.\n"
+                 "Call XkbGetRulesDflts to obtain the default ruleset.\n"
+                 "Compare obtained ruleset with the built-in defaults.\n");
+    test_expected("RMLVO defaults are the same as obtained.\n");
+
+    test_test(rmlvo.rules);
+    test_test(rmlvo.model);
+    test_test(rmlvo.layout);
+    test_test(rmlvo.variant);
+    test_test(rmlvo.options);
+    test_test(strcmp(rmlvo.rules, XKB_DFLT_RULES) == 0);
+    test_test(strcmp(rmlvo.model, XKB_DFLT_MODEL) == 0);
+    test_test(strcmp(rmlvo.layout, XKB_DFLT_LAYOUT) == 0);
+    test_test(strcmp(rmlvo.variant, XKB_DFLT_VARIANT) == 0);
+    test_test(strcmp(rmlvo.options, XKB_DFLT_OPTIONS) == 0);
+
+    return 0;
+}
+
+static int xkb_set_rules_test(void)
+{
+    XkbRMLVOSet rmlvo = {
+        .rules = "test-rules",
+        .model = "test-model",
+        .layout = "test-layout",
+        .variant = "test-variant",
+        .options = "test-options"
+    };
+    XkbRMLVOSet rmlvo_new = { NULL };
+
+    test_explain("Initialize an random XkbRMLVOSet.\n"
+                 "Call XkbGetRulesDflts to obtain the default ruleset.\n"
+                 "Compare obtained ruleset with the built-in defaults.\n");
+    test_expected("RMLVO defaults are the same as obtained.\n");
+
+    XkbSetRulesDflts(&rmlvo);
+    XkbGetRulesDflts(&rmlvo_new);
+
+    /* XkbGetRulesDflts strdups the values */
+    test_test(rmlvo.rules != rmlvo_new.rules);
+    test_test(rmlvo.model != rmlvo_new.model);
+    test_test(rmlvo.layout != rmlvo_new.layout);
+    test_test(rmlvo.variant != rmlvo_new.variant);
+    test_test(rmlvo.options != rmlvo_new.options);
+
+    test_test(strcmp(rmlvo.rules, rmlvo_new.rules) == 0);
+    test_test(strcmp(rmlvo.model, rmlvo_new.model) == 0);
+    test_test(strcmp(rmlvo.layout, rmlvo_new.layout) == 0);
+    test_test(strcmp(rmlvo.variant, rmlvo_new.variant) == 0);
+    test_test(strcmp(rmlvo.options, rmlvo_new.options) == 0);
+
+    return 0;
+}
+
+/* This test failed before XkbGetRulesDftlts changed to strdup.
+   We test this twice because the first time using XkbGetRulesDflts we obtain
+   the built-in defaults. The unexpected free isn't triggered until the second
+   XkbSetRulesDefaults.
+ */
+static int xkb_set_get_rules_test(void)
+{
+    XkbRMLVOSet rmlvo = { NULL };
+    XkbRMLVOSet rmlvo_backup;
+
+    test_explain("Get the default RMLVO set.\n"
+                 "Set the default RMLVO set.\n"
+                 "Get the default RMLVO set.\n"
+                 "Repeat the last two steps.\n");
+    test_expected("RMLVO set obtained is the same as previously set.\n");
+
+    XkbGetRulesDflts(&rmlvo);
+
+    /* pass 1 */
+    XkbSetRulesDflts(&rmlvo);
+    XkbGetRulesDflts(&rmlvo);
+
+    /* Make a backup copy */
+    rmlvo_backup.rules = strdup(rmlvo.rules);
+    rmlvo_backup.layout = strdup(rmlvo.layout);
+    rmlvo_backup.model = strdup(rmlvo.model);
+    rmlvo_backup.variant = strdup(rmlvo.variant);
+    rmlvo_backup.options = strdup(rmlvo.options);
+
+    /* pass 2 */
+    XkbSetRulesDflts(&rmlvo);
+
+    /* This test is iffy, because strictly we may be comparing against already
+     * freed memory */
+    test_test(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
+    test_test(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
+    test_test(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
+    test_test(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
+    test_test(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
+
+    XkbGetRulesDflts(&rmlvo);
+    test_test(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
+    test_test(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
+    test_test(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
+    test_test(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
+    test_test(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
+
+    return 0;
+}
+
+
+
+/**
+ * Linker flag makes this the main entry point.
+ */
+int main(int argc, char** argv)
+{
+    test_init(argc, argv);
+
+    test_call(xkb_set_get_rules_test);
+    test_call(xkb_get_rules_test);
+    test_call(xkb_set_rules_test);
+    return 0;
+}
diff --git a/test/xserver-testing.h b/test/xserver-testing.h
new file mode 100644
index 0000000..e07e831
--- /dev/null
+++ b/test/xserver-testing.h
@@ -0,0 +1,74 @@
+/**
+ * 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.
+ *
+ *  Authors: Peter Hutterer
+ *
+ */
+
+
+typedef enum {
+    TEST_VERBOSITY_NONE,
+    TEST_VERBOSITY_ERROR, /* default verbosity level */
+    TEST_VERBOSITY_INFO
+} test_verbosity;
+
+/**
+ * Use 'test_init' at the top of your main method.
+ */
+extern void test_init(int argc, char** argv);
+
+/**
+ * Use 'test_explain' at the start of the test function. It should
+ * describe short and concisely the test performed in this function.
+ */
+extern void test_explain(char *msg);
+
+/**
+ * Use 'test_expected' right after 'explain', and describe in as little words
+ * as possible the expected outcome of this test.
+ */
+extern void test_expected(char *msg);
+
+/**
+ * Use the call macro from your main() to call the various test functions.
+ * This macro returns from the current function on a non-successful (non-zero)
+ * outcome.
+ *
+ * int main(void) {
+ *      call(my_test_function);
+ *      return 0;
+ * }
+ *
+ */
+#define test_call(func) { int rc; if ((rc = func())) return rc; }
+
+/**
+ * Use the test macro to verify a particular condition.
+ * This macro returns from the current function if the condition is
+ * evaluated as false.
+ */
+#define test_test(condition) { \
+        if (!(condition)) { \
+                printf("Condition failed in %s:%d\n", __func__, __LINE__); \
+                return 1; \
+        } \
+    }
-- 
1.6.2.2.447.g4afa7



More information about the xorg-devel mailing list