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