1 # Copyright (C) 2016-2021 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 Fcntl qw(:DEFAULT SEEK_SET);
11 use PublicInbox::TestCommon;
12 use MIME::Base64 3.05; # Perl 5.10.0 / 5.9.2
13 my ($dir, $for_destroy) = tmpdir();
15 my $git = PublicInbox::Git->new($dir);
16 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
18 my $mime = PublicInbox::Eml->new(<<'EOF');
21 Subject: this is a subject
22 Message-ID: <a@example.com>
23 Date: Fri, 02 Oct 1993 00:00:00 +0000
28 my $v2 = require_git(2.6, 1);
29 my $smsg = bless {}, 'PublicInbox::Smsg' if $v2;
30 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
33 like($smsg->{blob}, qr/\A[a-f0-9]{40}\z/, 'got last object_id');
34 my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
35 open my $in, '+<', undef or BAIL_OUT "open(+<): $!";
36 print $in $mime->as_string or die "write failed: $!";
37 $in->flush or die "flush failed: $!";
38 seek($in, 0, SEEK_SET) or die "seek: $!";
39 chomp(my $hashed_obj = xqx(\@cmd, undef, { 0 => $in }));
40 is($?, 0, 'hash-object');
41 is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
45 my @revs = $git->qx(qw(rev-list HEAD));
46 is(scalar @revs, 1, 'one revision created');
48 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
49 $mime->header_set('From', $odd);
50 $mime->header_set('Message-ID', '<b@example.com>');
51 $mime->header_set('Subject', 'msg2');
52 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
54 @revs = $git->qx(qw(rev-list HEAD));
55 is(scalar @revs, 2, '2 revisions exist');
57 is($im->add($mime), undef, 'message only inserted once');
59 @revs = $git->qx(qw(rev-list HEAD));
60 is(scalar @revs, 2, '2 revisions exist');
62 foreach my $c ('c'..'z') {
63 $mime->header_set('Message-ID', "<$c\@example.com>");
64 $mime->header_set('Subject', "msg - $c");
65 like($im->add($mime), qr/\A:\d+\z/, "added $c message");
68 @revs = $git->qx(qw(rev-list HEAD));
69 is(scalar @revs, 26, '26 revisions exist after mass import');
70 my ($mark, $msg) = $im->remove($mime);
71 like($mark, qr/\A:\d+\z/, 'got mark');
72 like(ref($msg), qr/\bPublicInbox::(?:Eml|MIME)\b/, 'got old message deleted');
74 is(undef, $im->remove($mime), 'remove is idempotent');
76 # mismatch on identical Message-ID
77 $mime->header_set('Message-ID', '<a@example.com>');
78 ($mark, $msg) = $im->remove($mime);
79 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
80 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
81 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
83 $mime->header_set('Message-Id', '<failcheck@example.com>');
84 is($im->add($mime, sub { undef }), undef, 'check callback fails');
85 is($im->remove($mime), undef, 'message not added, so not removed');
86 is(undef, $im->checkpoint, 'checkpoint works before ->done');
88 is(undef, $im->checkpoint, 'checkpoint works after ->done');
91 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
93 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
96 ok($@, 'Import->add fails on non-existent dir');
98 my @cls = qw(PublicInbox::Eml);
100 require_mods('PublicInbox::MIME', 1);
101 push @cls, 'PublicInbox::MIME';
104 $main::badchars = "\n\0\r";
105 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
107 my $eml = $cls->new(<<EOF);
108 From: $from <spammer\@example.com>
109 Message-ID: <$cls\@example.com>
112 ok($im->add($eml), "added $cls message with nasty char in From");
115 my $bref = $git->cat_file('HEAD');
116 like($$bref, qr/^author Ba d \$main::badchars <spammer\@example\.com> /sm,
117 'latest commit accepted by spammer');
118 $git->qx(qw(fsck --no-progress --strict));
119 is($?, 0, 'fsck reported no errors');
120 $main::badchars = undef;