]> diplodocus.org Git - nmh/blob - test/inc/test-eom-align
inc/test-eom-align: Fix MMDF-mbox `B' size calculation.
[nmh] / test / inc / test-eom-align
1 #!/bin/sh
2 # Test all combinations of alignment of the end-of-message delimiter
3 # with the end of a stdio buffer
4
5 set -e
6
7 if test -z "${MH_OBJ_DIR}"; then
8 srcdir=`dirname "$0"`/../..
9 MH_OBJ_DIR=`cd "$srcdir" && pwd`; export MH_OBJ_DIR
10 fi
11
12 . "$MH_OBJ_DIR/test/common.sh"
13
14 setup_test
15
16 THISDIR="$srcdir/test/inc"
17
18 if [ -z "$VALGRIND_ME" ]; then
19 VALGRIND=
20 else
21 require_prog valgrind
22 # Lack of quotes here is important
23 VALGRIND="valgrind --quiet --error-exitcode=1"
24 echo "Running tests under valgrind: takes ages!"
25 fi
26
27 # First check that all our various pieces of text are
28 # intact. (Since we're dealing in exact byte alignment
29 # minor corruptions such as line ending changes could
30 # render the tests useless.)
31 (cd "$THISDIR" && \
32 for i in *.txt; do
33 echo `output_md5 $i`' '"$i" >> "$MH_TEST_DIR/inctest.md5sums"
34 done)
35 check "$THISDIR/md5sums" "$MH_TEST_DIR/inctest.md5sums" 'keep first'
36
37 FILLER="$THISDIR/filler.txt"
38 FROMLINE="$THISDIR/fromline.txt"
39 MMDF_DELIM='printf "\01\01\01\01\012"'
40 HDR="$THISDIR/msgheader.txt"
41
42 if grep From "$FILLER" >/dev/null; then
43 echo "Somebody's messed with $FILLER -- it must not contain"
44 echo "anything that might look like a message delimiter!"
45 exit 1
46 fi
47
48 # a sort of worst-case guess for the buffer size;
49 # obviously a buffer boundary for this will be a boundary
50 # for any smaller power of two size.
51 # If you need to increase this you'll need to make filler.txt
52 # bigger as well.
53 STDIO_BUFSZ=16384
54
55 FROMLINESZ=`wc -c < "$FROMLINE"`
56 MMDFDELIMLINESZ=5
57 HDRSZ=`wc -c < "$HDR"`
58
59 # makembox_A mboxname sz
60 # Assemble a mailbox into file mboxname, with two messages, such
61 # that the first is exactly sz bytes long (including its header
62 # and its initial 'From' line and the newline which terminates it
63 # but not the newline which mbox format demands after each message)
64 # We also leave the body of message one in mboxname.body
65 # (the body of message two is always $FILLER in its entirety)
66 makembox_A () {
67 MBOX="$1"
68 SZ=$2
69
70 arith_eval $SZ - $HDRSZ - $FROMLINESZ - 1; WANTSZ=$arith_val
71 dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
72 echo >> "$MBOX.body"
73 cat "$FROMLINE" "$HDR" "$MBOX.body" > "$MBOX"
74 echo >> "$MBOX"
75 cat "$FROMLINE" "$HDR" "$FILLER" >> "$MBOX"
76 echo >> "$MBOX"
77 }
78
79 # makemmdf_A mmdfboxname sz
80 # The first email in $MBOX will be sz bytes long, including its MMDF
81 # magic strings that top and tail it.
82 makemmdf_A () {
83 MBOX="$1"
84 SZ=$2
85
86 arith_eval $SZ - $MMDFDELIMLINESZ - $HDRSZ - 1 - $MMDFDELIMLINESZ; WANTSZ=$arith_val
87 dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
88 echo >> "$MBOX.body"
89 #### The trailing $MMDF_DELIM's allows $MBOX to exactly match a file
90 #### created by packf.
91 (eval "$MMDF_DELIM"; cat "$HDR" "$MBOX.body"; eval "$MMDF_DELIM"; \
92 eval "$MMDF_DELIM"; cat "$HDR" "$FILLER"; eval "$MMDF_DELIM") > "$MBOX"
93 }
94
95 # make_mbox_B mboxname sz
96 # Test B makes a mailbox with one message of sz bytes long,
97 # which ends in a partial mbox delimiter (ie part of the string
98 # \n\nFrom '). To both do this and be a valid mbox this means
99 # it has to end with two newlines (one of which is in the message
100 # body and one of which is the mbox format mandated one)
101 makembox_B () {
102 MBOX="$1"
103 SZ=$2
104
105 arith_eval $SZ - $HDRSZ - $FROMLINESZ - 1; WANTSZ=$arith_val
106 dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
107 echo >> "$MBOX.body"
108 cat "$FROMLINE" "$HDR" "$MBOX.body" > "$MBOX"
109 echo >> "$MBOX"
110 }
111
112 # makemmdf_B mmdfboxname sz
113 # The mbox file will be sz bytes and contain a single email, topped and
114 # tailed with MMDF's magic strings. The end of the email's body looks
115 # the same as the magic string for three bytes.
116 makemmdf_B () {
117 MBOX="$1"
118 SZ=$2
119
120 arith_eval $SZ - $MMDFDELIMLINESZ - $HDRSZ - 1 - 4 - $MMDFDELIMLINESZ; WANTSZ=$arith_val
121 dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
122 echo >> "$MBOX.body"
123 printf '\1\1\1\n' >>"$MBOX.body"
124 (eval "$MMDF_DELIM"; cat "$HDR" "$MBOX.body"; eval "$MMDF_DELIM") > "$MBOX"
125 }
126
127 # do_one_test_A sz mbox_type
128 # Do a single test with message one's body of size sz.
129 do_one_test_A () {
130 SZ=$1
131 MBOX_TYPE=$2
132 printf '%5s %s A\r' $SZ $MBOX_TYPE
133 case $MBOX_TYPE in
134 mbox) makembox_A "$MH_TEST_DIR/eom-align.mbox" $SZ ;;
135 mmdf) makemmdf_A "$MH_TEST_DIR/eom-align.mbox" $SZ ;;
136 esac
137 $VALGRIND inc -silent -file "$MH_TEST_DIR/eom-align.mbox"
138 # We know the messages should be 11 and 12 in inbox
139 # Now get the bodies back out.
140 body1="$MH_TEST_DIR/eom-align.inbox.body1"
141 body2="$MH_TEST_DIR/eom-align.inbox.body2"
142 sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/11" > "$body1"
143 sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/12" > "$body2"
144 check "$MH_TEST_DIR/eom-align.mbox.body" "$body1" 'keep first'
145 check "$FILLER" "$body2" 'keep first'
146 rmm 11 12
147 }
148
149 # do_one_test_B sz mbox_type
150 # Do a test type B
151 do_one_test_B () {
152 SZ=$1
153 MBOX_TYPE=$2
154 printf '%5s %s B\r' $SZ $MBOX_TYPE
155 case $MBOX_TYPE in
156 mbox) makembox_B "$MH_TEST_DIR/eom-align.mbox" $SZ ;;
157 mmdf) makemmdf_B "$MH_TEST_DIR/eom-align.mbox" $SZ ;;
158 esac
159 $VALGRIND inc -silent -file "$MH_TEST_DIR/eom-align.mbox"
160 # We know the message should be 11 in the inbox
161 body1="$MH_TEST_DIR/eom-align.inbox.body1"
162 sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/11" > "$body1"
163 check "$MH_TEST_DIR/eom-align.mbox.body" "$body1" 'keep first'
164 rmm 11
165 }
166
167 # Cover a decent range around the stdio buffer size to make sure we catch
168 # any corner cases whether they relate to total message size equal to
169 # buffer size or to body size equal to buffer size.
170 arith_eval $STDIO_BUFSZ - 16; START=$arith_val
171 arith_eval $STDIO_BUFSZ + $HDRSZ + $FROMLINESZ + 32; FINISH=$arith_val
172 [ -t 1 ] && echo \
173 "Testing inc of files with various alignments of eom marker with buffer size..."
174
175 #### Trouble spots for the particular input, with BUFSZ of 8192:
176 for sz in 3146 3147 7241 7242 7243 11338 11339 15433 15434 15435; do
177 do_one_test_A $sz mbox
178 do_one_test_A $sz mmdf
179 done
180
181 # Also 155, 174, 175, but they're too small for makembox_B to handle.
182 for sz in 684 4074 4097 4148 4610 4611 5121 5122 6654 6655 8170 \
183 8193 8244 8364 12266 12289 12340 16362 16385 16436; do
184 do_one_test_B $sz mbox
185 do_one_test_B $sz mmdf
186 done
187
188 i="$START"
189 while test $i -le $FINISH; do
190 do_one_test_A $i mbox
191 do_one_test_A $i mmdf
192 do_one_test_B $i mbox
193 do_one_test_B $i mmdf
194 i=`expr $i + 1`
195 done
196 printf '\n'
197
198 test ${failed:-0} -eq 0 && \
199 rm "$MH_TEST_DIR/eom-align.mbox" "$MH_TEST_DIR/eom-align.mbox.body"
200
201 exit $failed