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 28 h_getopt() 29 { 30 atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF 31 load: $1 32 args: $2 33 result: $3 34 EOF 35 cat stderr 36 rm -f stderr 37 } 38 39 h_getopt_long() 40 { 41 atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF 42 $1 43 args: $2 44 result: $3 45 EOF 46 cat stderr 47 rm -f stderr 48 } 49 50 atf_test_case getopt 51 getopt_head() 52 { 53 atf_set "descr" "Checks getopt(3)" 54 } 55 getopt_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 68 atf_test_case getopt_optval 69 getopt_optval_head() 70 { 71 atf_set "descr" "Checks getopt(3) with optional value" 72 } 73 getopt_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 80 atf_test_case getopt_long 81 getopt_long_head() 82 { 83 atf_set "descr" "Checks getopt_long(3)" 84 } 85 getopt_long_body() 86 { 87 # GNU libc tests with these 88 load="optstring: abc: 89 longopts: 5 90 longopt: required, required_argument, , 'r' 91 longopt: optional, optional_argument, , 'o' 92 longopt: none, no_argument, , 'n' 93 longopt: color, no_argument, , 'C' 94 longopt: 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 123 longopts: 3 124 longopt: list-foobar, no_argument, lopt, 'f' 125 longopt: list-goobar, no_argument, lopt, 'g' 126 longopt: list, no_argument, lopt, 'l'" 127 h_getopt_long "${load}" "foo --list" "-list|0" 128 } 129 130 131 atf_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