]> diplodocus.org Git - nmh/blob - test/imap/test-login
expect this commit to be force-replaced periodically
[nmh] / test / imap / test-login
1 #!/bin/sh
2
3 testname=${0#$srcdir/}
4
5 if test -z "$MH_OBJ_DIR"; then
6 srcdir=`dirname "$0"`/../..
7 MH_OBJ_DIR=`cd "$srcdir" && pwd`; export MH_OBJ_DIR
8 fi
9
10 . "$MH_OBJ_DIR/test/common.sh"
11
12 if [ $(uname) != FreeBSD ]; then
13 test_skip 'imap/test-login only working on FreeBSD for now'
14 fi
15
16 # common.sh sets this to something crazy; did i do that?!
17 testname=${0#$srcdir/}
18
19 imap_cleanup() {
20 #### Save exit status to use as status for this program.
21 status=$?
22 set +e
23
24 rm -f "$imapd" "$actual"
25 [ -n "$imapd_pid" ] && kill $imapd_pid
26
27 # TODO have cleanup allow additional cleanup registration?
28 # Or just allow it to accept status as argument.
29 (exit $status)
30 cleanup
31 }
32 trap imap_cleanup 0
33
34 covdir="$MH_OBJ_DIR/covdata/${MH_COVERAGE_PROFILE:-default}/$testname"
35 install -d "$covdir"
36
37 cat > "$MH_TEST_DIR/netrc" <<EOF
38 machine 127.0.0.1 login testuser password password
39 EOF
40
41 actual="$MH_TEST_DIR/$$.actual"
42
43 imapd="$MH_TEST_DIR/$$.imapd"
44
45 arith_val=0
46
47 start() {
48 rm -f "$MH_TEST_DIR/$profile/imap.d/socket"
49 status=0
50 LLVM_PROFILE_FILE="$covdir/$profile-%m.profraw" "$MH_OBJ_DIR/target/debug/mhimap" imap start > "$actual" 2>&1 || status=$?
51 }
52
53 ################################################################################
54 # Tests with nothing even listening.
55
56 start_test 'connection refused, explicit TLS'
57
58 arith_eval $arith_val + 1
59 setup_test_without_data profile$arith_val
60 trap imap_cleanup 0
61
62 cat >> "$MH" <<EOF
63 credentials: file-nopermcheck:$MH_TEST_DIR/netrc
64 Managed-Folders: imap
65 imap-log-level: error
66 imap-log-error-to: $MH_TEST_DIR/error
67 imap-manager: $MH_OBJ_DIR/target/debug/imap-folder-manager
68 imap-socket: $MH_TEST_DIR/$profile/imap.d/socket
69 imap-db: $MH_TEST_DIR/$profile/imap.d
70 imap-host: 127.0.0.1
71 imap-mailbox-root: test/
72 imap-port: 255
73 imap-tls: explicit but don't verify the cert i already did out of band
74 imap-auth: SASL
75 imap-user: testuser
76 EOF
77 if [ -n "$MH_TEST_IMAP_LOG_LEVEL" ]; then
78 echo "imap-log-level: $MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
79 echo "imap-log-$MH_TEST_IMAP_LOG_LEVEL-to: $MH_TEST_DIR/$profile/imap.d/log.$MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
80 fi
81
82 start
83 if ! grep -q 'Connection to "127.0.0.1:255" failed: Connection refused' "$actual"; then
84 cat "$actual" >&2
85 fail 'ECONN not reported'
86 fi
87
88 # Use fakeinetd for tests of the parts of explicit TLS handling before TLS negotiation.
89 start_fakeinetd "$imapd"
90 imapd_pid=$fakeinetd_pid
91
92 arith_eval $arith_val + 1
93 setup_test_without_data profile$arith_val
94 trap imap_cleanup 0
95
96 cat >> "$MH" <<EOF
97 credentials: file-nopermcheck:$MH_TEST_DIR/netrc
98 Managed-Folders: imap
99 imap-log-level: error
100 imap-log-error-to: $MH_TEST_DIR/error
101 imap-manager: $MH_OBJ_DIR/target/debug/imap-folder-manager
102 imap-socket: $MH_TEST_DIR/$profile/imap.d/socket
103 imap-db: $MH_TEST_DIR/$profile/imap.d
104 imap-host: 127.0.0.1
105 imap-mailbox-root: test/
106 imap-port: $fakeinetd_port
107 imap-tls: explicit but don't verify the cert i already did out of band
108 imap-auth: SASL
109 imap-user: testuser
110 EOF
111 if [ -n "$MH_TEST_IMAP_LOG_LEVEL" ]; then
112 echo "imap-log-level: $MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
113 echo "imap-log-$MH_TEST_IMAP_LOG_LEVEL-to: $MH_TEST_DIR/$profile/imap.d/log.$MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
114 fi
115
116 ################################################################################
117 start_test 'rudely close pre-greeting, explicit TLS'
118
119 cat > "$imapd" <<EOSCRIPT
120 #!/bin/sh
121 exit 1
122 EOSCRIPT
123 chmod 755 "$imapd"
124
125 start
126 if ! grep -q 'Received EOF on network read' "$actual"; then
127 cat "$actual" >&2
128 fail 'EOF error not reported'
129 fi
130
131 ################################################################################
132 start_test 'protocol violation in greeting, explicit TLS'
133
134 # Greets with simply "trash\r\n".
135
136 cat > "$imapd" <<EOSCRIPT
137 #!/bin/sh
138 cat <<EOF
139 trash
140 EOF
141 while read line; do :; done
142 EOSCRIPT
143
144 start
145 if ! grep -q 'protocol error: no space in response' "$actual"; then
146 cat "$actual" >&2
147 fail 'protocol error not reported'
148 fi
149
150 ################################################################################
151 start_test 'STARTTLS capability not offered'
152
153 cat > "$imapd" <<EOSCRIPT
154 #!/bin/sh
155 cat <<EOF
156 * OK [CAPABILITY IMAP4REV1 AUTH=PLAIN UIDPLUS]
157 EOF
158 while read line; do :; done
159 EOSCRIPT
160
161 start
162 if ! grep -q "server doesn't offer STARTTLS" "$actual"; then
163 cat "$actual" >&2
164 fail 'missing STARTTLS not reported'
165 fi
166
167 ################################################################################
168 start_test 'STARTTLS capability not offered after capabilities requested'
169
170 cat > "$imapd" <<EOSCRIPT
171 #!/bin/sh
172 cat <<EOF
173 * OK greeting
174 1 OK [CAPABILITY IMAP4REV1 AUTH=PLAIN UIDPLUS] CAPABILITY response
175 EOF
176 while read line; do :; done
177 EOSCRIPT
178
179 start
180 if ! grep -q "server doesn't offer STARTTLS" "$actual"; then
181 cat "$actual" >&2
182 fail 'missing STARTTLS not reported'
183 fi
184
185 ################################################################################
186 start_test 'rudely closed after STARTTLS'
187
188 cat > "$imapd" <<EOSCRIPT
189 #!/bin/sh
190 cat <<EOF
191 * OK [CAPABILITY IMAP4REV1 STARTTLS] greeting
192 EOF
193 read line
194 EOSCRIPT
195
196 start
197 if ! grep -q 'Received EOF on network read' "$actual"; then
198 cat "$actual" >&2
199 fail 'EOF not reported'
200 fi
201
202 ################################################################################
203 start_test 'trash response to STARTTLS'
204
205 cat > "$imapd" <<EOSCRIPT
206 #!/bin/sh
207 cat <<EOF
208 * OK [CAPABILITY IMAP4REV1 STARTTLS] greeting
209 trash
210 EOF
211 while read line; do :; done
212 EOSCRIPT
213
214 start
215 if ! grep -q 'protocol error: no space in response' "$actual"; then
216 cat "$actual" >&2
217 fail 'protocol error not reported'
218 fi
219
220 kill $imapd_pid
221 imapd_pid=
222
223 ################################################################################
224 start_test 'connection refused, implicit TLS'
225
226 arith_eval $arith_val + 1
227 setup_test_without_data profile$arith_val
228 trap imap_cleanup 0
229
230 cat >> "$MH" <<EOF
231 credentials: file-nopermcheck:$MH_TEST_DIR/netrc
232 Managed-Folders: imap
233 imap-log-level: error
234 imap-log-error-to: $MH_TEST_DIR/error
235 imap-manager: $MH_OBJ_DIR/target/debug/imap-folder-manager
236 imap-socket: $MH_TEST_DIR/$profile/imap.d/socket
237 imap-db: $MH_TEST_DIR/$profile/imap.d
238 imap-host: 127.0.0.1
239 imap-mailbox-root: test/
240 imap-port: 255
241 imap-tls: implicit but don't verify the cert i already did out of band
242 imap-auth: SASL
243 imap-user: testuser
244 EOF
245 if [ -n "$MH_TEST_IMAP_LOG_LEVEL" ]; then
246 echo "imap-log-level: $MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
247 echo "imap-log-$MH_TEST_IMAP_LOG_LEVEL-to: $MH_TEST_DIR/$profile/imap.d/log.$MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
248 fi
249
250 start
251 if ! grep -q 'Connection to "127.0.0.1:255" failed: Connection refused' "$actual"; then
252 cat "$actual" >&2
253 fail 'ECONN not reported'
254 fi
255
256 ################################################################################
257 # Start the server for the rest.
258
259 start_stunnel imap "$imapd"
260
261 arith_eval $arith_val + 1
262 setup_test_without_data profile$arith_val
263 trap imap_cleanup 0
264
265 cat >> "$MH" <<EOF
266 credentials: file-nopermcheck:$MH_TEST_DIR/netrc
267 Managed-Folders: imap
268 imap-log-level: error
269 imap-log-error-to: $MH_TEST_DIR/error
270 imap-manager: $MH_OBJ_DIR/target/debug/imap-folder-manager
271 imap-socket: $MH_TEST_DIR/$profile/imap.d/socket
272 imap-db: $MH_TEST_DIR/$profile/imap.d
273 imap-host: 127.0.0.1
274 imap-mailbox-root: test/
275 imap-port: $fakeinetd_port
276 imap-tls: implicit but don't verify the cert i already did out of band
277 imap-auth: SASL
278 imap-user: testuser
279 EOF
280 if [ -n "$MH_TEST_IMAP_LOG_LEVEL" ]; then
281 echo "imap-log-level: $MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
282 echo "imap-log-$MH_TEST_IMAP_LOG_LEVEL-to: $MH_TEST_DIR/$profile/imap.d/log.$MH_TEST_IMAP_LOG_LEVEL" >> "$MH"
283 fi
284
285 ################################################################################
286 start_test 'rudely close pre-greeting, implicit TLS'
287
288 cat > "$imapd" <<EOSCRIPT
289 #!/bin/sh
290 exit 1
291 EOSCRIPT
292
293 start
294 if ! grep -q 'TLS peer closed remote connection' "$actual"; then
295 cat "$actual" >&2
296 fail 'EOF error not reported'
297 fi
298
299 ################################################################################
300 start_test 'no SASL-IR capability'
301
302 cat > "$imapd" <<EOSCRIPT
303 #!/bin/sh
304 cat <<EOF
305 * OK [CAPABILITY IMAP4REV1 AUTH=PLAIN]
306 EOF
307 while read line; do :; done
308 EOSCRIPT
309
310 start
311 if ! grep -q "server doesn't offer SASL-IR; let us know and we'll support this configuration" "$actual"; then
312 cat "$actual" >&2
313 fail 'missing SASL-IR not reported'
314 fi
315
316 ################################################################################
317 start_test 'no UIDPLUS capability'
318
319 cat > "$imapd" <<EOSCRIPT
320 #!/bin/sh
321 cat <<EOF
322 * OK [CAPABILITY IMAP4REV1 SASL-IR AUTH=PLAIN] greeting
323 1 OK [CAPABILITY IMAP4REV1] AUTENTICATE response
324 EOF
325 while read line; do :; done
326 EOSCRIPT
327
328 start
329 if ! grep -q "server doesn't offer required capability UIDPLUS" "$actual"; then
330 cat "$actual" >&2
331 fail 'missing UIDPLUS not reported'
332 fi
333
334 ################################################################################
335 start_test 'protocol violation in greeting, implicit TLS'
336
337 # Greets with simply "trash\r\n".
338
339 cat > "$imapd" <<EOSCRIPT
340 #!/bin/sh
341 cat <<EOF
342 trash
343 EOF
344 while read line; do :; done
345 EOSCRIPT
346
347 start
348 if ! grep -q 'protocol error: no space in response' "$actual"; then
349 cat "$actual" >&2
350 fail 'protocol error not reported'
351 fi
352
353 ################################################################################
354 start_test 'protocol violation in AUTHENTICATE response'
355
356 # Responds to the AUTHENTICATE command with simply "trash\r\n".
357
358 cat > "$imapd" <<EOSCRIPT
359 #!/bin/sh
360 cat <<EOF
361 * OK [CAPABILITY IMAP4REV1 SASL-IR AUTH=PLAIN] greeting
362 trash
363 EOF
364 while read line; do :; done
365 EOSCRIPT
366
367 start
368 if ! grep -q 'protocol error: no space in response' "$actual"; then
369 cat "$actual" >&2
370 fail 'protocol error not reported'
371 fi
372
373 finish_test
374 exit $failed