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