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::ContentId qw(content_digest content_id);
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::MIME->create(
25 From => 'a@example.com',
26 To => 'test@example.com',
27 Subject => 'this is a subject',
28 'Message-ID' => '<a-mid@b>',
29 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
31 body => "hello world\n",
34 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
35 is($im->{shards}, 1, 'one shard when forced');
36 ok($im->add($mime), 'ordinary message added');
37 foreach my $f ("$inboxdir/msgmap.sqlite3",
38 glob("$inboxdir/xap*/*"),
39 glob("$inboxdir/xap*/*/*")) {
41 my ($bn) = (split(m!/!, $f))[-1];
42 is($st[2] & 07777, -f _ ? 0660 : 0770,
43 "default sharedRepository respected for $bn");
48 if ('ensure git configs are correct') {
49 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
50 qw(core.sharedRepository 0644));
51 is(system(@cmd), 0, "set sharedRepository in all.git");
52 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
53 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
54 is($v, '0644', 'child repo inherited core.sharedRepository');
55 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
56 is($v, 'true', 'child repo inherited repack.writeBitmaps');
61 local $SIG{__WARN__} = sub { push @warn, @_ };
62 is($im->add($mime), undef, 'obvious duplicate rejected');
63 is(scalar(@warn), 0, 'no warning about resent message');
66 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
67 is($im->add($mime), undef, 'secondary MID ignored if first matches');
68 my $sec = PublicInbox::MIME->new($mime->as_string);
69 $sec->header_set('Date');
70 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
71 ok($im->add($sec), 'secondary MID used if data is different');
72 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
73 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
74 is_deeply([ '<a-mid@b>', '<c@d>' ],
75 [ $sec->header_obj->header_raw('Message-Id') ],
76 'no new Message-Id added');
78 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
80 $mime->header_set('Message-Id', '<a-mid@b>');
81 $mime->body_set('different');
82 ok($im->add($mime), 'reused mid ok');
83 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
84 my @mids = $mime->header_obj->header_raw('Message-Id');
85 is($mids[0], '<a-mid@b>', 'original mid not changed');
86 like($mids[1], $sane_mid, 'new MID added');
87 is(scalar(@mids), 2, 'only one new MID added');
90 $mime->header_set('Message-Id', '<a-mid@b>');
91 $mime->body_set('this one needs a random mid');
92 my $hdr = $mime->header_obj;
93 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
94 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
95 my $fake = PublicInbox::MIME->new($mime->as_string);
96 $fake->header_set('Message-Id', "<$gen>");
97 ok($im->add($fake), 'fake added easily');
98 is_deeply(\@warn, [], 'no warnings from a faker');
99 ok($im->add($mime), 'random MID made');
100 like(join(' ', @warn), qr/using random/, 'warned about using random');
101 @mids = $mime->header_obj->header_raw('Message-Id');
102 is($mids[0], '<a-mid@b>', 'original mid not changed');
103 like($mids[1], $sane_mid, 'new MID added');
104 is(scalar(@mids), 2, 'only one new MID added');
107 $mime->header_set('Message-Id');
108 ok($im->add($mime), 'random MID made for MID free message');
109 @mids = $mime->header_obj->header_raw('Message-Id');
110 like($mids[0], $sane_mid, 'mid was generated');
111 is(scalar(@mids), 1, 'new generated');
114 $mime->header_set('Message-Id', '<space@ (NXDOMAIN) >');
115 ok($im->add($mime), 'message added with space in Message-Id');
116 is_deeply([], \@warn);
120 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
121 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
122 $mime->header_set('References', '<zz-mid@b>');
123 ok($im->add($mime), 'message with multiple Message-ID');
125 my ($total, undef) = $ibx->over->recent;
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_id($rm_mime), content_id($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 is($ibx->mm->num_for($smsg->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 $srch = $ibx->search->reopen;
234 my $mset = $srch->query('m:'.$smsg->mid, { mset => 1});
235 is($mset->size, 0, 'no longer found in Xapian');
236 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
237 is($srch->{over_ro}->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 $cmts = $im->purge($mime), 'purged message');
260 like($cmts->[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->search->{over_ro}->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)');
289 inboxdir => "$inboxdir/non-existent/subdir",
292 -primary_address => 'test@example.com',
295 my $nope = PublicInbox::V2Writable->new($tmp);
298 ok($@, 'V2Writable fails on non-existent dir');
301 my $v2w = PublicInbox::V2Writable->new($tmp, 1);
302 ok($v2w, 'creat flag works');
303 $v2w->{parallel} = 0;
305 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
306 open my $fh, '>>', $alt or die $!;
307 print $fh "$inboxdir/all.git/objects\n" or die $!;
308 chmod(0664, $fh) or die "fchmod: $!";
310 open $fh, '<', $alt or die $!;
311 my $before = do { local $/; <$fh> };
313 ok($v2w->git_init(3), 'init a new epoch');
314 open $fh, '<', $alt or die $!;
315 my $after = do { local $/; <$fh> };
316 ok(index($after, $before) > 0,
317 'old contents preserved after adding epoch');
318 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
319 'first line is newest epoch');
320 my $mode = (stat($alt))[2] & 07777;
321 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');