1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::ContentHash qw(content_digest content_hash);
8 use PublicInbox::TestCommon;
11 require_mods(qw(DBD::SQLite Search::Xapian));
12 local $ENV{HOME} = abs_path('t');
13 use_ok 'PublicInbox::V2Writable';
15 my ($inboxdir, $for_destroy) = tmpdir();
17 inboxdir => $inboxdir,
18 name => 'test-v2writable',
20 -primary_address => 'test@example.com',
22 $ibx = PublicInbox::Inbox->new($ibx);
23 my $mime = PublicInbox::Eml->new(<<'EOF');
26 Subject: this is a subject
28 Date: Fri, 02 Oct 1993 00:00:00 +0000
32 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
33 is($im->{shards}, 1, 'one shard when forced');
34 ok($im->add($mime), 'ordinary message added');
35 foreach my $f ("$inboxdir/msgmap.sqlite3",
36 glob("$inboxdir/xap*/*"),
37 glob("$inboxdir/xap*/*/*")) {
39 my ($bn) = (split(m!/!, $f))[-1];
40 is($st[2] & 07777, -f _ ? 0660 : 0770,
41 "default sharedRepository respected for $bn");
46 if ('ensure git configs are correct') {
47 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
48 qw(core.sharedRepository 0644));
49 is(xsys(@cmd), 0, "set sharedRepository in all.git");
50 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
51 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
52 is($v, '0644', 'child repo inherited core.sharedRepository');
53 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
54 is($v, 'true', 'child repo inherited repack.writeBitmaps');
59 local $SIG{__WARN__} = sub { push @warn, @_ };
60 is($im->add($mime), undef, 'obvious duplicate rejected');
61 is(scalar(@warn), 0, 'no warning about resent message');
64 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
65 is($im->add($mime), undef, 'secondary MID ignored if first matches');
66 my $sec = PublicInbox::Eml->new($mime->as_string);
67 $sec->header_set('Date');
68 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
69 ok($im->add($sec), 'secondary MID used if data is different');
70 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
71 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
72 is_deeply([ '<a-mid@b>', '<c@d>' ],
73 [ $sec->header_obj->header_raw('Message-Id') ],
74 'no new Message-Id added');
76 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
78 $mime->header_set('Message-Id', '<a-mid@b>');
79 $mime->body_set('different');
80 ok($im->add($mime), 'reused mid ok');
81 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
82 my @mids = $mime->header_obj->header_raw('Message-Id');
83 is($mids[0], '<a-mid@b>', 'original mid not changed');
84 like($mids[1], $sane_mid, 'new MID added');
85 is(scalar(@mids), 2, 'only one new MID added');
88 $mime->header_set('Message-Id', '<a-mid@b>');
89 $mime->body_set('this one needs a random mid');
90 my $hdr = $mime->header_obj;
91 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
92 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
93 my $fake = PublicInbox::Eml->new($mime->as_string);
94 $fake->header_set('Message-Id', "<$gen>");
95 ok($im->add($fake), 'fake added easily');
96 is_deeply(\@warn, [], 'no warnings from a faker');
97 ok($im->add($mime), 'random MID made');
98 like(join(' ', @warn), qr/using random/, 'warned about using random');
99 @mids = $mime->header_obj->header_raw('Message-Id');
100 is($mids[0], '<a-mid@b>', 'original mid not changed');
101 like($mids[1], $sane_mid, 'new MID added');
102 is(scalar(@mids), 2, 'only one new MID added');
105 $mime->header_set('Message-Id');
106 ok($im->add($mime), 'random MID made for MID free message');
107 @mids = $mime->header_obj->header_raw('Message-Id');
108 like($mids[0], $sane_mid, 'mid was generated');
109 is(scalar(@mids), 1, 'new generated');
112 $mime->header_set('Message-Id', '<space@ (NXDOMAIN) >');
113 ok($im->add($mime), 'message added with space in Message-Id');
114 is_deeply([], \@warn);
118 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
119 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
120 $mime->header_set('References', '<zz-mid@b>');
121 ok($im->add($mime), 'message with multiple Message-ID');
123 my $total = $ibx->over->dbh->selectrow_array(<<'');
124 SELECT COUNT(*) FROM over WHERE num > 0
126 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
127 my $mset1 = $ibx->search->reopen->mset('m:abcde@1');
128 is($mset1->size, 1, 'message found by first MID');
129 my $mset2 = $ibx->search->mset('m:abcde@2');
130 is($mset2->size, 1, 'message found by second MID');
131 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
132 'same document') if ($mset1->size);
134 my $alt = $ibx->search->mset('m:alt-id-for-nntp');
135 is($alt->size, 1, 'message found by alt MID (NNTP)');
136 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
137 'same document') if ($mset1->size);
138 $mime->header_set('X-Alt-Message-Id');
141 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
142 my $msgs = $ibx->over->get_thread($mid);
143 my $key = join(' ', sort(map { $_->{num} } @$msgs));
146 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
147 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
152 my $err = "$inboxdir/stderr.log";
153 my $out = "$inboxdir/stdout.log";
154 my $group = 'inbox.comp.test.v2writable';
155 my $pi_config = "$inboxdir/pi_config";
156 open my $fh, '>', $pi_config or die "open: $!\n";
158 [publicinbox "test-v2writable"]
161 address = test\@example.com
165 close $fh or die "close: $!\n";
166 my $sock = tcp_server();
167 ok($sock, 'sock created');
169 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
170 my $env = { PI_CONFIG => $pi_config };
171 my $td = start_script($cmd, $env, { 3 => $sock });
172 my $host_port = $sock->sockhost . ':' . $sock->sockport;
173 my $n = Net::NNTP->new($host_port);
175 my $x = $n->xover('1-');
177 foreach my $num (sort { $a <=> $b } keys %$x) {
178 my $mid = $x->{$num}->[3];
179 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
180 is_deeply($n->xhdr('Message-ID', $num),
181 { $num => $mid }, "XHDR lookup OK on num $num");
183 # FIXME PublicInbox::NNTP (server) doesn't handle spaces in
184 # Message-ID, but neither does Net::NNTP (client)
187 is_deeply($n->xhdr('Message-ID', $mid),
188 { $mid => $mid }, "XHDR lookup OK on MID $mid ($num)");
191 foreach my $mid (@{$n->newnews(0, $group)}) {
192 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
194 is_deeply([sort keys %nn], [sort keys %uniq]);
197 foreach my $num (@{$n->listgroup($group)}) {
198 is($lg{$num}++, 0, "num is unique in LISTGROUP");
200 is_deeply([sort keys %lg], [sort keys %$x],
201 'XOVER and LISTGROUPS return the same article numbers');
203 my $xref = $n->xhdr('Xref', '1-');
204 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
206 my $mids = $n->xhdr('Message-ID', '1-');
207 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
209 my $rover = $n->xrover('1-');
210 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
213 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
214 my @before = $git0->qx(@log, qw(--pretty=oneline));
215 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
216 $im = PublicInbox::V2Writable->new($ibx, {nproc => 2});
217 is($im->{shards}, 1, 'detected single shard from previous');
218 my ($mark, $rm_mime, $smsg) = $im->remove($mime, 'test removal');
219 is(content_hash($rm_mime), content_hash($mime),
220 'removed object returned matches');
221 ok(defined($mark), 'mark set');
223 my @after = $git0->qx(@log, qw(--pretty=oneline));
224 my $tip = shift @after;
225 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
226 'commit message propagated to git');
227 is_deeply(\@after, \@before, 'only one commit written to git');
228 my $mid = $smsg->{mid};
229 is($ibx->mm->num_for($mid), undef, 'no longer in Msgmap by mid');
230 my $num = $smsg->{num};
231 like($num, qr/\A\d+\z/, 'numeric number in return message');
232 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
233 my $mset = $ibx->search->reopen->mset('m:'.$mid);
234 is($mset->size, 0, 'no longer found in Xapian');
235 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
236 is($ibx->over->get_art($num), undef,
237 'removal propagated to Over DB');
239 my $after = $git0->qx(@log1);
240 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
242 ok(index($before, $oid) > 0, 'no new blob introduced');
244 fail('failed to extract blob from log output');
246 is($im->remove($mime, 'test removal'), undef,
247 'remove is idempotent');
250 $after, 'no git history made with idempotent remove');
252 ok(!$@, '->done is idempotent');
256 ok($im->add($mime), 'add message to be purged');
257 local $SIG{__WARN__} = sub {};
258 ok(my $cmts = $im->purge($mime), 'purged message');
259 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
263 is($im->purge($mime), undef, 'no-op returns undef');
269 local $SIG{__WARN__} = sub {};
270 $mime->header_set('Subject', 'long mid');
271 $mime->header_set('Message-ID', "<$x>");
272 ok($im->add($mime), 'add excessively long Message-ID');
274 $mime->header_set('Message-ID', "<$y>");
275 $mime->header_set('References', "<$x>");
276 ok($im->add($mime), 'add excessively long References');
279 my $msgs = $ibx->over->get_thread('x'x244);
280 is(2, scalar(@$msgs), 'got both messages');
281 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
282 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
283 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
288 inboxdir => "$inboxdir/non-existent/subdir",
291 -primary_address => 'test@example.com',
294 my $nope = PublicInbox::V2Writable->new($tmp);
297 ok($@, 'V2Writable fails on non-existent dir');
300 my $v2w = PublicInbox::V2Writable->new($tmp, 1);
301 ok($v2w, 'creat flag works');
302 $v2w->{parallel} = 0;
304 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
305 open my $fh, '>>', $alt or die $!;
306 print $fh "$inboxdir/all.git/objects\n" or die $!;
307 chmod(0664, $fh) or die "fchmod: $!";
309 open $fh, '<', $alt or die $!;
310 my $before = do { local $/; <$fh> };
312 ok($v2w->git_init(3), 'init a new epoch');
313 open $fh, '<', $alt or die $!;
314 my $after = do { local $/; <$fh> };
315 ok(index($after, $before) > 0,
316 'old contents preserved after adding epoch');
317 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
318 'first line is newest epoch');
319 my $mode = (stat($alt))[2] & 07777;
320 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');