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