Updated sendfiles(1):
[mmh] / test / post / test-sendfiles
1 #!/bin/sh
2 ######################################################
3 #
4 # Test sendfiles
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 . "$MH_OBJ_DIR/test/common.sh"
16
17 setup_test
18
19 expected=$MH_TEST_DIR/$$.expected
20 expected_err=$MH_TEST_DIR/$$.expected_err
21 actual=$MH_TEST_DIR/$$.actual
22 actual_err=$MH_TEST_DIR/$$.actual_err
23 localmbox=`$MH_LIB_DIR/ap -format "%(localmbox)" 0`
24 #### Use this script itself for the test file.
25 testfiledir="$srcdir/test/post"
26 testfile=`basename $0`
27
28 (cd "$MH_TEST_DIR"  &&
29 mkdir -p tmp  &&
30 cat >"$expected_err" <<EOF
31 $testfile
32 EOF
33 )
34 #### Copy fakesendmail and the testfile because we cd to the
35 #### test directory, because tar doesn't like absolute paths.
36 cp "$srcdir/test/fakesendmail" "$testfiledir/$testfile" "$MH_TEST_DIR"
37
38 #### Use sendmail/pipe below to override default mts.
39 mts_fakesendmail="$MHMTSCONF-fakesendmail"
40 sed -e 's/^mts:.*/mts: sendmail\/pipe/' "$MHMTSCONF" > "$mts_fakesendmail"
41 printf 'sendmail: %s/fakesendmail\n' "$MH_TEST_DIR" >>"$mts_fakesendmail"
42 MHMTSCONF="$mts_fakesendmail"
43
44 # $1: uncompress command
45 # $2: argument style:
46 #   new: -to recipient -subject subject
47 #   old: recipient subject
48 # remaining arguments: sendfiles switches
49 test_sendfiles ()
50 {
51   uncompress="$1"; shift
52   argstyle="$1"; shift
53
54   if [ "$argstyle" = 'new' ]; then
55     (cd "$MH_TEST_DIR"  &&
56      sendfiles "$@" -to recipient@example.com -subject "sendfiles test" \
57        "$testfile" >/dev/null 2>"$actual_err")
58   elif [ "$argstyle" = 'old' ]; then
59     (cd "$MH_TEST_DIR"  &&
60      sendfiles "$@" recipient@example.com "sendfiles test" \
61        "$testfile" >/dev/null 2>"$actual_err")
62   fi
63
64   check "$expected_err" "$actual_err" 'keep first'
65
66   # fakesendmail drops the message and any cc's into this mbox.
67   mbox="$MH_TEST_DIR"/Mail/fakesendmail.mbox
68   inc -silent -file "$mbox"
69   rm -f "$mbox" "$mbox.map"
70
71   mhlist -verbose last | sed -e 's/ *$//' >"$actual"
72   (cd $MH_TEST_DIR/tmp  &&
73    contents=`mhstore -noauto last 2>&1 | \
74              sed -e 's/storing message.*as file //'`  &&
75    $uncompress "$contents" | tar xpf -  &&  rm -f "$contents")
76
77   rmm last
78   check "$testfiledir/$testfile" "$MH_TEST_DIR/tmp/$testfile" \
79         'keep first'
80
81   [ -f "$expected" ]  &&  check "$expected" "$actual"  ||  rm -f "$actual"
82 }
83
84
85 # check -help
86 cat >"$expected" <<EOF
87 Usage: sendfiles [switches] -to recipient -subject subject file1 [file2 ...]
88   or
89        sendfiles [switches] recipient subject file1 [file2 ...]
90   switches are:
91   -compress [bzip2 | compress | gzip | lzma | none | zip]
92   -from <sender>
93   -[delay] <delay> (expressed in seconds)
94   -version
95   -help
96   Can use PERSON environment variable instead of -from switch.
97 EOF
98
99 sendfiles -help >"$actual" 2>&1
100 check "$expected" "$actual"
101
102 # check -version
103 case `sendfiles -v` in
104   sendfiles\ --*) ;;
105   *           ) printf '%s: sendfiles -v generated unexpected output\n' "$0" >&2
106                 failed=`expr ${failed:-0} + 1`;;
107 esac
108
109 # check unknown switch
110 run_test 'sendfiles -nonexistent' "Usage: sendfiles [switches] -to recipient \
111 -subject subject file1 [file2 ...]
112   or
113        sendfiles [switches] recipient subject file1 [file2 ...]
114   switches are:
115   -compress [bzip2 | compress | gzip | lzma | none | zip]
116   -from <sender>
117   -[delay] <delay> (expressed in seconds)
118   -version
119   -help
120   Can use PERSON environment variable instead of -from switch."
121
122 # check unknown compression method
123 run_test 'sendfiles -compress unknown to subject file' \
124 'sendfiles: unknown compression method "unknown"'"
125 Usage: sendfiles [switches] -to recipient \
126 -subject subject file1 [file2 ...]
127   or
128        sendfiles [switches] recipient subject file1 [file2 ...]
129   switches are:
130   -compress [bzip2 | compress | gzip | lzma | none | zip]
131   -from <sender>
132   -[delay] <delay> (expressed in seconds)
133   -version
134   -help
135   Can use PERSON environment variable instead of -from switch."
136
137 # Check basic operation, using specified compression method.
138 # Don't check the mhlist output, by not creating an $expected file,
139 # because it will vary with compression method and compressed file
140 # size.
141 [ "`findprog bzip2`" ]     &&  test_sendfiles 'bzip2 -cd' new -compress bzip2
142 [ "`findprog compress`" ]  &&  test_sendfiles uncompress new -compress compress
143 [ "`findprog gzip`" ]      &&  test_sendfiles 'gzip -cd' new -compress gzip
144 [ "`findprog gzip`" ]      &&  test_sendfiles 'gzip -cd' new -gzip
145 [ "`findprog lzma`" ]      &&  test_sendfiles 'lzma -cd' new -compress lzma
146 [ "`findprog zip`" ]       &&  test_sendfiles 'unzip -p' new -compress zip
147
148 # check with no compression
149 cat >"$expected" <<EOF
150  msg part  type/subtype              size description
151   11       application/octet-stream   10K
152              type="tar"
153              (extract with cat | tar xvpf -)
154 EOF
155
156 test_sendfiles cat new -compress none
157
158 # check with no compression, -none
159 cat >"$expected" <<EOF
160  msg part  type/subtype              size description
161   11       application/octet-stream   10K
162              type="tar"
163              (extract with cat | tar xvpf -)
164 EOF
165
166 test_sendfiles cat new -none
167
168 # check old argument style (without -to and -subject)
169 cat >"$expected" <<EOF
170  msg part  type/subtype              size description
171   11       application/octet-stream   10K
172              type="tar"
173              (extract with cat | tar xvpf -)
174 EOF
175
176 test_sendfiles cat old -compress none
177
178 # check -from
179 cat >"$expected" <<EOF
180  msg part  type/subtype              size description
181   11       application/octet-stream   10K
182              type="tar"
183              (extract with cat | tar xvpf -)
184 EOF
185
186 test_sendfiles cat new -compress none -from 'Sender <sender@example.com>'
187
188 # check PERSON environment variable
189 cat >"$expected" <<EOF
190  msg part  type/subtype              size description
191   11       application/octet-stream   10K
192              type="tar"
193              (extract with cat | tar xvpf -)
194 EOF
195
196 PERSON='Sender <sender@example.com>' test_sendfiles cat new -compress none
197
198 # check -delay, though for a small file it doesn't do anything
199 cat >"$expected" <<EOF
200  msg part  type/subtype              size description
201   11       application/octet-stream   10K
202              type="tar"
203              (extract with cat | tar xvpf -)
204 EOF
205
206 test_sendfiles cat new -compress none -delay 5
207
208 # check old style -delay, though for a small file it doesn't do anything
209 cat >"$expected" <<EOF
210  msg part  type/subtype              size description
211   11       application/octet-stream   10K
212              type="tar"
213              (extract with cat | tar xvpf -)
214 EOF
215
216 test_sendfiles cat new -compress none -5
217
218 rm -fr "$MH_TEST_DIR/fakesendmail" "$MH_TEST_DIR/$testfile" \
219        "$expected_err" "$MH_TEST_DIR/tmp"
220
221
222 exit ${failed:-0}