11553Srgrimes# $NetBSD: t_shift.sh,v 1.2 2016/05/17 09:05:14 kre Exp $ 21553Srgrimes# 31553Srgrimes# Copyright (c) 2016 The NetBSD Foundation, Inc. 41553Srgrimes# All rights reserved. 51553Srgrimes# 61553Srgrimes# Redistribution and use in source and binary forms, with or without 71553Srgrimes# modification, are permitted provided that the following conditions 81553Srgrimes# are met: 91553Srgrimes# 1. Redistributions of source code must retain the above copyright 101553Srgrimes# notice, this list of conditions and the following disclaimer. 111553Srgrimes# 2. Redistributions in binary form must reproduce the above copyright 121553Srgrimes# notice, this list of conditions and the following disclaimer in the 131553Srgrimes# documentation and/or other materials provided with the distribution. 141553Srgrimes# 158432Sjkh# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 161553Srgrimes# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 171553Srgrimes# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 181553Srgrimes# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 191553Srgrimes# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 201553Srgrimes# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 211553Srgrimes# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 221553Srgrimes# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 231553Srgrimes# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 241553Srgrimes# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 251553Srgrimes# POSSIBILITY OF SUCH DAMAGE. 261553Srgrimes# 271553Srgrimes# the implementation of "sh" to test 281553Srgrimes: ${TEST_SH:="/bin/sh"} 291553Srgrimes 301553Srgrimesatf_test_case basic_shift_test 311553Srgrimesbasic_shift_test_head() { 321553Srgrimes atf_set "descr" "Test correct operation of valid shifts" 331553Srgrimes} 341553Srgrimesbasic_shift_test_body() { 351553Srgrimes 361553Srgrimes for a in \ 371553Srgrimes "one-arg::0:one-arg" \ 381553Srgrimes "one-arg:1:0:one-arg" \ 391553Srgrimes "one-arg:0:1 one-arg" \ 401553Srgrimes "a b c::2 b c:a" \ 411553Srgrimes "a b c:1:2 b c:a" \ 421553Srgrimes "a b c:2:1 c:a:b" \ 431553Srgrimes "a b c:3:0:a:b:c" \ 441553Srgrimes "a b c:0:3 a b c" \ 451553Srgrimes "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"\ 461553Srgrimes "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" \ 471553Srgrimes "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" \ 481553Srgrimes "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" 491553Srgrimes do 501553Srgrimes oIFS="${IFS}" 511553Srgrimes IFS=:; set -- $a 521553Srgrimes IFS="${oIFS}" 536814Sdufault 541553Srgrimes init="$1"; n="$2"; res="$3"; shift 3 551553Srgrimes 561553Srgrimes not= 576814Sdufault for b 581553Srgrimes do 591553Srgrimes not="${not} -o not-match:$b" 601553Srgrimes done 611553Srgrimes 621553Srgrimes atf_check -s exit:0 -o "match:${res}" ${not} -e empty \ 631553Srgrimes ${TEST_SH} -c "set -- ${init}; shift $n;"' echo "$# $*"' 641553Srgrimes done 651553Srgrimes 661553Srgrimes atf_check -s exit:0 -o match:complete -o not-match:ERR -e empty \ 671553Srgrimes ${TEST_SH} -c \ 681553Srgrimes 'set -- a b c d e;while [ $# -gt 0 ];do shift||echo ERR;done;echo complete' 691553Srgrimes} 701553Srgrimes 711553Srgrimesatf_test_case excessive_shift 721553Srgrimesexcessive_shift_head() { 734791Swollman atf_set "descr" "Test acceptable operation of shift too many" 741553Srgrimes} 751553Srgrimes# In: 761553Srgrimes# 771553Srgrimes# http://pubs.opengroup.org/onlinepubs/9699919799 781553Srgrimes# /utilities/V3_chap02.html#tag_18_26_01 791553Srgrimes# 801553Srgrimes# (that URL should be one line, with the /util... immediately after ...9799) 811553Srgrimes# 821553Srgrimes# POSIX says of shift (in the "EXIT STATUS" paragraph): 831553Srgrimes# 841553Srgrimes# If the n operand is invalid or is greater than "$#", this may be considered 851553Srgrimes# a syntax error and a non-interactive shell may exit; if the shell does not 861553Srgrimes# exit in this case, a non-zero exit status shall be returned. 871553Srgrimes# Otherwise, zero shall be returned. 881553Srgrimes# 891553Srgrimes# NetBSD's sh treats it as an error and exits (if non-interactive, as here), 901553Srgrimes# other shells do not. 911553Srgrimes# 921553Srgrimes# Either behaviour is acceptable - so the test allows for both 931553Srgrimes# (and checks that if the shell does not exit, "shift" returns status != 0) 941553Srgrimes 951553Srgrimesexcessive_shift_body() { 961553Srgrimes for a in \ 971553Srgrimes "one-arg:2" \ 981553Srgrimes "one-arg:4" \ 991553Srgrimes "one-arg:13" \ 1001553Srgrimes "one two:3" \ 1011553Srgrimes "one two:7" \ 1021553Srgrimes "one two three four five:6" \ 1031553Srgrimes "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:16" \ 1041553Srgrimes "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:17" \ 1051553Srgrimes "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:30" \ 1061553Srgrimes "I II III IV V VI VII VIII IX X XI XII XIII XIV XV:9999" 1071553Srgrimes do 1081553Srgrimes oIFS="${IFS}" 1091553Srgrimes IFS=:; set -- $a 1101553Srgrimes IFS="${oIFS}" 1111553Srgrimes 1121553Srgrimes atf_check -s not-exit:0 -o match:OK -o not-match:ERR \ 1131553Srgrimes -e ignore ${TEST_SH} -c \ 1141553Srgrimes "set -- $1 ;"'echo OK:$#-'"$2;shift $2 && echo ERR" 1151553Srgrimes done 1166497Sbde} 1176497Sbde 1186497Sbdeatf_test_case function_shift 1196497Sbdefunction_shift_head() { 1201553Srgrimes atf_set "descr" "Test that shift in a function does not affect outside" 1211553Srgrimes} 1224791Swollmanfunction_shift_body() { 1231553Srgrimes : # later... 1241553Srgrimes} 1251553Srgrimes 1261553Srgrimesatf_test_case non_numeric_shift 1271553Srgrimesnon_numeric_shift_head() { 1281553Srgrimes atf_set "descr" "Test that non-numeric args to shift are detected" 1291553Srgrimes} 1301553Srgrimes 1311553Srgrimes# from the DESCRIPTION section at the URL mentioned with the excessive_shift 1321553Srgrimes# test: 1331553Srgrimes# 1341553Srgrimes# The value n shall be an unsigned decimal integer ... 1351553Srgrimes# 1361553Srgrimes# That is not hex (octal will be treated as if it were decimal, a leading 0 1371553Srgrimes# will simply be ignored - we test for this by giving an "octal" value that 1381553Srgrimes# would be OK if parsed as octal, but not if parsed (correctly) as decimal) 1391553Srgrimes# 1401553Srgrimes# Obviously total trash like roman numerals or alphabetic strings are out. 1411553Srgrimes# 1421553Srgrimes# Also no signed values (no + or -) and not a string that looks kind of like 1431553Srgrimes# a number, but only if you're generous 1441553Srgrimes# 1451553Srgrimes# But as the EXIT STATUS section quoted above says, with an invalid 'n' 1461553Srgrimes# the shell has the option of exiting, or returning status != 0, so 1471553Srgrimes# again this test allows both. 1481553Srgrimes 1491553Srgrimesnon_numeric_shift_body() { 1501553Srgrimes 1511553Srgrimes # there are 9 args set, 010 is 8 if parsed octal, 10 decimal 1521553Srgrimes for a in a I 0x12 010 5V -1 ' ' '' +1 ' 1' 1531553Srgrimes do 1541553Srgrimes atf_check -s not-exit:0 -o empty -e ignore ${TEST_SH} -c \ 1551553Srgrimes "set -- a b c d e f g h i; shift '$a' && echo ERROR" 1561553Srgrimes done 1571553Srgrimes} 1581553Srgrimes 1591553Srgrimesatf_test_case too_many_args 1601553Srgrimestoo_many_args_head() { 1611553Srgrimes # See PR bin/50896 1621553Srgrimes atf_set "descr" "Test that sh detects invalid extraneous args to shift" 1631553Srgrimes} 1641553Srgrimes# This is a syntax error, a non-interactive shell (us) must exit $? != 0 1651553Srgrimestoo_many_args_body() { 1661553Srgrimes # This tests the bug in PR bin/50896 is fixed 1671553Srgrimes 1681553Srgrimes for a in "1 1" "1 0" "1 2 3" "1 foo" "1 --" "-- 1" 1691553Srgrimes do 1701553Srgrimes atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \ 1711553Srgrimes " set -- a b c d; shift ${a} ; echo FAILED " 1721553Srgrimes done 1731553Srgrimes} 1741553Srgrimes 1751553Srgrimesatf_init_test_cases() { 1761553Srgrimes atf_add_test_case basic_shift_test 1771553Srgrimes atf_add_test_case excessive_shift 1781553Srgrimes atf_add_test_case function_shift 1791553Srgrimes atf_add_test_case non_numeric_shift 1801553Srgrimes atf_add_test_case too_many_args 1811553Srgrimes} 1821553Srgrimes