1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::InboxWritable;
8 use File::Temp qw/tempdir/;
9 require './t/common.perl';
10 require_git(2.6); # replace is v2 only, for now...
11 foreach my $mod (qw(DBD::SQLite)) {
13 plan skip_all => "$mod missing for $0" if $@;
16 sub test_replace ($$$) {
17 my ($v, $level, $opt) = @_;
18 diag "v$v $level replace";
19 my $this = "pi-$v-$level-replace";
20 my $tmpdir = tempdir("$this-tmp-XXXXXX", TMPDIR => 1, CLEANUP => 1);
21 my $ibx = PublicInbox::Inbox->new({
22 inboxdir => "$tmpdir/testbox",
25 -primary_address => 'test@example.com',
29 my $orig = PublicInbox::MIME->new(<<'EOF');
30 From: Barbra Streisand <effect@example.com>
33 Message-ID: <replace@example.com>
34 Date: Fri, 02 Oct 1993 00:00:00 +0000
36 Top secret info about my house in Malibu...
38 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
39 # fake a bunch of epochs
40 $im->{rotate_bytes} = $opt->{rotate_bytes} if $opt->{rotate_bytes};
43 $opt->{pre}->($im, 1, 2);
44 $orig->header_set('References', '<1@example.com>');
46 ok($im->add($orig), 'add message to be replaced');
48 $opt->{post}->($im, 3, { 4 => 'replace@example.com' });
51 my $thread_a = $ibx->over->get_thread('replace@example.com');
53 my %before = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
54 my $reject = PublicInbox::MIME->new($orig->as_string);
55 foreach my $mid (['<replace@example.com>', '<extra@example.com>'],
56 [], ['<replaced@example.com>']) {
57 $reject->header_set('Message-ID', @$mid);
58 my $ok = eval { $im->replace($orig, $reject) };
59 like($@, qr/Message-ID.*may not be changed/,
60 '->replace died on Message-ID change');
61 ok(!$ok, 'no replacement happened');
64 # prepare the replacement
65 my $expect = "Move along, nothing to see here\n";
66 my $repl = PublicInbox::MIME->new($orig->as_string);
67 $repl->header_set('From', '<redactor@example.com>');
68 $repl->header_set('Subject', 'redacted');
69 $repl->header_set('Date', 'Sat, 02 Oct 2010 00:00:00 +0000');
70 $repl->body_str_set($expect);
73 local $SIG{__WARN__} = sub { push @warn, @_ };
74 ok(my $cmts = $im->replace($orig, $repl), 'replaced message');
75 my $changed_epochs = 0;
76 for my $tip (@$cmts) {
77 next if !defined $tip;
79 like($tip, qr/\A[a-f0-9]{40}\z/,
80 'replace returned current commit');
82 is($changed_epochs, 1, 'only one epoch changed');
85 my $m = PublicInbox::MIME->new($ibx->msg_by_mid('replace@example.com'));
86 is($m->body, $expect, 'replaced message');
87 is_deeply(\@warn, [], 'no warnings on noop');
89 my @cat = qw(cat-file --buffer --batch --batch-all-objects);
91 my @all = $git->qx(@cat);
92 is_deeply([grep(/confidential/, @all)], [], 'nothing confidential');
93 is_deeply([grep(/Streisand/, @all)], [], 'Streisand who?');
94 is_deeply([grep(/\bOct 1993\b/, @all)], [], 'nothing from Oct 1993');
95 my $t19931002 = qr/ 749520000 /;
96 is_deeply([grep(/$t19931002/, @all)], [], "nothing matches $t19931002");
98 for my $dir (glob("$ibx->{inboxdir}/git/*.git")) {
99 my ($bn) = ($dir =~ m!([^/]+)\z!);
100 is(system(qw(git --git-dir), $dir, qw(fsck --strict)), 0,
101 "git fsck is clean in epoch $bn");
104 my $thread_b = $ibx->over->get_thread('replace@example.com');
105 is_deeply([sort map { $_->{mid} } @$thread_b],
106 [sort map { $_->{mid} } @$thread_a], 'threading preserved');
108 if (my $srch = $ibx->search) {
109 for my $q ('f:streisand', 's:confidential', 'malibu') {
110 my $msgs = $srch->query($q);
111 is_deeply($msgs, [], "no match for $q");
113 my @ok = ('f:redactor', 's:redacted', 'nothing to see');
115 push @ok, 'm:1@example.com', 'm:2@example.com',
116 's:message2', 's:message1';
119 push @ok, 'm:3@example.com', 'm:4@example.com',
120 's:message3', 's:message4';
123 my $msgs = $srch->query($q);
124 ok($msgs->[0], "got match for $q");
128 # check overview matches:
129 my %after = map {; delete($_->{blob}) => $_ } @{$ibx->recent};
130 my @before_blobs = keys %before;
131 foreach my $blob (@before_blobs) {
132 delete $before{$blob} if delete $after{$blob};
135 is(scalar keys %before, 1, 'one unique blob from before left');
136 is(scalar keys %after, 1, 'one unique blob from after left');
137 foreach my $blob (keys %before) {
138 is($git->check($blob), undef, 'old blob not found');
139 my $smsg = $before{$blob};
140 is($smsg->{subject}, 'confidential', 'before subject');
141 is($smsg->{mid}, 'replace@example.com', 'before MID');
143 foreach my $blob (keys %after) {
144 ok($git->check($blob), 'new blob found');
145 my $smsg = $after{$blob};
146 is($smsg->{subject}, 'redacted', 'after subject');
147 is($smsg->{mid}, 'replace@example.com', 'before MID');
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');
156 my ($im, @range) = @_;
159 if (ref($i) eq 'HASH') {
160 ($i, $irt) = each %$i;
162 my $sec = sprintf('%0d', $i);
163 my $mime = PublicInbox::MIME->new(<<EOF);
164 From: foo\@example.com
165 To: test\@example.com
166 Message-ID: <$i\@example.com>
167 Date: Fri, 02, Jan 1970 00:00:$sec +0000
174 $mime->header_set('References', "<$irt>");
181 my $opt = { pre => *pad_msgs };
182 test_replace(2, 'basic', {});
183 test_replace(2, 'basic', $opt);
184 test_replace(2, 'basic', $opt = { %$opt, post => *pad_msgs });
185 test_replace(2, 'basic', $opt = { %$opt, rotate_bytes => 1 });
188 require PublicInbox::Search;
189 PublicInbox::Search::load_xapian() or skip 'Search::Xapian missing', 8;
190 for my $l (qw(medium)) {
191 test_replace(2, $l, {});
192 $opt = { pre => *pad_msgs };
193 test_replace(2, $l, $opt);
194 test_replace(2, $l, $opt = { %$opt, post => *pad_msgs });
195 test_replace(2, $l, $opt = { %$opt, rotate_bytes => 1 });