2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
10 use PublicInbox::Import;
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 = $v2 ? bless({}, 'PublicInbox::Smsg') : undef;
31 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
34 skip 'git 2.6+ required', 3 if !$v2;
35 like($smsg->{blob}, qr/\A[a-f0-9]{40,64}\z/, 'got last object_id');
36 my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
37 open my $in, '+<', undef or BAIL_OUT "open(+<): $!";
38 print $in $mime->as_string or die "write failed: $!";
39 $in->flush or die "flush failed: $!";
40 seek $in, 0, SEEK_SET;
41 chomp(my $hashed_obj = xqx(\@cmd, undef, { 0 => $in }));
42 is($?, 0, 'hash-object');
43 is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
47 my @revs = $git->qx(qw(rev-list HEAD));
48 is(scalar @revs, 1, 'one revision created');
50 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
51 $mime->header_set('From', $odd);
52 $mime->header_set('Message-ID', '<b@example.com>');
53 $mime->header_set('Subject', 'msg2');
54 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
56 @revs = $git->qx(qw(rev-list HEAD));
57 is(scalar @revs, 2, '2 revisions exist');
59 is($im->add($mime), undef, 'message only inserted once');
61 @revs = $git->qx(qw(rev-list HEAD));
62 is(scalar @revs, 2, '2 revisions exist');
64 foreach my $c ('c'..'z') {
65 $mime->header_set('Message-ID', "<$c\@example.com>");
66 $mime->header_set('Subject', "msg - $c");
67 like($im->add($mime), qr/\A:\d+\z/, "added $c message");
70 @revs = $git->qx(qw(rev-list HEAD));
71 is(scalar @revs, 26, '26 revisions exist after mass import');
72 my ($mark, $msg) = $im->remove($mime);
73 like($mark, qr/\A:\d+\z/, 'got mark');
74 like(ref($msg), qr/\bPublicInbox::(?:Eml|MIME)\b/, 'got old message deleted');
76 is(undef, $im->remove($mime), 'remove is idempotent');
78 # mismatch on identical Message-ID
79 $mime->header_set('Message-ID', '<a@example.com>');
80 ($mark, $msg) = $im->remove($mime);
81 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
82 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
83 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
85 $mime->header_set('Message-Id', '<failcheck@example.com>');
86 is($im->add($mime, sub { undef }), undef, 'check callback fails');
87 is($im->remove($mime), undef, 'message not added, so not removed');
88 is(undef, $im->checkpoint, 'checkpoint works before ->done');
90 is(undef, $im->checkpoint, 'checkpoint works after ->done');
93 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
95 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
98 ok($@, 'Import->add fails on non-existent dir');
100 my @cls = qw(PublicInbox::Eml);
102 require_mods('Email::MIME', 1);
103 require PublicInbox::MIME;
104 push @cls, 'PublicInbox::MIME';
107 $main::badchars = "\n\0\r";
108 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
110 my $eml = $cls->new(<<EOF);
111 From: $from <spammer\@example.com>
112 Message-ID: <$cls\@example.com>
115 ok($im->add($eml), "added $cls message with nasty char in From");
118 my $bref = $git->cat_file('HEAD');
119 like($$bref, qr/^author Ba d \$main::badchars <spammer\@example\.com> /sm,
120 'latest commit accepted by spammer');
121 $git->qx(qw(fsck --no-progress --strict));
122 is($?, 0, 'fsck reported no errors');
123 $main::badchars = undef;