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

Gaetan Nadon memsize at videotron.ca
Tue Mar 8 15:23:51 PST 2011


On Tue, 2011-03-08 at 13:07 -0800, Alan Coopersmith wrote:

> 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;


Whitespace error

> +    
> +    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
> +


Reviewed-by: Gaetan Nadon <memsize at videotron.ca>

Would it be the time to provide some common support for unit testing?
We will soon end-up in a situation where every package does it
differently,
making it difficult to add test cases in multiple packages, having to
learn
each package option, variables and behaviors.

At a minimum, we should have a macro for the configure option so at
least
there is a common user interface.

Tests are only executed when running "make check", is the 
--enable-unit-tests really needed?


A quick summary of how things look today:

        libXau: always enabled; no config option; no "test" dir
        libXt and xserver: --enable-unit-tests; UNITTESTS; auto detected based on required support; has "test" dir
        libxkbcommon: always enabled; no config option; has "test" dir
        libxcb: autodetected based on "check"; HAVE_CHECK; has "tests" (plural) dir
        
        Outside xorg, but using all the lastest xorg macros, wacom has copied the server code verbatim





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x.org/archives/xorg-devel/attachments/20110308/49341dfc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://lists.x.org/archives/xorg-devel/attachments/20110308/49341dfc/attachment-0001.pgp>


More information about the xorg-devel mailing list