summaryrefslogtreecommitdiff
path: root/tests/util
diff options
context:
space:
mode:
Diffstat (limited to 'tests/util')
-rw-r--r--tests/util/concat-t.c60
-rw-r--r--tests/util/messages-krb5-t.c99
-rw-r--r--tests/util/messages-t.c201
-rwxr-xr-xtests/util/xmalloc-t127
-rw-r--r--tests/util/xmalloc-t.in126
-rw-r--r--tests/util/xmalloc.c85
6 files changed, 350 insertions, 348 deletions
diff --git a/tests/util/concat-t.c b/tests/util/concat-t.c
index 81824c8..ca7de2c 100644
--- a/tests/util/concat-t.c
+++ b/tests/util/concat-t.c
@@ -1,58 +1,46 @@
/*
* concat test suite.
*
- * Copyright 2004, 2005, 2006
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
+ * Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
- * Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
- * 2003 by The Internet Software Consortium and Rich Salz
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ * 2002, 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
-#include <tests/libtest.h>
-#include <util/util.h>
+#include <tests/tap/basic.h>
+#include <util/concat.h>
#define END (char *) 0
-
/*
* Memory leaks everywhere! Whoo-hoo!
*/
int
main(void)
{
- test_init(13);
-
- ok_string( 1, "a", concat("a", END));
- ok_string( 2, "ab", concat("a", "b", END));
- ok_string( 3, "ab", concat("ab", "", END));
- ok_string( 4, "ab", concat("", "ab", END));
- ok_string( 5, "", concat("", END));
- ok_string( 6, "abcde", concat("ab", "c", "", "de", END));
- ok_string( 7, "abcde", concat("abc", "de", END, "f", END));
-
- ok_string( 8, "/foo", concatpath("/bar", "/foo"));
- ok_string( 9, "/foo/bar", concatpath("/foo", "bar"));
- ok_string(10, "./bar", concatpath("/foo", "./bar"));
- ok_string(11, "/bar/baz/foo/bar", concatpath("/bar/baz", "foo/bar"));
- ok_string(12, "./foo", concatpath(NULL, "foo"));
- ok_string(13, "/foo/bar", concatpath(NULL, "/foo/bar"));
+ plan(13);
+
+ is_string("a", concat("a", END), "concat 1");
+ is_string("ab", concat("a", "b", END), "concat 2");
+ is_string("ab", concat("ab", "", END), "concat 3");
+ is_string("ab", concat("", "ab", END), "concat 4");
+ is_string("", concat("", END), "concat 5");
+ is_string("abcde", concat("ab", "c", "", "de", END), "concat 6");
+ is_string("abcde", concat("abc", "de", END, "f", END), "concat 7");
+
+ is_string("/foo", concatpath("/bar", "/foo"), "path 1");
+ is_string("/foo/bar", concatpath("/foo", "bar"), "path 2");
+ is_string("./bar", concatpath("/foo", "./bar"), "path 3");
+ is_string("/bar/baz/foo/bar", concatpath("/bar/baz", "foo/bar"), "path 4");
+ is_string("./foo", concatpath(NULL, "foo"), "path 5");
+ is_string("/foo/bar", concatpath(NULL, "/foo/bar"), "path 6");
return 0;
}
diff --git a/tests/util/messages-krb5-t.c b/tests/util/messages-krb5-t.c
new file mode 100644
index 0000000..02d8f92
--- /dev/null
+++ b/tests/util/messages-krb5-t.c
@@ -0,0 +1,99 @@
+/*
+ * Test suite for Kerberos error handling routines.
+ *
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2010 Board of Trustees, Leland Stanford Jr. University
+ *
+ * See LICENSE for licensing terms.
+ */
+
+#include <config.h>
+#include <portable/krb5.h>
+#include <portable/system.h>
+
+#include <tests/tap/basic.h>
+#include <tests/tap/process.h>
+#include <util/messages-krb5.h>
+#include <util/messages.h>
+#include <util/xmalloc.h>
+
+
+/*
+ * Test functions.
+ */
+static void
+test_warn(void)
+{
+ krb5_context ctx;
+ krb5_error_code code;
+ krb5_principal princ;
+
+ code = krb5_init_context(&ctx);
+ if (code < 0)
+ die_krb5(ctx, code, "cannot create context");
+ code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+ if (code < 0)
+ warn_krb5(ctx, code, "principal parse failed");
+ else
+ die("unexpected success parsing principal");
+ exit(0);
+}
+
+static void
+test_die(void)
+{
+ krb5_context ctx;
+ krb5_error_code code;
+ krb5_principal princ;
+
+ code = krb5_init_context(&ctx);
+ if (code < 0)
+ die_krb5(ctx, code, "cannot create context");
+ code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+ if (code < 0)
+ die_krb5(ctx, code, "principal parse failed");
+ else
+ die("unexpected success parsing principal");
+ exit(0);
+}
+
+
+/*
+ * Run the tests.
+ */
+int
+main(void)
+{
+ krb5_context ctx;
+ krb5_error_code code;
+ krb5_principal princ;
+ const char *message;
+ char *wanted;
+
+ plan(6 * 3);
+
+ /* First, we have to get what the correct error message is. */
+ code = krb5_init_context(&ctx);
+ if (code < 0)
+ bail("cannot create context");
+ code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+ message = krb5_get_error_message(ctx, code);
+
+ xasprintf(&wanted, "principal parse failed: %s\n", message);
+ is_function_output(test_warn, 0, wanted, "warn_krb5");
+ is_function_output(test_die, 1, wanted, "die_krb5");
+ free(wanted);
+
+ message_program_name = "msg-test";
+ xasprintf(&wanted, "msg-test: principal parse failed: %s\n", message);
+ is_function_output(test_warn, 0, wanted, "warn_krb5 with name");
+ is_function_output(test_die, 1, wanted, "die_krb5 with name");
+ free(wanted);
+
+ message_handlers_warn(0);
+ is_function_output(test_warn, 0, "", "warn_krb5 with no handlers");
+ message_handlers_die(0);
+ is_function_output(test_die, 1, "", "warn_krb5 with no handlers");
+
+ return 0;
+}
diff --git a/tests/util/messages-t.c b/tests/util/messages-t.c
index 3f7860e..fb82a42 100644
--- a/tests/util/messages-t.c
+++ b/tests/util/messages-t.c
@@ -1,25 +1,14 @@
/*
* Test suite for error handling routines.
*
- * Copyright 2004, 2005, 2006
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009, 2010 Board of Trustees, Leland Stanford Jr. University
+ * Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
- * Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
- * 2003 by The Internet Software Consortium and Rich Salz
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ * 2002, 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
#include <config.h>
@@ -30,67 +19,11 @@
#include <sys/stat.h>
#include <sys/wait.h>
-#include <tests/libtest.h>
-#include <util/util.h>
-
-#define END (char *) 0
-
-/* Test function type. */
-typedef void (*test_function_t)(void);
-
-
-/*
- * Fork and execute the provided function, connecting stdout and stderr to a
- * pipe. Captures the output into the provided buffer and returns the exit
- * status as a waitpid status value.
- */
-static int
-run_test(test_function_t function, char *buf, size_t buflen)
-{
- int fds[2];
- pid_t child;
- ssize_t count, status;
- int rval;
-
- /* Flush stdout before we start to avoid odd forking issues. */
- fflush(stdout);
-
- /* Set up the pipe and call the function, collecting its output. */
- if (pipe(fds) == -1)
- sysdie("can't create pipe");
- child = fork();
- if (child == (pid_t) -1) {
- sysdie("can't fork");
- } else if (child == 0) {
- /* In child. Set up our stdout and stderr. */
- close(fds[0]);
- if (dup2(fds[1], 1) == -1)
- _exit(255);
- if (dup2(fds[1], 2) == -1)
- _exit(255);
-
- /* Now, run the function and exit successfully if it returns. */
- (*function)();
- fflush(stdout);
- _exit(0);
- } else {
- /*
- * In the parent; close the extra file descriptor, read the output if
- * any, and then collect the exit status.
- */
- close(fds[1]);
- count = 0;
- do {
- status = read(fds[0], buf + count, buflen - count - 1);
- if (status > 0)
- count += status;
- } while (status > 0);
- buf[count < 0 ? 0 : count] = '\0';
- if (waitpid(child, &rval, 0) == (pid_t) -1)
- sysdie("waitpid failed");
- }
- return rval;
-}
+#include <tests/tap/basic.h>
+#include <tests/tap/process.h>
+#include <util/concat.h>
+#include <util/messages.h>
+#include <util/xmalloc.h>
/*
@@ -203,43 +136,20 @@ static void test24(void) {
/*
- * Given the test number, intended exit status and message, and the function
- * to run, print ok or not ok.
- */
-static void
-test_error(int n, int status, const char *output, test_function_t function)
-{
- int real_status;
- char buf[256];
- int succeeded = 1;
-
- real_status = run_test(function, buf, sizeof(buf));
- if (!WIFEXITED(real_status) || status != WEXITSTATUS(real_status)) {
- printf(" unexpected exit status %d\n", real_status);
- succeeded = 0;
- }
- if (strcmp(output, buf)) {
- printf(" unexpected output: %s", buf);
- printf(" expected output: %s", output);
- succeeded = 0;
- }
- printf("%sok %d\n", succeeded ? "" : "not ", n);
-}
-
-
-/*
- * Given the test number, intended status, intended message sans the appended
- * strerror output, errno, and the function to run, print ok or not ok.
+ * Given the intended status, intended message sans the appended strerror
+ * output, errno, and the function to run, check the output.
*/
static void
-test_strerror(int n, int status, const char *output, int error,
- test_function_t function)
+test_strerror(int status, const char *output, int error,
+ test_function_type function)
{
- char *full_output;
+ char *full_output, *name;
- full_output = concat(output, ": ", strerror(error), "\n", END);
- test_error(n, status, full_output, function);
+ full_output = concat(output, ": ", strerror(error), "\n", (char *) NULL);
+ xasprintf(&name, "strerror %d", testnum / 3 + 1);
+ is_function_output(function, status, full_output, name);
free(full_output);
+ free(name);
}
@@ -250,46 +160,47 @@ int
main(void)
{
char buff[32];
-
- test_init(24);
-
- test_error(1, 0, "warning\n", test1);
- test_error(2, 1, "fatal\n", test2);
- test_strerror(3, 0, "permissions", EPERM, test3);
- test_strerror(4, 1, "fatal access", EACCES, test4);
- test_error(5, 0, "test5: warning\n", test5);
- test_error(6, 1, "test6: fatal\n", test6);
- test_strerror(7, 0, "test7: perms 7", EPERM, test7);
- test_strerror(8, 1, "test8: fatal", EACCES, test8);
- test_error(9, 10, "fatal\n", test9);
- test_strerror(10, 10, "fatal perm", EPERM, test10);
- test_strerror(11, 10, "1st test11: fatal", EPERM, test11);
- test_error(12, 0, "7 0 warning\n", test12);
- test_error(13, 1, "5 0 fatal\n", test13);
+ char *output;
+
+ plan(24 * 3);
+
+ is_function_output(test1, 0, "warning\n", "test1");
+ is_function_output(test2, 1, "fatal\n", "test2");
+ test_strerror(0, "permissions", EPERM, test3);
+ test_strerror(1, "fatal access", EACCES, test4);
+ is_function_output(test5, 0, "test5: warning\n", "test5");
+ is_function_output(test6, 1, "test6: fatal\n", "test6");
+ test_strerror(0, "test7: perms 7", EPERM, test7);
+ test_strerror(1, "test8: fatal", EACCES, test8);
+ is_function_output(test9, 10, "fatal\n", "test9");
+ test_strerror(10, "fatal perm", EPERM, test10);
+ test_strerror(10, "1st test11: fatal", EPERM, test11);
+ is_function_output(test12, 0, "7 0 warning\n", "test12");
+ is_function_output(test13, 1, "5 0 fatal\n", "test13");
sprintf(buff, "%d", EPERM);
- test_error(14, 0,
- concat("7 ", buff, " warning\n7 ", buff, " warning\n", END),
- test14);
- test_error(15, 10,
- concat("5 ", buff, " fatal\n5 ", buff, " fatal\n", END),
- test15);
- test_error(16, 0,
- concat("test16: warning: ", strerror(EPERM), "\n7 ", buff,
- " warning\n", END),
- test16);
-
- test_error(17, 0, "notice\n", test17);
- test_error(18, 0, "test18: notice\n", test18);
- test_error(19, 0, "", test19);
- test_error(20, 0, "3 0 foo\n", test20);
- test_error(21, 0, "test23: baz\n", test21);
+ xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM);
+ is_function_output(test14, 0, output, "test14");
+ free(output);
+ xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM);
+ is_function_output(test15, 10, output, "test15");
+ free(output);
+ xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM),
+ EPERM);
+ is_function_output(test16, 0, output, "test16");
+ free(output);
+
+ is_function_output(test17, 0, "notice\n", "test17");
+ is_function_output(test18, 0, "test18: notice\n", "test18");
+ is_function_output(test19, 0, "", "test19");
+ is_function_output(test20, 0, "3 0 foo\n", "test20");
+ is_function_output(test21, 0, "test23: baz\n", "test21");
/* Make sure that it's possible to turn off a message type entirely. */
- test_error(22, 1, "", test22);
- test_error(23, 0, "", test23);
- test_error(24, 0, "first\nthird\n", test24);
+ is_function_output(test22, 1, "", "test22");
+ is_function_output(test23, 0, "", "test23");
+ is_function_output(test24, 0, "first\nthird\n", "test24");
return 0;
}
diff --git a/tests/util/xmalloc-t b/tests/util/xmalloc-t
new file mode 100755
index 0000000..02f54b5
--- /dev/null
+++ b/tests/util/xmalloc-t
@@ -0,0 +1,127 @@
+#! /bin/sh
+#
+# Test suite for xmalloc and friends.
+#
+# Written by Russ Allbery <rra@stanford.edu>
+# Copyright 2008, 2009, 2010 Board of Trustees, Leland Stanford Jr. University
+# Copyright 2004, 2005, 2006
+# by Internet Systems Consortium, Inc. ("ISC")
+# Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+# 2003 by The Internet Software Consortium and Rich Salz
+#
+# See LICENSE for licensing terms.
+
+. "$SOURCE/tap/libtap.sh"
+cd "$BUILD/util"
+
+# Run an xmalloc test. Takes the description, the expectd exit status, the
+# output, and the arguments.
+ok_xmalloc () {
+ local desc w_status w_output output status
+ desc="$1"
+ shift
+ w_status="$1"
+ shift
+ w_output="$1"
+ shift
+ output=`./xmalloc "$@" 2>&1`
+ status=$?
+ if [ "$w_status" -ne 0 ] ; then
+ output=`echo "$output" | sed 's/:.*//'`
+ fi
+ if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
+ ok "$desc" true
+ elif [ $status = 2 ] ; then
+ skip "no data limit support"
+ else
+ echo "# saw: ($status) $output"
+ echo "# not: ($w_status) $w_output"
+ ok "$desc" false
+ fi
+}
+
+# Skip this test suite unless maintainer-mode tests are enabled. All of the
+# failures in automated testing have been problems with the assumptions around
+# memory allocation or problems with the test suite, not problems with the
+# underlying xmalloc code.
+if [ -z "$RRA_MAINTAINER_TESTS" ] ; then
+ skip_all 'xmalloc tests only run for maintainer'
+fi
+
+# Total tests.
+plan 36
+
+# First run the tests expected to succeed.
+ok_xmalloc "malloc small" 0 "" "m" "21" "0"
+ok_xmalloc "malloc large" 0 "" "m" "3500000" "0"
+ok_xmalloc "malloc zero" 0 "" "m" "0" "0"
+ok_xmalloc "realloc small" 0 "" "r" "21" "0"
+ok_xmalloc "realloc large" 0 "" "r" "3500000" "0"
+ok_xmalloc "strdup small" 0 "" "s" "21" "0"
+ok_xmalloc "strdup large" 0 "" "s" "3500000" "0"
+ok_xmalloc "strndup small" 0 "" "n" "21" "0"
+ok_xmalloc "strndup large" 0 "" "n" "3500000" "0"
+ok_xmalloc "calloc small" 0 "" "c" "24" "0"
+ok_xmalloc "calloc large" 0 "" "c" "3500000" "0"
+ok_xmalloc "asprintf small" 0 "" "a" "24" "0"
+ok_xmalloc "asprintf large" 0 "" "a" "3500000" "0"
+ok_xmalloc "vasprintf small" 0 "" "v" "24" "0"
+ok_xmalloc "vasprintf large" 0 "" "v" "3500000" "0"
+
+# Now limit our memory to 3.5MB and then try the large ones again, all of
+# which should fail.
+#
+# The exact memory limits used here are essentially black magic. They need to
+# be large enough to allow the program to be loaded and do small allocations,
+# but not so large that we can't reasonably expect to allocate that much
+# memory normally. 3.5MB seems to work reasonably well on both Solaris and
+# Linux.
+#
+# We assume that there are enough miscellaneous allocations that an allocation
+# exactly as large as the limit will always fail.
+ok_xmalloc "malloc fail" 1 \
+ "failed to malloc 3500000 bytes at xmalloc.c line 38" \
+ "m" "3500000" "3500000"
+ok_xmalloc "realloc fail" 1 \
+ "failed to realloc 3500000 bytes at xmalloc.c line 66" \
+ "r" "3500000" "3500000"
+ok_xmalloc "strdup fail" 1 \
+ "failed to strdup 3500000 bytes at xmalloc.c line 97" \
+ "s" "3500000" "3500000"
+ok_xmalloc "strndup fail" 1 \
+ "failed to strndup 3500000 bytes at xmalloc.c line 124" \
+ "n" "3500000" "3500000"
+ok_xmalloc "calloc fail" 1 \
+ "failed to calloc 3500000 bytes at xmalloc.c line 148" \
+ "c" "3500000" "3500000"
+ok_xmalloc "asprintf fail" 1 \
+ "failed to asprintf 3500000 bytes at xmalloc.c line 173" \
+ "a" "3500000" "3500000"
+ok_xmalloc "vasprintf fail" 1 \
+ "failed to vasprintf 3500000 bytes at xmalloc.c line 193" \
+ "v" "3500000" "3500000"
+
+# Check our custom error handler.
+ok_xmalloc "malloc custom" 1 "malloc 3500000 xmalloc.c 38" \
+ "M" "3500000" "3500000"
+ok_xmalloc "realloc custom" 1 "realloc 3500000 xmalloc.c 66" \
+ "R" "3500000" "3500000"
+ok_xmalloc "strdup custom" 1 "strdup 3500000 xmalloc.c 97" \
+ "S" "3500000" "3500000"
+ok_xmalloc "strndup custom" 1 "strndup 3500000 xmalloc.c 124" \
+ "N" "3500000" "3500000"
+ok_xmalloc "calloc custom" 1 "calloc 3500000 xmalloc.c 148" \
+ "C" "3500000" "3500000"
+ok_xmalloc "asprintf custom" 1 "asprintf 3500000 xmalloc.c 173" \
+ "A" "3500000" "3500000"
+ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 193" \
+ "V" "3500000" "3500000"
+
+# Check the smaller ones again just for grins.
+ok_xmalloc "malloc retry" 0 "" "m" "21" "3500000"
+ok_xmalloc "realloc retry" 0 "" "r" "32" "3500000"
+ok_xmalloc "strdup retry" 0 "" "s" "64" "3500000"
+ok_xmalloc "strndup retry" 0 "" "n" "20" "3500000"
+ok_xmalloc "calloc retry" 0 "" "c" "24" "3500000"
+ok_xmalloc "asprintf retry" 0 "" "a" "30" "3500000"
+ok_xmalloc "vasprintf retry" 0 "" "v" "35" "3500000"
diff --git a/tests/util/xmalloc-t.in b/tests/util/xmalloc-t.in
deleted file mode 100644
index 5c18512..0000000
--- a/tests/util/xmalloc-t.in
+++ /dev/null
@@ -1,126 +0,0 @@
-#! /bin/sh
-#
-# Test suite for xmalloc and friends.
-#
-# Copyright 2004, 2005, 2006
-# by Internet Systems Consortium, Inc. ("ISC")
-# Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-# 2003 by The Internet Software Consortium and Rich Salz
-#
-# This code is derived from software contributed to the Internet Software
-# Consortium by Rich Salz.
-#
-# Permission to use, copy, modify, and distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-# PERFORMANCE OF THIS SOFTWARE.
-
-# The count starts at 1 and is updated each time ok is printed. printcount
-# takes "ok" or "not ok".
-count=1
-printcount () {
- echo "$1 $count $2"
- count=`expr $count + 1`
-}
-
-# Run a program expected to succeed, and print ok if it does.
-runsuccess () {
- output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
- status=$?
- if test $status = 0 && test -z "$output" ; then
- printcount "ok"
- else
- if test $status = 2 ; then
- printcount "ok" "# skip - no data limit support"
- else
- printcount "not ok"
- echo " $output"
- fi
- fi
-}
-
-# Run a program expected to fail and make sure it fails with an exit status
-# of 2 and the right failure message. Strip the colon and everything after
-# it off the error message since it's system-specific.
-runfailure () {
- output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
- status=$?
- output=`echo "$output" | sed 's/:.*//' \
- | sed 's% [^ ]*/xmalloc.c% xmalloc.c%'`
- if test $status = 1 && test x"$output" = x"$4" ; then
- printcount "ok"
- else
- if test $status = 2 ; then
- printcount "ok" "# skip - no data limit support"
- else
- printcount "not ok"
- echo " saw: $output"
- echo " not: $4"
- fi
- fi
-}
-
-# Find where the helper program is.
-xmalloc="@abs_top_builddir@/tests/util/xmalloc"
-
-# Total tests.
-echo 36
-
-# First run the tests expected to succeed.
-runsuccess "m" "21" "0"
-runsuccess "m" "128000" "0"
-runsuccess "m" "0" "0"
-runsuccess "r" "21" "0"
-runsuccess "r" "128000" "0"
-runsuccess "s" "21" "0"
-runsuccess "s" "128000" "0"
-runsuccess "n" "21" "0"
-runsuccess "n" "128000" "0"
-runsuccess "c" "24" "0"
-runsuccess "c" "128000" "0"
-runsuccess "a" "24" "0"
-runsuccess "a" "128000" "0"
-runsuccess "v" "24" "0"
-runsuccess "v" "128000" "0"
-
-# Now limit our memory to 120KB and then try the large ones again, all of
-# which should fail.
-runfailure "m" "128000" "120000" \
- "failed to malloc 128000 bytes at xmalloc.c line 61"
-runfailure "r" "128000" "120000" \
- "failed to realloc 128000 bytes at xmalloc.c line 90"
-runfailure "s" "64000" "120000" \
- "failed to strdup 64000 bytes at xmalloc.c line 121"
-runfailure "n" "64000" "120000" \
- "failed to strndup 64000 bytes at xmalloc.c line 148"
-runfailure "c" "128000" "120000" \
- "failed to calloc 128000 bytes at xmalloc.c line 172"
-runfailure "a" "64000" "120000" \
- "failed to asprintf 64000 bytes at xmalloc.c line 241"
-runfailure "v" "64000" "120000" \
- "failed to vasprintf 64000 bytes at xmalloc.c line 217"
-
-# Check our custom error handler.
-runfailure "M" "128000" "120000" "malloc 128000 xmalloc.c 61"
-runfailure "R" "128000" "120000" "realloc 128000 xmalloc.c 90"
-runfailure "S" "64000" "120000" "strdup 64000 xmalloc.c 121"
-runfailure "N" "64000" "120000" "strndup 64000 xmalloc.c 148"
-runfailure "C" "128000" "120000" "calloc 128000 xmalloc.c 172"
-runfailure "A" "64000" "120000" "asprintf 64000 xmalloc.c 241"
-runfailure "V" "64000" "120000" "vasprintf 64000 xmalloc.c 217"
-
-# Check the smaller ones again just for grins.
-runsuccess "m" "21" "96000"
-runsuccess "r" "32" "96000"
-runsuccess "s" "64" "96000"
-runsuccess "n" "20" "96000"
-runsuccess "c" "24" "96000"
-runsuccess "a" "30" "96000"
-runsuccess "v" "35" "96000"
diff --git a/tests/util/xmalloc.c b/tests/util/xmalloc.c
index bd0ab62..3bd5588 100644
--- a/tests/util/xmalloc.c
+++ b/tests/util/xmalloc.c
@@ -1,27 +1,17 @@
/*
* Test suite for xmalloc and family.
*
+ * Copyright 2008 Board of Trustees, Leland Stanford Jr. University
* Copyright 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
* 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
+#line 1 "xmalloc.c"
+
#include <config.h>
#include <portable/system.h>
@@ -32,7 +22,8 @@
/* Linux requires sys/time.h be included before sys/resource.h. */
#include <sys/resource.h>
-#include <util/util.h>
+#include <util/messages.h>
+#include <util/xmalloc.h>
/*
@@ -81,20 +72,19 @@ test_realloc(size_t size)
char *buffer;
size_t i;
- buffer = xmalloc(size / 2);
+ buffer = xmalloc(10);
if (buffer == NULL)
return 0;
- if (size / 2 > 0)
- memset(buffer, 1, size / 2);
+ memset(buffer, 1, 10);
buffer = xrealloc(buffer, size);
if (buffer == NULL)
return 0;
if (size > 0)
- memset(buffer + size / 2, 2, size - size / 2);
- for (i = 0; i < size / 2; i++)
+ memset(buffer + 10, 2, size - 10);
+ for (i = 0; i < 10; i++)
if (buffer[i] != 1)
return 0;
- for (i = size / 2; i < size; i++)
+ for (i = 10; i < size; i++)
if (buffer[i] != 2)
return 0;
free(buffer);
@@ -257,6 +247,7 @@ main(int argc, char *argv[])
int willfail = 0;
unsigned char code;
struct rlimit rl;
+ void *tmp;
if (argc < 3)
die("Usage error. Type, size, and limit must be given.");
@@ -269,6 +260,27 @@ main(int argc, char *argv[])
if (limit == 0 && errno != 0)
sysdie("Invalid limit");
+ /* If the code is capitalized, install our customized error handler. */
+ code = argv[1][0];
+ if (isupper(code)) {
+ xmalloc_error_handler = test_handler;
+ code = tolower(code);
+ }
+
+ /*
+ * Decide if the allocation should fail. If it should, set willfail to 2,
+ * so that if it unexpectedly succeeds, we exit with a status indicating
+ * that the test should be skipped.
+ */
+ max = size;
+ if (code == 's' || code == 'n' || code == 'a' || code == 'v') {
+ max += size;
+ if (limit > 0)
+ limit += size;
+ }
+ if (limit > 0 && max > limit)
+ willfail = 2;
+
/*
* If a memory limit was given and we can set memory limits, set it.
* Otherwise, exit 2, signalling to the driver that the test should be
@@ -277,37 +289,28 @@ main(int argc, char *argv[])
* the shell to die).
*/
if (limit > 0) {
-#if HAVE_SETRLIMIT && defined(RLIMIT_DATA)
+#if HAVE_SETRLIMIT && defined(RLIMIT_AS)
rl.rlim_cur = limit;
rl.rlim_max = limit;
- if (setrlimit(RLIMIT_DATA, &rl) < 0) {
+ if (setrlimit(RLIMIT_AS, &rl) < 0) {
syswarn("Can't set data limit to %lu", (unsigned long) limit);
exit(2);
}
+ if (size < limit || code == 'r') {
+ tmp = malloc(code == 'r' ? 10 : size);
+ if (tmp == NULL) {
+ syswarn("Can't allocate initial memory of %lu",
+ (unsigned long) size);
+ exit(2);
+ }
+ free(tmp);
+ }
#else
warn("Data limits aren't supported.");
exit(2);
#endif
}
- /* If the code is capitalized, install our customized error handler. */
- code = argv[1][0];
- if (isupper(code)) {
- xmalloc_error_handler = test_handler;
- code = tolower(code);
- }
-
- /*
- * Decide if the allocation should fail. If it should, set willfail to 2,
- * so that if it unexpectedly succeeds, we exit with a status indicating
- * that the test should be skipped.
- */
- max = size;
- if (code == 's' || code == 'n' || code == 'a' || code == 'v')
- max *= 2;
- if (limit > 0 && max > limit)
- willfail = 2;
-
switch (code) {
case 'c': exit(test_calloc(size) ? willfail : 1);
case 'm': exit(test_malloc(size) ? willfail : 1);