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