]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netbsd-tests/bin/sh/t_varval.sh
MFC r305358,r305449,r305451,r306367,r306397,r309474:
[FreeBSD/stable/10.git] / contrib / netbsd-tests / bin / sh / t_varval.sh
1 # $NetBSD: t_varval.sh,v 1.1 2016/03/16 15:49:19 christos Exp $
2 #
3 # Copyright (c) 2016 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
29
30 # Test all kinds of weird values in various ways to use shell $... expansions
31
32 oneline()
33 {
34         q="'"
35         test $# -eq 4 && q=""
36
37         v=$( printf '\\%3.3o' $(( $2 & 0xFF )) )
38         printf "%s" "$1"
39         if [ $2 != 39 ]; then
40                 printf "%sprefix${v}suffix%s" "$q" "$q"
41         elif [ $# -ne 4 ]; then
42                 printf %s prefix\"\'\"suffix
43         else
44                 printf %s prefix\'suffix
45         fi
46         printf "%s\n" "$3"
47 }
48
49 mkdata() {
50         quote= pfx=
51         while [ $# -gt 0 ]
52         do
53                 case "$1" in
54                 --)     shift; break;;
55                 -q)     quote=no; shift; continue;;
56                 esac
57
58                 pfx="${pfx}${pfx:+ }${1}"
59                 shift
60         done
61
62         sfx=
63         while [ $# -gt 0 ]
64         do
65                 sfx="${sfx}${sfx:+ }${1}"
66                 shift
67         done
68
69         i=1             # '\0' is not expected to work, anywhere...
70         while [ $i -lt 256 ]
71         do
72                 oneline "${pfx}" "$i" "${sfx}" $quote
73                 i=$(( $i + 1 ))
74         done
75 }
76
77 atf_test_case aaa
78 aaa_head() {
79         atf_set "descr" "Check that this test has a hope of working. " \
80                 "Just give up on these tests if the aaa test fails".
81 }
82 aaa_body() {
83         oneline "echo " 9 '' |
84                 atf_check -s exit:0 -o inline:'prefix\tsuffix\n' -e empty \
85                         ${TEST_SH}
86
87         oneline "VAR=" 65 '; echo "${#VAR}:${VAR}"' |
88                 atf_check -s exit:0 -o inline:'13:prefixAsuffix\n' -e empty \
89                         ${TEST_SH}
90
91         oneline "VAR=" 1 '; echo "${#VAR}:${VAR}"' |
92                 atf_check -s exit:0 -o inline:'13:prefix\ 1suffix\n' -e empty \
93                         ${TEST_SH}
94
95         oneline "VAR=" 10 '; echo "${#VAR}:${VAR}"' |
96                 atf_check -s exit:0 -o inline:'13:prefix\nsuffix\n' -e empty \
97                         ${TEST_SH}
98
99         rm -f prefix* 2>/dev/null || :
100         oneline "echo hello >" 45 "" |
101                 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
102         test -f "prefix-suffix" ||
103                 atf_fail "failed to create prefix-suffix (45)"
104         test -s "prefix-suffix" ||
105                 atf_fail "no data in prefix-suffix (45)"
106         test "$(cat prefix-suffix)" = "hello" ||
107                 atf_fail "incorrect data in prefix-suffix (45)"
108
109         return 0
110 }
111
112 atf_test_case assignment
113 assignment_head() {
114         atf_set "descr" "Check that all chars can be assigned to vars"
115 }
116 assignment_body() {
117         atf_require_prog grep
118         atf_require_prog rm
119
120         rm -f results || :
121         mkdata "VAR=" -- '; echo ${#VAR}' |
122                 atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
123         test -z $( grep -v "^13$" results ) ||
124                 atf_fail "Incorrect lengths: $(grep -nv '^13$' results)"
125
126         return 0
127 }
128
129 atf_test_case cmdline
130 cmdline_head() {
131         atf_set "descr" "Check vars containing all chars can be used"
132 }
133 cmdline_body() {
134         atf_require_prog rm
135         atf_require_prog wc
136
137         rm -f results || :
138         mkdata "VAR=" -- '; echo "${VAR}"' |
139                 atf_check -s exit:0 -o save:results -e empty ${TEST_SH}
140
141         # 256 because one output line contains a \n ...
142         test $( wc -l < results ) -eq 256 ||
143                 atf_fail "incorrect line count in results"
144         test $(wc -c < results) -eq $(( 255 * 14 )) ||
145                 atf_fail "incorrect character count in results"
146
147         return 0
148 }
149
150 atf_test_case redirect
151 redirect_head() {
152         atf_set "descr" "Check vars containing all chars can be used"
153 }
154 redirect_body() {
155         atf_require_prog ls
156         atf_require_prog wc
157         atf_require_prog rm
158         atf_require_prog mkdir
159         atf_require_prog rmdir
160
161         nl='
162 '
163
164         rm -f prefix* suffix || :
165
166         mkdir prefix            # one of the files will be prefix/suffix
167         mkdata "VAR=" -- '; echo "${VAR}" > "${VAR}"' |
168                 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
169
170         test -f "prefix/suffix" ||
171                 atf_fail "Failed to create file in subdirectory"
172         test $( wc -l < "prefix/suffix" ) -eq 1 ||
173                 atf_fail "Not exactly one line in prefix/suffix file"
174
175         atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
176         atf_check -s exit:0 -o empty -e empty rmdir "prefix"
177
178         test -f "prefix${nl}suffix" ||
179                 atf_fail "Failed to create file with newline in its name"
180         test $( wc -l < "prefix${nl}suffix" ) -eq 2 ||
181                 atf_fail "NewLine file did not contain embedded newline"
182
183         atf_check -s exit:0 -o empty -e empty rm "prefix${nl}suffix"
184
185         # Now there should be 253 files left...
186         test $( ls | wc -l ) -eq 253 ||
187                 atf_fail \
188    "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
189
190         # and each of them should have a name that is 13 chars long (+ \n)
191         test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
192                 atf_fail "File names do not appear to be as expected"
193
194         return 0
195 }
196
197 atf_test_case read
198 read_head() {
199         atf_set "descr" "Check vars containing all chars can be used"
200 }
201 read_body() {
202         atf_require_prog ls
203         atf_require_prog wc
204         atf_require_prog rm
205         atf_require_prog mkdir
206         atf_require_prog rmdir
207
208         nl='
209 '
210
211         rm -f prefix* suffix || :
212
213         mkdir prefix            # one of the files will be prefix/suffix
214         mkdata -q |
215                 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '
216                         while read -r VAR
217                         do
218                                 # skip the mess made by embedded newline
219                                 case "${VAR}" in
220                                 (prefix | suffix)       continue;;
221                                 esac
222                                 echo "${VAR}" > "${VAR}"
223                         done'
224
225         test -f "prefix/suffix" ||
226                 atf_fail "Failed to create file in subdirectory"
227         test $( wc -l < "prefix/suffix" ) -eq 1 ||
228                 atf_fail "Not exactly one line in prefix/suffix file"
229
230         atf_check -s exit:0 -o empty -e empty rm "prefix/suffix"
231         atf_check -s exit:0 -o empty -e empty rmdir "prefix"
232
233         # Now there should be 253 files left...
234         test $( ls | wc -l ) -eq 253 ||
235                 atf_fail \
236    "Did not create all expected files: wanted: 253, found ($( ls | wc -l ))"
237
238         # and each of them should have a name that is 13 chars long (+ \n)
239         test $( ls | wc -c ) -eq $(( 253 * 14 )) ||
240                 atf_fail "File names do not appear to be as expected"
241
242         return 0
243 }
244
245 atf_init_test_cases() {
246         atf_add_test_case aaa
247         atf_add_test_case assignment
248         atf_add_test_case cmdline
249         atf_add_test_case redirect
250         atf_add_test_case read
251 }