[PATCH:libXt] Add test framework similar to xserver and use it to test XtAsprintf

Alan Coopersmith alan.coopersmith at oracle.com
Tue Mar 8 13:07:42 PST 2011


Simple test case just compares the results of snprintf to a static buffer
with the new buffer returned by XtAsprintf.

Test run currently fails due to correctly detecting the bug in null
terminating the XtAsprintf returned string.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 Makefile.am      |    2 +-
 configure.ac     |   18 +++++++++++++++++
 test/Alloc.c     |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/Makefile.am |   11 ++++++++++
 4 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100644 test/Alloc.c
 create mode 100644 test/Makefile.am

diff --git a/Makefile.am b/Makefile.am
index e9e0316..8ef09ca 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,7 +19,7 @@
 #  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 #  PERFORMANCE OF THIS SOFTWARE.
 
-SUBDIRS = util src include man specs
+SUBDIRS = util src include man specs test
 
 ACLOCAL_AMFLAGS = -I m4
 
diff --git a/configure.ac b/configure.ac
index 8945729..94db440 100644
--- a/configure.ac
+++ b/configure.ac
@@ -95,6 +95,23 @@ if test "x$XKB" = "xyes" ; then
 	AC_DEFINE(XKB, 1, [Define to 1 to use XKB for keysym resolution.])
 fi
 
+AC_ARG_ENABLE(unit-tests,    AS_HELP_STRING([--enable-unit-tests],
+                                  [Enable unit-tests (default: auto)]),
+                                [UNITTESTS=$enableval], [UNITTESTS=auto])
+if test "x$UNITTESTS" != xno ; then
+       PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.16],
+                         [HAVE_GLIB=yes], [HAVE_GLIB=no])
+fi
+if test "x$UNITTESTS" = xyes; then
+       if test "x$HAVE_GLIB" = xno; then
+           AC_MSG_ERROR([glib required to build unit tests])
+       fi
+elif test "x$UNITTESTS" = xauto; then
+       UNITTESTS="$HAVE_GLIB"
+fi
+
+AM_CONDITIONAL(UNITTESTS, [test "x$UNITTESTS" = xyes])
+
 # Replaces XFileSearchPathDefault from Imake configs
 XFILESEARCHPATHDEFAULT='$(sysconfdir)/X11/%L/%T/%N%C%S:$(sysconfdir)/X11/%l/%T/%N%C%S:$(sysconfdir)/X11/%T/%N%C%S:$(sysconfdir)/X11/%L/%T/%N%S:$(sysconfdir)/X11/%l/%T/%N%S:$(sysconfdir)/X11/%T/%N%S:$(datadir)/X11/%L/%T/%N%C%S:$(datadir)/X11/%l/%T/%N%C%S:$(datadir)/X11/%T/%N%C%S:$(datadir)/X11/%L/%T/%N%S:$(datadir)/X11/%l/%T/%N%S:$(datadir)/X11/%T/%N%S:$(libdir)/X11/%L/%T/%N%C%S:$(libdir)/X11/%l/%T/%N%C%S:$(libdir)/X11/%T/%N%C%S:$(libdir)/X11/%L/%T/%N%S:$(libdir)/X11/%l/%T/%N%S:$(libdir)/X11/%T/%N%S'
 
@@ -144,5 +161,6 @@ AC_CONFIG_FILES([Makefile
 		include/Makefile
 		man/Makefile
 		specs/Makefile
+		test/Makefile
 		xt.pc])
 AC_OUTPUT
diff --git a/test/Alloc.c b/test/Alloc.c
new file mode 100644
index 0000000..5f9e27a
--- /dev/null
+++ b/test/Alloc.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ *
+ * 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.
+ */
+
+#include <X11/Intrinsic.h>
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+
+static const char *program_name;
+
+static void test_XtAsprintf(void)
+{
+    char snbuf[1024];
+    char *asbuf;
+    gint32 r = g_test_rand_int();
+    int snlen, aslen;
+    
+    snlen = snprintf(snbuf, sizeof(snbuf), "%s: %d\n", program_name, r);
+    aslen = XtAsprintf(&asbuf, "%s: %d\n", program_name, r);
+
+    g_assert(asbuf != NULL);
+    g_assert(snlen == aslen);
+    g_assert(strcmp(snbuf, asbuf) == 0);
+}
+
+int main(int argc, char** argv)
+{
+    program_name = argv[0];
+
+    g_test_init(&argc, &argv, NULL);
+    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
+
+    g_test_add_func("/Xt/Alloc/XtAsprintf", test_XtAsprintf);
+
+    return g_test_run();
+}
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..3e76bc5
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,11 @@
+if UNITTESTS
+check_PROGRAMS = Alloc
+
+TESTS=$(check_PROGRAMS)
+
+AM_CFLAGS = $(CWARNFLAGS) $(XT_CFLAGS) $(GLIB_CFLAGS)
+INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include
+LDADD= $(top_builddir)/src/libXt.la $(GLIB_LIBS)
+
+endif
+
-- 
1.7.3.2



More information about the xorg-devel mailing list