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