summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2013-05-24 22:24:55 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2013-08-15 16:52:23 -0700
commit9089ae455f9df222aa85bbbcb4526874c0d97099 (patch)
treea51cf4e64ea8c3f022d8e5cfbbd88369be0c325d
parent66514a4af7eaa47e8718434356d7efce95e570cf (diff)
Add unit tests for Array allocation functions
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am2
-rw-r--r--configure.ac10
-rw-r--r--test/Array.c62
-rw-r--r--test/Makefile.am13
5 files changed, 84 insertions, 4 deletions
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