#!perl
use Cassandane::Tiny;

sub test_email_import_zerobyte
  :needs_component_sieve :NoCheckSyslog
    ($self)
{
    my $jmap = $self->{jmap};

    my @testCases = ({
        desc => "unencoded zero byte in MIME body",
        mime => <<"EOF",
From: \"Some Example Sender\" <example\@local>\r\n
To: baseball\@local\r\n
Subject: test email\r\n
Date: Wed, 7 Dec 2016 22:11:11 +1100\r\n
MIME-Version: 1.0\r\n
Content-Type: text/plain; charset="UTF-8"\r\n
\r\n
This is a test email with a \x{0}-byte.\r\n
EOF
    }, {
        desc => "unencoded zero byte in MIME headers",
        mime => <<"EOF",
From: from\@local\r\n
To: to\@local\r\n
Subject: subject with \x{0}-byte\r\n
Date: Wed, 7 Dec 2016 22:11:11 +1100\r\n
MIME-Version: 1.0\r\n
Content-Type: text/plain; charset="UTF-8"\r\n
\r\n
This is a test email.\r\n
EOF
    });

    foreach (@testCases) {
        xlog $self, "Upload MIME blob for test '$_->{desc}'";
        my $blobId = $jmap->Upload($_->{mime}, "message/rfc822")->{blobId};

        xlog $self, "Import email from blob $blobId";
        my $res = $jmap->CallMethods([ [
            'Email/import',
            {
                emails => {
                    "1" => {
                        blobId     => $blobId,
                        mailboxIds => { '$inbox' => JSON::true },
                    },
                },
            },
            "R1"
        ] ]);
        $self->assert_str_equals("invalidEmail",
            $res->[0][1]{notCreated}{1}{type});
    }
}
