mhmail now supports -profile -resend.
[mmh] / test / post / 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 [addrs ... [switches]]
76   switches are:
77   -b(ody) text
78   -c(c) addrs ...
79   -f(rom) addr
80   -su(bject) text
81   -r(esent)
82   -pr(ofile)
83   -se(nd)
84   -nose(nd)
85   -v(ersion)
86   -h(elp)
87   and all post(8)/send(1) switches
88 EOF
89
90 mhmail -help >$actual 2>&1
91 check $expected $actual
92
93
94 # check -version
95 # Verified same behavior as compiled mhmail.
96 case `mhmail -v` in
97   mhmail\ --*) ;;
98   *           ) echo "$0: mhmail -v generated unexpected output" 1>&2
99                 failed=`expr ${failed:-0} + 1`;;
100 esac
101
102
103 # check with no arguments
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" 1>&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 tmpfil=`head -1 $actual | sed -e 's/://'`
121
122 cat > "$expected" <<EOF
123 To: recipient@example.com
124 From: sender@localhost
125
126
127 EOF
128
129 cat > "$expected_err" <<EOF
130 EOF
131
132 check "$expected" "$actual"
133 check "$expected_err" "$actual_err"
134 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
135
136
137 # check -send
138 # Not supported by compiled mhmail.
139 cat > "$expected" <<EOF
140 EHLO nosuchhost.example.com
141 MAIL FROM:<sender@localhost>
142 RCPT TO:<recipient@example.com>
143 DATA
144 To: recipient@example.com
145 From: sender@localhost
146 Date:
147
148 message
149 .
150 QUIT
151 EOF
152
153 test_mhmail "$expected" "-from sender@localhost -nosend -send" '|' message
154 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
155
156
157 # check -from
158 # Verified same behavior as compiled mhmail.
159 cat > "$expected" <<EOF
160 EHLO nosuchhost.example.com
161 MAIL FROM:<sender@localhost>
162 RCPT TO:<recipient@example.com>
163 DATA
164 To: recipient@example.com
165 From: sender@localhost
166 Date:
167
168 message
169 .
170 QUIT
171 EOF
172
173 test_mhmail "$expected" "-from sender@localhost" '|' message
174 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
175
176
177 # check -from and -body
178 # Verified same behavior as compiled mhmail.
179 cat > "$expected" <<EOF
180 EHLO nosuchhost.example.com
181 MAIL FROM:<sender@localhost>
182 RCPT TO:<recipient@example.com>
183 DATA
184 To: recipient@example.com
185 From: sender@localhost
186 Date:
187
188 body
189 .
190 QUIT
191 EOF
192
193 test_mhmail "$expected" "-from sender@localhost" -b body
194 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
195
196
197 # check -from and -cc
198 # Verified same behavior as compiled mhmail.
199 cat > "$expected" <<EOF
200 EHLO nosuchhost.example.com
201 MAIL FROM:<sender@localhost>
202 RCPT TO:<recipient@example.com>
203 RCPT TO:<recipient2@example.com>
204 DATA
205 To: recipient@example.com
206 Cc: recipient2@example.com
207 From: sender@localhost
208 Date:
209
210 message
211 .
212 QUIT
213 EOF
214
215 test_mhmail "$expected" \
216     "-from sender@localhost -cc recipient2@example.com" '|' message
217 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
218
219
220 # check -from and multiple -cc addresses
221 # Verified same behavior as compiled mhmail.
222 cat > "$expected" <<EOF
223 EHLO nosuchhost.example.com
224 MAIL FROM:<sender@localhost>
225 RCPT TO:<recipient@example.com>
226 RCPT TO:<recipient2@example.com>
227 RCPT TO:<recipient3@example.com>
228 RCPT TO:<recipient4@example.com>
229 DATA
230 To: recipient@example.com
231 Cc: recipient2@example.com, recipient3@example.com,
232     recipient4@example.com
233 From: sender@localhost
234 Date:
235
236 message
237 .
238 QUIT
239 EOF
240
241 test_mhmail "$expected" \
242     '-from sender@localhost -cc recipient2@example.com recipient3@example.com '\
243 'recipient4@example.com' '|' message
244 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
245
246
247 # check -from and -subject
248 # Verified same behavior as compiled mhmail.
249 cat > "$expected" <<EOF
250 EHLO nosuchhost.example.com
251 MAIL FROM:<sender@localhost>
252 RCPT TO:<recipient@example.com>
253 DATA
254 To: recipient@example.com
255 Subject: Test
256 From: sender@localhost
257 Date:
258
259 message
260 .
261 QUIT
262 EOF
263
264 test_mhmail "$expected" '-from sender@localhost -subject Test' '|' message
265 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
266
267
268 # check -from and -profile
269 # Show that -profile causes mhmail to 1) read the profile and
270 # 2) use send(1) by added a send switch to the profile and
271 # verifying that it gets used.
272 # Not supported by compiled mhmail.
273 printf "send: -msgid\n" >> $MH
274
275 cat > "$expected" <<EOF
276 EHLO nosuchhost.example.com
277 MAIL FROM:<sender@localhost>
278 RCPT TO:<recipient@example.com>
279 DATA
280 To: recipient@example.com
281 From: sender@localhost
282 Date:
283 Message-ID:
284
285 message
286 .
287 QUIT
288 EOF
289
290 test_mhmail "$expected" '-from sender@localhost -profile' '|' message
291 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
292
293
294 # check repeated -from and -subject arguments
295 # Verified same behavior as compiled mhmail.
296 cat > "$expected" <<EOF
297 EHLO nosuchhost.example.com
298 MAIL FROM:<sender2@localhost>
299 RCPT TO:<recipient@example.com>
300 DATA
301 To: recipient@example.com
302 Subject: Subject2
303 From: sender2@localhost
304 Date:
305
306 message
307 .
308 QUIT
309 EOF
310
311 test_mhmail "$expected" '-from sender@localhost -from sender2@localhost '\
312 '-subject Subject1 -subject Subject2' -b message
313 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
314
315 # check repeated -body arguments
316 # Verified same behavior as compiled mhmail.
317 cat > "$expected" <<EOF
318 EHLO nosuchhost.example.com
319 MAIL FROM:<sender@localhost>
320 RCPT TO:<recipient@example.com>
321 DATA
322 To: recipient@example.com
323 From: sender@localhost
324 Date:
325
326 body2
327 .
328 QUIT
329 EOF
330
331 test_mhmail "$expected" "-from sender@localhost -body body1" -b body2
332 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
333
334
335 # check multiple -cc arguments
336 # Verified same behavior as compiled mhmail.
337 cat > "$expected" <<EOF
338 EHLO nosuchhost.example.com
339 MAIL FROM:<sender@localhost>
340 RCPT TO:<recipient@example.com>
341 RCPT TO:<cc1@example.com>
342 RCPT TO:<cc2@example.com>
343 DATA
344 To: recipient@example.com
345 Cc: cc1@example.com, cc2@example.com
346 From: sender@localhost
347 Date:
348
349 message
350 .
351 QUIT
352 EOF
353
354 test_mhmail "$expected" \
355   '-from sender@localhost -cc cc1@example.com -cc cc2@example.com' -b message
356 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
357
358
359 # check separated -cc arguments
360 # Verified same behavior as compiled mhmail.
361 cat > "$expected" <<EOF
362 EHLO nosuchhost.example.com
363 MAIL FROM:<sender@localhost>
364 RCPT TO:<recipient@example.com>
365 RCPT TO:<cc1@example.com>
366 RCPT TO:<cc2@example.com>
367 DATA
368 To: recipient@example.com
369 Cc: cc1@example.com, cc2@example.com
370 Subject: Test
371 From: sender@localhost
372 Date:
373
374 message
375 .
376 QUIT
377 EOF
378
379 test_mhmail "$expected" \
380   '-from sender@localhost -cc cc1@example.com -subject Test cc2@example.com' \
381   -b message
382 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
383
384
385 # check with no newline on stdin
386 # Shows different behavior than compiled mhmail, which was silent in this case.
387 cat > "$expected" <<EOF
388 EOF
389
390 cat > "$expected_err" <<EOF
391 mhmail: empty message not sent, use -body '' to force.
392 EOF
393
394 set +e
395 printf '' | mhmail recipient@example.com -server 127.0.0.1 -port $localport \
396   >"$actual" 2>"$actual_err"
397 set -e
398
399 check "$expected" "$actual"
400 check "$expected_err" "$actual_err"
401 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
402
403
404 # check with one newline on stdin
405 # Verified same behavior as compiled mhmail.
406 cat > "$expected" <<EOF
407 EHLO nosuchhost.example.com
408 MAIL FROM:<sender@localhost>
409 RCPT TO:<recipient@example.com>
410 DATA
411 To: recipient@example.com
412 From: sender@localhost
413 Date:
414
415
416 .
417 QUIT
418 EOF
419
420 test_mhmail "$expected" '-from sender@localhost' '|' '\n'
421 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
422
423
424 # check with multiple newlines on stdin
425 # Verified same behavior as compiled mhmail.
426 cat > "$expected" <<EOF
427 EHLO nosuchhost.example.com
428 MAIL FROM:<sender@localhost>
429 RCPT TO:<recipient@example.com>
430 DATA
431 To: recipient@example.com
432 From: sender@localhost
433 Date:
434
435
436
437
438 .
439 QUIT
440 EOF
441
442 test_mhmail "$expected" '-from sender@localhost' '|' '\n\n\n'
443 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
444
445
446 # check with text and no trailing newline on stdin
447 # Verified same behavior as compiled mhmail.
448 cat > "$expected" <<EOF
449 EHLO nosuchhost.example.com
450 MAIL FROM:<sender@localhost>
451 RCPT TO:<recipient@example.com>
452 DATA
453 To: recipient@example.com
454 From: sender@localhost
455 Date:
456
457 no newline in input
458 .
459 QUIT
460 EOF
461
462 test_mhmail "$expected" '-from sender@localhost' '|' 'no newline in input'
463 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
464
465
466 # check with text and multiple trailing blank lines on stdin
467 # Verified same behavior as compiled mhmail.
468 cat > "$expected" <<EOF
469 EHLO nosuchhost.example.com
470 MAIL FROM:<sender@localhost>
471 RCPT TO:<recipient@example.com>
472 DATA
473 To: recipient@example.com
474 From: sender@localhost
475 Date:
476
477 here's some text
478
479
480 .
481 QUIT
482 EOF
483
484 test_mhmail "$expected" '-from sender@localhost' '|' "here's some text\n\n\n"
485 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
486
487
488 # check with no newline to -body
489 # Verified same behavior as compiled mhmail.
490 cat > "$expected" <<EOF
491 EHLO nosuchhost.example.com
492 MAIL FROM:<sender@localhost>
493 RCPT TO:<recipient@example.com>
494 DATA
495 To: recipient@example.com
496 From: sender@localhost
497 Date:
498
499
500 .
501 QUIT
502 EOF
503
504 test_mhmail "$expected" '-from sender@localhost' -b ''
505 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
506
507
508 # check with one newline to -body
509 # Shows different behavior than compiled mhmail, which suppressed the newline.
510 cat > "$expected" <<EOF
511 EHLO nosuchhost.example.com
512 MAIL FROM:<sender@localhost>
513 RCPT TO:<recipient@example.com>
514 DATA
515 To: recipient@example.com
516 From: sender@localhost
517 Date:
518
519
520
521 .
522 QUIT
523 EOF
524
525 test_mhmail "$expected" '-from sender@localhost' -b '
526 '
527 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
528
529
530 # check with multiple newlines to -body
531 # Shows different behavior than compiled mhmail, which suppressed one
532 #   of the newlines.
533 cat > "$expected" <<EOF
534 EHLO nosuchhost.example.com
535 MAIL FROM:<sender@localhost>
536 RCPT TO:<recipient@example.com>
537 DATA
538 To: recipient@example.com
539 From: sender@localhost
540 Date:
541
542
543
544
545
546 .
547 QUIT
548 EOF
549
550 test_mhmail "$expected" '-from sender@localhost' -b '
551
552
553 '
554 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
555
556
557 # check with text and no trailing newline to -body
558 # Verified same behavior as compiled mhmail.
559 cat > "$expected" <<EOF
560 EHLO nosuchhost.example.com
561 MAIL FROM:<sender@localhost>
562 RCPT TO:<recipient@example.com>
563 DATA
564 To: recipient@example.com
565 From: sender@localhost
566 Date:
567
568 no newline in input
569 .
570 QUIT
571 EOF
572
573 test_mhmail "$expected" '-from sender@localhost' -b 'no newline in input'
574 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
575
576
577 # check with text and multiple trailing blank lines to -body
578 # Shows different behavior than compiled mhmail, which suppressed one
579 #   of the newlines.
580 cat > "$expected" <<EOF
581 EHLO nosuchhost.example.com
582 MAIL FROM:<sender@localhost>
583 RCPT TO:<recipient@example.com>
584 DATA
585 To: recipient@example.com
586 From: sender@localhost
587 Date:
588
589 here's some text
590
591
592 .
593 QUIT
594 EOF
595
596 test_mhmail "$expected" '-from sender@localhost' -b "here's some text
597
598 "
599 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
600
601
602 # check -resent
603 # Verified same behavior as compiled mhmail.
604 cat > "$expected" <<EOF
605 EHLO nosuchhost.example.com
606 MAIL FROM:<recipient@example.com>
607 RCPT TO:<recipient@example.com>
608 DATA
609 Resent-To: recipient@example.com
610 Resent-From: recipient@example.com
611 To: recipient@example.com
612 From: sender@localhost
613 Date:
614 Resent-Date:
615
616 please resend this message, #1
617 .
618 QUIT
619 EOF
620
621 test_mhmail "$expected" '-from recipient@example.com -resent' \
622   -b 'To: recipient@example.com
623 From: sender@localhost
624 Date: Sat Jun 16 18:35:15 -0500
625
626 please resend this message, #1'
627
628 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
629
630 # check -resent -profile, using stdin
631 # Not supported by compiled mhmail.
632 cat > "$expected" <<EOF
633 EHLO nosuchhost.example.com
634 MAIL FROM:<recipient@example.com>
635 RCPT TO:<recipient@example.com>
636 DATA
637 To: recipient@example.com
638 From: sender@localhost
639 Date:
640 Resent-To: recipient@example.com
641 Resent-From: recipient@example.com
642 Resent-Date:
643
644 please resend this message, #2
645 .
646 QUIT
647 EOF
648
649 test_mhmail "$expected" \
650   '-from recipient@example.com -resent -profile -nomsgid' \
651   '|' 'To: recipient@example.com
652 From: sender@localhost
653 Date: Sat Jun 16 18:35:15 -0500
654
655 please resend this message, #2'
656
657 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
658
659
660 # check -resent -profile, using -b
661 # Not supported by compiled mhmail.
662 cat > "$expected" <<EOF
663 EHLO nosuchhost.example.com
664 MAIL FROM:<recipient@example.com>
665 RCPT TO:<recipient@example.com>
666 DATA
667 To: recipient@example.com
668 From: sender@localhost
669 Date:
670 Resent-To: recipient@example.com
671 Resent-From: recipient@example.com
672 Resent-Date:
673
674 please resend this message, #3
675 .
676 QUIT
677 EOF
678
679 test_mhmail "$expected" \
680   '-from recipient@example.com -resent -profile -nomsgid' \
681   -b 'To: recipient@example.com
682 From: sender@localhost
683 Date: Sat Jun 16 18:35:15 -0500
684
685 please resend this message, #3'
686
687 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
688
689
690 exit ${failed:-0}