]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_to_mail.t
www: drop --subject from "git send-email" instructions
[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 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";
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'), '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");
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, cmd => 'test' }, '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}; # to be recreated
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         $wcb = $wcb_get->($mbox, $fn);
123         ok(-f $fn && !-s _, 'truncated mbox destination');
124         $wcb->(\($dup = $buf), $deadbeef);
125         $commit->($wcb);
126         open $fh, '<', $fn or BAIL_OUT $!;
127         is(do { local $/; <$fh> }, $raw, 'wrote identical content');
128         $raw;
129 };
130
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);
135         my $x = $res->[0];
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');
139 });
140
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);
145         my $x = $res->[0];
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');
149 });
150
151 my $zsfx2cmd = PublicInbox::MboxReader->can('zsfx2cmd');
152 for my $zsfx (qw(gz bz2 xz)) {
153         SKIP: {
154                 my $cmd = eval { $zsfx2cmd->($zsfx, 0, $lei) };
155                 skip $@, 3 if $@;
156                 my $dc_cmd = eval { $zsfx2cmd->($zsfx, 1, $lei) };
157                 ok($dc_cmd, "decompressor for .$zsfx");
158                 my $f = "$fn.$zsfx";
159                 my $wcb = $wcb_get->($mbox, $f);
160                 $wcb->(\(my $dup = $buf), { %$deadbeef });
161                 $commit->($wcb);
162                 my $uncompressed = xqx([@$dc_cmd, $f]);
163                 is($uncompressed, $orig, "$zsfx works unlocked");
164
165                 unlink $f or BAIL_OUT "unlink $!";
166                 $wcb = $wcb_get->($mbox, $f);
167                 $wcb->(\($dup = $buf), { %$deadbeef });
168                 $commit->($wcb);
169                 is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
170
171                 local $lei->{opt} = { augment => 1 };
172                 $wcb = $wcb_get->($mbox, $f);
173                 $wcb->(\($dup = $buf . "\nx\n"), { %$deadbeef });
174                 $commit->($wcb);
175
176                 my $cat = popen_rd([@$dc_cmd, $f]);
177                 my @raw;
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");
182
183                 local $lei->{opt} = { augment => 1 };
184                 $wcb = $wcb_get->($mbox, $f);
185                 $wcb->(\($dup = $buf . "\ny\n"), { %$deadbeef });
186                 $commit->($wcb);
187
188                 my @raw3;
189                 $cat = popen_rd([@$dc_cmd, $f]);
190                 PublicInbox::MboxReader->$mbox($cat,
191                         sub { push @raw3, shift->as_string });
192                 my $y = pop @raw3;
193                 is_deeply(\@raw3, \@raw, 'previous messages preserved');
194                 like($y, qr/\nblah\n\ny\n\z/s, "augmented $zsfx (atomic)");
195         }
196 }
197
198 my $as_orig = sub {
199         my ($eml) = @_;
200         $eml->header_set('Status');
201         $eml->as_string;
202 };
203
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);
209         $commit->($wcb);
210         my $cmp = '';
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');
214
215         local $lei->{opt} = { augment => 1 };
216         $wcb = $wcb_get->('mboxo', $fn);
217         $wcb->(\($x = $buf . "\nx\n"), $deadbeef) for (1..2);
218         $commit->($wcb);
219         open $fh, '<', $fn or BAIL_OUT $!;
220         my @x;
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');
225 }
226
227 { # stdout support
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);
232         $commit->($wcb);
233         seek($tmp, 0, SEEK_SET) or BAIL_OUT $!;
234         my $cmp = '';
235         PublicInbox::MboxReader->mboxrd($tmp, sub { $cmp .= $as_orig->(@_) });
236         is($cmp, $buf, 'message written to stdout');
237 }
238
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);
246         $commit->($wcb);
247         my $cmp = '';
248         $cat->blocking(1);
249         PublicInbox::MboxReader->mboxo($cat, sub { $cmp .= $as_orig->(@_) });
250         is($cmp, $buf, 'message written to FIFO');
251 }
252
253 { # Maildir support
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);
260
261         my @f;
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');
265
266         $wcb = $wcb_get->('maildir', $md);
267         my $deadcafe = { blob => 'deadcafe', kw => [] };
268         $wcb->(\($x = $buf."\nx\n"), $deadcafe);
269
270         my @x = ();
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');
276
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
281         @f = ();
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');
290 }
291
292 done_testing;