Home | History | Annotate | Line # | Download | only in indent
t_errors.sh revision 1.2
      1 #! /bin/sh
      2 # $NetBSD: t_errors.sh,v 1.2 2021/10/14 17:42:13 rillig Exp $
      3 #
      4 # Copyright (c) 2021 The NetBSD Foundation, Inc.
      5 # All rights reserved.
      6 #
      7 # Redistribution and use in source and binary forms, with or without
      8 # modification, are permitted provided that the following conditions
      9 # are met:
     10 # 1. Redistributions of source code must retain the above copyright
     11 #    notice, this list of conditions and the following disclaimer.
     12 # 2. Redistributions in binary form must reproduce the above copyright
     13 #    notice, this list of conditions and the following disclaimer in the
     14 #    documentation and/or other materials provided with the distribution.
     15 #
     16 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26 # POSSIBILITY OF SUCH DAMAGE.
     27 #
     28 # $FreeBSD$
     29 
     30 # Tests for error handling in indent.
     31 
     32 indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
     33 nl='
     34 '
     35 
     36 expect_error()
     37 {
     38 	local msg
     39 
     40 	msg="$1"
     41 	shift
     42 
     43 	atf_check -s 'exit:1' \
     44 	    -e "inline:$msg$nl" \
     45 	    "$indent" "$@"
     46 }
     47 
     48 atf_test_case 'option_unknown'
     49 option_unknown_body()
     50 {
     51 	expect_error \
     52 	    'indent: Command line: unknown option "-Z-unknown"' \
     53 	    -Z-unknown
     54 }
     55 
     56 atf_test_case 'option_bool_trailing_garbage'
     57 option_bool_trailing_garbage_body()
     58 {
     59 	expect_error \
     60 	    'indent: Command line: unknown option "-bacchus"' \
     61 	    -bacchus
     62 }
     63 
     64 atf_test_case 'option_int_missing_parameter'
     65 option_int_missing_parameter_body()
     66 {
     67 	expect_error \
     68 	    'indent: Command line: option "-ts" requires an integer parameter' \
     69 	    -tsx
     70 }
     71 
     72 atf_test_case 'option_profile_not_found'
     73 option_profile_not_found_body()
     74 {
     75 	expect_error \
     76 	    'indent: profile ./nonexistent: No such file or directory' \
     77 	    -P./nonexistent
     78 }
     79 
     80 atf_test_case 'option_typedefs_not_found'
     81 option_typedefs_not_found_body()
     82 {
     83 	expect_error \
     84 	    'indent: cannot open file ./nonexistent' \
     85 	    -U./nonexistent
     86 }
     87 
     88 atf_test_case 'option_buffer_overflow'
     89 option_buffer_overflow_body()
     90 {
     91 	opt='12345678123456781234567812345678'	# 32
     92 	opt="$opt$opt$opt$opt$opt$opt$opt$opt"	# 256
     93 	opt="$opt$opt$opt$opt$opt$opt$opt$opt"	# 2048
     94 	opt="$opt$opt$opt$opt$opt$opt$opt$opt"	# 16384
     95 	printf '%s\n' "-$opt" > indent.pro
     96 
     97 	# TODO: The call to 'diag' should be replaced with 'errx'.
     98 	expect_error \
     99 	    'Error@1: buffer overflow in indent.pro, starting with '\''-123456781'\''' \
    100 	    -Pindent.pro
    101 }
    102 
    103 atf_test_case 'option_special_missing_param'
    104 option_special_missing_param_body()
    105 {
    106 	# TODO: Write '-cli' instead of only 'cli'.
    107 	expect_error \
    108 	    'indent: Command line: ``cli'\'\'' requires a parameter' \
    109 	    -cli
    110 
    111 	expect_error \
    112 	    'indent: Command line: ``T'\'\'' requires a parameter' \
    113 	    -T
    114 
    115 	expect_error \
    116 	    'indent: Command line: ``U'\'\'' requires a parameter' \
    117 	    -U
    118 }
    119 
    120 atf_test_case 'unterminated_comment'
    121 unterminated_comment_body()
    122 {
    123 	echo '/*' > comment.c
    124 
    125 	atf_check -s 'exit:1' \
    126 	    -o 'inline:/*'"$nl"' *'"$nl" \
    127 	    -e 'inline:/**INDENT** Error@2: Unterminated comment */'"$nl" \
    128 	    "$indent" -st < comment.c
    129 }
    130 
    131 atf_init_test_cases()
    132 {
    133 	atf_add_test_case 'option_unknown'
    134 	atf_add_test_case 'option_bool_trailing_garbage'
    135 	atf_add_test_case 'option_int_missing_parameter'
    136 	atf_add_test_case 'option_profile_not_found'
    137 	atf_add_test_case 'option_buffer_overflow'
    138 	atf_add_test_case 'option_typedefs_not_found'
    139 	atf_add_test_case 'option_special_missing_param'
    140 	atf_add_test_case 'unterminated_comment'
    141 }
    142