1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#!/usr/bin/env perl
#
# Test the reading from file of message bodies which are multiparts
#
use strict;
use warnings;
use lib qw(. .. tests);
use Tools;
use Test::More tests => 313;
use Mail::Box::Parser::Perl;
use Mail::Message::Body::Lines;
use Mail::Message::Body::Multipart;
use Mail::Message::Head;
my $getbodytype = sub {'Mail::Message::Body::Lines'};
###
### First pass through all messages, with correct data, if available
###
my $parser = Mail::Box::Parser::Perl->new(filename => $src);
ok(defined $parser, "creation of parser");
$parser->pushSeparator('From ');
my (@msgs, $msgnr);
while(1)
{ my (undef, $sep) = $parser->readSeparator;
last unless $sep;
$msgnr++;
my $count = @msgs;
like($sep, qr/^From /, "1 from $count");
my $head = Mail::Message::Head->new;
ok(defined $head, "1 head count");
$head->read($parser);
my $cl = int $head->get('Content-Length');
my $li = int $head->get('Lines');
unless($head->isMultipart)
{ # Skip non-multipart
Mail::Message::Body::Lines->new->read($parser, $head, undef, $cl, $li);
next;
}
my $message;
my $body = Mail::Message::Body::Multipart->new(message => \$message);
my $mp = $head->get('Content-Type')->comment;
if($mp =~ m/['"](.*?)["']/)
{ $body->boundary($1);
}
$body->read($parser, $head, $getbodytype, $cl, $li);
ok(defined $body, "1 body $count");
my $size = $body->size;
my $lines = $body->nrLines;
my $su = $head->get('Subject');
cmp_ok($lines, "==", $li, "1 lines $count")
if defined $li;
$cl -= $li if $crlf_platform;
cmp_ok($size , "==", $cl, "1 size $count")
if defined $cl;
my $msg =
{ size => $size
, lines => $lines
, fields => scalar $head->names
, sep => $sep
, subject=> $su
};
push @msgs, $msg;
}
cmp_ok(@msgs, "==", 3);
$parser->stop;
###
### Now read the whole folder again, but without help of content-length
### and nor lines.
###
undef $parser;
$parser = Mail::Box::Parser::Perl->new(filename => $src);
$parser->pushSeparator('From ');
my $count = 0;
while(1)
{ my (undef, $sep) = $parser->readSeparator;
last unless $sep;
like($sep, qr/^From /, "2 from $count");
my $head = Mail::Message::Head->new->read($parser);
ok(defined $head, "2 head $count");
unless($head->isMultipart)
{ # Skip non-multipart
Mail::Message::Body::Lines->new->read($parser, $head, undef);
next;
}
my $msg = $msgs[$count];
my $message;
my $body = Mail::Message::Body::Multipart->new(message => \$message);
ok(defined $body, "2 body $count");
my $mp = $head->get('Content-Type')->comment;
if($mp =~ m/['"](.*?)["']/)
{ $body->boundary($1);
}
$body->read($parser, $head, $getbodytype);
my $su = $head->get('Subject');
my $size = $body->size;
my $lines = $body->nrLines;
cmp_ok($size, "==", $msg->{size}, "2 size $count");
cmp_ok($lines, "==", $msg->{lines}, "2 lines $count");
is($su, $msg->{subject}, "2 subject $count")
if defined $su && defined $msg->{subject};
cmp_ok($head->names , "==", $msg->{fields}, "2 names $count");
is($sep, $msg->{sep}, "2 sep $count");
$count++;
}
$parser->stop;
###
### Now read the whole folder again, but with deceiving values for
### content-length and lines
###
undef $parser;
$parser = Mail::Box::Parser::Perl->new(filename => $src);
$parser->pushSeparator('From ');
$count = 0;
while(1)
{ my (undef, $sep) = $parser->readSeparator;
last unless $sep;
like($sep, qr/^From /, "3 From $count");
my $head = Mail::Message::Head->new->read($parser);
ok(defined $head, "3 Head $count");
unless($head->isMultipart)
{ # Skip non-multipart
Mail::Message::Body::Lines->new->read($parser, $head, undef);
next;
}
my $msg = $msgs[$count];
my $message;
my $body = Mail::Message::Body::Multipart->new(message => \$message);
ok(defined $body, "3 Body $count");
my $mp = $head->get('Content-Type')->comment;
if($mp =~ m/['"](.*?)["']/)
{ $body->boundary($1);
}
$body->read($parser, $head, $getbodytype, $msg->{size}-15, $msg->{lines}-3);
my $su = $head->get('Subject');
my $size = $body->size;
my $lines = $body->nrLines;
cmp_ok($size, '==', $msg->{size}, "3 size $count");
cmp_ok($lines, '==', $msg->{lines}, "3 lines $count");
is($su, $msg->{subject}, "3 subject $count")
if defined $su && defined $msg->{subject};
cmp_ok($head->names, '==', $msg->{fields}, "3 name $count");
is($sep, $msg->{sep}, "3 sep $count");
$count++;
}
|