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 PublicInbox::TestCommon;
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 local $ENV{HOME} = abs_path('t');
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",
27 -primary_address => 'test@example.com',
31 my $orig = PublicInbox::MIME->new(<<'EOF');
32 From: Barbra Streisand <effect@example.com>
35 Message-ID: <replace@example.com>
36 Date: Fri, 02 Oct 1993 00:00:00 +0000
38 Top secret info about my house in Malibu...
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};
45 $opt->{pre}->($im, 1, 2);
46 $orig->header_set('References', '<1@example.com>');
48 ok($im->add($orig), 'add message to be replaced');
50 $opt->{post}->($im, 3, { 4 => 'replace@example.com' });
53 my $thread_a = $ibx->over->get_thread('replace@example.com');
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');
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);
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;
81 like($tip, qr/\A[a-f0-9]{40}\z/,
82 'replace returned current commit');
84 is($changed_epochs, 1, 'only one epoch changed');
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');
91 my @cat = qw(cat-file --buffer --batch --batch-all-objects);
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");
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");
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');
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");
116 my @ok = ('f:redactor', 's:redacted', 'nothing to see');
118 push @ok, 'm:1@example.com', 'm:2@example.com',
119 's:message2', 's:message1';
122 push @ok, 'm:3@example.com', 'm:4@example.com',
123 's:message3', 's:message4';
126 my $msgs = $srch->query($q);
127 ok($msgs->[0], "got match for $q");
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};
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');
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');
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');
159 my ($im, @range) = @_;
162 if (ref($i) eq 'HASH') {
163 ($i, $irt) = each %$i;
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
177 $mime->header_set('References', "<$irt>");
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 });
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 });