]> Sergey Matveev's repositories - public-inbox.git/blob - t/replace.t
2346c3a19d1103cbee1d00f4f5d1512bebd51c84
[public-inbox.git] / t / replace.t
1 # Copyright (C) 2019 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::MIME;
7 use PublicInbox::InboxWritable;
8 use PublicInbox::TestCommon;
9 use Cwd qw(abs_path);
10 require_git(2.6); # replace is v2 only, for now...
11 foreach my $mod (qw(DBD::SQLite)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for $0" if $@;
14 }
15
16 local $ENV{HOME} = abs_path('t');
17
18 sub test_replace ($$$) {
19         my ($v, $level, $opt) = @_;
20         diag "v$v $level replace";
21         my $this = "pi-$v-$level-replace";
22         my ($tmpdir, $for_destroy) = tmpdir($this);
23         my $ibx = PublicInbox::Inbox->new({
24                 inboxdir => "$tmpdir/testbox",
25                 name => $this,
26                 version => $v,
27                 -primary_address => 'test@example.com',
28                 indexlevel => $level,
29         });
30
31         my $orig = PublicInbox::MIME->new(<<'EOF');
32 From: Barbra Streisand <effect@example.com>
33 To: test@example.com
34 Subject: confidential
35 Message-ID: <replace@example.com>
36 Date: Fri, 02 Oct 1993 00:00:00 +0000
37
38 Top secret info about my house in Malibu...
39 EOF
40         my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
41         # fake a bunch of epochs
42         $im->{rotate_bytes} = $opt->{rotate_bytes} if $opt->{rotate_bytes};
43
44         if ($opt->{pre}) {
45                 $opt->{pre}->($im, 1, 2);
46                 $orig->header_set('References', '<1@example.com>');
47         }
48         ok($im->add($orig), 'add message to be replaced');
49         if ($opt->{post}) {
50                 $opt->{post}->($im, 3, { 4 => 'replace@example.com' });
51         }
52         $im->done;
53         my $thread_a = $ibx->over->get_thread('replace@example.com');
54
55         my %before = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
56         my $reject = PublicInbox::MIME->new($orig->as_string);
57         foreach my $mid (['<replace@example.com>', '<extra@example.com>'],
58                                 [], ['<replaced@example.com>']) {
59                 $reject->header_set('Message-ID', @$mid);
60                 my $ok = eval { $im->replace($orig, $reject) };
61                 like($@, qr/Message-ID.*may not be changed/,
62                         '->replace died on Message-ID change');
63                 ok(!$ok, 'no replacement happened');
64         }
65
66         # prepare the replacement
67         my $expect = "Move along, nothing to see here\n";
68         my $repl = PublicInbox::MIME->new($orig->as_string);
69         $repl->header_set('From', '<redactor@example.com>');
70         $repl->header_set('Subject', 'redacted');
71         $repl->header_set('Date', 'Sat, 02 Oct 2010 00:00:00 +0000');
72         $repl->body_str_set($expect);
73
74         my @warn;
75         local $SIG{__WARN__} = sub { push @warn, @_ };
76         ok(my $cmts = $im->replace($orig, $repl), 'replaced message');
77         my $changed_epochs = 0;
78         for my $tip (@$cmts) {
79                 next if !defined $tip;
80                 $changed_epochs++;
81                 like($tip, qr/\A[a-f0-9]{40}\z/,
82                         'replace returned current commit');
83         }
84         is($changed_epochs, 1, 'only one epoch changed');
85
86         $im->done;
87         my $m = PublicInbox::MIME->new($ibx->msg_by_mid('replace@example.com'));
88         is($m->body, $expect, 'replaced message');
89         is_deeply(\@warn, [], 'no warnings on noop');
90
91         my @cat = qw(cat-file --buffer --batch --batch-all-objects);
92         my $git = $ibx->git;
93         my @all = $git->qx(@cat);
94         is_deeply([grep(/confidential/, @all)], [], 'nothing confidential');
95         is_deeply([grep(/Streisand/, @all)], [], 'Streisand who?');
96         is_deeply([grep(/\bOct 1993\b/, @all)], [], 'nothing from Oct 1993');
97         my $t19931002 = qr/ 749520000 /;
98         is_deeply([grep(/$t19931002/, @all)], [], "nothing matches $t19931002");
99
100         for my $dir (glob("$ibx->{inboxdir}/git/*.git")) {
101                 my ($bn) = ($dir =~ m!([^/]+)\z!);
102                 is(system(qw(git --git-dir), $dir,
103                                         qw(fsck --strict --no-progress)),
104                         0, "git fsck is clean in epoch $bn");
105         }
106
107         my $thread_b = $ibx->over->get_thread('replace@example.com');
108         is_deeply([sort map { $_->{mid} } @$thread_b],
109                 [sort map { $_->{mid} } @$thread_a], 'threading preserved');
110
111         if (my $srch = $ibx->search) {
112                 for my $q ('f:streisand', 's:confidential', 'malibu') {
113                         my $msgs = $srch->query($q);
114                         is_deeply($msgs, [], "no match for $q");
115                 }
116                 my @ok = ('f:redactor', 's:redacted', 'nothing to see');
117                 if ($opt->{pre}) {
118                         push @ok, 'm:1@example.com', 'm:2@example.com',
119                                 's:message2', 's:message1';
120                 }
121                 if ($opt->{post}) {
122                         push @ok, 'm:3@example.com', 'm:4@example.com',
123                                 's:message3', 's:message4';
124                 }
125                 for my $q (@ok) {
126                         my $msgs = $srch->query($q);
127                         ok($msgs->[0], "got match for $q");
128                 }
129         }
130
131         # check overview matches:
132         my %after = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
133         my @before_blobs = keys %before;
134         foreach my $blob (@before_blobs) {
135                 delete $before{$blob} if delete $after{$blob};
136         }
137
138         is(scalar keys %before, 1, 'one unique blob from before left');
139         is(scalar keys %after, 1, 'one unique blob from after left');
140         foreach my $blob (keys %before) {
141                 is($git->check($blob), undef, 'old blob not found');
142                 my $smsg = $before{$blob};
143                 is($smsg->{subject}, 'confidential', 'before subject');
144                 is($smsg->{mid}, 'replace@example.com', 'before MID');
145         }
146         foreach my $blob (keys %after) {
147                 ok($git->check($blob), 'new blob found');
148                 my $smsg = $after{$blob};
149                 is($smsg->{subject}, 'redacted', 'after subject');
150                 is($smsg->{mid}, 'replace@example.com', 'before MID');
151         }
152         @warn = ();
153         is($im->replace($orig, $repl), undef, 'no-op replace returns undef');
154         is($im->purge($orig), undef, 'no-op purge returns undef');
155         is_deeply(\@warn, [], 'no warnings on noop');
156 }
157
158 sub pad_msgs {
159         my ($im, @range) = @_;
160         for my $i (@range) {
161                 my $irt;
162                 if (ref($i) eq 'HASH') {
163                         ($i, $irt) = each %$i;
164                 }
165                 my $sec = sprintf('%0d', $i);
166                 my $mime = PublicInbox::MIME->new(<<EOF);
167 From: foo\@example.com
168 To: test\@example.com
169 Message-ID: <$i\@example.com>
170 Date: Fri, 02, Jan 1970 00:00:$sec +0000
171 Subject: message$i
172
173 message number$i
174 EOF
175
176                 if (defined($irt)) {
177                         $mime->header_set('References', "<$irt>");
178                 }
179
180                 $im->add($mime);
181         }
182 }
183
184 my $opt = { pre => *pad_msgs };
185 test_replace(2, 'basic', {});
186 test_replace(2, 'basic', $opt);
187 test_replace(2, 'basic', $opt = { %$opt, post => *pad_msgs });
188 test_replace(2, 'basic', $opt = { %$opt, rotate_bytes => 1 });
189
190 SKIP: {
191         require PublicInbox::Search;
192         PublicInbox::Search::load_xapian() or skip 'Search::Xapian missing', 8;
193         for my $l (qw(medium)) {
194                 test_replace(2, $l, {});
195                 $opt = { pre => *pad_msgs };
196                 test_replace(2, $l, $opt);
197                 test_replace(2, $l, $opt = { %$opt, post => *pad_msgs });
198                 test_replace(2, $l, $opt = { %$opt, rotate_bytes => 1 });
199         }
200 };
201
202 done_testing();