1 1.3 kre # $NetBSD: t_shift.sh,v 1.3 2024/09/21 20:48:50 kre Exp $ 2 1.1 christos # 3 1.1 christos # Copyright (c) 2016 The NetBSD Foundation, Inc. 4 1.1 christos # All rights reserved. 5 1.1 christos # 6 1.1 christos # Redistribution and use in source and binary forms, with or without 7 1.1 christos # modification, are permitted provided that the following conditions 8 1.1 christos # are met: 9 1.1 christos # 1. Redistributions of source code must retain the above copyright 10 1.1 christos # notice, this list of conditions and the following disclaimer. 11 1.1 christos # 2. Redistributions in binary form must reproduce the above copyright 12 1.1 christos # notice, this list of conditions and the following disclaimer in the 13 1.1 christos # documentation and/or other materials provided with the distribution. 14 1.1 christos # 15 1.1 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 christos # POSSIBILITY OF SUCH DAMAGE. 26 1.1 christos # 27 1.1 christos # the implementation of "sh" to test 28 1.1 christos : ${TEST_SH:="/bin/sh"} 29 1.1 christos 30 1.1 christos atf_test_case basic_shift_test 31 1.1 christos basic_shift_test_head() { 32 1.1 christos atf_set "descr" "Test correct operation of valid shifts" 33 1.1 christos } 34 1.1 christos basic_shift_test_body() { 35 1.1 christos 36 1.1 christos for a in \ 37 1.1 christos "one-arg::0:one-arg" \ 38 1.3 kre "one-arg:--:0:one-arg" \ 39 1.1 christos "one-arg:1:0:one-arg" \ 40 1.3 kre "one-arg:-- 1:0:one-arg" \ 41 1.3 kre "one-arg:-- 0:1 one-arg" \ 42 1.1 christos "a b c::2 b c:a" \ 43 1.1 christos "a b c:1:2 b c:a" \ 44 1.1 christos "a b c:2:1 c:a:b" \ 45 1.1 christos "a b c:3:0:a:b:c" \ 46 1.1 christos "a b c:0:3 a b c" \ 47 1.1 christos "a b c d e f g h i j k l m n o p:1:15 b c d e f g h i j k l m n o p"\ 48 1.1 christos "a b c d e f g h i j k l m n o p:9:7 j k l m n o p:a:b:c:g:h:i" \ 49 1.1 christos "a b c d e f g h i j k l m n o p:13:3 n o p:a:b:c:d:k:l:m" \ 50 1.1 christos "a b c d e f g h i j k l m n o p:16:0:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p" 51 1.1 christos do 52 1.1 christos oIFS="${IFS}" 53 1.1 christos IFS=:; set -- $a 54 1.1 christos IFS="${oIFS}" 55 1.1 christos 56 1.1 christos init="$1"; n="$2"; res="$3"; shift 3 57 1.1 christos 58 1.1 christos not= 59 1.1 christos for b 60 1.1 christos do 61 1.1 christos not="${not} -o not-match:$b" 62 1.1 christos done 63 1.1 christos 64 1.1 christos atf_check -s exit:0 -o "match:${res}" ${not} -e empty \ 65 1.1 christos ${TEST_SH} -c "set -- ${init}; shift $n;"' echo "$# $*"' 66 1.1 christos done 67 1.1 christos 68 1.1 christos atf_check -s exit:0 -o match:complete -o not-match:ERR -e empty \ 69 1.1 christos ${TEST_SH} -c \ 70 1.1 christos 'set -- a b c d e;while [ $# -gt 0 ];do shift||echo ERR;done;echo complete' 71 1.1 christos } 72 1.1 christos 73 1.3 kre atf_test_case basic_rotate 74 1.3 kre basic_rotate_head() { 75 1.3 kre atf_set "descr" "Test correct operation of valid rotates" 76 1.3 kre } 77 1.3 kre basic_rotate_body() { 78 1.3 kre 79 1.3 kre for a in \ 80 1.3 kre "one-arg:-0:1 one-arg" \ 81 1.3 kre "one-arg:-1:1 one-arg" \ 82 1.3 kre "one-arg:-- -1:1 one-arg" \ 83 1.3 kre "a b c:-1:3 c a b" \ 84 1.3 kre "a b c:-2:3 b c a" \ 85 1.3 kre "a b c:-3:3 a b c" \ 86 1.3 kre "a b c:-- -3:3 a b c" \ 87 1.3 kre "a b c:-0:3 a b c" \ 88 1.3 kre "a b c d e f g h i j k l m n o:-1:15 o a b c d e f g h i j k l m n" \ 89 1.3 kre "a b c d e f g h i j k l m n o:-2:15 n o a b c d e f g h i j k l m" \ 90 1.3 kre "a b c d e f g h i j k l m n o:-9:15 g h i j k l m n o a b c d e f" \ 91 1.3 kre "a b c d e f g h i j k l m n o:-13:15 c d e f g h i j k l m n o a b" \ 92 1.3 kre "a b c d e f g h i j k l m n o:-14:15 b c d e f g h i j k l m n o a" \ 93 1.3 kre "a b c d e f g h i j k l m n o:-15:15 a b c d e f g h i j k l m n o" 94 1.3 kre do 95 1.3 kre oIFS="${IFS}" 96 1.3 kre IFS=:; set -- $a 97 1.3 kre IFS="${oIFS}" 98 1.3 kre 99 1.3 kre init="$1"; n="$2"; res="$3"; shift 3 100 1.3 kre 101 1.3 kre atf_check -s exit:0 -o "match:${res}" -e empty \ 102 1.3 kre ${TEST_SH} -c "set -- ${init}; shift $n;"' echo "$# $*"' 103 1.3 kre done 104 1.3 kre } 105 1.3 kre 106 1.1 christos atf_test_case excessive_shift 107 1.1 christos excessive_shift_head() { 108 1.1 christos atf_set "descr" "Test acceptable operation of shift too many" 109 1.1 christos } 110 1.1 christos # In: 111 1.1 christos # 112 1.1 christos # http://pubs.opengroup.org/onlinepubs/9699919799 113 1.1 christos # /utilities/V3_chap02.html#tag_18_26_01 114 1.1 christos # 115 1.1 christos # (that URL should be one line, with the /util... immediately after ...9799) 116 1.1 christos # 117 1.1 christos # POSIX says of shift (in the "EXIT STATUS" paragraph): 118 1.1 christos # 119 1.1 christos # If the n operand is invalid or is greater than "$#", this may be considered 120 1.1 christos # a syntax error and a non-interactive shell may exit; if the shell does not 121 1.1 christos # exit in this case, a non-zero exit status shall be returned. 122 1.1 christos # Otherwise, zero shall be returned. 123 1.1 christos # 124 1.1 christos # NetBSD's sh treats it as an error and exits (if non-interactive, as here), 125 1.1 christos # other shells do not. 126 1.1 christos # 127 1.1 christos # Either behaviour is acceptable - so the test allows for both 128 1.1 christos # (and checks that if the shell does not exit, "shift" returns status != 0) 129 1.1 christos 130 1.1 christos excessive_shift_body() { 131 1.1 christos for a in \ 132 1.1 christos "one-arg:2" \ 133 1.1 christos "one-arg:4" \ 134 1.1 christos "one-arg:13" \ 135 1.1 christos "one two:3" \ 136 1.1 christos "one two:7" \ 137 1.1 christos "one two three four five:6" \ 138 1.1 christos "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:16" \ 139 1.1 christos "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:17" \ 140 1.1 christos "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:30" \ 141 1.1 christos "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:9999" 142 1.1 christos do 143 1.1 christos oIFS="${IFS}" 144 1.1 christos IFS=:; set -- $a 145 1.1 christos IFS="${oIFS}" 146 1.1 christos 147 1.1 christos atf_check -s not-exit:0 -o match:OK -o not-match:ERR \ 148 1.1 christos -e ignore ${TEST_SH} -c \ 149 1.1 christos "set -- $1 ;"'echo OK:$#-'"$2;shift $2 && echo ERR" 150 1.1 christos done 151 1.1 christos } 152 1.1 christos 153 1.3 kre atf_test_case excessive_rotate 154 1.3 kre excessive_rotate_head() { 155 1.3 kre atf_set "descr" "Test acceptable operation of rotate too many" 156 1.3 kre } 157 1.3 kre 158 1.3 kre excessive_rotate_body() { 159 1.3 kre for a in \ 160 1.3 kre "one-arg:2" \ 161 1.3 kre "one-arg:4" \ 162 1.3 kre "one-arg:13" \ 163 1.3 kre "one two:3" \ 164 1.3 kre "one two:7" \ 165 1.3 kre "one two three four five:6" \ 166 1.3 kre "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:16" \ 167 1.3 kre "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:17" \ 168 1.3 kre "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:30" \ 169 1.3 kre "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:9999" 170 1.3 kre do 171 1.3 kre oIFS="${IFS}" 172 1.3 kre IFS=:; set -- $a 173 1.3 kre IFS="${oIFS}" 174 1.3 kre 175 1.3 kre atf_check -s not-exit:0 -o match:OK -o not-match:ERR \ 176 1.3 kre -e ignore ${TEST_SH} -c \ 177 1.3 kre "set -- $1 ;echo OK:$#:-$2; shift -$2 && echo ERR" 178 1.3 kre done 179 1.3 kre } 180 1.3 kre 181 1.1 christos atf_test_case function_shift 182 1.1 christos function_shift_head() { 183 1.1 christos atf_set "descr" "Test that shift in a function does not affect outside" 184 1.1 christos } 185 1.1 christos function_shift_body() { 186 1.2 kre : # later... 187 1.1 christos } 188 1.1 christos 189 1.1 christos atf_test_case non_numeric_shift 190 1.1 christos non_numeric_shift_head() { 191 1.1 christos atf_set "descr" "Test that non-numeric args to shift are detected" 192 1.1 christos } 193 1.1 christos 194 1.1 christos # from the DESCRIPTION section at the URL mentioned with the excessive_shift 195 1.1 christos # test: 196 1.1 christos # 197 1.1 christos # The value n shall be an unsigned decimal integer ... 198 1.1 christos # 199 1.1 christos # That is not hex (octal will be treated as if it were decimal, a leading 0 200 1.1 christos # will simply be ignored - we test for this by giving an "octal" value that 201 1.1 christos # would be OK if parsed as octal, but not if parsed (correctly) as decimal) 202 1.1 christos # 203 1.1 christos # Obviously total trash like roman numerals or alphabetic strings are out. 204 1.1 christos # 205 1.1 christos # Also no signed values (no + or -) and not a string that looks kind of like 206 1.1 christos # a number, but only if you're generous 207 1.1 christos # 208 1.1 christos # But as the EXIT STATUS section quoted above says, with an invalid 'n' 209 1.1 christos # the shell has the option of exiting, or returning status != 0, so 210 1.1 christos # again this test allows both. 211 1.1 christos 212 1.1 christos non_numeric_shift_body() { 213 1.1 christos 214 1.1 christos # there are 9 args set, 010 is 8 if parsed octal, 10 decimal 215 1.3 kre for a in a I 0x12 010 5V ' ' '' +1 ' 1' 216 1.1 christos do 217 1.1 christos atf_check -s not-exit:0 -o empty -e ignore ${TEST_SH} -c \ 218 1.1 christos "set -- a b c d e f g h i; shift '$a' && echo ERROR" 219 1.3 kre atf_check -s not-exit:0 -o empty -e ignore ${TEST_SH} -c \ 220 1.3 kre "set -- a b c d e f g h i; shift -- '$a' && echo ERROR" 221 1.1 christos done 222 1.1 christos } 223 1.1 christos 224 1.1 christos atf_test_case too_many_args 225 1.1 christos too_many_args_head() { 226 1.1 christos # See PR bin/50896 227 1.1 christos atf_set "descr" "Test that sh detects invalid extraneous args to shift" 228 1.1 christos } 229 1.1 christos # This is a syntax error, a non-interactive shell (us) must exit $? != 0 230 1.1 christos too_many_args_body() { 231 1.1 christos # This tests the bug in PR bin/50896 is fixed 232 1.1 christos 233 1.3 kre for a in "1 1" "1 0" "1 2 3" "1 foo" "1 --" "-- 1 2" "-1 2" 234 1.1 christos do 235 1.1 christos atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \ 236 1.1 christos " set -- a b c d; shift ${a} ; echo FAILED " 237 1.1 christos done 238 1.1 christos } 239 1.1 christos 240 1.1 christos atf_init_test_cases() { 241 1.1 christos atf_add_test_case basic_shift_test 242 1.1 christos atf_add_test_case excessive_shift 243 1.1 christos atf_add_test_case function_shift 244 1.1 christos atf_add_test_case non_numeric_shift 245 1.1 christos atf_add_test_case too_many_args 246 1.3 kre atf_add_test_case basic_rotate 247 1.3 kre atf_add_test_case excessive_rotate 248 1.1 christos } 249