1 # Copyright (C) 2018-2021 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',
21 -primary_address => 'test@example.com',
23 $ibx = PublicInbox::Inbox->new($ibx);
24 my $mime = PublicInbox::Eml->new(<<'EOF');
27 Subject: this is a subject
29 Date: Fri, 02 Oct 1993 00:00:00 +0000
33 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
34 is($im->{shards}, 1, 'one shard when forced');
35 ok($im->add($mime), 'ordinary message added');
36 foreach my $f ("$inboxdir/msgmap.sqlite3",
37 glob("$inboxdir/xap*/*"),
38 glob("$inboxdir/xap*/*/*")) {
40 my ($bn) = (split(m!/!, $f))[-1];
41 is($st[2] & 07777, -f _ ? 0660 : 0770,
42 "default sharedRepository respected for $bn");
47 if ('ensure git configs are correct') {
48 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
49 qw(core.sharedRepository 0644));
50 is(xsys(@cmd), 0, "set sharedRepository in all.git");
51 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
52 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
53 is($v, '0644', 'child repo inherited core.sharedRepository');
54 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
55 is($v, 'true', 'child repo inherited repack.writeBitmaps');
60 local $SIG{__WARN__} = sub { push @warn, @_ };
61 is($im->add($mime), undef, 'obvious duplicate rejected');
62 is(scalar(@warn), 0, 'no warning about resent message');
65 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
66 is($im->add($mime), undef, 'secondary MID ignored if first matches');
67 my $sec = PublicInbox::Eml->new($mime->as_string);
68 $sec->header_set('Date');
69 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
70 ok($im->add($sec), 'secondary MID used if data is different');
71 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
72 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
73 is_deeply([ '<a-mid@b>', '<c@d>' ],
74 [ $sec->header_obj->header_raw('Message-Id') ],
75 'no new Message-Id added');
77 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
79 $mime->header_set('Message-Id', '<a-mid@b>');
80 $mime->body_set('different');
81 ok($im->add($mime), 'reused mid ok');
82 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
83 my @mids = $mime->header_obj->header_raw('Message-Id');
84 is($mids[0], '<a-mid@b>', 'original mid not changed');
85 like($mids[1], $sane_mid, 'new MID added');
86 is(scalar(@mids), 2, 'only one new MID added');
89 $mime->header_set('Message-Id', '<a-mid@b>');
90 $mime->body_set('this one needs a random mid');
91 my $hdr = $mime->header_obj;
92 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
93 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
94 my $fake = PublicInbox::Eml->new($mime->as_string);
95 $fake->header_set('Message-Id', "<$gen>");
96 ok($im->add($fake), 'fake added easily');
97 is_deeply(\@warn, [], 'no warnings from a faker');
98 ok($im->add($mime), 'random MID made');
99 like(join(' ', @warn), qr/using random/, 'warned about using random');
100 @mids = $mime->header_obj->header_raw('Message-Id');
101 is($mids[0], '<a-mid@b>', 'original mid not changed');
102 like($mids[1], $sane_mid, 'new MID added');
103 is(scalar(@mids), 2, 'only one new MID added');
106 $mime->header_set('Message-Id');
107 ok($im->add($mime), 'random MID made for MID free message');
108 @mids = $mime->header_obj->header_raw('Message-Id');
109 like($mids[0], $sane_mid, 'mid was generated');
110 is(scalar(@mids), 1, 'new generated');
113 $mime->header_set('Message-Id', '<space@ (NXDOMAIN) >');
114 ok($im->add($mime), 'message added with space in Message-Id');
115 is_deeply([], \@warn);
119 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
120 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
121 $mime->header_set('References', '<zz-mid@b>');
122 ok($im->add($mime), 'message with multiple Message-ID');
124 my $total = $ibx->over->dbh->selectrow_array(<<'');
125 SELECT COUNT(*) FROM over WHERE num > 0
127 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
128 my $mset1 = $ibx->search->reopen->mset('m:abcde@1');
129 is($mset1->size, 1, 'message found by first MID');
130 my $mset2 = $ibx->search->mset('m:abcde@2');
131 is($mset2->size, 1, 'message found by second MID');
132 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
133 'same document') if ($mset1->size);
135 my $alt = $ibx->search->mset('m:alt-id-for-nntp');
136 is($alt->size, 1, 'message found by alt MID (NNTP)');
137 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
138 'same document') if ($mset1->size);
139 $mime->header_set('X-Alt-Message-Id');
142 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
143 my $msgs = $ibx->over->get_thread($mid);
144 my $key = join(' ', sort(map { $_->{num} } @$msgs));
147 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
148 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
153 my $err = "$inboxdir/stderr.log";
154 my $out = "$inboxdir/stdout.log";
155 my $group = 'inbox.comp.test.v2writable';
156 my $pi_config = "$inboxdir/pi_config";
157 open my $fh, '>', $pi_config or die "open: $!\n";
159 [publicinbox "test-v2writable"]
162 address = test\@example.com
166 close $fh or die "close: $!\n";
167 my $sock = tcp_server();
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 = tcp_host_port($sock);
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 $cmt = $im->purge($mime), 'purged message');
259 like($cmt->[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)');
287 inboxdir => "$inboxdir/non-existent/subdir",
290 -primary_address => 'test@example.com',
293 my $nope = PublicInbox::V2Writable->new($tmp);
296 ok($@, 'V2Writable fails on non-existent dir');
299 my $v2w = PublicInbox::V2Writable->new($tmp, 1);
300 ok($v2w, 'creat flag works');
301 $v2w->{parallel} = 0;
303 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
304 open my $fh, '>>', $alt or die $!;
305 print $fh "$inboxdir/all.git/objects\n" or die $!;
306 chmod(0664, $fh) or die "fchmod: $!";
308 open $fh, '<', $alt or die $!;
309 my $before = do { local $/; <$fh> };
311 ok($v2w->git_init(3), 'init a new epoch');
312 open $fh, '<', $alt or die $!;
313 my $after = do { local $/; <$fh> };
314 ok(index($after, $before) > 0,
315 'old contents preserved after adding epoch');
316 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
317 'first line is newest epoch');
318 my $mode = (stat($alt))[2] & 07777;
319 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');