2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.10.1; # FIXME: check 5.12 unicode_strings compat
6 use PublicInbox::ContentHash qw(content_digest content_hash);
7 use PublicInbox::TestCommon;
8 use PublicInbox::Spawn qw(popen_rd);
11 use autodie qw(chmod close kill open read);
13 require_mods(qw(DBD::SQLite Xapian));
14 local $ENV{HOME} = abs_path('t');
15 use_ok 'PublicInbox::V2Writable';
17 my ($inboxdir, $for_destroy) = tmpdir();
19 inboxdir => $inboxdir,
20 name => 'test-v2writable',
23 -primary_address => 'test@example.com',
25 $ibx = PublicInbox::Inbox->new($ibx);
26 my $mime = PublicInbox::Eml->new(<<'EOF');
29 Subject: this is a subject
31 Date: Fri, 02 Oct 1993 00:00:00 +0000
35 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
36 is($im->{shards}, 1, 'one shard when forced');
37 ok($im->add($mime), 'ordinary message added');
38 foreach my $f ("$inboxdir/msgmap.sqlite3",
39 glob("$inboxdir/xap*/*"),
40 glob("$inboxdir/xap*/*/*")) {
42 my ($bn) = (split(m!/!, $f))[-1];
43 is($st[2] & 07777, -f _ ? 0660 : 0770,
44 "default sharedRepository respected for $bn");
49 if ('ensure git configs are correct') {
50 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
51 qw(core.sharedRepository 0644));
52 is(xsys(@cmd), 0, "set sharedRepository in all.git");
53 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
54 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
55 is($v, '0644', 'child repo inherited core.sharedRepository');
56 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
57 is($v, 'true', 'child repo inherited repack.writeBitmaps');
62 local $SIG{__WARN__} = sub { push @warn, @_ };
63 is($im->add($mime), undef, 'obvious duplicate rejected');
64 is(scalar(@warn), 0, 'no warning about resent message');
67 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
68 is($im->add($mime), undef, 'secondary MID ignored if first matches');
69 my $sec = PublicInbox::Eml->new($mime->as_string);
70 $sec->header_set('Date');
71 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
72 ok($im->add($sec), 'secondary MID used if data is different');
73 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
74 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
75 is_deeply([ '<a-mid@b>', '<c@d>' ],
76 [ $sec->header_obj->header_raw('Message-Id') ],
77 'no new Message-Id added');
79 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
81 $mime->header_set('Message-Id', '<a-mid@b>');
82 $mime->body_set('different');
83 ok($im->add($mime), 'reused mid ok');
84 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
85 my @mids = $mime->header_obj->header_raw('Message-Id');
86 is($mids[0], '<a-mid@b>', 'original mid not changed');
87 like($mids[1], $sane_mid, 'new MID added');
88 is(scalar(@mids), 2, 'only one new MID added');
91 $mime->header_set('Message-Id', '<a-mid@b>');
92 $mime->body_set('this one needs a random mid');
93 my $hdr = $mime->header_obj;
94 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
95 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
96 my $fake = PublicInbox::Eml->new($mime->as_string);
97 $fake->header_set('Message-Id', "<$gen>");
98 ok($im->add($fake), 'fake added easily');
99 is_deeply(\@warn, [], 'no warnings from a faker');
100 ok($im->add($mime), 'random MID made');
101 like(join(' ', @warn), qr/using random/, 'warned about using random');
102 @mids = $mime->header_obj->header_raw('Message-Id');
103 is($mids[0], '<a-mid@b>', 'original mid not changed');
104 like($mids[1], $sane_mid, 'new MID added');
105 is(scalar(@mids), 2, 'only one new MID added');
108 $mime->header_set('Message-Id');
109 ok($im->add($mime), 'random MID made for MID free message');
110 @mids = $mime->header_obj->header_raw('Message-Id');
111 like($mids[0], $sane_mid, 'mid was generated');
112 is(scalar(@mids), 1, 'new generated');
115 $mime->header_set('Message-Id', '<space@ (NXDOMAIN) >');
116 ok($im->add($mime), 'message added with space in Message-Id');
117 is_deeply([], \@warn);
121 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
122 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
123 $mime->header_set('References', '<zz-mid@b>');
124 ok($im->add($mime), 'message with multiple Message-ID');
126 my $total = $ibx->over->dbh->selectrow_array(<<'');
127 SELECT COUNT(*) FROM over WHERE num > 0
129 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
130 my $mset1 = $ibx->search->reopen->mset('m:abcde@1');
131 is($mset1->size, 1, 'message found by first MID');
132 my $mset2 = $ibx->search->mset('m:abcde@2');
133 is($mset2->size, 1, 'message found by second MID');
134 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
135 'same document') if ($mset1->size);
137 my $alt = $ibx->search->mset('m:alt-id-for-nntp');
138 is($alt->size, 1, 'message found by alt MID (NNTP)');
139 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
140 'same document') if ($mset1->size);
141 $mime->header_set('X-Alt-Message-Id');
144 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
145 my $msgs = $ibx->over->get_thread($mid);
146 my $key = join(' ', sort(map { $_->{num} } @$msgs));
149 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
150 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
154 require_mods('Net::NNTP', 1);
155 my $err = "$inboxdir/stderr.log";
156 my $out = "$inboxdir/stdout.log";
157 my $group = 'inbox.comp.test.v2writable';
158 my $pi_config = "$inboxdir/pi_config";
159 open my $fh, '>', $pi_config;
161 [publicinbox "test-v2writable"]
164 address = test\@example.com
168 my $sock = tcp_server();
170 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
171 my $env = { PI_CONFIG => $pi_config };
172 my $td = start_script($cmd, $env, { 3 => $sock });
173 my $host_port = tcp_host_port($sock);
174 my $n = Net::NNTP->new($host_port);
176 my $x = $n->xover('1-');
178 foreach my $num (sort { $a <=> $b } keys %$x) {
179 my $mid = $x->{$num}->[3];
180 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
181 is_deeply($n->xhdr('Message-ID', $num),
182 { $num => $mid }, "XHDR lookup OK on num $num");
184 # FIXME PublicInbox::NNTP (server) doesn't handle spaces in
185 # Message-ID, but neither does Net::NNTP (client)
188 is_deeply($n->xhdr('Message-ID', $mid),
189 { $mid => $mid }, "XHDR lookup OK on MID $mid ($num)");
192 foreach my $mid (@{$n->newnews(0, $group)}) {
193 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
195 is_deeply([sort keys %nn], [sort keys %uniq]);
198 foreach my $num (@{$n->listgroup($group)}) {
199 is($lg{$num}++, 0, "num is unique in LISTGROUP");
201 is_deeply([sort keys %lg], [sort keys %$x],
202 'XOVER and LISTGROUPS return the same article numbers');
204 my $xref = $n->xhdr('Xref', '1-');
205 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
207 my $mids = $n->xhdr('Message-ID', '1-');
208 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
210 my $rover = $n->xrover('1-');
211 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
214 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
215 my @before = $git0->qx(@log, qw(--pretty=oneline));
216 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
217 $im = PublicInbox::V2Writable->new($ibx, {nproc => 2});
218 is($im->{shards}, 1, 'detected single shard from previous');
219 my ($mark, $rm_mime, $smsg) = $im->remove($mime, 'test removal');
220 is(content_hash($rm_mime), content_hash($mime),
221 'removed object returned matches');
222 ok(defined($mark), 'mark set');
224 my @after = $git0->qx(@log, qw(--pretty=oneline));
225 my $tip = shift @after;
226 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
227 'commit message propagated to git');
228 is_deeply(\@after, \@before, 'only one commit written to git');
229 my $mid = $smsg->{mid};
230 is($ibx->mm->num_for($mid), undef, 'no longer in Msgmap by mid');
231 my $num = $smsg->{num};
232 like($num, qr/\A\d+\z/, 'numeric number in return message');
233 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
234 my $mset = $ibx->search->reopen->mset('m:'.$mid);
235 is($mset->size, 0, 'no longer found in Xapian');
236 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
237 is($ibx->over->get_art($num), undef,
238 'removal propagated to Over DB');
240 my $after = $git0->qx(@log1);
241 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
243 ok(index($before, $oid) > 0, 'no new blob introduced');
245 fail('failed to extract blob from log output');
247 is($im->remove($mime, 'test removal'), undef,
248 'remove is idempotent');
251 $after, 'no git history made with idempotent remove');
253 ok(!$@, '->done is idempotent');
257 ok($im->add($mime), 'add message to be purged');
258 local $SIG{__WARN__} = sub {};
259 ok(my $cmt = $im->purge($mime), 'purged message');
260 like($cmt->[0], qr/\A[a-f0-9]{40,}\z/, 'purge returned current commit');
264 is($im->purge($mime), undef, 'no-op returns undef');
270 local $SIG{__WARN__} = sub {};
271 $mime->header_set('Subject', 'long mid');
272 $mime->header_set('Message-ID', "<$x>");
273 ok($im->add($mime), 'add excessively long Message-ID');
275 $mime->header_set('Message-ID', "<$y>");
276 $mime->header_set('References', "<$x>");
277 ok($im->add($mime), 'add excessively long References');
280 my $msgs = $ibx->over->get_thread('x'x244);
281 is(2, scalar(@$msgs), 'got both messages');
282 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
283 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
284 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
287 if ('UTF-8 References') {
289 local $SIG{__WARN__} = sub { push @w, @_ };
293 Message-ID: <horrible\@example>
294 References: <\xc4\x80\@example>
297 ok($im->add(PublicInbox::Eml->new($msg."a\n")), 'UTF-8 References 1');
298 ok($im->add(PublicInbox::Eml->new($msg."b\n")), 'UTF-8 References 2');
300 ok(!grep(/Wide character/, @w), 'no wide characters') or xbail(\@w);
304 inboxdir => "$inboxdir/non-existent/subdir",
307 -primary_address => 'test@example.com',
310 my $nope = PublicInbox::V2Writable->new($tmp);
313 ok($@, 'V2Writable fails on non-existent dir');
316 my $v2w = PublicInbox::V2Writable->new($tmp, { nproc => 1 });
317 ok($v2w, 'creat flag works');
318 $v2w->{parallel} = 0;
320 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
321 open my $fh, '>>', $alt;
322 print $fh "$inboxdir/all.git/objects\n";
326 my $before = do { local $/; <$fh> };
328 ok($v2w->{mg}->add_epoch(3), 'init a new epoch');
330 my $after = do { local $/; <$fh> };
331 ok(index($after, $before) > 0,
332 'old contents preserved after adding epoch');
333 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
334 'first line is newest epoch');
335 my $mode = (stat($alt))[2] & 07777;
336 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');
340 my $strace = strace_inject(1);
341 my $eml = eml_load 't/plack-qp.eml';
342 my $gfi_err = "$inboxdir/gfi.err";
343 open my $fh, '>', my $trace = "$inboxdir/trace.out";
344 my $rd = popen_rd([ $strace, '-p', $$, '-o', $trace,
345 '-e', 'inject=pwrite64:error=ENOSPC'], undef, { 2 => 1 });
346 $rd->poll_in(10_000) or die 'strace not ready';
348 open my $olderr, '>&', \*STDERR;
349 open STDERR, '>>', $gfi_err;
351 open STDERR, '>&', $olderr;
352 }, 'v2w->add fails on ENOSPC';
353 like $@, qr/ disk is full/, '$@ reports ENOSPC';
355 kill 'TERM', $rd->attached_pid;
358 $im->add($eml) or xbail 'cannot add message to start fast-import';
359 my $pid = $im->{im}->{io}->attached_pid or xbail 'no import pid';
360 open $fh, '>', $trace;
362 $rd = popen_rd([$strace, '-p', $pid, '-o', $trace,
363 '-e', 'inject=write:error=ENOSPC:when=1'],
365 $rd->poll_in(10_000) or die 'strace not ready';
366 ok !eval { $im->done }, 'done fails with ENOSPC';
367 ok $@, '$@ set on ENOSPC';
368 kill 'TERM', $rd->attached_pid;
370 open $fh, '<', $gfi_err;
371 read $fh, my $errbuf, -s $fh;
372 like $errbuf, qr/fatal:/, 'fatal git error noted';
373 open $fh, '>', $gfi_err;
377 $im->add($eml) or xbail '->add fails after fixing ENOSPC';
379 ok !$im->add($eml), '->add detects existing message';
381 is -s $fh, 0, 'nothing new in fast-import stderr';