Moved test-mhmail to its own test directory.
[mmh] / test / mhmail / 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/^Resent-Date:.*/Resent-Date:/' \
60         -e 's/^Message-ID:.*/Message-ID:/' "$actual" > "$actual".nodate
61     rm -f "$actual"
62
63     check "$actual".nodate "$1"
64 }
65
66 expected=$MH_TEST_DIR/test-mhmail$$.expected
67 expected_err=$MH_TEST_DIR/test-mhmail$$.expected_err
68 actual=$MH_TEST_DIR/test-mhmail$$.actual
69 actual_err=$MH_TEST_DIR/test-mhmail$$.actual_err
70
71
72 # check -help
73 # Verified behavior consistent with compiled sendmail.
74 cat >$expected <<EOF
75 Usage: mhmail [addrs ... [switches]]
76   switches are:
77   -b(ody) text
78   -c(c) addrs ...
79   -f(rom) addr
80   -hea(der-field) name:field [-hea(der-field) name:field] ...
81   -su(bject) text
82   -r(esent)
83   -pr(ofile)
84   -se(nd)
85   -nose(nd)
86   -v(ersion)
87   -hel(p)
88   and all post(8)/send(1) switches
89 EOF
90
91 mhmail -help >$actual 2>&1
92 check $expected $actual
93
94
95 # check -version
96 # Verified same behavior as compiled mhmail.
97 case `mhmail -v` in
98   mhmail\ --*) ;;
99   *           ) echo "$0: mhmail -v generated unexpected output" 1>&2
100                 failed=`expr ${failed:-0} + 1`;;
101 esac
102
103
104 # check with no arguments
105 # That will just run inc, which we don't want to do anything,
106 # so tell inc to just display its version.
107 # Verified same behavior as compiled mhmail.
108 printf "inc: -version\n" >> $MH
109 case `mhmail` in
110   inc\ --*) ;;
111   *           ) echo "$0: mhmail generated unexpected output" 1>&2
112                 failed=`expr ${failed:-0} + 1`;;
113 esac
114
115
116 # check -nosend
117 # Not supported by compiled mhmail.
118 mhmail -nosend recipient@example.com -from sender@localhost \
119   -server 127.0.0.1 -port $localport -body '' >"$actual" 2>"$actual_err"
120
121 tmpfil=`head -1 $actual | sed -e 's/://'`
122
123 cat > "$expected" <<EOF
124 To: recipient@example.com
125 From: sender@localhost
126
127
128 EOF
129
130 cat > "$expected_err" <<EOF
131 EOF
132
133 check "$expected" "$actual"
134 check "$expected_err" "$actual_err"
135 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
136
137
138 # check -send
139 # Not supported by compiled mhmail.
140 cat > "$expected" <<EOF
141 EHLO nosuchhost.example.com
142 MAIL FROM:<sender@localhost>
143 RCPT TO:<recipient@example.com>
144 DATA
145 To: recipient@example.com
146 From: sender@localhost
147 Date:
148
149 message
150 .
151 QUIT
152 EOF
153
154 test_mhmail "$expected" "-from sender@localhost -nosend -send" '|' message
155 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
156
157
158 # check -from
159 # Verified same behavior as compiled mhmail.
160 cat > "$expected" <<EOF
161 EHLO nosuchhost.example.com
162 MAIL FROM:<sender@localhost>
163 RCPT TO:<recipient@example.com>
164 DATA
165 To: recipient@example.com
166 From: sender@localhost
167 Date:
168
169 message
170 .
171 QUIT
172 EOF
173
174 test_mhmail "$expected" "-from sender@localhost" '|' message
175 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
176
177
178 # check -from and -body
179 # Verified same behavior as compiled mhmail.
180 cat > "$expected" <<EOF
181 EHLO nosuchhost.example.com
182 MAIL FROM:<sender@localhost>
183 RCPT TO:<recipient@example.com>
184 DATA
185 To: recipient@example.com
186 From: sender@localhost
187 Date:
188
189 body
190 .
191 QUIT
192 EOF
193
194 test_mhmail "$expected" "-from sender@localhost" -b body
195 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
196
197
198 # check -from and -cc
199 # Verified same behavior as compiled mhmail.
200 cat > "$expected" <<EOF
201 EHLO nosuchhost.example.com
202 MAIL FROM:<sender@localhost>
203 RCPT TO:<recipient@example.com>
204 RCPT TO:<recipient2@example.com>
205 DATA
206 To: recipient@example.com
207 Cc: recipient2@example.com
208 From: sender@localhost
209 Date:
210
211 message
212 .
213 QUIT
214 EOF
215
216 test_mhmail "$expected" \
217     "-from sender@localhost -cc recipient2@example.com" '|' message
218 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
219
220
221 # check -from and multiple -cc addresses
222 # Verified same behavior as compiled mhmail.
223 cat > "$expected" <<EOF
224 EHLO nosuchhost.example.com
225 MAIL FROM:<sender@localhost>
226 RCPT TO:<recipient@example.com>
227 RCPT TO:<recipient2@example.com>
228 RCPT TO:<recipient3@example.com>
229 RCPT TO:<recipient4@example.com>
230 DATA
231 To: recipient@example.com
232 Cc: recipient2@example.com, recipient3@example.com,
233     recipient4@example.com
234 From: sender@localhost
235 Date:
236
237 message
238 .
239 QUIT
240 EOF
241
242 test_mhmail "$expected" \
243     '-from sender@localhost -cc recipient2@example.com recipient3@example.com '\
244 'recipient4@example.com' '|' message
245 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
246
247
248 # check -from and -subject
249 # Verified same behavior as compiled mhmail.
250 cat > "$expected" <<EOF
251 EHLO nosuchhost.example.com
252 MAIL FROM:<sender@localhost>
253 RCPT TO:<recipient@example.com>
254 DATA
255 To: recipient@example.com
256 Subject: Test
257 From: sender@localhost
258 Date:
259
260 message
261 .
262 QUIT
263 EOF
264
265 test_mhmail "$expected" '-from sender@localhost -subject Test' '|' message
266 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
267
268
269 # check -from and -profile
270 # Show that -profile causes mhmail to 1) read the profile and
271 # 2) use send(1) by added a send switch to the profile and
272 # verifying that it gets used.
273 # Not supported by compiled mhmail.
274 printf "send: -msgid\n" >> $MH
275
276 cat > "$expected" <<EOF
277 EHLO nosuchhost.example.com
278 MAIL FROM:<sender@localhost>
279 RCPT TO:<recipient@example.com>
280 DATA
281 To: recipient@example.com
282 From: sender@localhost
283 Date:
284 Message-ID:
285
286 message
287 .
288 QUIT
289 EOF
290
291 test_mhmail "$expected" '-from sender@localhost -profile' '|' message
292 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
293
294
295 # check repeated -from and -subject arguments
296 # Verified same behavior as compiled mhmail.
297 cat > "$expected" <<EOF
298 EHLO nosuchhost.example.com
299 MAIL FROM:<sender2@localhost>
300 RCPT TO:<recipient@example.com>
301 DATA
302 To: recipient@example.com
303 Subject: Subject2
304 From: sender2@localhost
305 Date:
306
307 message
308 .
309 QUIT
310 EOF
311
312 test_mhmail "$expected" '-from sender@localhost -from sender2@localhost '\
313 '-subject Subject1 -subject Subject2' -b message
314 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
315
316 # check repeated -body arguments
317 # Verified same behavior as compiled mhmail.
318 cat > "$expected" <<EOF
319 EHLO nosuchhost.example.com
320 MAIL FROM:<sender@localhost>
321 RCPT TO:<recipient@example.com>
322 DATA
323 To: recipient@example.com
324 From: sender@localhost
325 Date:
326
327 body2
328 .
329 QUIT
330 EOF
331
332 test_mhmail "$expected" "-from sender@localhost -body body1" -b body2
333 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
334
335
336 # check multiple -cc arguments
337 # Verified same behavior as compiled mhmail.
338 cat > "$expected" <<EOF
339 EHLO nosuchhost.example.com
340 MAIL FROM:<sender@localhost>
341 RCPT TO:<recipient@example.com>
342 RCPT TO:<cc1@example.com>
343 RCPT TO:<cc2@example.com>
344 DATA
345 To: recipient@example.com
346 Cc: cc1@example.com, cc2@example.com
347 From: sender@localhost
348 Date:
349
350 message
351 .
352 QUIT
353 EOF
354
355 test_mhmail "$expected" \
356   '-from sender@localhost -cc cc1@example.com -cc cc2@example.com' -b message
357 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
358
359
360 # check separated -cc arguments
361 # Verified same behavior as compiled mhmail.
362 cat > "$expected" <<EOF
363 EHLO nosuchhost.example.com
364 MAIL FROM:<sender@localhost>
365 RCPT TO:<recipient@example.com>
366 RCPT TO:<cc1@example.com>
367 RCPT TO:<cc2@example.com>
368 DATA
369 To: recipient@example.com
370 Cc: cc1@example.com, cc2@example.com
371 Subject: Test
372 From: sender@localhost
373 Date:
374
375 message
376 .
377 QUIT
378 EOF
379
380 test_mhmail "$expected" \
381   '-from sender@localhost -cc cc1@example.com -subject Test cc2@example.com' \
382   -b message
383 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
384
385
386 # check with no newline on stdin
387 # Shows different behavior than compiled mhmail, which was silent in this case.
388 cat > "$expected" <<EOF
389 EOF
390
391 cat > "$expected_err" <<EOF
392 mhmail: empty message not sent, use -body '' to force.
393 EOF
394
395 set +e
396 printf '' | mhmail recipient@example.com -server 127.0.0.1 -port $localport \
397   >"$actual" 2>"$actual_err"
398 set -e
399
400 check "$expected" "$actual"
401 check "$expected_err" "$actual_err"
402 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
403
404
405 # check with one newline on stdin
406 # Verified same behavior as compiled mhmail.
407 cat > "$expected" <<EOF
408 EHLO nosuchhost.example.com
409 MAIL FROM:<sender@localhost>
410 RCPT TO:<recipient@example.com>
411 DATA
412 To: recipient@example.com
413 From: sender@localhost
414 Date:
415
416
417 .
418 QUIT
419 EOF
420
421 test_mhmail "$expected" '-from sender@localhost' '|' '\n'
422 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
423
424
425 # check with multiple newlines 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
437
438
439 .
440 QUIT
441 EOF
442
443 test_mhmail "$expected" '-from sender@localhost' '|' '\n\n\n'
444 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
445
446
447 # check with text and no trailing newline on stdin
448 # Verified same behavior as compiled mhmail.
449 cat > "$expected" <<EOF
450 EHLO nosuchhost.example.com
451 MAIL FROM:<sender@localhost>
452 RCPT TO:<recipient@example.com>
453 DATA
454 To: recipient@example.com
455 From: sender@localhost
456 Date:
457
458 no newline in input
459 .
460 QUIT
461 EOF
462
463 test_mhmail "$expected" '-from sender@localhost' '|' 'no newline in input'
464 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
465
466
467 # check with text and multiple trailing blank lines on stdin
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 here's some text
479
480
481 .
482 QUIT
483 EOF
484
485 test_mhmail "$expected" '-from sender@localhost' '|' "here's some text\n\n\n"
486 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
487
488
489 # check with no newline to -body
490 # Verified same behavior as compiled mhmail.
491 cat > "$expected" <<EOF
492 EHLO nosuchhost.example.com
493 MAIL FROM:<sender@localhost>
494 RCPT TO:<recipient@example.com>
495 DATA
496 To: recipient@example.com
497 From: sender@localhost
498 Date:
499
500
501 .
502 QUIT
503 EOF
504
505 test_mhmail "$expected" '-from sender@localhost' -b ''
506 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
507
508
509 # check with one newline to -body
510 # Shows different behavior than compiled mhmail, which suppressed the newline.
511 cat > "$expected" <<EOF
512 EHLO nosuchhost.example.com
513 MAIL FROM:<sender@localhost>
514 RCPT TO:<recipient@example.com>
515 DATA
516 To: recipient@example.com
517 From: sender@localhost
518 Date:
519
520
521
522 .
523 QUIT
524 EOF
525
526 test_mhmail "$expected" '-from sender@localhost' -b '
527 '
528 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
529
530
531 # check with multiple newlines to -body
532 # Shows different behavior than compiled mhmail, which suppressed one
533 #   of the newlines.
534 cat > "$expected" <<EOF
535 EHLO nosuchhost.example.com
536 MAIL FROM:<sender@localhost>
537 RCPT TO:<recipient@example.com>
538 DATA
539 To: recipient@example.com
540 From: sender@localhost
541 Date:
542
543
544
545
546
547 .
548 QUIT
549 EOF
550
551 test_mhmail "$expected" '-from sender@localhost' -b '
552
553
554 '
555 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
556
557
558 # check with text and no trailing newline to -body
559 # Verified same behavior as compiled mhmail.
560 cat > "$expected" <<EOF
561 EHLO nosuchhost.example.com
562 MAIL FROM:<sender@localhost>
563 RCPT TO:<recipient@example.com>
564 DATA
565 To: recipient@example.com
566 From: sender@localhost
567 Date:
568
569 no newline in input
570 .
571 QUIT
572 EOF
573
574 test_mhmail "$expected" '-from sender@localhost' -b 'no newline in input'
575 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
576
577
578 # check with text and multiple trailing blank lines to -body
579 # Shows different behavior than compiled mhmail, which suppressed one
580 #   of the newlines.
581 cat > "$expected" <<EOF
582 EHLO nosuchhost.example.com
583 MAIL FROM:<sender@localhost>
584 RCPT TO:<recipient@example.com>
585 DATA
586 To: recipient@example.com
587 From: sender@localhost
588 Date:
589
590 here's some text
591
592
593 .
594 QUIT
595 EOF
596
597 test_mhmail "$expected" '-from sender@localhost' -b "here's some text
598
599 "
600 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
601
602
603 # check -resent
604 # Verified same behavior as compiled mhmail.
605 cat > "$expected" <<EOF
606 EHLO nosuchhost.example.com
607 MAIL FROM:<orig_recipient@example.com>
608 RCPT TO:<recipient@example.com>
609 DATA
610 Resent-To: recipient@example.com
611 Resent-From: orig_recipient@example.com
612 To: recipient@example.com
613 From: sender@localhost
614 Date:
615 Resent-Date:
616
617 please resend this message, 1
618 .
619 QUIT
620 EOF
621
622 test_mhmail "$expected" '-from orig_recipient@example.com -resent' \
623   -b 'To: recipient@example.com
624 From: sender@localhost
625 Date: Sat Jun 16 18:35:15 -0500
626
627 please resend this message, 1'
628
629 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
630
631 # check -resent -profile, using stdin
632 # Not supported by compiled mhmail.
633 cat > "$expected" <<EOF
634 EHLO nosuchhost.example.com
635 MAIL FROM:<orig_recipient@example.com>
636 RCPT TO:<recipient@example.com>
637 DATA
638 To: recipient@example.com
639 From: sender@localhost
640 Date:
641 Resent-To: recipient@example.com
642 Resent-From: orig_recipient@example.com
643 Resent-Date:
644
645 please resend this message, 2
646 .
647 QUIT
648 EOF
649
650 test_mhmail "$expected" \
651   '-from orig_recipient@example.com -resent -profile -nomsgid' \
652   '|' 'To: recipient@example.com
653 From: sender@localhost
654 Date: Sat Jun 16 18:35:15 -0500
655
656 please resend this message, 2'
657
658 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
659
660
661 # check -resent -profile, using -b
662 # Not supported by compiled mhmail.
663 cat > "$expected" <<EOF
664 EHLO nosuchhost.example.com
665 MAIL FROM:<orig_recipient@example.com>
666 RCPT TO:<recipient@example.com>
667 DATA
668 To: recipient@example.com
669 From: sender@localhost
670 Date:
671 Resent-To: recipient@example.com
672 Resent-From: orig_recipient@example.com
673 Resent-Date:
674
675 please resend this message, 3
676 .
677 QUIT
678 EOF
679
680 test_mhmail "$expected" \
681   '-from orig_recipient@example.com -resent -profile -nomsgid' \
682   -b 'To: recipient@example.com
683 From: sender@localhost
684 Date: Sat Jun 16 18:35:15 -0500
685
686 please resend this message, 3'
687
688 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
689
690
691 # check -header-field.
692 # Not supported by compiled mhmail.
693 cat > "$expected" <<EOF
694 EHLO nosuchhost.example.com
695 MAIL FROM:<sender@example.com>
696 RCPT TO:<recipient@example.com>
697 DATA
698 To: recipient@example.com
699 From: sender@example.com
700 User-Agent: nmh
701 Date:
702
703 with added header field
704 .
705 QUIT
706 EOF
707
708 test_mhmail "$expected" \
709   '-from sender@example.com -header-field User-Agent:nmh' \
710   -b 'with added header field'
711
712 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
713
714
715 # check multiple -header-fields.
716 # Not supported by compiled mhmail.
717 cat > "$expected" <<EOF
718 EHLO nosuchhost.example.com
719 MAIL FROM:<sender@example.com>
720 RCPT TO:<recipient@example.com>
721 DATA
722 To: recipient@example.com
723 From: sender@example.com
724 MIME-Version: 1.0
725 Content-Type: text/plain;charset=utf-8
726 Content-Transfer-Encoding: 8bit
727 Date:
728
729 with added header fields
730 .
731 QUIT
732 EOF
733
734 test_mhmail "$expected" \
735   "-from sender@example.com -header-field MIME-Version:1.0 \
736 -header-field Content-Type:text/plain;charset=utf-8 \
737 -header-field Content-Transfer-Encoding:8bit" \
738   -b 'with added header fields'
739
740 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
741
742
743 exit ${failed:-0}