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 $srch = $ibx->search;
128 my $mset1 = $srch->reopen->query('m:abcde@1', { mset => 1 });
129 is($mset1->size, 1, 'message found by first MID');
130 my $mset2 = $srch->reopen->query('m:abcde@2', { mset => 1 });
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 = $srch->reopen->query('m:alt-id-for-nntp', { mset => 1 });
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();
168 ok($sock, 'sock created');
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 = $sock->sockhost . ':' . $sock->sockport;
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 $srch = $ibx->search->reopen;
235 my $mset = $srch->query('m:'.$mid, { mset => 1});
236 is($mset->size, 0, 'no longer found in Xapian');
237 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
238 is($ibx->over->get_art($num), undef,
239 'removal propagated to Over DB');
241 my $after = $git0->qx(@log1);
242 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
244 ok(index($before, $oid) > 0, 'no new blob introduced');
246 fail('failed to extract blob from log output');
248 is($im->remove($mime, 'test removal'), undef,
249 'remove is idempotent');
252 $after, 'no git history made with idempotent remove');
254 ok(!$@, '->done is idempotent');
258 ok($im->add($mime), 'add message to be purged');
259 local $SIG{__WARN__} = sub {};
260 ok(my $cmts = $im->purge($mime), 'purged message');
261 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
265 is($im->purge($mime), undef, 'no-op returns undef');
271 local $SIG{__WARN__} = sub {};
272 $mime->header_set('Subject', 'long mid');
273 $mime->header_set('Message-ID', "<$x>");
274 ok($im->add($mime), 'add excessively long Message-ID');
276 $mime->header_set('Message-ID', "<$y>");
277 $mime->header_set('References', "<$x>");
278 ok($im->add($mime), 'add excessively long References');
281 my $msgs = $ibx->over->get_thread('x'x244);
282 is(2, scalar(@$msgs), 'got both messages');
283 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
284 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
285 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
290 inboxdir => "$inboxdir/non-existent/subdir",
293 -primary_address => 'test@example.com',
296 my $nope = PublicInbox::V2Writable->new($tmp);
299 ok($@, 'V2Writable fails on non-existent dir');
302 my $v2w = PublicInbox::V2Writable->new($tmp, 1);
303 ok($v2w, 'creat flag works');
304 $v2w->{parallel} = 0;
306 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
307 open my $fh, '>>', $alt or die $!;
308 print $fh "$inboxdir/all.git/objects\n" or die $!;
309 chmod(0664, $fh) or die "fchmod: $!";
311 open $fh, '<', $alt or die $!;
312 my $before = do { local $/; <$fh> };
314 ok($v2w->git_init(3), 'init a new epoch');
315 open $fh, '<', $alt or die $!;
316 my $after = do { local $/; <$fh> };
317 ok(index($after, $before) > 0,
318 'old contents preserved after adding epoch');
319 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
320 'first line is newest epoch');
321 my $mode = (stat($alt))[2] & 07777;
322 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');