diff options
Diffstat (limited to 'tests/util')
-rw-r--r-- | tests/util/messages-krb5-t.c | 41 | ||||
-rw-r--r-- | tests/util/messages-t.c | 126 | ||||
-rwxr-xr-x | tests/util/xmalloc-t | 130 | ||||
-rw-r--r-- | tests/util/xmalloc.c | 72 |
4 files changed, 230 insertions, 139 deletions
diff --git a/tests/util/messages-krb5-t.c b/tests/util/messages-krb5-t.c index 02d8f92..e3ffe75 100644 --- a/tests/util/messages-krb5-t.c +++ b/tests/util/messages-krb5-t.c @@ -1,10 +1,30 @@ /* * Test suite for Kerberos error handling routines. * + * The canonical version of this file is maintained in the rra-c-util package, + * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>. + * * Written by Russ Allbery <rra@stanford.edu> - * Copyright 2010 Board of Trustees, Leland Stanford Jr. University + * Copyright 2010, 2011 + * The Board of Trustees of the Leland Stanford Junior University + * + * 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 shall be included in + * all copies or substantial portions of the Software. * - * See LICENSE for licensing terms. + * 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 <config.h> @@ -13,6 +33,7 @@ #include <tests/tap/basic.h> #include <tests/tap/process.h> +#include <util/macros.h> #include <util/messages-krb5.h> #include <util/messages.h> #include <util/xmalloc.h> @@ -22,7 +43,7 @@ * Test functions. */ static void -test_warn(void) +test_warn(void *data UNUSED) { krb5_context ctx; krb5_error_code code; @@ -40,7 +61,7 @@ test_warn(void) } static void -test_die(void) +test_die(void *data UNUSED) { krb5_context ctx; krb5_error_code code; @@ -80,20 +101,20 @@ main(void) 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"); + is_function_output(test_warn, NULL, 0, wanted, "warn_krb5"); + is_function_output(test_die, NULL, 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"); + is_function_output(test_warn, NULL, 0, wanted, "warn_krb5 with name"); + is_function_output(test_die, NULL, 1, wanted, "die_krb5 with name"); free(wanted); message_handlers_warn(0); - is_function_output(test_warn, 0, "", "warn_krb5 with no handlers"); + is_function_output(test_warn, NULL, 0, "", "warn_krb5 with no handlers"); message_handlers_die(0); - is_function_output(test_die, 1, "", "warn_krb5 with no handlers"); + is_function_output(test_die, NULL, 1, "", "warn_krb5 with no handlers"); return 0; } diff --git a/tests/util/messages-t.c b/tests/util/messages-t.c index a58f82c..54f1cf1 100644 --- a/tests/util/messages-t.c +++ b/tests/util/messages-t.c @@ -1,14 +1,31 @@ /* * Test suite for error handling routines. * + * The canonical version of this file is maintained in the rra-c-util package, + * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>. + * * 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 (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, - * 2002, 2003 by The Internet Software Consortium and Rich Salz + * Copyright 2002, 2004, 2005 Russ Allbery <rra@stanford.edu> + * Copyright 2009, 2010, 2011, 2012 + * The Board of Trustees of the Leland Stanford Junior University + * + * 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 shall be included in + * all copies or substantial portions of the Software. * - * See LICENSE for licensing terms. + * 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 <config.h> @@ -21,7 +38,7 @@ #include <tests/tap/basic.h> #include <tests/tap/process.h> -#include <util/concat.h> +#include <util/macros.h> #include <util/messages.h> #include <util/xmalloc.h> @@ -29,24 +46,27 @@ /* * Test functions. */ -static void test1(void) { warn("warning"); } -static void test2(void) { die("fatal"); } -static void test3(void) { errno = EPERM; syswarn("permissions"); } -static void test4(void) { errno = EACCES; sysdie("fatal access"); } -static void test5(void) { +static void test1(void *data UNUSED) { warn("warning"); } +static void test2(void *data UNUSED) { die("fatal"); } +static void test3(void *data UNUSED) { errno = EPERM; syswarn("permissions"); } +static void test4(void *data UNUSED) { + errno = EACCES; + sysdie("fatal access"); +} +static void test5(void *data UNUSED) { message_program_name = "test5"; warn("warning"); } -static void test6(void) { +static void test6(void *data UNUSED) { message_program_name = "test6"; die("fatal"); } -static void test7(void) { +static void test7(void *data UNUSED) { message_program_name = "test7"; errno = EPERM; syswarn("perms %d", 7); } -static void test8(void) { +static void test8(void *data UNUSED) { message_program_name = "test8"; errno = EACCES; sysdie("%st%s", "fa", "al"); @@ -54,17 +74,17 @@ static void test8(void) { static int return10(void) { return 10; } -static void test9(void) { +static void test9(void *data UNUSED) { message_fatal_cleanup = return10; die("fatal"); } -static void test10(void) { +static void test10(void *data UNUSED) { message_program_name = 0; message_fatal_cleanup = return10; errno = EPERM; sysdie("fatal perm"); } -static void test11(void) { +static void test11(void *data UNUSED) { message_program_name = "test11"; message_fatal_cleanup = return10; errno = EPERM; @@ -72,61 +92,61 @@ static void test11(void) { sysdie("fatal"); } -static void log_msg(int len, const char *format, va_list args, int error) { - fprintf(stderr, "%d %d ", len, error); +static void log_msg(size_t len, const char *format, va_list args, int error) { + fprintf(stderr, "%lu %d ", (unsigned long) len, error); vfprintf(stderr, format, args); fprintf(stderr, "\n"); } -static void test12(void) { +static void test12(void *data UNUSED) { message_handlers_warn(1, log_msg); warn("warning"); } -static void test13(void) { +static void test13(void *data UNUSED) { message_handlers_die(1, log_msg); die("fatal"); } -static void test14(void) { +static void test14(void *data UNUSED) { message_handlers_warn(2, log_msg, log_msg); errno = EPERM; syswarn("warning"); } -static void test15(void) { +static void test15(void *data UNUSED) { message_handlers_die(2, log_msg, log_msg); message_fatal_cleanup = return10; errno = EPERM; sysdie("fatal"); } -static void test16(void) { +static void test16(void *data UNUSED) { message_handlers_warn(2, message_log_stderr, log_msg); message_program_name = "test16"; errno = EPERM; syswarn("warning"); } -static void test17(void) { notice("notice"); } -static void test18(void) { +static void test17(void *data UNUSED) { notice("notice"); } +static void test18(void *data UNUSED) { message_program_name = "test18"; notice("notice"); } -static void test19(void) { debug("debug"); } -static void test20(void) { +static void test19(void *data UNUSED) { debug("debug"); } +static void test20(void *data UNUSED) { message_handlers_notice(1, log_msg); notice("foo"); } -static void test21(void) { +static void test21(void *data UNUSED) { message_handlers_debug(1, message_log_stdout); message_program_name = "test23"; debug("baz"); } -static void test22(void) { +static void test22(void *data UNUSED) { message_handlers_die(0); die("hi mom!"); } -static void test23(void) { +static void test23(void *data UNUSED) { message_handlers_warn(0); warn("this is a test"); } -static void test24(void) { +static void test24(void *data UNUSED) { notice("first"); message_handlers_notice(0); notice("second"); @@ -145,9 +165,9 @@ test_strerror(int status, const char *output, int error, { char *full_output, *name; - full_output = concat(output, ": ", strerror(error), "\n", (char *) NULL); + xasprintf(&full_output, "%s: %s\n", output, strerror(error)); xasprintf(&name, "strerror %lu", testnum / 3 + 1); - is_function_output(function, status, full_output, "%s", name); + is_function_output(function, NULL, status, full_output, "%s", name); free(full_output); free(name); } @@ -164,43 +184,43 @@ main(void) plan(24 * 3); - is_function_output(test1, 0, "warning\n", "test1"); - is_function_output(test2, 1, "fatal\n", "test2"); + is_function_output(test1, NULL, 0, "warning\n", "test1"); + is_function_output(test2, NULL, 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"); + is_function_output(test5, NULL, 0, "test5: warning\n", "test5"); + is_function_output(test6, NULL, 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"); + is_function_output(test9, NULL, 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"); + is_function_output(test12, NULL, 0, "7 0 warning\n", "test12"); + is_function_output(test13, NULL, 1, "5 0 fatal\n", "test13"); sprintf(buff, "%d", EPERM); xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM); - is_function_output(test14, 0, output, "test14"); + is_function_output(test14, NULL, 0, output, "test14"); free(output); xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM); - is_function_output(test15, 10, output, "test15"); + is_function_output(test15, NULL, 10, output, "test15"); free(output); xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM), EPERM); - is_function_output(test16, 0, output, "test16"); + is_function_output(test16, NULL, 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"); + is_function_output(test17, NULL, 0, "notice\n", "test17"); + is_function_output(test18, NULL, 0, "test18: notice\n", "test18"); + is_function_output(test19, NULL, 0, "", "test19"); + is_function_output(test20, NULL, 0, "3 0 foo\n", "test20"); + is_function_output(test21, NULL, 0, "test23: baz\n", "test21"); /* Make sure that it's possible to turn off a message type entirely. */ - is_function_output(test22, 1, "", "test22"); - is_function_output(test23, 0, "", "test23"); - is_function_output(test24, 0, "first\nthird\n", "test24"); + is_function_output(test22, NULL, 1, "", "test22"); + is_function_output(test23, NULL, 0, "", "test23"); + is_function_output(test24, NULL, 0, "first\nthird\n", "test24"); return 0; } diff --git a/tests/util/xmalloc-t b/tests/util/xmalloc-t index 67d95f6..b6c6dfd 100755 --- a/tests/util/xmalloc-t +++ b/tests/util/xmalloc-t @@ -2,14 +2,31 @@ # # Test suite for xmalloc and friends. # +# The canonical version of this file is maintained in the rra-c-util package, +# which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>. +# # 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 +# Copyright 2000, 2001, 2006 Russ Allbery <rra@stanford.edu> +# Copyright 2008, 2009, 2010, 2012 +# The Board of Trustees of the Leland Stanford Junior University +# +# 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: # -# See LICENSE for licensing terms. +# The above copyright notice and this permission notice 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. . "$SOURCE/tap/libtap.sh" cd "$BUILD/util" @@ -24,18 +41,16 @@ ok_xmalloc () { shift w_output="$1" shift - output=`./xmalloc "$@" 2>&1` + output=`strip_colon_error ./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 + diag "$output" skip "no data limit support" else - echo "# saw: ($status) $output" - echo "# not: ($w_status) $w_output" + diag "saw: ($status) $output" + diag "not: ($w_status) $w_output" ok "$desc" false fi } @@ -53,75 +68,76 @@ 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 large" 0 "" "m" "5500000" "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 "realloc large" 0 "" "r" "5500000" "0" ok_xmalloc "strdup small" 0 "" "s" "21" "0" -ok_xmalloc "strdup large" 0 "" "s" "3500000" "0" +ok_xmalloc "strdup large" 0 "" "s" "5500000" "0" ok_xmalloc "strndup small" 0 "" "n" "21" "0" -ok_xmalloc "strndup large" 0 "" "n" "3500000" "0" +ok_xmalloc "strndup large" 0 "" "n" "5500000" "0" ok_xmalloc "calloc small" 0 "" "c" "24" "0" -ok_xmalloc "calloc large" 0 "" "c" "3500000" "0" +ok_xmalloc "calloc large" 0 "" "c" "5500000" "0" ok_xmalloc "asprintf small" 0 "" "a" "24" "0" -ok_xmalloc "asprintf large" 0 "" "a" "3500000" "0" +ok_xmalloc "asprintf large" 0 "" "a" "5500000" "0" ok_xmalloc "vasprintf small" 0 "" "v" "24" "0" -ok_xmalloc "vasprintf large" 0 "" "v" "3500000" "0" +ok_xmalloc "vasprintf large" 0 "" "v" "5500000" "0" -# Now limit our memory to 3.5MB and then try the large ones again, all of +# Now limit our memory to 5.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. +# memory normally. The amount of memory required varies a lot based on what +# shared libraries are loaded, and if it's too small, all memory allocations +# fail. 5.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" + "failed to malloc 5500000 bytes at xmalloc.c line 38" \ + "m" "5500000" "5500000" ok_xmalloc "realloc fail" 1 \ - "failed to realloc 3500000 bytes at xmalloc.c line 66" \ - "r" "3500000" "3500000" + "failed to realloc 5500000 bytes at xmalloc.c line 66" \ + "r" "5500000" "5500000" ok_xmalloc "strdup fail" 1 \ - "failed to strdup 3500000 bytes at xmalloc.c line 97" \ - "s" "3500000" "3500000" + "failed to strdup 5500000 bytes at xmalloc.c line 97" \ + "s" "5500000" "5500000" ok_xmalloc "strndup fail" 1 \ - "failed to strndup 3500000 bytes at xmalloc.c line 124" \ - "n" "3500000" "3500000" + "failed to strndup 5500000 bytes at xmalloc.c line 143" \ + "n" "5500000" "5500000" ok_xmalloc "calloc fail" 1 \ - "failed to calloc 3500000 bytes at xmalloc.c line 148" \ - "c" "3500000" "3500000" + "failed to calloc 5500000 bytes at xmalloc.c line 167" \ + "c" "5500000" "5500000" ok_xmalloc "asprintf fail" 1 \ - "failed to asprintf 3500000 bytes at xmalloc.c line 172" \ - "a" "3500000" "3500000" + "failed to asprintf 5500000 bytes at xmalloc.c line 191" \ + "a" "5500000" "5500000" ok_xmalloc "vasprintf fail" 1 \ - "failed to vasprintf 3500000 bytes at xmalloc.c line 192" \ - "v" "3500000" "3500000" + "failed to vasprintf 5500000 bytes at xmalloc.c line 210" \ + "v" "5500000" "5500000" # 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 172" \ - "A" "3500000" "3500000" -ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 192" \ - "V" "3500000" "3500000" +ok_xmalloc "malloc custom" 1 "malloc 5500000 xmalloc.c 38" \ + "M" "5500000" "5500000" +ok_xmalloc "realloc custom" 1 "realloc 5500000 xmalloc.c 66" \ + "R" "5500000" "5500000" +ok_xmalloc "strdup custom" 1 "strdup 5500000 xmalloc.c 97" \ + "S" "5500000" "5500000" +ok_xmalloc "strndup custom" 1 "strndup 5500000 xmalloc.c 143" \ + "N" "5500000" "5500000" +ok_xmalloc "calloc custom" 1 "calloc 5500000 xmalloc.c 167" \ + "C" "5500000" "5500000" +ok_xmalloc "asprintf custom" 1 "asprintf 5500000 xmalloc.c 191" \ + "A" "5500000" "5500000" +ok_xmalloc "vasprintf custom" 1 "vasprintf 5500000 xmalloc.c 210" \ + "V" "5500000" "5500000" # 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" +ok_xmalloc "malloc retry" 0 "" "m" "21" "5500000" +ok_xmalloc "realloc retry" 0 "" "r" "32" "5500000" +ok_xmalloc "strdup retry" 0 "" "s" "64" "5500000" +ok_xmalloc "strndup retry" 0 "" "n" "20" "5500000" +ok_xmalloc "calloc retry" 0 "" "c" "24" "5500000" +ok_xmalloc "asprintf retry" 0 "" "a" "30" "5500000" +ok_xmalloc "vasprintf retry" 0 "" "v" "35" "5500000" diff --git a/tests/util/xmalloc.c b/tests/util/xmalloc.c index c37396e..394cab5 100644 --- a/tests/util/xmalloc.c +++ b/tests/util/xmalloc.c @@ -1,13 +1,30 @@ /* * 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 + * The canonical version of this file is maintained in the rra-c-util package, + * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>. * - * See LICENSE for licensing terms. + * Copyright 2000, 2001, 2006 Russ Allbery <rra@stanford.edu> + * Copyright 2008, 2012 + * The Board of Trustees of the Leland Stanford Junior University + * + * 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 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. */ #line 1 "xmalloc.c" @@ -62,9 +79,9 @@ test_malloc(size_t size) /* - * Allocate half the memory given, write to it, then reallocate to the desired - * size, writing to the rest and then checking it all. Returns true on - * success, false on any failure. + * Allocate 10 bytes of memory given, write to it, then reallocate to the + * desired size, writing to the rest and then checking it all. Returns true + * on success, false on any failure. */ static int test_realloc(size_t size) @@ -119,15 +136,34 @@ test_strdup(size_t size) /* * Generate a string of the size indicated plus some, call xstrndup on it, and - * then ensure the result matches. Returns true on success, false on any - * failure. + * then ensure the result matches. Also test xstrdup on a string that's + * shorter than the specified size and ensure that we don't copy too much, and + * on a string that's not nul-terminated. Returns true on success, false on + * any failure. */ static int test_strndup(size_t size) { char *string, *copy; - int match, toomuch; + int shortmatch, nonulmatch, match, toomuch; + + /* Copy a short string. */ + string = xmalloc(5); + memcpy(string, "test", 5); + copy = xstrndup(string, size); + shortmatch = strcmp(string, copy); + free(string); + free(copy); + /* Copy a string that's not nul-terminated. */ + string = xmalloc(4); + memcpy(string, "test", 4); + copy = xstrndup(string, 4); + nonulmatch = strcmp(copy, "test"); + free(string); + free(copy); + + /* Now the test of running out of memory. */ string = xmalloc(size + 1); if (string == NULL) return 0; @@ -141,7 +177,7 @@ test_strndup(size_t size) toomuch = strcmp(string, copy); free(string); free(copy); - return (match == 0 && toomuch != 0); + return (shortmatch == 0 && nonulmatch == 0 && match == 0 && toomuch != 0); } @@ -195,16 +231,14 @@ test_asprintf(size_t size) /* Wrapper around vasprintf to do the va_list stuff. */ -static int +static void xvasprintf_wrapper(char **strp, const char *format, ...) { va_list args; - int status; va_start(args, format); - status = xvasprintf(strp, format, args); + xvasprintf(strp, format, args); va_end(args); - return status; } @@ -298,8 +332,8 @@ main(int argc, char *argv[]) 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); + syswarn("Can't allocate initial memory of %lu (limit %lu)", + (unsigned long) size, (unsigned long) limit); exit(2); } free(tmp); |