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