Replaced mhmail -debug switch with -nosend, and added -send.
[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 recipient4@example.com" '|' message
243 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
244
245
246 # check -from and -subject
247 # Verified same behavior as compiled mhmail.
248 cat > "$expected" <<EOF
249 EHLO nosuchhost.example.com
250 MAIL FROM:<sender@localhost>
251 RCPT TO:<recipient@example.com>
252 DATA
253 To: recipient@example.com
254 Subject: Test
255 From: sender@localhost
256 Date:
257
258 message
259 .
260 QUIT
261 EOF
262
263 test_mhmail "$expected" \
264   "-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" \
291   "-from sender@localhost -profile" '|' message
292 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
293
294
295 # check repeated -from and -subject arguments
296 # Verified same behavior as compiled mhmail.
297 cat > "$expected" <<EOF
298 EHLO nosuchhost.example.com
299 MAIL FROM:<sender2@localhost>
300 RCPT TO:<recipient@example.com>
301 DATA
302 To: recipient@example.com
303 Subject: Subject2
304 From: sender2@localhost
305 Date:
306
307 message
308 .
309 QUIT
310 EOF
311
312 test_mhmail "$expected" "-from sender@localhost -from sender2@localhost \
313 -subject Subject1 -subject Subject2" -b message
314 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
315
316 # check repeated -body arguments
317 # Verified same behavior as compiled mhmail.
318 cat > "$expected" <<EOF
319 EHLO nosuchhost.example.com
320 MAIL FROM:<sender@localhost>
321 RCPT TO:<recipient@example.com>
322 DATA
323 To: recipient@example.com
324 From: sender@localhost
325 Date:
326
327 body2
328 .
329 QUIT
330 EOF
331
332 test_mhmail "$expected" "-from sender@localhost -body body1" -b body2
333 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
334
335
336 # check multiple -cc arguments
337 # Verified same behavior as compiled mhmail.
338 cat > "$expected" <<EOF
339 EHLO nosuchhost.example.com
340 MAIL FROM:<sender@localhost>
341 RCPT TO:<recipient@example.com>
342 RCPT TO:<cc1@example.com>
343 RCPT TO:<cc2@example.com>
344 DATA
345 To: recipient@example.com
346 Cc: cc1@example.com, cc2@example.com
347 From: sender@localhost
348 Date:
349
350 message
351 .
352 QUIT
353 EOF
354
355 test_mhmail "$expected" "-from sender@localhost \
356 -cc cc1@example.com -cc cc2@example.com" -b message
357 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
358
359
360 # check separated -cc arguments
361 # Verified same behavior as compiled mhmail.
362 cat > "$expected" <<EOF
363 EHLO nosuchhost.example.com
364 MAIL FROM:<sender@localhost>
365 RCPT TO:<recipient@example.com>
366 RCPT TO:<cc1@example.com>
367 RCPT TO:<cc2@example.com>
368 DATA
369 To: recipient@example.com
370 Cc: cc1@example.com, cc2@example.com
371 Subject: Test
372 From: sender@localhost
373 Date:
374
375 message
376 .
377 QUIT
378 EOF
379
380 test_mhmail "$expected" "-from sender@localhost \
381 -cc cc1@example.com -subject Test cc2@example.com" -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
617
618 .
619 QUIT
620 EOF
621
622 test_mhmail "$expected" "-from recipient@example.com -resent" \
623   -b "To: recipient@example.com
624 From: sender@localhost
625 Date: Sat Jun 16 18:35:15 -0500
626
627 please resend this message
628 "
629
630 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
631
632
633 exit ${failed:-0}