t_input.sh revision 1.1 1 1.1 kre # $NetBSD: t_input.sh,v 1.1 2021/02/16 09:46:24 kre Exp $
2 1.1 kre #
3 1.1 kre # Copyright (c) 2021 The NetBSD Foundation, Inc.
4 1.1 kre # All rights reserved.
5 1.1 kre #
6 1.1 kre # Redistribution and use in source and binary forms, with or without
7 1.1 kre # modification, are permitted provided that the following conditions
8 1.1 kre # are met:
9 1.1 kre # 1. Redistributions of source code must retain the above copyright
10 1.1 kre # notice, this list of conditions and the following disclaimer.
11 1.1 kre # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kre # notice, this list of conditions and the following disclaimer in the
13 1.1 kre # documentation and/or other materials provided with the distribution.
14 1.1 kre #
15 1.1 kre # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 kre # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 kre # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 kre # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 kre # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 kre # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 kre # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 kre # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 kre # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 kre # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kre # POSSIBILITY OF SUCH DAMAGE.
26 1.1 kre #
27 1.1 kre # the implementation of "sh" to test
28 1.1 kre : ${TEST_SH:="/bin/sh"}
29 1.1 kre
30 1.1 kre # This set of tests checks the low level shell (script) input
31 1.1 kre # systrem (reading script files, nested files, fines read while
32 1.1 kre # reading strings, ..) and correctly dropping nul bytes from file data
33 1.1 kre
34 1.1 kre # other shell input (the read builtin for example) is not covered here.
35 1.1 kre
36 1.1 kre atf_test_case nul_elimination
37 1.1 kre
38 1.1 kre nul_elimination_head() {
39 1.1 kre atf_set "descr" "verifies that \0 chars in input are properly ignored"
40 1.1 kre }
41 1.1 kre
42 1.1 kre nul_elimination_body() {
43 1.1 kre atf_require_prog printf
44 1.1 kre atf_require_prog stat
45 1.1 kre
46 1.1 kre # these really should always be present, but...
47 1.1 kre atf_require_prog dd
48 1.1 kre atf_require_prog cat
49 1.1 kre atf_require_prog rm
50 1.1 kre
51 1.1 kre # please do not make even trivial changes (like correcting spelling)
52 1.1 kre # to the following script without taking care to fix the following
53 1.1 kre # tests, even minor changes can defeat the purpose of the test
54 1.1 kre cat > helper.sh <<- EOF
55 1.1 kre # a line of commentary that does nothing
56 1.1 kre # another line of comments (these just make the script bigger)
57 1.1 kre printf A
58 1.1 kre eval "printf B"
59 1.1 kre if printf C
60 1.1 kre then
61 1.1 kre printf D
62 1.1 kre fi
63 1.1 kre for x in E F G H
64 1.1 kre do
65 1.1 kre printf "\$x"
66 1.1 kre done
67 1.1 kre printf \\
68 1.1 kre I
69 1.1 kre printf '\n'
70 1.1 kre exit 0
71 1.1 kre EOF
72 1.1 kre
73 1.1 kre # this first test simply verifies that the script works as
74 1.1 kre # expected, should it fail, it is not a problem with \0 chars
75 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
76 1.1 kre ${TEST_SH} helper.sh
77 1.1 kre
78 1.1 kre size=$(stat -f %z helper.sh)
79 1.1 kre
80 1.1 kre # Now insert \0 chars at specifically chosen spots in script
81 1.1 kre for loc in 0 10 40 41 42 104 105 106 110 111 112 113 119 127 128 \
82 1.1 kre 144 150 162 165 168 187 202 203 204 205 214 215 216 224 225
83 1.1 kre do
84 1.1 kre # at each location try varying the number of \0's inserted
85 1.1 kre for N in 1 2 4 7
86 1.1 kre do
87 1.1 kre OF="helper-${N}@${loc}.sh"
88 1.1 kre
89 1.1 kre test "${loc}" -gt 0 &&
90 1.1 kre dd if=helper.sh bs=1 count="${loc}" \
91 1.1 kre of="${OF}" >/dev/null 2>&1
92 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
93 1.1 kre oseek="$(( loc + N ))" \
94 1.1 kre of="${OF}" >/dev/null 2>&1
95 1.1 kre
96 1.1 kre test "$(stat -f %z "${OF}")" -eq "$(( size + N ))" ||
97 1.1 kre atf_fail "${N}@${loc}: build failure"
98 1.1 kre
99 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
100 1.1 kre ${TEST_SH} "${OF}"
101 1.1 kre
102 1.1 kre rm -f "${OF}"
103 1.1 kre done
104 1.1 kre
105 1.1 kre done
106 1.1 kre
107 1.1 kre # Next insert \0 chars at multiple chosen locations
108 1.1 kre # nb: offsets in each arg must be non-decreasing
109 1.1 kre for locs in '0 225' '0 224' '0 127 223' '0 10 15' '0 48 55' \
110 1.1 kre '0 0 0 225 225 225' '0 0 127 128 224 224 225' \
111 1.1 kre '104 106' '105 110' '113 119' '113 119 122' '113 127' \
112 1.1 kre '129 130 131 132 133 136 139 140' '184 185 186 187' \
113 1.1 kre '202 203 203 204 204 205 205 206 207'
114 1.1 kre do
115 1.1 kre set -- $locs
116 1.1 kre gaps=$#
117 1.1 kre
118 1.1 kre IFS=-
119 1.1 kre OF="helper-${*}.sh"
120 1.1 kre unset IFS
121 1.1 kre
122 1.1 kre loc=$1; shift
123 1.1 kre test "${loc}" -gt 0 &&
124 1.1 kre dd if=helper.sh bs=1 count="${loc}" \
125 1.1 kre of="${OF}" >/dev/null 2>&1
126 1.1 kre G=1
127 1.1 kre for N
128 1.1 kre do
129 1.1 kre count=$(( N - loc ))
130 1.1 kre if [ "${count}" -eq 0 ]
131 1.1 kre then
132 1.1 kre printf '\0' >> "${OF}"
133 1.1 kre else
134 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
135 1.1 kre oseek="$(( loc + G ))" count="${count}" \
136 1.1 kre of="${OF}" >/dev/null 2>&1
137 1.1 kre fi
138 1.1 kre loc=$N
139 1.1 kre G=$(( G + 1 ))
140 1.1 kre done
141 1.1 kre
142 1.1 kre if [ "${loc}" -lt "${size}" ]
143 1.1 kre then
144 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
145 1.1 kre oseek="$(( loc + G ))" of="${OF}" >/dev/null 2>&1
146 1.1 kre else
147 1.1 kre printf '\0' >> "${OF}"
148 1.1 kre fi
149 1.1 kre
150 1.1 kre test "$(stat -f %z "${OF}")" -eq "$(( size + gaps ))" ||
151 1.1 kre atf_fail "${locs}: build failure"
152 1.1 kre
153 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
154 1.1 kre ${TEST_SH} "${OF}"
155 1.1 kre
156 1.1 kre rm -f "${OF}"
157 1.1 kre done
158 1.1 kre
159 1.1 kre # Now just insert \0's in random locations in the script,
160 1.1 kre # hoping that if the somewhat carefully selected insertions
161 1.1 kre # above fail to trigger a bug, then if any (bugs) remain,
162 1.1 kre # eventually Murphy will prevail, and one of these tests will catch it.
163 1.1 kre
164 1.1 kre test "${RANDOM}" = "${RANDOM}" &&
165 1.1 kre atf_skip 'ATF shell does not support $RANDOM'
166 1.1 kre
167 1.1 kre # To be added later...
168 1.1 kre
169 1.1 kre return 0
170 1.1 kre }
171 1.1 kre
172 1.1 kre atf_init_test_cases() {
173 1.1 kre atf_add_test_case nul_elimination
174 1.1 kre # atf_add_test_case file_recursion
175 1.1 kre # atf_add_test_case file_string_recursion
176 1.1 kre # atf_add_test_case file_recursion_nul
177 1.1 kre # atf_add_test_case file_string_recursion_nul
178 1.1 kre }
179