1# $NetBSD: t_getopt.sh,v 1.3 2023/05/10 23:44:15 gutteridge Exp $ 2# 3# Copyright (c) 2008 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 28h_getopt() 29{ 30 atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF 31load: $1 32args: $2 33result: $3 34EOF 35 cat stderr 36 rm -f stderr 37} 38 39h_getopt_long() 40{ 41 atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF 42$1 43args: $2 44result: $3 45EOF 46 cat stderr 47 rm -f stderr 48} 49 50atf_test_case getopt 51getopt_head() 52{ 53 atf_set "descr" "Checks getopt(3)" 54} 55getopt_body() 56{ 57 load="c:d" 58 59 h_getopt "${load}" "foo -c 1 -d foo" "c=1,d|1" 60 h_getopt "${load}" "foo -d foo bar" "d|2" 61 h_getopt "${load}" "foo -c 2 foo bar" "c=2|2" 62 h_getopt "${load}" "foo -e 1 foo bar" "!?|3" 63 h_getopt "${load}" "foo -d -- -c 1" "d|2" 64 h_getopt "${load}" "foo -c- 1" "c=-|1" 65 h_getopt "${load}" "foo -d - 1" "d|2" 66} 67 68atf_test_case getopt_optval 69getopt_optval_head() 70{ 71 atf_set "descr" "Checks getopt(3) with optional value" 72} 73getopt_optval_body() 74{ 75 h_getopt "o::" "foo -o" "o=(null)|0" 76 h_getopt "o::" "foo -o1 2" "o=1|1" 77 h_getopt "o::" "foo -o 1 2" "o=(null)|2" 78} 79 80atf_test_case getopt_long 81getopt_long_head() 82{ 83 atf_set "descr" "Checks getopt_long(3)" 84} 85getopt_long_body() 86{ 87 # GNU libc tests with these 88 load="optstring: abc: 89longopts: 5 90longopt: required, required_argument, , 'r' 91longopt: optional, optional_argument, , 'o' 92longopt: none, no_argument, , 'n' 93longopt: color, no_argument, , 'C' 94longopt: colour, no_argument, , 'C'" 95 96 h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0" 97 h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0" 98 99 # This is problematic 100 # 101 # GNU libc 2.1.3 this fails with ambiguous result 102 # h_getopt_long "${load}" "foo --col" "!?|0" 103 # 104 # GNU libc 2.2 >= this works 105 h_getopt_long "${load}" "foo --col" "-color|0" 106 107 h_getopt_long "${load}" "foo --colour" "-colour|0" 108 109 # This is the real GNU libc test! 110 args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour" 111 # GNU libc 2.1.3 this fails with ambiguous 112 #result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1" 113 # 114 # GNU libc 2.2 >= this works 115 result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1" 116 h_getopt_long "${load}" "${args}" "${result}" 117 118 # 119 # The order of long options in the long option array should not matter. 120 # An exact match should never be treated as ambiguous. 121 # 122 load="optstring: fgl 123longopts: 3 124longopt: list-foobar, no_argument, lopt, 'f' 125longopt: list-goobar, no_argument, lopt, 'g' 126longopt: list, no_argument, lopt, 'l'" 127 h_getopt_long "${load}" "foo --list" "-list|0" 128} 129 130 131atf_init_test_cases() 132{ 133 atf_add_test_case getopt 134 atf_add_test_case getopt_optval 135 atf_add_test_case getopt_long 136} 137