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