]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
t/import: quiet warning, clobber variable
[public-inbox.git] / t / import.t
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>
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 PublicInbox::Spawn qw(spawn);
11 use Fcntl qw(:DEFAULT SEEK_SET);
12 use File::Temp qw/tempfile/;
13 use PublicInbox::TestCommon;
14 use MIME::Base64 3.05; # Perl 5.10.0 / 5.9.2
15 my ($dir, $for_destroy) = tmpdir();
16
17 my $git = PublicInbox::Git->new($dir);
18 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
19 $im->init_bare;
20 my $mime = PublicInbox::Eml->new(<<'EOF');
21 From: a@example.com
22 To: b@example.com
23 Subject: this is a subject
24 Message-ID: <a@example.com>
25 Date: Fri, 02 Oct 1993 00:00:00 +0000
26
27 hello world
28 EOF
29
30 my $v2 = require_git(2.6, 1);
31 my $smsg = bless {}, 'PublicInbox::Smsg' if $v2;
32 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
33
34 if ($v2) {
35         like($smsg->{blob}, qr/\A[a-f0-9]{40}\z/, 'got last object_id');
36         my $raw_email = $smsg->{-raw_email};
37         is($mime->as_string, $$raw_email, 'string matches');
38         is($smsg->{raw_bytes}, length($$raw_email), 'length matches');
39         my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
40         my $in = tempfile();
41         print $in $mime->as_string or die "write failed: $!";
42         $in->flush or die "flush failed: $!";
43         seek($in, 0, SEEK_SET);
44         my $out = tempfile();
45         my $pid = spawn(\@cmd, {}, { 0 => $in, 1 => $out });
46         is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
47         is($?, 0, 'hash-object');
48         seek($out, 0, SEEK_SET);
49         chomp(my $hashed_obj = <$out>);
50         is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
51 }
52
53 $im->done;
54 my @revs = $git->qx(qw(rev-list HEAD));
55 is(scalar @revs, 1, 'one revision created');
56
57 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
58 $mime->header_set('From', $odd);
59 $mime->header_set('Message-ID', '<b@example.com>');
60 $mime->header_set('Subject', 'msg2');
61 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
62 $im->done;
63 @revs = $git->qx(qw(rev-list HEAD));
64 is(scalar @revs, 2, '2 revisions exist');
65
66 is($im->add($mime), undef, 'message only inserted once');
67 $im->done;
68 @revs = $git->qx(qw(rev-list HEAD));
69 is(scalar @revs, 2, '2 revisions exist');
70
71 foreach my $c ('c'..'z') {
72         $mime->header_set('Message-ID', "<$c\@example.com>");
73         $mime->header_set('Subject', "msg - $c");
74         like($im->add($mime), qr/\A:\d+\z/, "added $c message");
75 }
76 $im->done;
77 @revs = $git->qx(qw(rev-list HEAD));
78 is(scalar @revs, 26, '26 revisions exist after mass import');
79 my ($mark, $msg) = $im->remove($mime);
80 like($mark, qr/\A:\d+\z/, 'got mark');
81 like(ref($msg), qr/\bPublicInbox::(?:Eml|MIME)\b/, 'got old message deleted');
82
83 is(undef, $im->remove($mime), 'remove is idempotent');
84
85 # mismatch on identical Message-ID
86 $mime->header_set('Message-ID', '<a@example.com>');
87 ($mark, $msg) = $im->remove($mime);
88 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
89 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
90 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
91
92 $mime->header_set('Message-Id', '<failcheck@example.com>');
93 is($im->add($mime, sub { undef }), undef, 'check callback fails');
94 is($im->remove($mime), undef, 'message not added, so not removed');
95 is(undef, $im->checkpoint, 'checkpoint works before ->done');
96 $im->done;
97 is(undef, $im->checkpoint, 'checkpoint works after ->done');
98 $im->checkpoint;
99
100 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
101 eval {
102         my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
103         $nope->add($mime);
104 };
105 ok($@, 'Import->add fails on non-existent dir');
106
107 my @cls = qw(PublicInbox::Eml);
108 SKIP: {
109         require_mods('PublicInbox::MIME', 1);
110         push @cls, 'PublicInbox::MIME';
111 };
112
113 $main::badchars = "\n\0\r";
114 my $from = '=?UTF-8?B?'. encode_base64("B\ra\nd\0\$main::badchars", ''). '?=';
115 for my $cls (@cls) {
116         my $eml = $cls->new(<<EOF);
117 From: $from <spammer\@example.com>
118 Message-ID: <$cls\@example.com>
119
120 EOF
121         ok($im->add($eml), "added $cls message with nasty char in From");
122 }
123 $im->done;
124 my $bref = $git->cat_file('HEAD');
125 like($$bref, qr/^author Ba d \$main::badchars <spammer\@example\.com> /sm,
126          'latest commit accepted by spammer');
127 $git->qx(qw(fsck --no-progress --strict));
128 is($?, 0, 'fsck reported no errors');
129 $main::badchars = undef;
130
131 done_testing();