Rearranged test suite environment variables so that individual tests
[mmh] / 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="${MH_OBJ_DIR}/test/inc"
17
18 if [ ! -z "$VALGRIND_ME" ]; then
19     require_prog valgrind
20     # Lack of quotes here is important
21     VALGRIND="valgrind --quiet --error-exitcode=1"
22     echo "Running tests under valgrind: takes ages!"
23 else
24     VALGRIND=
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" && output_md5 *.txt > "$MH_TEST_DIR/inctest.md5sums")
32 diff -u "$THISDIR/md5sums" "$MH_TEST_DIR/inctest.md5sums"
33
34 FILLER="$THISDIR/filler.txt"
35 FROMLINE="$THISDIR/fromline.txt"
36 HDR="$THISDIR/msgheader.txt"
37
38 if grep -q From "$FILLER"; then
39    echo "Somebody's messed with $FILLER -- it must not contain"
40    echo "anything that might look like a message delimiter!"
41    exit 1
42 fi
43
44 # a sort of worst-case guess for the buffer size;
45 # obviously a buffer boundary for this will be a boundary
46 # for any smaller power of two size.
47 # If you need to increase this you'll need to make filler.txt
48 # bigger as well.
49 STDIO_BUFSZ=16384
50
51 FROMLINESZ="$(wc -c "$FROMLINE" | cut -d ' ' -f 1)"
52 HDRSZ="$(wc -c "$HDR" | cut -d ' ' -f 1)"
53
54 # makembox_A mboxname sz
55 # Assemble a mailbox into file mboxname, with two messages, such
56 # that the first is exactly sz bytes long (including its header
57 # and its initial 'From' line and the newline which terminates it
58 # but not the newline which mbox format demands after each message)
59 # We also leave the body of message one in mboxname.body
60 # (the body of message two is always $FILLER in its entirety)
61 makembox_A () {
62   MBOX="$1"
63   SZ=$2
64
65   WANTSZ="$(($SZ - $HDRSZ - $FROMLINESZ - 1))"
66   dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
67   echo >> "$MBOX.body"
68   cat "$FROMLINE" "$HDR" "$MBOX.body" > "$MBOX"
69   echo >> "$MBOX"
70   cat "$FROMLINE" "$HDR" "$FILLER" >> "$MBOX"
71   echo >> "$MBOX"
72 }
73
74 # make_mbox_B mboxname sz
75 # Test B makes a mailbox with one message of sz bytes long,
76 # which ends in a partial mbox delimiter (ie part of the string
77 # \n\nFrom '). To both do this and be a valid mbox this means
78 # it has to end with two newlines (one of which is in the message
79 # body and one of which is the mbox format mandated one)
80 makembox_B () {
81   MBOX="$1"
82   SZ=$2
83
84   WANTSZ="$(($SZ - $HDRSZ - $FROMLINESZ - 1))"
85   dd if="$FILLER" of="$MBOX.body" bs="$WANTSZ" count=1 2>/dev/null
86   echo >> "$MBOX.body"
87   cat "$FROMLINE" "$HDR" "$MBOX.body" > "$MBOX"
88   echo >> "$MBOX"
89 }
90
91 # do_one_test_A sz
92 # Do a single test with message one's body of size sz.
93 do_one_test_A () {
94   SZ=$1
95   makembox_A "$MH_TEST_DIR/eom-align.mbox" $STDIO_BUFSZ
96   $VALGRIND inc -silent -file "$MH_TEST_DIR/eom-align.mbox"
97   # We know the messages should be 11 and 12 in inbox
98   # Now get the bodies back out.
99   sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/11" > "$MH_TEST_DIR/eom-align.inbox.body1"
100   sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/12" > "$MH_TEST_DIR/eom-align.inbox.body2"
101   diff -u "$MH_TEST_DIR/eom-align.mbox.body" "$MH_TEST_DIR/eom-align.inbox.body1"
102   diff -u "$FILLER" "$MH_TEST_DIR/eom-align.inbox.body2"
103   rmm 11 12
104 }
105
106 # do_one_test_B sz
107 # Do a test type B
108 do_one_test_B () {
109   SZ=$1
110   makembox_B "$MH_TEST_DIR/eom-align.mbox" $STDIO_BUFSZ
111   $VALGRIND inc -silent -file "$MH_TEST_DIR/eom-align.mbox"
112   # We know the message should be 11 in the inbox
113   sed -e '1,/^$/d' "$MH_TEST_DIR/Mail/inbox/11" > "$MH_TEST_DIR/eom-align.inbox.body1"
114   diff -u "$MH_TEST_DIR/eom-align.mbox.body" "$MH_TEST_DIR/eom-align.inbox.body1"
115   rmm 11
116 }
117
118
119 # Cover a decent range around the stdio buffer size to make sure we catch
120 # any corner cases whether they relate to total message size equal to
121 # buffer size or to body size equal to buffer size.
122 START=$(($STDIO_BUFSZ - 16))
123 FINISH=$(($STDIO_BUFSZ + $HDRSZ + $FROMLINESZ + 32))
124 echo "Testing inc of files with various alignments of eom marker with buffer size..."
125 i="$START"
126 while test $i -le $FINISH; do
127   progress_update $i $START $FINISH
128   do_one_test_A $i
129   do_one_test_B $i
130   i=$(expr $i + 1)
131 done
132 progress_done