Added checks for missing mandatory arguments to mhmail.
[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:/' \
61         -e 's/^Content-ID:.*/Content-ID:/' "$actual" > "$actual".nodate
62     rm -f "$actual"
63
64     check "$actual".nodate "$1"
65 }
66
67 expected=$MH_TEST_DIR/test-mhmail$$.expected
68 expected_err=$MH_TEST_DIR/test-mhmail$$.expected_err
69 actual=$MH_TEST_DIR/test-mhmail$$.actual
70 actual_err=$MH_TEST_DIR/test-mhmail$$.actual_err
71
72
73 # check -help
74 # Verified behavior consistent with compiled sendmail.
75 cat >$expected <<EOF
76 Usage: mhmail [addrs ... [switches]]
77   switches are:
78   -at(tach) file [-at(tach) file] ...
79   -b(ody) text
80   -c(c) addrs ...
81   -f(rom) addr
82   -hea(derfield) name:value [-hea(derfield) name:value] ...
83   -su(bject) text
84   -r(esent)
85   -pr(ofile)
86   -se(nd)
87   -nose(nd)
88   -v(ersion)
89   -hel(p)
90   and all post(8)/send(1) switches
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; 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; 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; 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 arguments
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 arguments
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 arguments
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 arguments
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 with no newline on stdin
403 # Shows different behavior than compiled mhmail, which was silent in this case.
404 cat > "$expected" <<EOF
405 EOF
406
407 cat > "$expected_err" <<EOF
408 mhmail: empty message not sent, use -body '' to force.
409 EOF
410
411 set +e
412 printf '' | mhmail recipient@example.com -server 127.0.0.1 -port $localport \
413   >"$actual" 2>"$actual_err"
414 set -e
415
416 check "$expected" "$actual"
417 check "$expected_err" "$actual_err"
418 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
419
420
421 # check with one newline on stdin
422 # Verified same behavior as compiled mhmail.
423 cat > "$expected" <<EOF
424 EHLO nosuchhost.example.com
425 MAIL FROM:<sender@localhost>
426 RCPT TO:<recipient@example.com>
427 DATA
428 To: recipient@example.com
429 From: sender@localhost
430 Date:
431
432
433 .
434 QUIT
435 EOF
436
437 test_mhmail "$expected" '-from sender@localhost' '|' '\n'
438 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
439
440
441 # check with multiple newlines on stdin
442 # Verified same behavior as compiled mhmail.
443 cat > "$expected" <<EOF
444 EHLO nosuchhost.example.com
445 MAIL FROM:<sender@localhost>
446 RCPT TO:<recipient@example.com>
447 DATA
448 To: recipient@example.com
449 From: sender@localhost
450 Date:
451
452
453
454
455 .
456 QUIT
457 EOF
458
459 test_mhmail "$expected" '-from sender@localhost' '|' '\n\n\n'
460 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
461
462
463 # check with text and no trailing newline on stdin
464 # Verified same behavior as compiled mhmail.
465 cat > "$expected" <<EOF
466 EHLO nosuchhost.example.com
467 MAIL FROM:<sender@localhost>
468 RCPT TO:<recipient@example.com>
469 DATA
470 To: recipient@example.com
471 From: sender@localhost
472 Date:
473
474 no newline in input
475 .
476 QUIT
477 EOF
478
479 test_mhmail "$expected" '-from sender@localhost' '|' 'no newline in input'
480 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
481
482
483 # check with text and multiple trailing blank lines on stdin
484 # Verified same behavior as compiled mhmail.
485 cat > "$expected" <<EOF
486 EHLO nosuchhost.example.com
487 MAIL FROM:<sender@localhost>
488 RCPT TO:<recipient@example.com>
489 DATA
490 To: recipient@example.com
491 From: sender@localhost
492 Date:
493
494 here's some text
495
496
497 .
498 QUIT
499 EOF
500
501 test_mhmail "$expected" '-from sender@localhost' '|' "here's some text\n\n\n"
502 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
503
504
505 # check with no newline to -body
506 # Verified same behavior as compiled mhmail.
507 cat > "$expected" <<EOF
508 EHLO nosuchhost.example.com
509 MAIL FROM:<sender@localhost>
510 RCPT TO:<recipient@example.com>
511 DATA
512 To: recipient@example.com
513 From: sender@localhost
514 Date:
515
516
517 .
518 QUIT
519 EOF
520
521 test_mhmail "$expected" '-from sender@localhost' -b ''
522 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
523
524
525 # check with one newline to -body
526 # Shows different behavior than compiled mhmail, which suppressed the newline.
527 cat > "$expected" <<EOF
528 EHLO nosuchhost.example.com
529 MAIL FROM:<sender@localhost>
530 RCPT TO:<recipient@example.com>
531 DATA
532 To: recipient@example.com
533 From: sender@localhost
534 Date:
535
536
537
538 .
539 QUIT
540 EOF
541
542 test_mhmail "$expected" '-from sender@localhost' -b '
543 '
544 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
545
546
547 # check with multiple newlines to -body
548 # Shows different behavior than compiled mhmail, which suppressed one
549 #   of the newlines.
550 cat > "$expected" <<EOF
551 EHLO nosuchhost.example.com
552 MAIL FROM:<sender@localhost>
553 RCPT TO:<recipient@example.com>
554 DATA
555 To: recipient@example.com
556 From: sender@localhost
557 Date:
558
559
560
561
562
563 .
564 QUIT
565 EOF
566
567 test_mhmail "$expected" '-from sender@localhost' -b '
568
569
570 '
571 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
572
573
574 # check with text and no trailing newline to -body
575 # Verified same behavior as compiled mhmail.
576 cat > "$expected" <<EOF
577 EHLO nosuchhost.example.com
578 MAIL FROM:<sender@localhost>
579 RCPT TO:<recipient@example.com>
580 DATA
581 To: recipient@example.com
582 From: sender@localhost
583 Date:
584
585 no newline in input
586 .
587 QUIT
588 EOF
589
590 test_mhmail "$expected" '-from sender@localhost' -b 'no newline in input'
591 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
592
593
594 # check with text and multiple trailing blank lines to -body
595 # Shows different behavior than compiled mhmail, which suppressed one
596 #   of the newlines.
597 cat > "$expected" <<EOF
598 EHLO nosuchhost.example.com
599 MAIL FROM:<sender@localhost>
600 RCPT TO:<recipient@example.com>
601 DATA
602 To: recipient@example.com
603 From: sender@localhost
604 Date:
605
606 here's some text
607
608
609 .
610 QUIT
611 EOF
612
613 test_mhmail "$expected" '-from sender@localhost' -b "here's some text
614
615 "
616 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
617
618
619 # check -resent
620 # Verified same behavior as compiled mhmail.
621 cat > "$expected" <<EOF
622 EHLO nosuchhost.example.com
623 MAIL FROM:<orig_recipient@example.com>
624 RCPT TO:<recipient@example.com>
625 DATA
626 Resent-To: recipient@example.com
627 Resent-From: orig_recipient@example.com
628 To: recipient@example.com
629 From: sender@localhost
630 Date:
631 Resent-Date:
632
633 please resend this message, 1
634 .
635 QUIT
636 EOF
637
638 test_mhmail "$expected" '-from orig_recipient@example.com -resent' \
639   -b 'To: recipient@example.com
640 From: sender@localhost
641 Date: Sat Jun 16 18:35:15 -0500
642
643 please resend this message, 1'
644
645 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
646
647 # check -resent -profile, using stdin
648 # Not supported by compiled mhmail.
649 cat > "$expected" <<EOF
650 EHLO nosuchhost.example.com
651 MAIL FROM:<orig_recipient@example.com>
652 RCPT TO:<recipient@example.com>
653 DATA
654 To: recipient@example.com
655 From: sender@localhost
656 Date:
657 Resent-To: recipient@example.com
658 Resent-From: orig_recipient@example.com
659 Resent-Date:
660
661 please resend this message, 2
662 .
663 QUIT
664 EOF
665
666 test_mhmail "$expected" \
667   '-from orig_recipient@example.com -resent -profile -nomsgid' \
668   '|' 'To: recipient@example.com
669 From: sender@localhost
670 Date: Sat Jun 16 18:35:15 -0500
671
672 please resend this message, 2'
673
674 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
675
676
677 # check -resent -profile, using -b
678 # Not supported by compiled mhmail.
679 cat > "$expected" <<EOF
680 EHLO nosuchhost.example.com
681 MAIL FROM:<orig_recipient@example.com>
682 RCPT TO:<recipient@example.com>
683 DATA
684 To: recipient@example.com
685 From: sender@localhost
686 Date:
687 Resent-To: recipient@example.com
688 Resent-From: orig_recipient@example.com
689 Resent-Date:
690
691 please resend this message, 3
692 .
693 QUIT
694 EOF
695
696 test_mhmail "$expected" \
697   '-from orig_recipient@example.com -resent -profile -nomsgid' \
698   -b 'To: recipient@example.com
699 From: sender@localhost
700 Date: Sat Jun 16 18:35:15 -0500
701
702 please resend this message, 3'
703
704 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
705
706
707 # check -headerfield.
708 # Not supported by compiled mhmail.
709 cat > "$expected" <<EOF
710 EHLO nosuchhost.example.com
711 MAIL FROM:<sender@example.com>
712 RCPT TO:<recipient@example.com>
713 DATA
714 To: recipient@example.com
715 From: sender@example.com
716 User-Agent: nmh
717 Date:
718
719 with added header field
720 .
721 QUIT
722 EOF
723
724 test_mhmail "$expected" \
725   '-from sender@example.com -headerfield User-Agent:nmh' \
726   -b 'with added header field'
727
728 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
729
730
731 # check multiple -headerfields.
732 # Not supported by compiled mhmail.
733 cat > "$expected" <<EOF
734 EHLO nosuchhost.example.com
735 MAIL FROM:<sender@example.com>
736 RCPT TO:<recipient@example.com>
737 DATA
738 To: recipient@example.com
739 From: sender@example.com
740 MIME-Version: 1.0
741 Content-Type: text/plain;charset=utf-8
742 Content-Transfer-Encoding: 8bit
743 Date:
744
745 with added header fields
746 .
747 QUIT
748 EOF
749
750 test_mhmail "$expected" \
751   "-from sender@example.com -headerfield MIME-Version:1.0 \
752 -headerfield Content-Type:text/plain;charset=utf-8 \
753 -headerfield Content-Transfer-Encoding:8bit" \
754   -b 'with added header fields'
755
756 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
757
758
759 # check -attach
760 # Not supported by compiled mhmail.
761 cat > "$expected" <<EOF
762 EHLO nosuchhost.example.com
763 MAIL FROM:<sender@example.com>
764 RCPT TO:<recipient@example.com>
765 DATA
766 To: recipient@example.com
767 From: sender@example.com
768 MIME-Version: 1.0
769 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
770 Content-ID:
771 Date:
772 Message-ID:
773
774 ------- =_aaaaaaaaaa0
775 Content-Type: text/plain; charset="us-ascii"
776
777 See how easy it is to add an attachment!
778
779 ------- =_aaaaaaaaaa0
780 Content-Type: text/plain; name="attachment.txt"; charset="us-ascii"
781 Content-Description: attachment.txt
782 Content-Disposition: attachment; filename="attachment.txt"
783
784 The future disappears into memory, With only a moment between,
785 Forever dwells in that moment, hope is what remains to be seen
786 Forever dwells in that moment, hope is what remains to be seen.
787
788 ------- =_aaaaaaaaaa0--
789 .
790 QUIT
791 EOF
792
793 test_mhmail "$expected" \
794   "-from sender@example.com -attach ${srcdir}/test/mhmail/attachment.txt" \
795   -b 'See how easy it is to add an attachment!'
796
797 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
798
799
800 exit ${failed:-0}