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