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