#!/bin/sh
######################################################
#
# Test m_getfld() with various parts of messages
# falling on buffer boundaries
#
######################################################

set -e

if test -z "${MH_OBJ_DIR}"; then
    srcdir=`dirname "$0"`/../..
    MH_OBJ_DIR=`cd "$srcdir" && pwd`; export MH_OBJ_DIR
fi

. "$MH_OBJ_DIR/test/common.sh"

setup_test

expected="$MH_TEST_DIR/$$.expected"
actual="$MH_TEST_DIR/$$.actual"

# check a boundary condition in m_getfld():  its internal message
# buffer holds exactly up to the end of a header field body in a
# message part
msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat >"$msgfile" <<EOF
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart_001"
Content-class: urn:content-classes:message
Date: Thu, 19 May 2011 00:00:00 -0000
From: sender@example.com
To: recipient@example.com

This is a multi-part message in MIME format.

--NextPart_001
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="NextPart_002"

[encoded data, 7742 characters]
XXXXXXXXXXXXXXXXXXXXXXXX

--NextPart_002
Content-Type: text/plain;
    charset="utf-8"

This is the text/plain part.

--NextPart_002--

--NextPart_001--
EOF

# Write the expected output.
cat >"$expected" <<EOF
[ Message inbox:$msgnum ]
[ part 1.1 - text/plain -   29B  ]
This is the text/plain part.
EOF

run_prog mhshow -part 1.1 -form mhl.null $msgnum >"$actual" 2>&1
check "$expected" "$actual" : check number 1

# check a boundary condition in m_getfld():  its internal message
# buffer holds exactly up to the end of a header field name in a
# message part
msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat >"$msgfile" <<EOF
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart_001"
Content-class: urn:content-classes:message
Date: Thu, 19 May 2011 00:00:00 -0000
From: sender@example.com
To: recipient@example.com

This is a multi-part message in MIME format.

--NextPart_001
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="NextPart_002"

[encoded data, 7742 characters]
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

--NextPart_002
Content-Type: text/plain;
    charset="utf-8"

This is the text/plain part.

--NextPart_002--

--NextPart_001--
EOF

# Write the expected output.
cat >"$expected" <<EOF
[ Message inbox:$msgnum ]
[ part 1.1 - text/plain -   29B  ]
This is the text/plain part.
EOF

run_prog mhshow -part 1.1 -form mhl.null $msgnum >"$actual" 2>&1
check "$expected" "$actual" : check number 2

# check a boundary condition in m_getfld():  its internal message
# buffer holds exactly up to the end of a message part boundary
msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat >"$msgfile" <<EOF
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart_001"
Content-class: urn:content-classes:message
Date: Thu, 19 May 2011 00:00:00 -0000
From: sender@example.com
To: recipient@example.com

This is a multi-part message in MIME format.

--NextPart_001
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="NextPart_002"

[encoded data, 7738 characters]

The 8192'nd character is the '2' at the end of the following NextPart_002.

--NextPart_002
Content-Type: text/plain;
    charset="utf-8"
Content-Description: the text/plain part

This is the text/plain part.

--NextPart_002--

--NextPart_001--
EOF

# Write the expected output.
cat >"$expected" <<EOF
[ Message inbox:$msgnum ]
[ part 1.1 - text/plain - the text/plain part  29B  ]
This is the text/plain part.
EOF

run_prog mhshow -part 1.1 -form mhl.null $msgnum >"$actual" 2>&1
check "$expected" "$actual" : check number 3


exit $failed
