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