]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/xmalloc-t
Change my email address to eagle@eyrie.org
[kerberos/krb5-strength.git] / tests / util / xmalloc-t
1 #! /bin/sh
2 #
3 # Test suite for xmalloc and friends.
4 #
5 # The canonical version of this file is maintained in the rra-c-util package,
6 # which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
7 #
8 # Written by Russ Allbery <eagle@eyrie.org>
9 # Copyright 2000, 2001, 2006 Russ Allbery <eagle@eyrie.org>
10 # Copyright 2008, 2009, 2010, 2012
11 #     The Board of Trustees of the Leland Stanford Junior University
12 #
13 # Permission is hereby granted, free of charge, to any person obtaining a
14 # copy of this software and associated documentation files (the "Software"),
15 # to deal in the Software without restriction, including without limitation
16 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 # and/or sell copies of the Software, and to permit persons to whom the
18 # Software is furnished to do so, subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be included in
21 # all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 # DEALINGS IN THE SOFTWARE.
30
31 . "$SOURCE/tap/libtap.sh"
32 cd "$BUILD/util"
33
34 # Run an xmalloc test.  Takes the description, the expectd exit status, the
35 # output, and the arguments.
36 ok_xmalloc () {
37     local desc w_status w_output output status
38     desc="$1"
39     shift
40     w_status="$1"
41     shift
42     w_output="$1"
43     shift
44     output=`strip_colon_error ./xmalloc "$@" 2>&1`
45     status=$?
46     if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
47         ok "$desc" true
48     elif [ $status = 2 ] ; then
49         diag "$output"
50         skip "no data limit support"
51     else
52         diag "saw: ($status) $output"
53         diag "not: ($w_status) $w_output"
54         ok "$desc" false
55     fi
56 }
57
58 # Skip this test suite unless maintainer-mode tests are enabled.  All of the
59 # failures in automated testing have been problems with the assumptions around
60 # memory allocation or problems with the test suite, not problems with the
61 # underlying xmalloc code.
62 if [ -z "$RRA_MAINTAINER_TESTS" ] ; then
63     skip_all 'xmalloc tests only run for maintainer'
64 fi
65
66 # Total tests.
67 plan 36
68
69 # First run the tests expected to succeed.
70 ok_xmalloc "malloc small"    0 "" "m" "21"      "0"
71 ok_xmalloc "malloc large"    0 "" "m" "5500000" "0"
72 ok_xmalloc "malloc zero"     0 "" "m" "0"       "0"
73 ok_xmalloc "realloc small"   0 "" "r" "21"      "0"
74 ok_xmalloc "realloc large"   0 "" "r" "5500000" "0"
75 ok_xmalloc "strdup small"    0 "" "s" "21"      "0"
76 ok_xmalloc "strdup large"    0 "" "s" "5500000" "0"
77 ok_xmalloc "strndup small"   0 "" "n" "21"      "0"
78 ok_xmalloc "strndup large"   0 "" "n" "5500000" "0"
79 ok_xmalloc "calloc small"    0 "" "c" "24"      "0"
80 ok_xmalloc "calloc large"    0 "" "c" "5500000" "0"
81 ok_xmalloc "asprintf small"  0 "" "a" "24"      "0"
82 ok_xmalloc "asprintf large"  0 "" "a" "5500000" "0"
83 ok_xmalloc "vasprintf small" 0 "" "v" "24"      "0"
84 ok_xmalloc "vasprintf large" 0 "" "v" "5500000" "0"
85
86 # Now limit our memory to 5.5MB and then try the large ones again, all of
87 # which should fail.
88 #
89 # The exact memory limits used here are essentially black magic.  They need to
90 # be large enough to allow the program to be loaded and do small allocations,
91 # but not so large that we can't reasonably expect to allocate that much
92 # memory normally.  The amount of memory required varies a lot based on what
93 # shared libraries are loaded, and if it's too small, all memory allocations
94 # fail.  5.5MB seems to work reasonably well on both Solaris and Linux.
95 #
96 # We assume that there are enough miscellaneous allocations that an allocation
97 # exactly as large as the limit will always fail.
98 ok_xmalloc "malloc fail" 1 \
99     "failed to malloc 5500000 bytes at xmalloc.c line 38" \
100     "m" "5500000" "5500000"
101 ok_xmalloc "realloc fail" 1 \
102     "failed to realloc 5500000 bytes at xmalloc.c line 66" \
103     "r" "5500000" "5500000"
104 ok_xmalloc "strdup fail" 1 \
105     "failed to strdup 5500000 bytes at xmalloc.c line 97" \
106     "s" "5500000" "5500000"
107 ok_xmalloc "strndup fail" 1 \
108     "failed to strndup 5500000 bytes at xmalloc.c line 143" \
109     "n" "5500000" "5500000"
110 ok_xmalloc "calloc fail" 1 \
111     "failed to calloc 5500000 bytes at xmalloc.c line 167" \
112     "c" "5500000" "5500000"
113 ok_xmalloc "asprintf fail" 1 \
114     "failed to asprintf 5500000 bytes at xmalloc.c line 191" \
115     "a" "5500000" "5500000"
116 ok_xmalloc "vasprintf fail" 1 \
117     "failed to vasprintf 5500000 bytes at xmalloc.c line 210" \
118     "v" "5500000" "5500000"
119
120 # Check our custom error handler.
121 ok_xmalloc "malloc custom"    1 "malloc 5500000 xmalloc.c 38" \
122     "M" "5500000" "5500000"
123 ok_xmalloc "realloc custom"   1 "realloc 5500000 xmalloc.c 66" \
124     "R" "5500000" "5500000"
125 ok_xmalloc "strdup custom"    1 "strdup 5500000 xmalloc.c 97" \
126     "S" "5500000" "5500000"
127 ok_xmalloc "strndup custom"   1 "strndup 5500000 xmalloc.c 143" \
128     "N" "5500000" "5500000"
129 ok_xmalloc "calloc custom"    1 "calloc 5500000 xmalloc.c 167" \
130     "C" "5500000" "5500000"
131 ok_xmalloc "asprintf custom"  1 "asprintf 5500000 xmalloc.c 191" \
132     "A" "5500000" "5500000"
133 ok_xmalloc "vasprintf custom" 1 "vasprintf 5500000 xmalloc.c 210" \
134     "V" "5500000" "5500000"
135
136 # Check the smaller ones again just for grins.
137 ok_xmalloc "malloc retry"    0 "" "m" "21" "5500000"
138 ok_xmalloc "realloc retry"   0 "" "r" "32" "5500000"
139 ok_xmalloc "strdup retry"    0 "" "s" "64" "5500000"
140 ok_xmalloc "strndup retry"   0 "" "n" "20" "5500000"
141 ok_xmalloc "calloc retry"    0 "" "c" "24" "5500000"
142 ok_xmalloc "asprintf retry"  0 "" "a" "30" "5500000"
143 ok_xmalloc "vasprintf retry" 0 "" "v" "35" "5500000"