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>
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();
16 my $git = PublicInbox::Git->new($dir);
17 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
19 my $mime = PublicInbox::Eml->new(<<'EOF');
22 Subject: this is a subject
23 Message-ID: <a@example.com>
24 Date: Fri, 02 Oct 1993 00:00:00 +0000
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');
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");
53 my @revs = $git->qx(qw(rev-list HEAD));
54 is(scalar @revs, 1, 'one revision created');
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');
62 @revs = $git->qx(qw(rev-list HEAD));
63 is(scalar @revs, 2, '2 revisions exist');
65 is($im->add($mime), undef, 'message only inserted once');
67 @revs = $git->qx(qw(rev-list HEAD));
68 is(scalar @revs, 2, '2 revisions exist');
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");
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');
82 is(undef, $im->remove($mime), 'remove is idempotent');
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');
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');
96 is(undef, $im->checkpoint, 'checkpoint works after ->done');
99 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
101 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
104 ok($@, 'Import->add fails on non-existent dir');
106 my @cls = qw(PublicInbox::Eml);
108 require_mods('PublicInbox::MIME', 1);
109 push @cls, 'PublicInbox::MIME';
112 $main::badchars = "\n\0\r";
113 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
115 my $eml = $cls->new(<<EOF);
116 From: $from <spammer\@example.com>
117 Message-ID: <$cls\@example.com>
120 ok($im->add($eml), "added $cls message with nasty char in From");
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;