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