]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
No ext_urls
[public-inbox.git] / t / import.t
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>
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 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();
14
15 my $git = PublicInbox::Git->new($dir);
16 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
17 $im->init_bare;
18 my $mime = PublicInbox::Eml->new(<<'EOF');
19 From: a@example.com
20 To: b@example.com
21 Subject: this is a subject
22 Message-ID: <a@example.com>
23 Date: Fri, 02 Oct 1993 00:00:00 +0000
24
25 hello world
26 EOF
27
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');
31
32 if ($v2) {
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");
42 }
43
44 $im->done;
45 my @revs = $git->qx(qw(rev-list HEAD));
46 is(scalar @revs, 1, 'one revision created');
47
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');
53 $im->done;
54 @revs = $git->qx(qw(rev-list HEAD));
55 is(scalar @revs, 2, '2 revisions exist');
56
57 is($im->add($mime), undef, 'message only inserted once');
58 $im->done;
59 @revs = $git->qx(qw(rev-list HEAD));
60 is(scalar @revs, 2, '2 revisions exist');
61
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");
66 }
67 $im->done;
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');
73
74 is(undef, $im->remove($mime), 'remove is idempotent');
75
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');
82
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');
87 $im->done;
88 is(undef, $im->checkpoint, 'checkpoint works after ->done');
89 $im->checkpoint;
90
91 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
92 eval {
93         my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
94         $nope->add($mime);
95 };
96 ok($@, 'Import->add fails on non-existent dir');
97
98 my @cls = qw(PublicInbox::Eml);
99 SKIP: {
100         require_mods('PublicInbox::MIME', 1);
101         push @cls, 'PublicInbox::MIME';
102 };
103
104 $main::badchars = "\n\0\r";
105 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
106 for my $cls (@cls) {
107         my $eml = $cls->new(<<EOF);
108 From: $from <spammer\@example.com>
109 Message-ID: <$cls\@example.com>
110
111 EOF
112         ok($im->add($eml), "added $cls message with nasty char in From");
113 }
114 $im->done;
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;
121
122 done_testing();