1 | #! /bin/sh |
---|
2 | # |
---|
3 | # Test suite for xmalloc and friends. |
---|
4 | # |
---|
5 | # Written by Russ Allbery <rra@stanford.edu> |
---|
6 | # Copyright 2008, 2009 Board of Trustees, Leland Stanford Jr. University |
---|
7 | # Copyright 2004, 2005, 2006 |
---|
8 | # by Internet Systems Consortium, Inc. ("ISC") |
---|
9 | # Copyright 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
---|
10 | # 2003 by The Internet Software Consortium and Rich Salz |
---|
11 | # |
---|
12 | # See LICENSE for licensing terms. |
---|
13 | |
---|
14 | . "$SOURCE/tap/libtap.sh" |
---|
15 | cd "$BUILD/util" |
---|
16 | |
---|
17 | # Run an xmalloc test. Takes the description, the expectd exit status, the |
---|
18 | # output, and the arguments. |
---|
19 | ok_xmalloc () { |
---|
20 | local desc w_status w_output output status |
---|
21 | desc="$1" |
---|
22 | shift |
---|
23 | w_status="$1" |
---|
24 | shift |
---|
25 | w_output="$1" |
---|
26 | shift |
---|
27 | output=`./xmalloc "$@" 2>&1` |
---|
28 | status=$? |
---|
29 | if [ "$w_status" -ne 0 ] ; then |
---|
30 | output=`echo "$output" | sed 's/:.*//'` |
---|
31 | fi |
---|
32 | if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then |
---|
33 | ok "$desc" true |
---|
34 | elif [ $status = 2 ] ; then |
---|
35 | skip "no data limit support" |
---|
36 | else |
---|
37 | echo "# saw: ($status) $output" |
---|
38 | echo "# not: ($w_status) $w_output" |
---|
39 | ok "$desc" false |
---|
40 | fi |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | # Total tests. |
---|
45 | plan 36 |
---|
46 | |
---|
47 | # First run the tests expected to succeed. |
---|
48 | ok_xmalloc "malloc small" 0 "" "m" "21" "0" |
---|
49 | ok_xmalloc "malloc large" 0 "" "m" "3500000" "0" |
---|
50 | ok_xmalloc "malloc zero" 0 "" "m" "0" "0" |
---|
51 | ok_xmalloc "realloc small" 0 "" "r" "21" "0" |
---|
52 | ok_xmalloc "realloc large" 0 "" "r" "3500000" "0" |
---|
53 | ok_xmalloc "strdup small" 0 "" "s" "21" "0" |
---|
54 | ok_xmalloc "strdup large" 0 "" "s" "3500000" "0" |
---|
55 | ok_xmalloc "strndup small" 0 "" "n" "21" "0" |
---|
56 | ok_xmalloc "strndup large" 0 "" "n" "3500000" "0" |
---|
57 | ok_xmalloc "calloc small" 0 "" "c" "24" "0" |
---|
58 | ok_xmalloc "calloc large" 0 "" "c" "3500000" "0" |
---|
59 | ok_xmalloc "asprintf small" 0 "" "a" "24" "0" |
---|
60 | ok_xmalloc "asprintf large" 0 "" "a" "3500000" "0" |
---|
61 | ok_xmalloc "vasprintf small" 0 "" "v" "24" "0" |
---|
62 | ok_xmalloc "vasprintf large" 0 "" "v" "3500000" "0" |
---|
63 | |
---|
64 | # Now limit our memory to 3.5MB and then try the large ones again, all of |
---|
65 | # which should fail. |
---|
66 | # |
---|
67 | # The exact memory limits used here are essentially black magic. They need to |
---|
68 | # be large enough to allow the program to be loaded and do small allocations, |
---|
69 | # but not so large that we can't reasonably expect to allocate that much |
---|
70 | # memory normally. 3.5MB seems to work reasonably well on both Solaris and |
---|
71 | # Linux. |
---|
72 | # |
---|
73 | # We assume that there are enough miscellaneous allocations that an allocation |
---|
74 | # exactly as large as the limit will always fail. |
---|
75 | ok_xmalloc "malloc fail" 1 \ |
---|
76 | "failed to malloc 3500000 bytes at xmalloc.c line 37" \ |
---|
77 | "m" "3500000" "3500000" |
---|
78 | ok_xmalloc "realloc fail" 1 \ |
---|
79 | "failed to realloc 3500000 bytes at xmalloc.c line 65" \ |
---|
80 | "r" "3500000" "3500000" |
---|
81 | ok_xmalloc "strdup fail" 1 \ |
---|
82 | "failed to strdup 3500000 bytes at xmalloc.c line 96" \ |
---|
83 | "s" "3500000" "3500000" |
---|
84 | ok_xmalloc "strndup fail" 1 \ |
---|
85 | "failed to strndup 3500000 bytes at xmalloc.c line 123" \ |
---|
86 | "n" "3500000" "3500000" |
---|
87 | ok_xmalloc "calloc fail" 1 \ |
---|
88 | "failed to calloc 3500000 bytes at xmalloc.c line 147" \ |
---|
89 | "c" "3500000" "3500000" |
---|
90 | ok_xmalloc "asprintf fail" 1 \ |
---|
91 | "failed to asprintf 3500000 bytes at xmalloc.c line 172" \ |
---|
92 | "a" "3500000" "3500000" |
---|
93 | ok_xmalloc "vasprintf fail" 1 \ |
---|
94 | "failed to vasprintf 3500000 bytes at xmalloc.c line 192" \ |
---|
95 | "v" "3500000" "3500000" |
---|
96 | |
---|
97 | # Check our custom error handler. |
---|
98 | ok_xmalloc "malloc custom" 1 "malloc 3500000 xmalloc.c 37" \ |
---|
99 | "M" "3500000" "3500000" |
---|
100 | ok_xmalloc "realloc custom" 1 "realloc 3500000 xmalloc.c 65" \ |
---|
101 | "R" "3500000" "3500000" |
---|
102 | ok_xmalloc "strdup custom" 1 "strdup 3500000 xmalloc.c 96" \ |
---|
103 | "S" "3500000" "3500000" |
---|
104 | ok_xmalloc "strndup custom" 1 "strndup 3500000 xmalloc.c 123" \ |
---|
105 | "N" "3500000" "3500000" |
---|
106 | ok_xmalloc "calloc custom" 1 "calloc 3500000 xmalloc.c 147" \ |
---|
107 | "C" "3500000" "3500000" |
---|
108 | ok_xmalloc "asprintf custom" 1 "asprintf 3500000 xmalloc.c 172" \ |
---|
109 | "A" "3500000" "3500000" |
---|
110 | ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 192" \ |
---|
111 | "V" "3500000" "3500000" |
---|
112 | |
---|
113 | # Check the smaller ones again just for grins. |
---|
114 | ok_xmalloc "malloc retry" 0 "" "m" "21" "3500000" |
---|
115 | ok_xmalloc "realloc retry" 0 "" "r" "32" "3500000" |
---|
116 | ok_xmalloc "strdup retry" 0 "" "s" "64" "3500000" |
---|
117 | ok_xmalloc "strndup retry" 0 "" "n" "20" "3500000" |
---|
118 | ok_xmalloc "calloc retry" 0 "" "c" "24" "3500000" |
---|
119 | ok_xmalloc "asprintf retry" 0 "" "a" "30" "3500000" |
---|
120 | ok_xmalloc "vasprintf retry" 0 "" "v" "35" "3500000" |
---|