]> Sergey Matveev's repositories - public-inbox.git/blob - t/mbox_reader.t
mboxreader: new class for reading various mbox formats
[public-inbox.git] / t / mbox_reader.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use List::Util qw(shuffle);
9 use PublicInbox::Eml;
10 use Fcntl qw(SEEK_SET);
11 require_ok 'PublicInbox::MboxReader';
12 require_ok 'PublicInbox::LeiToMail';
13 my %raw = (
14         hdr_only => "From: header-only\@example.com\n\n",
15         small_from => "From: small-from\@example.com\n\nFrom hell\n",
16         small => "From: small\@example.com\n\nfrom hell\n",
17         big_hdr_only => "From: big-header\@example.com\n" .
18                 (('A: '.('a' x 72)."\n") x 1000)."\n",
19         big_body => "From: big-body\@example.com\n\n".
20                 (('b: '.('b' x 72)."\n") x 1000) .
21                 "From hell\n",
22         big_all => "From: big-all\@example.com\n".
23                 (("A: ".('a' x 72)."\n") x 1000). "\n" .
24                 (("b: ".('b' x 72)."\n") x 1000) .
25                 "From hell\n",
26 );
27
28 if ($ENV{TEST_EXTRA}) {
29         for my $fn (glob('t/*.eml'), glob('t/*/*.{patch,eml}')) {
30                 $raw{$fn} = eml_load($fn)->as_string;
31         }
32 }
33
34 my $reader = PublicInbox::MboxReader->new;
35 my $write_in_full = PublicInbox::LeiToMail->can('write_in_full');
36 my $check_fmt = sub {
37         my $fmt = shift;
38         my @order = shuffle(keys %raw);
39         my $eml2mbox = PublicInbox::LeiToMail->can("eml2$fmt");
40         open my $fh, '+>', undef or BAIL_OUT "open: $!";
41         for my $k (@order) {
42                 my $eml = PublicInbox::Eml->new($raw{$k});
43                 my $buf = $eml2mbox->($eml);
44                 $write_in_full->($fh, $buf, undef);
45         }
46         seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
47         $reader->$fmt($fh, sub {
48                 my ($eml) = @_;
49                 my $cur = shift @order;
50                 my @cl = $eml->header_raw('Content-Length');
51                 if ($fmt =~ /\Amboxcl/) {
52                         is(scalar(@cl), 1, "Content-Length set $fmt $cur");
53                         my $raw = $eml->body_raw;
54                         my $adj = 0;
55                         if ($fmt eq 'mboxcl') {
56                                 my @from = ($raw =~ /^(From )/smg);
57                                 $adj = scalar(@from);
58                         }
59                         is(length($raw), $cl[0] - $adj,
60                                 "Content-Length is correct $fmt $cur");
61                         # clobber for ->as_string comparison below
62                         $eml->header_set('Content-Length');
63                 } else {
64                         is(scalar(@cl), 0, "Content-Length unset $fmt $cur");
65                 }
66                 my $orig = PublicInbox::Eml->new($raw{$cur});
67                 is($eml->as_string, $orig->as_string,
68                         "read back original $fmt $cur");
69         });
70 };
71 my @mbox = qw(mboxrd mboxo mboxcl mboxcl2);
72 for my $fmt (@mbox) { $check_fmt->($fmt) }
73 s/\n/\r\n/sg for (values %raw);
74 for my $fmt (@mbox) { $check_fmt->($fmt) }
75
76 done_testing;