LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
unset LC_CTYPE
unset LC_NUMERIC
unset LC_TIME
unset LC_COLLATE
unset LC_MONETARY
unset LC_MESSAGES
unset LC_PAPER
unset LC_NAME
unset LC_ADDRESS
unset LC_TELEPHONE
unset LC_MEASUREMENT
unset LC_IDENTIFICATION
unset LC_ALL
unset CHARSET
unset MM_CHARSET
export LANG
export LC_ALL

cat >testsuite.dat <<EOF
Subject: test
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=aaa


--aaa
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

=C3=81BC

--aaa
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

ABC

--aaa
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=C1

--aaa
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Á

--aaa
Content-Type: message/rfc822

Subject: testing

test message

--aaa---
EOF
./reformime -r <testsuite.dat | sed 's/=_.*//;s/X-Mime-Autoconverted:.*//'
./reformime -r7 <testsuite.dat | sed 's/=_.*//;s/X-Mime-Autoconverted:.*//'
./reformime -r8 <testsuite.dat | sed 's/=_.*//;s/X-Mime-Autoconverted:.*//'

cat >testsuite.dat <<EOF
From: nobody
To: nobody
Subject: this is
   a test

testing
EOF
./headercheck 0 "" <testsuite.dat
echo "--"
./headercheck 2 "" <testsuite.dat
echo "--"
./headercheck 1 "" <testsuite.dat

cat >testsuite.dat <<EOF
From: nobody
To: nobody
Mime-Version: 1.0
Content-Type: message/rfc822

From: nobody@example.com
To: nobody@example.com
Message-ID: <a@b>
Subject: Example
   subject

EOF
echo "---"
./headercheck 0 "1.1" <testsuite.dat
echo "--"
./headercheck 2 "1.1" <testsuite.dat
echo "--"
./headercheck 1 "1.1" <testsuite.dat

cat >testsuite.dat <<EOF
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=aaa


--aaa
Content-Type: text/plain; name*=n%41me
Content-Disposition: attachment; filename*1=n%41me;
				 filename*2*=".%65xe";
				 filename*0*=iso8859-1'en_US'file

Testing

--aaa
Content-Type: text/plain; name=name
Content-Disposition: attachment; filename=name.exe; filename*1=n%41me;
				 filename*2*=".%65xe";
				 filename*0*=iso8859-1'en_US'file

Testing

--aaa---
EOF
./reformime <testsuite.dat
./reformime -i <testsuite.dat | sed 's/^charset:.*/charset: iso-8859-1/'

echo "foo" >confmdtest

./makemime -c application/octet-stream -C utf-8 -N footest confmdtest

./makemime -c application/octet-stream -C utf-8 -N footest:1 confmdtest
./makemime -c application/octet-stream -C utf-8 -N f:::::::::::::::::::::::::1 confmdtest

./reformime -h '=?iso-8859-1?Q?H=F3la!?= test =?iso-8859-1?Q?H=F3la!_?=  =?iso-8859-1?Q?H=F3la!?=  test'
rm -f confmdtest

cat >testsuite.dat <<EOF
Subject: test
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Description: =?iso-8859-1?Q?H=F3la!?= test =?iso-8859-1?Q?H=F3la!_?=
		     =?iso-8859-1?Q?H=F3la!?=  test

testing
EOF
./reformime -i <./testsuite.dat
./reformime -H '=?iso-8859-1?Q?H=F3la!?= test <test1@example.com>'
./reformime -H '=?iso-8859-1?Q?H=F3la=22?= test <test1@example.com>'
./reformime -O 'Hóla <user@Hóla.example.com>, user@example.com (Hóla), John Smith <user@example.com>, user2@example.com, "John Smith (Accounting)" <user3@example.com>, "Hóla Smith (Accounting)" <user4@example.com>, john smith <user@hóla.example.com>'

./reformime -H '=?UTF-8?B?SMOzbGE=?= <user@xn--hla-gna.example.com>, =?UTF-8?B?SMOzbGE=?= <user@example.com>, John Smith <user@example.com>, user2@example.com, "John Smith (Accounting)" <user3@example.com>, =?UTF-8?B?SMOzbGE=?= Smith =?UTF-8?B?KEFjY291bnRpbmcp?= <user4@example.com>, john smith <user@xn--hla-gna.example.com>'

./reformime -H 'list: address1 <address1@example.com>, address2 <address2@example.com>;'

./reformime -O 'list: address1 <address1@example.com>, address2 <address2@example.com>;'

./reformime -u <<EOF
From: Sam =?iso-8859-1?B?SOhsbG8=?= <nobody@xn--8ca.example.com>
Subject: =?iso-8859-1?B?SOhsbG8=?=
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="aaa"

Ignore this

