Added -clobber switch to mhstore(1) [Bug #11160].
[mmh] / test / mhstore / test-mhstore
1 #!/bin/sh
2 ######################################################
3 #
4 # Test mhstore
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 actual=$MH_TEST_DIR/$$.actual
21
22 cd $MH_TEST_DIR
23
24 # check with no options and no current message
25 run_test 'mhstore' 'mhstore: no cur message'
26
27 # check with no options and current message
28 folder +inbox 5 > /dev/null
29 cat > $expected <<EOF
30 This is message number 5
31 EOF
32 run_test 'mhstore' 'storing message 5 as file 5.txt'
33 check $expected 5.txt 'keep first'
34
35 # check with folder and current message
36 run_test 'mhstore +inbox' 'storing message 5 as file 5.txt'
37 check $expected 5.txt 'keep first'
38
39 # check with specified message
40 run_test 'mhstore 5' 'storing message 5 as file 5.txt'
41 check $expected 5.txt 'keep first'
42
43 # check -file
44 run_test "mhstore -file $MH_TEST_DIR/Mail/inbox/5" \
45   "storing message $MH_TEST_DIR/Mail/inbox/5 as file 5.txt"
46 check $expected 5.txt 'keep first'
47
48 # check -file -
49 stored_contents=`mhstore -file - < $MH_TEST_DIR/Mail/inbox/5 2>&1 | \
50   sed 's/.*as file //'`
51 check $expected $stored_contents
52
53 # check message number greater than highest
54 run_test 'mhstore 11' "mhstore: message 11 doesn't exist"
55
56 # check multiple messages
57 run_test 'mhstore 4 6' 'storing message 4 as file 4.txt
58 storing message 6 as file 6.txt'
59 cat > $expected <<EOF
60 This is message number 4
61 EOF
62 check $expected 4.txt
63 cat > $expected <<EOF
64 This is message number 6
65 EOF
66 check $expected 6.txt
67
68 # Write message with a text/plain subpart.
69 msgfile=`mhpath new`
70 cat > $msgfile <<EOF
71 To: recipient@example.com
72 From: sender@example.com
73 Subject: mhlist test
74 MIME-Version: 1.0
75 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
76
77 ------- =_aaaaaaaaaa0
78 Content-Type: multipart/related;
79     type="multipart/alternative";
80     boundary="subpart__1.1"
81
82 --subpart__1.1
83 Content-Type: text/plain; charset="iso-8859-1"
84 Content-Disposition: attachment; filename="test1.txt"
85
86 This is the first text/plain part, in a subpart.
87
88 --subpart__1.1--
89
90 ------- =_aaaaaaaaaa0
91 Content-Type: text/plain; charset="iso-8859-1"
92 Content-Disposition: attachment; filename="test2.txt"
93 Content-MD5: kq+Hnc2SD/eKwAnkFBDuEA==
94 Content-Transfer-Encoding: quoted-printable
95
96 This is the second text/plain part.
97
98 ------- =_aaaaaaaaaa0
99 Content-Type: text/plain; charset="iso-8859-1"
100 Content-Disposition: attachment; filename="test3.txt"
101
102 This is the third text/plain part.
103
104 ------- =_aaaaaaaaaa0
105 Content-Type: text/plain; charset="iso-8859-1"; name="test4.txt"
106 Content-Disposition: attachment; filename="test4.txt"
107 Content-Transfer-Encoding: base64
108
109 VGhpcyBpcyB0aGUgZm91cnRoIHRleHQvcGxhaW4gcGFydC4K
110
111 ------- =_aaaaaaaaaa0--
112 EOF
113
114 # check -part
115 cat > $expected <<EOF
116 This is the third text/plain part.
117 EOF
118 run_test 'mhstore last -part 3' 'storing message 11 part 3 as file 11.3.txt'
119 check $expected 11.3.txt
120
121 # check -part of a subpart
122 cat > $expected <<EOF
123 This is the first text/plain part, in a subpart.
124 EOF
125 run_test 'mhstore last -part 1.1' \
126          'storing message 11 part 1.1 as file 11.1.1.txt'
127 check $expected 11.1.1.txt 'keep first'
128
129 # check -type
130 run_test 'mhstore last -type text/plain' \
131   'storing message 11 part 1.1 as file 11.1.1.txt
132 storing message 11 part 2 as file 11.2.txt
133 storing message 11 part 3 as file 11.3.txt
134 storing message 11 part 4 as file 11.4.txt'
135 check $expected 11.1.1.txt
136 cat > $expected <<EOF
137 This is the second text/plain part.
138 EOF
139 check $expected 11.2.txt
140 cat > $expected <<EOF
141 This is the third text/plain part.
142 EOF
143 check $expected 11.3.txt
144 cat > $expected <<EOF
145 This is the fourth text/plain part.
146 EOF
147 check $expected 11.4.txt
148
149 # check -auto
150 cat > $expected <<EOF
151 This is the fourth text/plain part.
152 EOF
153 run_test 'mhstore last -part 4 -auto' \
154   'storing message 11 part 4 as file test4.txt'
155 check $expected test4.txt 'keep first'
156
157 # check -noauto, the default
158 run_test 'mhstore last -part 4 -auto -noauto' \
159   'storing message 11 part 4 as file 11.4.txt'
160 check $expected 11.4.txt 'keep first'
161
162 # check -verbose, which doesn't do anything
163 run_test 'mhstore last -part 4 -verbose' \
164   'storing message 11 part 4 as file 11.4.txt'
165 check $expected 11.4.txt 'keep first'
166
167 # check -noverbose, the default
168 run_test 'mhstore last -part 4 -verbose -noverbose' \
169   'storing message 11 part 4 as file 11.4.txt'
170 check $expected 11.4.txt
171
172 # check -check
173 cat > $expected <<EOF
174 This is the second text/plain part.
175 EOF
176 run_test 'mhstore last -part 2 -check' \
177          'storing message 11 part 2 as file 11.2.txt'
178 check $expected 11.2.txt 'keep first'
179
180 # check -check with bad MD5 checksum
181 sed 's/\(Content-MD5: \)kq+Hnc/\1BADBAD/' $msgfile > $MH_TEST_DIR/$$.tmp
182 mv -f $MH_TEST_DIR/$$.tmp $msgfile
183 run_test 'mhstore last -part 2 -check' \
184 'storing message 11 part 2 as file 11.2.txt
185 mhstore: content integrity suspect (digest mismatch) -- continuing
186          (content text/plain in message 11, part 2)'
187 check $expected 11.2.txt 'keep first'
188
189 # check -nocheck, the default
190 run_test 'mhstore last -part 2 -check -nocheck' \
191          'storing message 11 part 2 as file 11.2.txt'
192 check $expected 11.2.txt
193
194 # check reassembly of message/partial messages (RFC 1521, 7.3.2)
195 msgfile=`mhpath new`
196 cat > $msgfile <<EOF
197 To: recipient@example.com
198 From: sender@example.com
199 Subject: mhlist test
200 MIME-Version: 1.0
201 Content-Type: message/partial; id="test-mhstore message/partial test"; number=1
202
203 Content-Type: text/plain; charset="iso-8859-1"
204
205 This is part one of three.
206 EOF
207
208 msgfile=`mhpath new`
209 cat > $msgfile <<EOF
210 To: recipient@example.com
211 From: sender@example.com
212 Subject: mhlist test
213 MIME-Version: 1.0
214 Content-Type: message/partial; id="test-mhstore message/partial test"; number=2
215
216 This is part two of three.
217 EOF
218
219 msgfile=`mhpath new`
220 cat > $msgfile <<EOF
221 To: recipient@example.com
222 From: sender@example.com
223 Subject: mhlist test
224 MIME-Version: 1.0
225 Content-Type: message/partial; id="test-mhstore message/partial test"; number=3;
226   total=3
227
228 This is part three of three.
229 EOF
230
231 run_test 'mhstore last:3' \
232          'reassembling partials 12,13,14 to folder inbox as message 15'
233
234 cat > $expected <<EOF
235 This is part one of three.
236 This is part two of three.
237 This is part three of three.
238 EOF
239 run_test 'mhstore last' 'storing message 15 as file 15.txt'
240 check $expected 15.txt
241
242 # check -clobber always
243 folder +inbox 7 > /dev/null
244 touch 7.txt
245 cat > $expected <<EOF
246 This is message number 7
247 EOF
248 run_test 'mhstore' 'storing message 7 as file 7.txt'
249 check $expected 7.txt 'keep first'
250 run_test 'mhstore -clobber always' 'storing message 7 as file 7.txt'
251 check $expected 7.txt 'keep first'
252
253 # check -clobber auto
254 touch 7.txt
255 run_test 'mhstore -clobber auto' 'storing message 7 as file 7-1.txt'
256 check $expected 7-1.txt 'keep first'
257 touch 7-1.txt
258 run_test 'mhstore -clobber auto' 'storing message 7 as file 7-2.txt'
259 check $expected 7-2.txt 'keep first'
260
261 # check -clobber suffix
262 run_test 'mhstore -clobber suffix' 'storing message 7 as file 7.txt.1'
263 check $expected 7.txt.1 'keep first'
264 touch 7.txt.1
265 run_test 'mhstore -clobber suffix' 'storing message 7 as file 7.txt.2'
266 check $expected 7.txt.2 'keep first'
267
268 # Don't check -clobber ask because it requires connection to a
269 # terminal, and this test won't always be run with one.
270
271 # check -clobber never.  Its exit status is the number of files not overwritten.
272 run_test 'mhstore -clobber never' \
273          "mhstore: will not overwrite `pwd`/7.txt with -clobber never"
274 set +e
275 mhstore -clobber never >/dev/null 2>&1
276 run_test "echo $?" 1
277 set -e
278
279 /bin/rm -f 7.txt 7-1.txt 7.txt.1
280
281 # check with relative nmh-storage profile component
282 storagedir=storagedir
283 dir="$MH_TEST_DIR/Mail/inbox/$storagedir"
284 mkdir "$dir"
285 echo "nmh-storage: $storagedir" >> $MH
286 cat > $expected <<EOF
287 This is the fourth text/plain part.
288 EOF
289 run_test 'mhstore 11 -part 4' \
290 "storing message 11 part 4 as file $storagedir/11.4.txt"
291 check $expected "$dir/11.4.txt" 'keep first'
292
293 # check with relative nmh-storage profile component and -auto
294 run_test 'mhstore 11 -part 4 -auto' \
295 "storing message 11 part 4 as file $storagedir/test4.txt"
296 check $expected "$dir/test4.txt" 'keep first'
297
298 # check with absolute nmh-storage profile component
299 absstoragedir="$MH_TEST_DIR/$storagedir"
300 mkdir "$absstoragedir"
301 sed "s%\(nmh-storage: \)storagedir%\1$absstoragedir%" $MH > $MH.new
302 mv -f $MH.new $MH
303 run_test 'mhstore 11 -part 4' \
304 "storing message 11 part 4 as file $storagedir/11.4.txt"
305 check $expected "$absstoragedir/11.4.txt" 'keep first'
306
307 # check with absolute nmh-storage profile component and -auto
308 run_test 'mhstore 11 -part 4 -auto' \
309 "storing message 11 part 4 as file $storagedir/test4.txt"
310 check $expected "$absstoragedir/test4.txt"
311 rmdir "$absstoragedir"
312
313
314 exit $failed