]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_to_mail.t
lei import: add IMAP and (maildir|mbox*):$PATHNAME support
[public-inbox.git] / t / lei_to_mail.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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 PublicInbox::Eml;
9 use Fcntl qw(SEEK_SET);
10 use PublicInbox::Spawn qw(popen_rd which);
11 use List::Util qw(shuffle);
12 require_mods(qw(DBD::SQLite));
13 require PublicInbox::MdirReader;
14 require PublicInbox::MboxReader;
15 require PublicInbox::LeiOverview;
16 require PublicInbox::LEI;
17 use_ok 'PublicInbox::LeiToMail';
18 my $from = "Content-Length: 10\nSubject: x\n\nFrom hell\n";
19 my $noeol = "Subject: x\n\nFrom hell";
20 my $crlf = $noeol;
21 $crlf =~ s/\n/\r\n/g;
22 my $kw = [qw(seen answered flagged)];
23 my $smsg = { kw => $kw, blob => '0'x40 };
24 my @MBOX = qw(mboxcl2 mboxrd mboxcl mboxo);
25 for my $mbox (@MBOX) {
26         my $m = "eml2$mbox";
27         my $cb = PublicInbox::LeiToMail->can($m);
28         my $s = $cb->(PublicInbox::Eml->new($from), $smsg);
29         is(substr($$s, -1, 1), "\n", "trailing LF in normal $mbox");
30         my $eml = PublicInbox::Eml->new($s);
31         is($eml->header('Status'), 'OR', "Status: set by $m");
32         is($eml->header('X-Status'), 'AF', "X-Status: set by $m");
33         if ($mbox eq 'mboxcl2') {
34                 like($eml->body_raw, qr/^From /, "From not escaped $m");
35         } else {
36                 like($eml->body_raw, qr/^>From /, "From escaped once by $m");
37         }
38         my @cl = $eml->header('Content-Length');
39         if ($mbox =~ /mboxcl/) {
40                 is(scalar(@cl), 1, "$m only has one Content-Length header");
41                 is($cl[0] + length("\n"),
42                         length($eml->body_raw), "$m Content-Length matches");
43         } else {
44                 is(scalar(@cl), 0, "$m clobbered Content-Length");
45         }
46         $s = $cb->(PublicInbox::Eml->new($noeol), $smsg);
47         is(substr($$s, -1, 1), "\n",
48                 "trailing LF added by $m when original lacks EOL");
49         $eml = PublicInbox::Eml->new($s);
50         if ($mbox eq 'mboxcl2') {
51                 is($eml->body_raw, "From hell\n", "From not escaped by $m");
52         } else {
53                 is($eml->body_raw, ">From hell\n", "From escaped once by $m");
54         }
55         $s = $cb->(PublicInbox::Eml->new($crlf), $smsg);
56         is(substr($$s, -2, 2), "\r\n",
57                 "trailing CRLF added $m by original lacks EOL");
58         $eml = PublicInbox::Eml->new($s);
59         if ($mbox eq 'mboxcl2') {
60                 is($eml->body_raw, "From hell\r\n", "From not escaped by $m");
61         } else {
62                 is($eml->body_raw, ">From hell\r\n", "From escaped once by $m");
63         }
64         if ($mbox =~ /mboxcl/) {
65                 is($eml->header('Content-Length') + length("\r\n"),
66                         length($eml->body_raw), "$m Content-Length matches");
67         } elsif ($mbox eq 'mboxrd') {
68                 $s = $cb->($eml, $smsg);
69                 $eml = PublicInbox::Eml->new($s);
70                 is($eml->body_raw,
71                         ">>From hell\r\n\r\n", "From escaped again by $m");
72         }
73 }
74
75 my ($tmpdir, $for_destroy) = tmpdir();
76 local $ENV{TMPDIR} = $tmpdir;
77 open my $err, '>>', "$tmpdir/lei.err" or BAIL_OUT $!;
78 my $lei = bless { 2 => $err }, 'PublicInbox::LEI';
79 my $commit = sub {
80         $_[0] = undef; # wcb
81         delete $lei->{1};
82 };
83 my $buf = <<'EOM';
84 From: x@example.com
85 Subject: x
86
87 blah
88 EOM
89 my $fn = "$tmpdir/x.mbox";
90 my ($mbox) = shuffle(@MBOX); # pick one, shouldn't matter
91 my $wcb_get = sub {
92         my ($fmt, $dst) = @_;
93         delete $lei->{dedupe};
94         $lei->{ovv} = bless {
95                 fmt => $fmt,
96                 dst => $dst
97         }, 'PublicInbox::LeiOverview';
98         my $l2m = PublicInbox::LeiToMail->new($lei);
99         SKIP: {
100                 require_mods('Storable', 1);
101                 my $dup = Storable::thaw(Storable::freeze($l2m));
102                 is_deeply($dup, $l2m, "$fmt round-trips through storable");
103         }
104         $l2m->pre_augment($lei);
105         $l2m->do_augment($lei);
106         $l2m->post_augment($lei);
107         $l2m->write_cb($lei);
108 };
109
110 my $deadbeef = { blob => 'deadbeef', kw => [ qw(seen) ] };
111 my $orig = do {
112         my $wcb = $wcb_get->($mbox, $fn);
113         is(ref $wcb, 'CODE', 'write_cb returned callback');
114         ok(-f $fn && !-s _, 'empty file created');
115         $wcb->(\(my $dup = $buf), $deadbeef);
116         $commit->($wcb);
117         open my $fh, '<', $fn or BAIL_OUT $!;
118         my $raw = do { local $/; <$fh> };
119         like($raw, qr/^blah\n/sm, 'wrote content');
120         unlink $fn or BAIL_OUT $!;
121
122         local $lei->{opt} = { jobs => 2 };
123         $wcb = $wcb_get->($mbox, $fn);
124         ok(-f $fn && !-s _, 'truncated mbox destination');
125         $wcb->(\($dup = $buf), $deadbeef);
126         $commit->($wcb);
127         open $fh, '<', $fn or BAIL_OUT $!;
128         is(do { local $/; <$fh> }, $raw, 'jobs > 1');
129         $raw;
130 };
131
132 test_lei(sub {
133         ok(lei(qw(import -f), $mbox, $fn), 'imported mbox');
134         ok(lei(qw(q s:x)), 'lei q works') or diag $lei_err;
135         my $res = json_utf8->decode($lei_out);
136         my $x = $res->[0];
137         is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
138         is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
139         is($res->[1], undef, 'only one result');
140 });
141
142 test_lei(sub {
143         lei_ok('import', "$mbox:$fn", \'imported mbox:/path') or diag $lei_err;
144         lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
145         my $res = json_utf8->decode($lei_out);
146         my $x = $res->[0];
147         is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
148         is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
149         is($res->[1], undef, 'only one result');
150 });
151
152 for my $zsfx (qw(gz bz2 xz)) { # XXX should we support zst, zz, lzo, lzma?
153         my $zsfx2cmd = PublicInbox::LeiToMail->can('zsfx2cmd');
154         SKIP: {
155                 my $cmd = eval { $zsfx2cmd->($zsfx, 0, $lei) };
156                 skip $@, 3 if $@;
157                 my $dc_cmd = eval { $zsfx2cmd->($zsfx, 1, $lei) };
158                 ok($dc_cmd, "decompressor for .$zsfx");
159                 my $f = "$fn.$zsfx";
160                 my $wcb = $wcb_get->($mbox, $f);
161                 $wcb->(\(my $dup = $buf), $deadbeef);
162                 $commit->($wcb);
163                 my $uncompressed = xqx([@$dc_cmd, $f]);
164                 is($uncompressed, $orig, "$zsfx works unlocked");
165
166                 local $lei->{opt} = { jobs => 2 }; # for atomic writes
167                 unlink $f or BAIL_OUT "unlink $!";
168                 $wcb = $wcb_get->($mbox, $f);
169                 $wcb->(\($dup = $buf), $deadbeef);
170                 $commit->($wcb);
171                 is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
172
173                 local $lei->{opt} = { augment => 1 };
174                 $wcb = $wcb_get->($mbox, $f);
175                 $wcb->(\($dup = $buf . "\nx\n"), $deadbeef);
176                 $commit->($wcb);
177
178                 my $cat = popen_rd([@$dc_cmd, $f]);
179                 my @raw;
180                 PublicInbox::MboxReader->$mbox($cat,
181                         sub { push @raw, shift->as_string });
182                 like($raw[1], qr/\nblah\n\nx\n\z/s, "augmented $zsfx");
183                 like($raw[0], qr/\nblah\n\z/s, "original preserved $zsfx");
184
185                 local $lei->{opt} = { augment => 1, jobs => 2 };
186                 $wcb = $wcb_get->($mbox, $f);
187                 $wcb->(\($dup = $buf . "\ny\n"), $deadbeef);
188                 $commit->($wcb);
189
190                 my @raw3;
191                 $cat = popen_rd([@$dc_cmd, $f]);
192                 PublicInbox::MboxReader->$mbox($cat,
193                         sub { push @raw3, shift->as_string });
194                 my $y = pop @raw3;
195                 is_deeply(\@raw3, \@raw, 'previous messages preserved');
196                 like($y, qr/\nblah\n\ny\n\z/s, "augmented $zsfx (atomic)");
197         }
198 }
199
200 my $as_orig = sub {
201         my ($eml) = @_;
202         $eml->header_set('Status');
203         $eml->as_string;
204 };
205
206 unlink $fn or BAIL_OUT $!;
207 if ('default deduplication uses content_hash') {
208         my $wcb = $wcb_get->('mboxo', $fn);
209         $deadbeef->{kw} = [];
210         $wcb->(\(my $x = $buf), $deadbeef) for (1..2);
211         $commit->($wcb);
212         my $cmp = '';
213         open my $fh, '<', $fn or BAIL_OUT $!;
214         PublicInbox::MboxReader->mboxo($fh, sub { $cmp .= $as_orig->(@_) });
215         is($cmp, $buf, 'only one message written');
216
217         local $lei->{opt} = { augment => 1 };
218         $wcb = $wcb_get->('mboxo', $fn);
219         $wcb->(\($x = $buf . "\nx\n"), $deadbeef) for (1..2);
220         $commit->($wcb);
221         open $fh, '<', $fn or BAIL_OUT $!;
222         my @x;
223         PublicInbox::MboxReader->mboxo($fh, sub { push @x, $as_orig->(@_) });
224         is(scalar(@x), 2, 'augmented mboxo');
225         is($x[0], $cmp, 'original message preserved');
226         is($x[1], $buf . "\nx\n", 'new message appended');
227 }
228
229 { # stdout support
230         open my $tmp, '+>', undef or BAIL_OUT $!;
231         local $lei->{1} = $tmp;
232         my $wcb = $wcb_get->('mboxrd', '/dev/stdout');
233         $wcb->(\(my $x = $buf), $deadbeef);
234         $commit->($wcb);
235         seek($tmp, 0, SEEK_SET) or BAIL_OUT $!;
236         my $cmp = '';
237         PublicInbox::MboxReader->mboxrd($tmp, sub { $cmp .= $as_orig->(@_) });
238         is($cmp, $buf, 'message written to stdout');
239 }
240
241 SKIP: { # FIFO support
242         use POSIX qw(mkfifo);
243         my $fn = "$tmpdir/fifo";
244         mkfifo($fn, 0600) or skip("mkfifo not supported: $!", 1);
245         my $cat = popen_rd([which('cat'), $fn]);
246         my $wcb = $wcb_get->('mboxo', $fn);
247         $wcb->(\(my $x = $buf), $deadbeef);
248         $commit->($wcb);
249         my $cmp = '';
250         PublicInbox::MboxReader->mboxo($cat, sub { $cmp .= $as_orig->(@_) });
251         is($cmp, $buf, 'message written to FIFO');
252 }
253
254 { # Maildir support
255         my $each_file = PublicInbox::MdirReader->can('maildir_each_file');
256         my $md = "$tmpdir/maildir/";
257         my $wcb = $wcb_get->('maildir', $md);
258         is(ref($wcb), 'CODE', 'got Maildir callback');
259         my $b4dc0ffee = { blob => 'badc0ffee', kw => [] };
260         $wcb->(\(my $x = $buf), $b4dc0ffee);
261
262         my @f;
263         $each_file->($md, sub { push @f, shift });
264         open my $fh, $f[0] or BAIL_OUT $!;
265         is(do { local $/; <$fh> }, $buf, 'wrote to Maildir');
266
267         $wcb = $wcb_get->('maildir', $md);
268         my $deadcafe = { blob => 'deadcafe', kw => [] };
269         $wcb->(\($x = $buf."\nx\n"), $deadcafe);
270
271         my @x = ();
272         $each_file->($md, sub { push @x, shift });
273         is(scalar(@x), 1, 'wrote one new file');
274         ok(!-f $f[0], 'old file clobbered');
275         open $fh, $x[0] or BAIL_OUT $!;
276         is(do { local $/; <$fh> }, $buf."\nx\n", 'wrote new file to Maildir');
277
278         local $lei->{opt}->{augment} = 1;
279         $wcb = $wcb_get->('maildir', $md);
280         $wcb->(\($x = $buf."\ny\n"), $deadcafe);
281         $wcb->(\($x = $buf."\ny\n"), $b4dc0ffee); # skipped by dedupe
282         @f = ();
283         $each_file->($md, sub { push @f, shift });
284         is(scalar grep(/\A\Q$x[0]\E\z/, @f), 1, 'old file still there');
285         my @new = grep(!/\A\Q$x[0]\E\z/, @f);
286         is(scalar @new, 1, '1 new file written (b4dc0ffee skipped)');
287         open $fh, $x[0] or BAIL_OUT $!;
288         is(do { local $/; <$fh> }, $buf."\nx\n", 'old file untouched');
289         open $fh, $new[0] or BAIL_OUT $!;
290         is(do { local $/; <$fh> }, $buf."\ny\n", 'new file written');
291 }
292
293 done_testing;