t_input.sh revision 1.2 1 1.2 kre # $NetBSD: t_input.sh,v 1.2 2024/10/14 12:26:28 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.2 kre atf_expect_fail "nuls are now errors, not ignored, revisit later"
52 1.2 kre
53 1.1 kre # please do not make even trivial changes (like correcting spelling)
54 1.1 kre # to the following script without taking care to fix the following
55 1.1 kre # tests, even minor changes can defeat the purpose of the test
56 1.1 kre cat > helper.sh <<- EOF
57 1.1 kre # a line of commentary that does nothing
58 1.1 kre # another line of comments (these just make the script bigger)
59 1.1 kre printf A
60 1.1 kre eval "printf B"
61 1.1 kre if printf C
62 1.1 kre then
63 1.1 kre printf D
64 1.1 kre fi
65 1.1 kre for x in E F G H
66 1.1 kre do
67 1.1 kre printf "\$x"
68 1.1 kre done
69 1.1 kre printf \\
70 1.1 kre I
71 1.1 kre printf '\n'
72 1.1 kre exit 0
73 1.1 kre EOF
74 1.1 kre
75 1.1 kre # this first test simply verifies that the script works as
76 1.1 kre # expected, should it fail, it is not a problem with \0 chars
77 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
78 1.1 kre ${TEST_SH} helper.sh
79 1.1 kre
80 1.1 kre size=$(stat -f %z helper.sh)
81 1.1 kre
82 1.1 kre # Now insert \0 chars at specifically chosen spots in script
83 1.1 kre for loc in 0 10 40 41 42 104 105 106 110 111 112 113 119 127 128 \
84 1.1 kre 144 150 162 165 168 187 202 203 204 205 214 215 216 224 225
85 1.1 kre do
86 1.1 kre # at each location try varying the number of \0's inserted
87 1.1 kre for N in 1 2 4 7
88 1.1 kre do
89 1.1 kre OF="helper-${N}@${loc}.sh"
90 1.1 kre
91 1.1 kre test "${loc}" -gt 0 &&
92 1.1 kre dd if=helper.sh bs=1 count="${loc}" \
93 1.1 kre of="${OF}" >/dev/null 2>&1
94 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
95 1.1 kre oseek="$(( loc + N ))" \
96 1.1 kre of="${OF}" >/dev/null 2>&1
97 1.1 kre
98 1.1 kre test "$(stat -f %z "${OF}")" -eq "$(( size + N ))" ||
99 1.1 kre atf_fail "${N}@${loc}: build failure"
100 1.1 kre
101 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
102 1.1 kre ${TEST_SH} "${OF}"
103 1.1 kre
104 1.1 kre rm -f "${OF}"
105 1.1 kre done
106 1.1 kre
107 1.1 kre done
108 1.1 kre
109 1.1 kre # Next insert \0 chars at multiple chosen locations
110 1.1 kre # nb: offsets in each arg must be non-decreasing
111 1.1 kre for locs in '0 225' '0 224' '0 127 223' '0 10 15' '0 48 55' \
112 1.1 kre '0 0 0 225 225 225' '0 0 127 128 224 224 225' \
113 1.1 kre '104 106' '105 110' '113 119' '113 119 122' '113 127' \
114 1.1 kre '129 130 131 132 133 136 139 140' '184 185 186 187' \
115 1.1 kre '202 203 203 204 204 205 205 206 207'
116 1.1 kre do
117 1.1 kre set -- $locs
118 1.1 kre gaps=$#
119 1.1 kre
120 1.1 kre IFS=-
121 1.1 kre OF="helper-${*}.sh"
122 1.1 kre unset IFS
123 1.1 kre
124 1.1 kre loc=$1; shift
125 1.1 kre test "${loc}" -gt 0 &&
126 1.1 kre dd if=helper.sh bs=1 count="${loc}" \
127 1.1 kre of="${OF}" >/dev/null 2>&1
128 1.1 kre G=1
129 1.1 kre for N
130 1.1 kre do
131 1.1 kre count=$(( N - loc ))
132 1.1 kre if [ "${count}" -eq 0 ]
133 1.1 kre then
134 1.1 kre printf '\0' >> "${OF}"
135 1.1 kre else
136 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
137 1.1 kre oseek="$(( loc + G ))" count="${count}" \
138 1.1 kre of="${OF}" >/dev/null 2>&1
139 1.1 kre fi
140 1.1 kre loc=$N
141 1.1 kre G=$(( G + 1 ))
142 1.1 kre done
143 1.1 kre
144 1.1 kre if [ "${loc}" -lt "${size}" ]
145 1.1 kre then
146 1.1 kre dd if=helper.sh bs=1 skip="${loc}" \
147 1.1 kre oseek="$(( loc + G ))" of="${OF}" >/dev/null 2>&1
148 1.1 kre else
149 1.1 kre printf '\0' >> "${OF}"
150 1.1 kre fi
151 1.1 kre
152 1.1 kre test "$(stat -f %z "${OF}")" -eq "$(( size + gaps ))" ||
153 1.1 kre atf_fail "${locs}: build failure"
154 1.1 kre
155 1.1 kre atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
156 1.1 kre ${TEST_SH} "${OF}"
157 1.1 kre
158 1.1 kre rm -f "${OF}"
159 1.1 kre done
160 1.1 kre
161 1.1 kre # Now just insert \0's in random locations in the script,
162 1.1 kre # hoping that if the somewhat carefully selected insertions
163 1.1 kre # above fail to trigger a bug, then if any (bugs) remain,
164 1.1 kre # eventually Murphy will prevail, and one of these tests will catch it.
165 1.1 kre
166 1.1 kre test "${RANDOM}" = "${RANDOM}" &&
167 1.1 kre atf_skip 'ATF shell does not support $RANDOM'
168 1.1 kre
169 1.1 kre # To be added later...
170 1.1 kre
171 1.1 kre return 0
172 1.1 kre }
173 1.1 kre
174 1.1 kre atf_init_test_cases() {
175 1.1 kre atf_add_test_case nul_elimination
176 1.1 kre # atf_add_test_case file_recursion
177 1.1 kre # atf_add_test_case file_string_recursion
178 1.1 kre # atf_add_test_case file_recursion_nul
179 1.1 kre # atf_add_test_case file_string_recursion_nul
180 1.1 kre }
181