2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
9 use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK);
10 use PublicInbox::Spawn qw(popen_rd);
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";
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) {
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'), 'RO', "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");
36 like($eml->body_raw, qr/^>From /, "From escaped once by $m");
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");
44 is(scalar(@cl), 0, "$m clobbered Content-Length");
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");
53 is($eml->body_raw, ">From hell\n", "From escaped once by $m");
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");
62 is($eml->body_raw, ">From hell\r\n", "From escaped once by $m");
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);
71 ">>From hell\r\n\r\n", "From escaped again by $m");
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, cmd => 'test' }, 'PublicInbox::LEI';
89 my $fn = "$tmpdir/x.mbox";
90 my ($mbox) = shuffle(@MBOX); # pick one, shouldn't matter
93 delete $lei->{dedupe}; # to be recreated
97 }, 'PublicInbox::LeiOverview';
98 my $l2m = PublicInbox::LeiToMail->new($lei);
100 require_mods('Storable', 1);
101 my $dup = Storable::thaw(Storable::freeze($l2m));
102 is_deeply($dup, $l2m, "$fmt round-trips through storable");
104 $l2m->pre_augment($lei);
105 $l2m->do_augment($lei);
106 $l2m->post_augment($lei);
107 $l2m->write_cb($lei);
110 my $deadbeef = { blob => 'deadbeef', kw => [ qw(seen) ] };
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);
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 $!;
122 $wcb = $wcb_get->($mbox, $fn);
123 ok(-f $fn && !-s _, 'truncated mbox destination');
124 $wcb->(\($dup = $buf), $deadbeef);
126 open $fh, '<', $fn or BAIL_OUT $!;
127 is(do { local $/; <$fh> }, $raw, 'wrote identical content');
131 test_lei({tmpdir => "$tmpdir/using -F"}, sub {
132 lei_ok(qw(import -F), $mbox, $fn, \'imported mbox');
133 lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
134 my $res = json_utf8->decode($lei_out);
136 is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
137 is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
138 is($res->[1], undef, 'only one result');
141 test_lei({tmpdir => "$tmpdir/using TYPE: prefix"}, sub {
142 lei_ok('import', "$mbox:$fn", \'imported mbox:/path') or diag $lei_err;
143 lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
144 my $res = json_utf8->decode($lei_out);
146 is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
147 is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
148 is($res->[1], undef, 'only one result');
151 my $zsfx2cmd = PublicInbox::MboxReader->can('zsfx2cmd');
152 for my $zsfx (qw(gz bz2 xz)) {
154 my $cmd = eval { $zsfx2cmd->($zsfx, 0, $lei) };
156 my $dc_cmd = eval { $zsfx2cmd->($zsfx, 1, $lei) };
157 ok($dc_cmd, "decompressor for .$zsfx");
159 my $wcb = $wcb_get->($mbox, $f);
160 $wcb->(\(my $dup = $buf), { %$deadbeef });
162 my $uncompressed = xqx([@$dc_cmd, $f]);
163 is($uncompressed, $orig, "$zsfx works unlocked");
165 unlink $f or BAIL_OUT "unlink $!";
166 $wcb = $wcb_get->($mbox, $f);
167 $wcb->(\($dup = $buf), { %$deadbeef });
169 is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
171 local $lei->{opt} = { augment => 1 };
172 $wcb = $wcb_get->($mbox, $f);
173 $wcb->(\($dup = $buf . "\nx\n"), { %$deadbeef });
176 my $cat = popen_rd([@$dc_cmd, $f]);
178 PublicInbox::MboxReader->$mbox($cat,
179 sub { push @raw, shift->as_string });
180 like($raw[1], qr/\nblah\n\nx\n\z/s, "augmented $zsfx");
181 like($raw[0], qr/\nblah\n\z/s, "original preserved $zsfx");
183 local $lei->{opt} = { augment => 1 };
184 $wcb = $wcb_get->($mbox, $f);
185 $wcb->(\($dup = $buf . "\ny\n"), { %$deadbeef });
189 $cat = popen_rd([@$dc_cmd, $f]);
190 PublicInbox::MboxReader->$mbox($cat,
191 sub { push @raw3, shift->as_string });
193 is_deeply(\@raw3, \@raw, 'previous messages preserved');
194 like($y, qr/\nblah\n\ny\n\z/s, "augmented $zsfx (atomic)");
200 $eml->header_set('Status');
204 unlink $fn or BAIL_OUT $!;
205 if ('default deduplication uses content_hash') {
206 my $wcb = $wcb_get->('mboxo', $fn);
207 $deadbeef->{kw} = [];
208 $wcb->(\(my $x = $buf), $deadbeef) for (1..2);
211 open my $fh, '<', $fn or BAIL_OUT $!;
212 PublicInbox::MboxReader->mboxo($fh, sub { $cmp .= $as_orig->(@_) });
213 is($cmp, $buf, 'only one message written');
215 local $lei->{opt} = { augment => 1 };
216 $wcb = $wcb_get->('mboxo', $fn);
217 $wcb->(\($x = $buf . "\nx\n"), $deadbeef) for (1..2);
219 open $fh, '<', $fn or BAIL_OUT $!;
221 PublicInbox::MboxReader->mboxo($fh, sub { push @x, $as_orig->(@_) });
222 is(scalar(@x), 2, 'augmented mboxo');
223 is($x[0], $cmp, 'original message preserved');
224 is($x[1], $buf . "\nx\n", 'new message appended');
228 open my $tmp, '+>', undef or BAIL_OUT $!;
229 local $lei->{1} = $tmp;
230 my $wcb = $wcb_get->('mboxrd', '/dev/stdout');
231 $wcb->(\(my $x = $buf), $deadbeef);
233 seek($tmp, 0, SEEK_SET) or BAIL_OUT $!;
235 PublicInbox::MboxReader->mboxrd($tmp, sub { $cmp .= $as_orig->(@_) });
236 is($cmp, $buf, 'message written to stdout');
239 SKIP: { # FIFO support
240 use POSIX qw(mkfifo);
241 my $fn = "$tmpdir/fifo";
242 mkfifo($fn, 0600) or skip("mkfifo not supported: $!", 1);
243 sysopen(my $cat, $fn, O_RDONLY|O_NONBLOCK) or BAIL_OUT $!;
244 my $wcb = $wcb_get->('mboxo', $fn);
245 $wcb->(\(my $x = $buf), $deadbeef);
249 PublicInbox::MboxReader->mboxo($cat, sub { $cmp .= $as_orig->(@_) });
250 is($cmp, $buf, 'message written to FIFO');
254 my $mdr = PublicInbox::MdirReader->new;
255 my $md = "$tmpdir/maildir/";
256 my $wcb = $wcb_get->('maildir', $md);
257 is(ref($wcb), 'CODE', 'got Maildir callback');
258 my $b4dc0ffee = { blob => 'badc0ffee', kw => [] };
259 $wcb->(\(my $x = $buf), $b4dc0ffee);
262 $mdr->maildir_each_file($md, sub { push @f, shift });
263 open my $fh, '<', $f[0] or BAIL_OUT $!;
264 is(do { local $/; <$fh> }, $buf, 'wrote to Maildir');
266 $wcb = $wcb_get->('maildir', $md);
267 my $deadcafe = { blob => 'deadcafe', kw => [] };
268 $wcb->(\($x = $buf."\nx\n"), $deadcafe);
271 $mdr->maildir_each_file($md, sub { push @x, shift });
272 is(scalar(@x), 1, 'wrote one new file');
273 ok(!-f $f[0], 'old file clobbered');
274 open $fh, '<', $x[0] or BAIL_OUT $!;
275 is(do { local $/; <$fh> }, $buf."\nx\n", 'wrote new file to Maildir');
277 local $lei->{opt}->{augment} = 1;
278 $wcb = $wcb_get->('maildir', $md);
279 $wcb->(\($x = $buf."\ny\n"), $deadcafe);
280 $wcb->(\($x = $buf."\ny\n"), $b4dc0ffee); # skipped by dedupe
282 $mdr->maildir_each_file($md, sub { push @f, shift });
283 is(scalar grep(/\A\Q$x[0]\E\z/, @f), 1, 'old file still there');
284 my @new = grep(!/\A\Q$x[0]\E\z/, @f);
285 is(scalar @new, 1, '1 new file written (b4dc0ffee skipped)');
286 open $fh, '<', $x[0] or BAIL_OUT $!;
287 is(do { local $/; <$fh> }, $buf."\nx\n", 'old file untouched');
288 open $fh, '<', $new[0] or BAIL_OUT $!;
289 is(do { local $/; <$fh> }, $buf."\ny\n", 'new file written');