--aaa
Content-Type: message/rfc822

Subject: embedded message
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="bbb"

Ignore this too

--bbb
Content-Type: TEXT/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

H=C2llo!

--bbb--

--aaa--
EOF
./reformime -u <<EOF
Subject: test
Mime-Version: 1.0
Content-Type: text/plain; charset="utf-7"

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+AMM
EOF

./reformime -u >/dev/null <<EOF
Subject: test
Mime-Version: 1.0
Content-Type: text/plain; charset="utf-7"

AA+AM
BB
EOF

x="XXXXXXXX"
x="
$x$x$x$x$x$x$x$x"
x="$x$x$x$x"
x="$x$x$x$x"
x="$x$x"

(cat <<EOF
Subject: test
Mime-Version: 1.0
Content-Type: text/plain; charset='iso-8859-15'
$x
EOF
) | tr 'X' '\244' | ./reformime -u | tail -33

cat >testsuite.dat <<EOF
From: MAILER-DAEMON@localhost.localdomain
Subject: Undelivered Mail Returned to Sender
To: user@localhost
Mime-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status; boundary="aaaa"

This is a MIME-formatted message.  If you see this text it means that your
E-mail software does not support MIME-formatted messages.

--aaaa
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Description: Notification

Delivery Status Notification

--aaaa
Content-Type: message/delivery-status
Content-Transfer-Encoding: quoted-printable
Content-Description: Delivery report

Reporting-MTA: dns; mx.domain.com

Final-Recipient: RFC822; user1@localhost.com=20
Original-Recipient: rfc822;lists@domain.com
Action: FAILED
Status: 5.0.0
Remote-MTA: dns; mx.domain.com
Diagnostic-Code: smtp; 517 SPF failure

Final-Recipient: rfc822; user2@localhost.com
Original-Recipient: rfc822;lists@domain.com
Action: failed
Status: 5.0.0
Remote-MTA: dns; mx.domain.com
Diagnostic-Code: smtp; 517 SPF failure

--aaaa
Content-Type: message/rfc822
Content-Description: Undelivered Message

Subject: Test

--aaaa--
EOF

./reformime -d <testsuite.dat
./reformime -D <testsuite.dat

cat >testsuite.dat <<EOF
From: MAILER-DAEMON@localhost.localdomain
Subject: Undelivered Mail Returned to Sender
To: user@localhost
Mime-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status; boundary="aaaa"

This is a MIME-formatted message.  If you see this text it means that your
E-mail software does not support MIME-formatted messages.

--aaaa
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Description: Notification

Delivery Status Notification

--aaaa
Content-Type: message/delivery-status
Content-Transfer-Encoding: 7bit
Content-Description: Delivery report

Reporting-MTA: dns; mx.domain.com

Final-Recipient: rfc822; test2@xn--80akhbyknj4f.net
Original-Recipient: utf-8; test12@испытание.net
Action: failed
Status: 5.0.0
Remote-MTA: dns; mx.domain.com
Diagnostic-Code: smtp; 517 SPF failure

Final-Recipient: rfc822; test3@xn--tst-6la.example.com
Original-Recipient: utf-8; test13@tèst.example.com
Action: failed
Status: 5.0.0
Remote-MTA: dns; mx.domain.com
Diagnostic-Code: smtp; 517 SPF failure

--aaaa
Content-Type: message/rfc822
Content-Description: Undelivered Message

Subject: Test

--aaaa--
EOF

./reformime -d <testsuite.dat
./reformime -D <testsuite.dat
LANG=en_US.iso-8859-1 ./reformime -d <testsuite.dat
LANG=en_US.iso-8859-1 ./reformime -D <testsuite.dat

cat >testsuite1.dat <<EOF
Mime-Version: 1.0
From: Hóla <user@Hóla.example.com>
Subject: UTF-8 message

test
EOF

cat >testsuite2.dat <<EOF
Mime-Version: 1.0
From:  <user@example.com>
Subject: RFC2822 message

test
EOF

VALGRIND=""
if test "`which valgrind 2>/dev/null`" != ""
then
    VALGRIND="valgrind --tool=memcheck --leak-check=yes --error-exitcode=1 "
fi

echo "=== DIGEST 1 ==="
$VALGRIND ./reformime -m testsuite1.dat testsuite2.dat >testsuite.out.dat

sed 's/boundary=.*//;/^--/d' <testsuite.out.dat

echo "=== DIGEST 2 ==="
cat >testsuite.in.dat <<EOF
testsuite1.dat
testsuite2.dat
EOF
$VALGRIND ./reformime -m <testsuite.in.dat >testsuite.out.dat

sed 's/boundary=.*//;/^--/d' <testsuite.out.dat

rm testsuite*.dat
