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 200 201 202 203 204 205 206 207 208 209
|
#!/usr/bin/perl -T
#
# Test the reading from file of message bodies which have their content
# stored in a single string.
use strict;
use warnings;
use lib qw(. .. tests);
use Tools;
use Test::More tests => 945;
use Mail::Box::Parser::Perl;
use Mail::Message::Body::String;
use Mail::Message::Head;
###
### First carefully read the first message
###
my $parser = Mail::Box::Parser::Perl->new(filename => $src);
ok(defined $parser, "creation of parser");
exit 1 unless defined $parser;
$parser->pushSeparator('From ');
my ($where, $sep) = $parser->readSeparator;
cmp_ok($where, "==", 0, "begin at file-start");
ok(defined $sep, "reading first separator");
like($sep, qr/^From /, "correctness first separator")
if defined $sep;
my $head = Mail::Message::Head->new;
ok(defined $head);
$head->read($parser);
ok(defined $head);
ok($head, "overloaded boolean");
my $hard_coded_lines_msg0 = 33;
my $hard_coded_length_msg0 = 1280;
my $binary_size = $hard_coded_length_msg0
+ ($crlf_platform ? $hard_coded_lines_msg0 : 0);
my $length = int $head->get('Content-Length');
cmp_ok($length, "==", $binary_size, "first message size");
my $lines = int $head->get('Lines');
cmp_ok($lines, "==", $hard_coded_lines_msg0, "first message lines");
my $body = Mail::Message::Body::String->new;
$body->read($parser, $head, undef, $length, $lines);
ok(defined $body, "reading of first body");
my @lines = $body->lines;
$length -= @lines if $crlf_platform;
cmp_ok($body->size, "==", $length, "size of body");
cmp_ok(@lines, "==", $lines, "lines of body");
#
# Try to read the rest of the folder, with specified content-length
# and lines if available.
#
my @msgs;
push @msgs, # first message already read.
{ fields => scalar $head->names
, lines => $hard_coded_lines_msg0
, size => $hard_coded_length_msg0
, sep => $sep
, subject=> $head->get('subject')
};
while(1)
{ my ($where, $sep) = $parser->readSeparator;
last unless $sep;
my $count = @msgs;
like($sep, qr/^From /, "1 from $count");
$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');
my $su = $head->get('Subject');
$body = Mail::Message::Body::String->new
->read($parser, $head, undef, $cl, $li);
ok(defined $body, "1 body $count");
my $size = $body->size;
my $lines = $body->nrLines;
cmp_ok($li , "==", $lines, "1 lines $count")
if defined $li;
$cl -= $li if $crlf_platform;
cmp_ok($cl , "==", $size, "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, "==", 45);
$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($sep = $parser->readSeparator)
{ my $msg = $msgs[$count];
like($sep, qr/^From /, "2 from $count");
$head = Mail::Message::Head->new->read($parser);
ok(defined $head, "2 head $count");
$body = Mail::Message::Body::String->new->read($parser, $head, undef);
ok(defined $body, "2 body $count");
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 ($where, $sep) = $parser->readSeparator;
last unless $sep;
my $msg = $msgs[$count];
like($sep, qr/^From /, "3 From $count");
$head = Mail::Message::Head->new->read($parser);
ok(defined $head, "3 Head $count");
$body = Mail::Message::Body::String->new;
$body->read($parser, $head, undef, $msg->{size}-15, $msg->{lines}-3);
ok(defined $body, "3 Body $count");
my $su = $head->get('Subject');
my $size = $body->size;
my $lines = $body->nrLines;
# two messages contain one trailing blank, which is removed because
# of the wrong number of lines. The will have an extra OK.
my $wrong = $count==14 || $count==18;
cmp_ok($size, '==', $msg->{size}, "3 size $count")
unless $wrong;
cmp_ok($lines, '==', $msg->{lines}, "3 lines $count")
unless $wrong;
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++;
}
|