[PATCH:libXdmcp 1/4] Add unit tests for Array allocation functions

Alan Coopersmith alan.coopersmith at oracle.com
Fri Sep 27 21:48:00 PDT 2013


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 .gitignore       |    1 +
 Makefile.am      |    2 +-
 configure.ac     |   10 ++++++---
 test/Array.c     |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/Makefile.am |   13 ++++++++++++
 5 files changed, 84 insertions(+), 4 deletions(-)
 create mode 100644 test/Array.c
 create mode 100644 test/Makefile.am

diff --git a/.gitignore b/.gitignore
index 07ac69e..edf9a16 100644
--- a/.gitignore
+++ b/.gitignore
@@ -76,3 +76,4 @@ core
 #		Edit the following section as needed
 # For example, !report.pc overrides *.pc. See 'man gitignore'
 # 
+test/Array
diff --git a/Makefile.am b/Makefile.am
index c3b85aa..ca54099 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS=doc
+SUBDIRS=doc . test
 
 lib_LTLIBRARIES = libXdmcp.la
 
diff --git a/configure.ac b/configure.ac
index d8ddfae..4e85650 100644
--- a/configure.ac
+++ b/configure.ac
@@ -35,10 +35,10 @@ AM_MAINTAINER_MODE
 AC_LIBTOOL_WIN32_DLL
 AC_PROG_LIBTOOL
 
-# Require xorg-macros minimum of 1.12 for DocBook external references
+# Require xorg-macros minimum of 1.16 for unit testing with memory checks
 m4_ifndef([XORG_MACROS_VERSION],
-          [m4_fatal([must install xorg-macros 1.12 or later before running autoconf/autogen])])
-XORG_MACROS_VERSION(1.12)
+          [m4_fatal([must install xorg-macros 1.16 or later before running autoconf/autogen])])
+XORG_MACROS_VERSION(1.16)
 XORG_DEFAULT_OPTIONS
 XORG_ENABLE_DOCS
 XORG_WITH_XMLTO(0.0.22)
@@ -72,7 +72,11 @@ AM_CONDITIONAL(HASXDMAUTH,test x$HASXDMAUTH = xyes)
 XORG_WITH_LINT
 XORG_LINT_LIBRARY([Xdmcp])
 
+#  --enable-unit-tests
+XORG_ENABLE_UNIT_TESTS([yes])
+
 AC_CONFIG_FILES([Makefile
 		doc/Makefile
+		test/Makefile
 		xdmcp.pc])
 AC_OUTPUT
diff --git a/test/Array.c b/test/Array.c
new file mode 100644
index 0000000..0f3430e
--- /dev/null
+++ b/test/Array.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2013, 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 <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <X11/Xdmcp.h>
+#include <inttypes.h>
+
+/* Test what happens if you try to allocate an array with too many entries */
+#define TestAllocOversize(type, len) {                          \
+    type newArray = { -1, (void *) -1 };                        \
+    int result;                                                 \
+    printf("Checking XdmcpAlloc%s(%d)...\n", #type, len);       \
+    result = XdmcpAlloc##type(&newArray, len)    ;              \
+    assert(result == FALSE);                                    \
+    assert(newArray.length == 0);                               \
+    assert(newArray.data == NULL);                              \
+    printf("Checking XdmcpRealloc%s(%d)...\n", #type, len);     \
+    result = XdmcpRealloc##type(&newArray, len);                \
+    assert(result == FALSE);                                    \
+    assert(newArray.length == 0);                               \
+    assert(newArray.data == NULL);                              \
+    XdmcpDispose##type(&newArray);                              \
+}
+
+static void
+TestAllocOversizeArrays(void)
+{
+    TestAllocOversize(ARRAY8, UINT16_MAX + 1);
+    TestAllocOversize(ARRAY16, UINT8_MAX + 1);
+    TestAllocOversize(ARRAY32, UINT8_MAX + 1);
+    TestAllocOversize(ARRAYofARRAY8, UINT8_MAX + 1);
+}
+
+int
+main(int argc, char **argv)
+{
+    TestAllocOversizeArrays();
+
+    exit(0);
+}
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..d86719e
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,13 @@
+if ENABLE_UNIT_TESTS
+
+check_PROGRAMS = Array
+
+TESTS=$(check_PROGRAMS)
+
+AM_CFLAGS = $(CWARNFLAGS) $(XDMCP_CFLAGS)
+AM_CPPFLAGS = -I$(top_srcdir)/include
+LDADD= $(top_builddir)/libXdmcp.la $(XDMCP_LIBS)
+
+AM_TESTS_ENVIRONMENT = $(XORG_MALLOC_DEBUG_ENV)
+
+endif ENABLE_UNIT_TESTS
-- 
1.7.9.2



More information about the xorg-devel mailing list