Replaced compiled mhmail with script that was formerly named mhmail.in.
[mmh] / test / post / test-mhmail
1 #!/bin/sh
2 ######################################################
3 #
4 # Test mhmail
5 #
6 ######################################################
7
8 set -e
9
10 if test -z "${MH_OBJ_DIR}"; then
11     srcdir=`dirname $0`/../..
12     MH_OBJ_DIR=`cd $srcdir && pwd`; export MH_OBJ_DIR
13 fi
14
15 . "${srcdir}/test/post/test-post-common.sh"
16
17 # Customize test_post () for use with mhmail.
18 # $1 is expected output file, provided by caller
19 # $2 is mhmail switches, except for -body
20 # $3 of -b signifies use -body switch, | signifies provide body on stdin
21 # $4 contains message body.  When using stdin, can contain printf(1) format
22 #    specifiers.
23 test_mhmail ()
24 {
25     "${MH_OBJ_DIR}/test/fakesmtp" "$actual" $localport &
26     pid="$!"
27
28     # The server doesn't always come up fast enough, so sleep and
29     # retry a few times if it fails...
30     status=1
31     for i in 0 1 2 3 4 5 6 7 8 9; do
32         if [ $3 = '|' ]; then
33           if printf "$4" | mhmail recipient@example.com $2 \
34              -server 127.0.0.1 -port $localport; then
35               status=0
36               break
37           fi
38         else
39           if mhmail recipient@example.com $2 -body "$4" \
40              -server 127.0.0.1 -port $localport; then
41               status=0
42               break
43           fi
44         fi
45         sleep 1
46     done
47     [ $status -eq 0 ] || exit 1
48
49     wait ${pid}
50
51     #
52     # It's hard to calculate the exact Date: header post is going to
53     # use, so we'll just use sed to remove the actual date so we can easily
54     # compare it against our "correct" output.  And same for
55     # Message-ID.
56     #
57
58     sed -e 's/^Date:.*/Date:/' \
59         -e 's/^Message-ID:.*/Message-ID:/' "$actual" > "$actual".nodate
60     rm -f "$actual"
61
62     check "$actual".nodate "$1"
63 }
64
65 expected=$MH_TEST_DIR/test-mhmail$$.expected
66 expected_err=$MH_TEST_DIR/test-mhmail$$.expected_err
67 actual=$MH_TEST_DIR/test-mhmail$$.actual
68 actual_err=$MH_TEST_DIR/test-mhmail$$.actual_err
69
70
71 # check -help
72 # Verified behavior consistent with compiled sendmail.
73 cat >$expected <<EOF
74 Usage: mhmail [addrs ... [switches]]
75   switches are:
76   -b(ody) text
77   -c(c) addrs ...
78   -f(rom) addr
79   -s(ubject) text
80   -pr(ofile)
81   -v(ersion)
82   -h(elp)
83   -d(ebug)
84   and all post(8)/send(1) switches
85 EOF
86
87 mhmail -help >$actual 2>&1
88 check $expected $actual
89
90
91 # check -version
92 # Verified same behavior as compiled mhmail.
93 case `mhmail -v` in
94   mhmail\ --*) ;;
95   *           ) echo "$0: mhmail -v generated unexpected output" 1>&2
96                 failed=`expr ${failed:-0} + 1`;;
97 esac
98
99
100 # check with no arguments
101 # That will just run inc, which we don't want to do anything,
102 # so tell inc to just display its version.
103 # Verified same behavior as compiled mhmail.
104 printf "inc: -version\n" >> $MH
105 case `mhmail` in
106   inc\ --*) ;;
107   *           ) echo "$0: mhmail generated unexpected output" 1>&2
108                 failed=`expr ${failed:-0} + 1`;;
109 esac
110
111
112 # check -debug
113 # Not supported by compiled mhmail.
114 mhmail -debug recipient@example.com -from sender@localhost \
115   -server 127.0.0.1 -port $localport -body '' >"$actual" 2>"$actual_err"
116
117 tmpfil=`head -1 $actual | sed -e 's/://'`
118
119 cat > "$expected" <<EOF
120 ${tmpfil}:
121 To: recipient@example.com
122 From: sender@localhost
123
124
125 $MH_INST_DIR$auxexecdir/post ${tmpfil} -server 127.0.0.1 -port $localport
126 EOF
127
128 cat > "$expected_err" <<EOF
129 EOF
130
131 check "$expected" "$actual"
132 check "$expected_err" "$actual_err"
133 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
134
135
136 # check -from
137 # Verified same behavior as compiled mhmail.
138 cat > "$expected" <<EOF
139 EHLO nosuchhost.example.com
140 MAIL FROM:<sender@localhost>
141 RCPT TO:<recipient@example.com>
142 DATA
143 To: recipient@example.com
144 From: sender@localhost
145 Date:
146
147 message
148 .
149 QUIT
150 EOF
151
152 test_mhmail "$expected" "-from sender@localhost" '|' message
153 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
154
155
156 # check -from and -body
157 # Verified same behavior as compiled mhmail.
158 cat > "$expected" <<EOF
159 EHLO nosuchhost.example.com
160 MAIL FROM:<sender@localhost>
161 RCPT TO:<recipient@example.com>
162 DATA
163 To: recipient@example.com
164 From: sender@localhost
165 Date:
166
167 body
168 .
169 QUIT
170 EOF
171
172 test_mhmail "$expected" "-from sender@localhost" -b body
173 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
174
175
176 # check -from and -cc
177 # Verified same behavior as compiled mhmail.
178 cat > "$expected" <<EOF
179 EHLO nosuchhost.example.com
180 MAIL FROM:<sender@localhost>
181 RCPT TO:<recipient@example.com>
182 RCPT TO:<recipient2@example.com>
183 DATA
184 To: recipient@example.com
185 Cc: recipient2@example.com
186 From: sender@localhost
187 Date:
188
189 message
190 .
191 QUIT
192 EOF
193
194 test_mhmail "$expected" \
195     "-from sender@localhost -cc recipient2@example.com" '|' message
196 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
197
198
199 # check -from and multiple -cc addresses
200 # Verified same behavior as compiled mhmail.
201 cat > "$expected" <<EOF
202 EHLO nosuchhost.example.com
203 MAIL FROM:<sender@localhost>
204 RCPT TO:<recipient@example.com>
205 RCPT TO:<recipient2@example.com>
206 RCPT TO:<recipient3@example.com>
207 RCPT TO:<recipient4@example.com>
208 DATA
209 To: recipient@example.com
210 Cc: recipient2@example.com, recipient3@example.com,
211     recipient4@example.com
212 From: sender@localhost
213 Date:
214
215 message
216 .
217 QUIT
218 EOF
219
220 test_mhmail "$expected" \
221     "-from sender@localhost -cc recipient2@example.com recipient3@example.com recipient4@example.com" '|' message
222 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
223
224
225 # check -from and -subject
226 # Verified same behavior as compiled mhmail.
227 cat > "$expected" <<EOF
228 EHLO nosuchhost.example.com
229 MAIL FROM:<sender@localhost>
230 RCPT TO:<recipient@example.com>
231 DATA
232 To: recipient@example.com
233 Subject: Test
234 From: sender@localhost
235 Date:
236
237 message
238 .
239 QUIT
240 EOF
241
242 test_mhmail "$expected" \
243   "-from sender@localhost -subject Test" '|' message
244 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
245
246
247 # check -from and -profile
248 # Show that -profile causes mhmail to 1) read the profile and
249 # 2) use send(1) by added a send switch to the profile and
250 # verifying that it gets used.
251 # Not supported by compiled mhmail.
252 printf "send: -msgid\n" >> $MH
253
254 cat > "$expected" <<EOF
255 EHLO nosuchhost.example.com
256 MAIL FROM:<sender@localhost>
257 RCPT TO:<recipient@example.com>
258 DATA
259 To: recipient@example.com
260 From: sender@localhost
261 Date:
262 Message-ID:
263
264 message
265 .
266 QUIT
267 EOF
268
269 test_mhmail "$expected" \
270   "-from sender@localhost -profile" '|' message
271 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
272
273
274 # check repeated -from and -subject arguments
275 # Verified same behavior as compiled mhmail.
276 cat > "$expected" <<EOF
277 EHLO nosuchhost.example.com
278 MAIL FROM:<sender2@localhost>
279 RCPT TO:<recipient@example.com>
280 DATA
281 To: recipient@example.com
282 Subject: Subject2
283 From: sender2@localhost
284 Date:
285
286 message
287 .
288 QUIT
289 EOF
290
291 test_mhmail "$expected" "-from sender@localhost -from sender2@localhost \
292 -subject Subject1 -subject Subject2" -b message
293 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
294
295 # check repeated -body arguments
296 # Verified same behavior as compiled mhmail.
297 cat > "$expected" <<EOF
298 EHLO nosuchhost.example.com
299 MAIL FROM:<sender@localhost>
300 RCPT TO:<recipient@example.com>
301 DATA
302 To: recipient@example.com
303 From: sender@localhost
304 Date:
305
306 body2
307 .
308 QUIT
309 EOF
310
311 test_mhmail "$expected" "-from sender@localhost -body body1" -b body2
312 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
313
314
315 # check multiple -cc arguments
316 # Verified same behavior as compiled mhmail.
317 cat > "$expected" <<EOF
318 EHLO nosuchhost.example.com
319 MAIL FROM:<sender@localhost>
320 RCPT TO:<recipient@example.com>
321 RCPT TO:<cc1@example.com>
322 RCPT TO:<cc2@example.com>
323 DATA
324 To: recipient@example.com
325 Cc: cc1@example.com, cc2@example.com
326 From: sender@localhost
327 Date:
328
329 message
330 .
331 QUIT
332 EOF
333
334 test_mhmail "$expected" "-from sender@localhost \
335 -cc cc1@example.com -cc cc2@example.com" -b message
336 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
337
338
339 # check separated -cc arguments
340 # Verified same behavior as compiled mhmail.
341 cat > "$expected" <<EOF
342 EHLO nosuchhost.example.com
343 MAIL FROM:<sender@localhost>
344 RCPT TO:<recipient@example.com>
345 RCPT TO:<cc1@example.com>
346 RCPT TO:<cc2@example.com>
347 DATA
348 To: recipient@example.com
349 Cc: cc1@example.com, cc2@example.com
350 Subject: Test
351 From: sender@localhost
352 Date:
353
354 message
355 .
356 QUIT
357 EOF
358
359 test_mhmail "$expected" "-from sender@localhost \
360 -cc cc1@example.com -subject Test cc2@example.com" -b message
361 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
362
363
364 # check with no newline on stdin
365 # Shows different behavior than compiled mhmail, which was silent in this case.
366 cat > "$expected" <<EOF
367 EOF
368
369 cat > "$expected_err" <<EOF
370 mhmail: empty message not sent, use -body '' to force.
371 EOF
372
373 set +e
374 printf '' | mhmail recipient@example.com -server 127.0.0.1 -port $localport \
375   >"$actual" 2>"$actual_err"
376 set -e
377
378 check "$expected" "$actual"
379 check "$expected_err" "$actual_err"
380 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
381
382
383 # check with one newline on stdin
384 # Verified same behavior as compiled mhmail.
385 cat > "$expected" <<EOF
386 EHLO nosuchhost.example.com
387 MAIL FROM:<sender@localhost>
388 RCPT TO:<recipient@example.com>
389 DATA
390 To: recipient@example.com
391 From: sender@localhost
392 Date:
393
394
395 .
396 QUIT
397 EOF
398
399 test_mhmail "$expected" "-from sender@localhost" '|' '\n'
400 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
401
402
403 # check with multiple newlines on stdin
404 # Verified same behavior as compiled mhmail.
405 cat > "$expected" <<EOF
406 EHLO nosuchhost.example.com
407 MAIL FROM:<sender@localhost>
408 RCPT TO:<recipient@example.com>
409 DATA
410 To: recipient@example.com
411 From: sender@localhost
412 Date:
413
414
415
416
417 .
418 QUIT
419 EOF
420
421 test_mhmail "$expected" "-from sender@localhost" '|' '\n\n\n'
422 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
423
424
425 # check with text and no trailing newline on stdin
426 # Verified same behavior as compiled mhmail.
427 cat > "$expected" <<EOF
428 EHLO nosuchhost.example.com
429 MAIL FROM:<sender@localhost>
430 RCPT TO:<recipient@example.com>
431 DATA
432 To: recipient@example.com
433 From: sender@localhost
434 Date:
435
436 no newline in input
437 .
438 QUIT
439 EOF
440
441 test_mhmail "$expected" "-from sender@localhost" '|' 'no newline in input'
442 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
443
444
445 # check with text and multiple trailing blank lines on stdin
446 # Verified same behavior as compiled mhmail.
447 cat > "$expected" <<EOF
448 EHLO nosuchhost.example.com
449 MAIL FROM:<sender@localhost>
450 RCPT TO:<recipient@example.com>
451 DATA
452 To: recipient@example.com
453 From: sender@localhost
454 Date:
455
456 here's some text
457
458
459 .
460 QUIT
461 EOF
462
463 test_mhmail "$expected" "-from sender@localhost" '|' "here's some text\n\n\n"
464 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
465
466
467 # check with no newline to -body
468 # Verified same behavior as compiled mhmail.
469 cat > "$expected" <<EOF
470 EHLO nosuchhost.example.com
471 MAIL FROM:<sender@localhost>
472 RCPT TO:<recipient@example.com>
473 DATA
474 To: recipient@example.com
475 From: sender@localhost
476 Date:
477
478
479 .
480 QUIT
481 EOF
482
483 test_mhmail "$expected" "-from sender@localhost" -b ''
484 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
485
486
487 # check with one newline to -body
488 # Shows different behavior than compiled mhmail, which suppressed the newline.
489 cat > "$expected" <<EOF
490 EHLO nosuchhost.example.com
491 MAIL FROM:<sender@localhost>
492 RCPT TO:<recipient@example.com>
493 DATA
494 To: recipient@example.com
495 From: sender@localhost
496 Date:
497
498
499
500 .
501 QUIT
502 EOF
503
504 test_mhmail "$expected" "-from sender@localhost" -b '
505 '
506 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
507
508
509 # check with multiple newlines to -body
510 # Shows different behavior than compiled mhmail, which suppressed one
511 #   of the newlines.
512 cat > "$expected" <<EOF
513 EHLO nosuchhost.example.com
514 MAIL FROM:<sender@localhost>
515 RCPT TO:<recipient@example.com>
516 DATA
517 To: recipient@example.com
518 From: sender@localhost
519 Date:
520
521
522
523
524
525 .
526 QUIT
527 EOF
528
529 test_mhmail "$expected" "-from sender@localhost" -b '
530
531
532 '
533 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
534
535
536 # check with text and no trailing newline to -body
537 # Verified same behavior as compiled mhmail.
538 cat > "$expected" <<EOF
539 EHLO nosuchhost.example.com
540 MAIL FROM:<sender@localhost>
541 RCPT TO:<recipient@example.com>
542 DATA
543 To: recipient@example.com
544 From: sender@localhost
545 Date:
546
547 no newline in input
548 .
549 QUIT
550 EOF
551
552 test_mhmail "$expected" "-from sender@localhost" -b 'no newline in input'
553 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
554
555
556 # check with text and multiple trailing blank lines to -body
557 # Shows different behavior than compiled mhmail, which suppressed one
558 #   of the newlines.
559 cat > "$expected" <<EOF
560 EHLO nosuchhost.example.com
561 MAIL FROM:<sender@localhost>
562 RCPT TO:<recipient@example.com>
563 DATA
564 To: recipient@example.com
565 From: sender@localhost
566 Date:
567
568 here's some text
569
570
571 .
572 QUIT
573 EOF
574
575 test_mhmail "$expected" "-from sender@localhost" -b "here's some text
576
577 "
578 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
579
580
581 exit ${failed:-0}