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