]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
9a88416f2cabb0b2529288f05c4eb5611928b2ca
[public-inbox.git] / t / import.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Eml;
7 use PublicInbox::Smsg;
8 use PublicInbox::Git;
9 use PublicInbox::Import;
10 use PublicInbox::Spawn qw(spawn);
11 use Fcntl qw(:DEFAULT SEEK_SET);
12 use PublicInbox::TestCommon;
13 use MIME::Base64 3.05; # Perl 5.10.0 / 5.9.2
14 my ($dir, $for_destroy) = tmpdir();
15
16 my $git = PublicInbox::Git->new($dir);
17 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
18 $im->init_bare;
19 my $mime = PublicInbox::Eml->new(<<'EOF');
20 From: a@example.com
21 To: b@example.com
22 Subject: this is a subject
23 Message-ID: <a@example.com>
24 Date: Fri, 02 Oct 1993 00:00:00 +0000
25
26 hello world
27 EOF
28
29 my $v2 = require_git(2.6, 1);
30 my $smsg = bless {}, 'PublicInbox::Smsg' if $v2;
31 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
32
33 if ($v2) {
34         like($smsg->{blob}, qr/\A[a-f0-9]{40}\z/, 'got last object_id');
35         my $raw_email = $smsg->{-raw_email};
36         is($mime->as_string, $$raw_email, 'string matches');
37         is($smsg->{raw_bytes}, length($$raw_email), 'length matches');
38         my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
39         open my $in, '+<', undef or BAIL_OUT "open(+<): $!";
40         print $in $mime->as_string or die "write failed: $!";
41         $in->flush or die "flush failed: $!";
42         seek($in, 0, SEEK_SET);
43         open my $out, '+<', undef or BAIL_OUT "open(+<): $!";
44         my $pid = spawn(\@cmd, {}, { 0 => $in, 1 => $out });
45         is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
46         is($?, 0, 'hash-object');
47         seek($out, 0, SEEK_SET);
48         chomp(my $hashed_obj = <$out>);
49         is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
50 }
51
52 $im->done;
53 my @revs = $git->qx(qw(rev-list HEAD));
54 is(scalar @revs, 1, 'one revision created');
55
56 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
57 $mime->header_set('From', $odd);
58 $mime->header_set('Message-ID', '<b@example.com>');
59 $mime->header_set('Subject', 'msg2');
60 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
61 $im->done;
62 @revs = $git->qx(qw(rev-list HEAD));
63 is(scalar @revs, 2, '2 revisions exist');
64
65 is($im->add($mime), undef, 'message only inserted once');
66 $im->done;
67 @revs = $git->qx(qw(rev-list HEAD));
68 is(scalar @revs, 2, '2 revisions exist');
69
70 foreach my $c ('c'..'z') {
71         $mime->header_set('Message-ID', "<$c\@example.com>");
72         $mime->header_set('Subject', "msg - $c");
73         like($im->add($mime), qr/\A:\d+\z/, "added $c message");
74 }
75 $im->done;
76 @revs = $git->qx(qw(rev-list HEAD));
77 is(scalar @revs, 26, '26 revisions exist after mass import');
78 my ($mark, $msg) = $im->remove($mime);
79 like($mark, qr/\A:\d+\z/, 'got mark');
80 like(ref($msg), qr/\bPublicInbox::(?:Eml|MIME)\b/, 'got old message deleted');
81
82 is(undef, $im->remove($mime), 'remove is idempotent');
83
84 # mismatch on identical Message-ID
85 $mime->header_set('Message-ID', '<a@example.com>');
86 ($mark, $msg) = $im->remove($mime);
87 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
88 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
89 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
90
91 $mime->header_set('Message-Id', '<failcheck@example.com>');
92 is($im->add($mime, sub { undef }), undef, 'check callback fails');
93 is($im->remove($mime), undef, 'message not added, so not removed');
94 is(undef, $im->checkpoint, 'checkpoint works before ->done');
95 $im->done;
96 is(undef, $im->checkpoint, 'checkpoint works after ->done');
97 $im->checkpoint;
98
99 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
100 eval {
101         my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
102         $nope->add($mime);
103 };
104 ok($@, 'Import->add fails on non-existent dir');
105
106 my @cls = qw(PublicInbox::Eml);
107 SKIP: {
108         require_mods('PublicInbox::MIME', 1);
109         push @cls, 'PublicInbox::MIME';
110 };
111
112 $main::badchars = "\n\0\r";
113 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
114 for my $cls (@cls) {
115         my $eml = $cls->new(<<EOF);
116 From: $from <spammer\@example.com>
117 Message-ID: <$cls\@example.com>
118
119 EOF
120         ok($im->add($eml), "added $cls message with nasty char in From");
121 }
122 $im->done;
123 my $bref = $git->cat_file('HEAD');
124 like($$bref, qr/^author Ba d \$main::badchars <spammer\@example\.com> /sm,
125          'latest commit accepted by spammer');
126 $git->qx(qw(fsck --no-progress --strict));
127 is($?, 0, 'fsck reported no errors');
128 $main::badchars = undef;
129
130 done_testing();