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);
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');
115 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
116 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
117 $mime->header_set('References', '<zz-mid@b>');
118 ok($im->add($mime), 'message with multiple Message-ID');
120 my ($total, undef) = $ibx->over->recent;
121 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
122 my $srch = $ibx->search;
123 my $mset1 = $srch->reopen->query('m:abcde@1', { mset => 1 });
124 is($mset1->size, 1, 'message found by first MID');
125 my $mset2 = $srch->reopen->query('m:abcde@2', { mset => 1 });
126 is($mset2->size, 1, 'message found by second MID');
127 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
128 'same document') if ($mset1->size);
130 my $alt = $srch->reopen->query('m:alt-id-for-nntp', { mset => 1 });
131 is($alt->size, 1, 'message found by alt MID (NNTP)');
132 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
133 'same document') if ($mset1->size);
134 $mime->header_set('X-Alt-Message-Id');
137 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
138 my $msgs = $ibx->over->get_thread($mid);
139 my $key = join(' ', sort(map { $_->{num} } @$msgs));
142 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
143 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
148 my $err = "$inboxdir/stderr.log";
149 my $out = "$inboxdir/stdout.log";
150 my $group = 'inbox.comp.test.v2writable';
151 my $pi_config = "$inboxdir/pi_config";
152 open my $fh, '>', $pi_config or die "open: $!\n";
154 [publicinbox "test-v2writable"]
157 address = test\@example.com
161 close $fh or die "close: $!\n";
162 my $sock = tcp_server();
163 ok($sock, 'sock created');
165 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
166 my $env = { PI_CONFIG => $pi_config };
167 my $td = start_script($cmd, $env, { 3 => $sock });
168 my $host_port = $sock->sockhost . ':' . $sock->sockport;
169 my $n = Net::NNTP->new($host_port);
171 my $x = $n->xover('1-');
173 foreach my $num (sort { $a <=> $b } keys %$x) {
174 my $mid = $x->{$num}->[3];
175 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
176 is_deeply($n->xhdr('Message-ID', $num),
177 { $num => $mid }, "XHDR lookup OK on num $num");
178 is_deeply($n->xhdr('Message-ID', $mid),
179 { $mid => $mid }, "XHDR lookup OK on MID $num");
182 foreach my $mid (@{$n->newnews(0, $group)}) {
183 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
185 is_deeply([sort keys %nn], [sort keys %uniq]);
188 foreach my $num (@{$n->listgroup($group)}) {
189 is($lg{$num}++, 0, "num is unique in LISTGROUP");
191 is_deeply([sort keys %lg], [sort keys %$x],
192 'XOVER and LISTGROUPS return the same article numbers');
194 my $xref = $n->xhdr('Xref', '1-');
195 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
197 my $mids = $n->xhdr('Message-ID', '1-');
198 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
200 my $rover = $n->xrover('1-');
201 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
204 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
205 my @before = $git0->qx(@log, qw(--pretty=oneline));
206 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
207 $im = PublicInbox::V2Writable->new($ibx, {nproc => 2});
208 is($im->{shards}, 1, 'detected single shard from previous');
209 my $smsg = $im->remove($mime, 'test removal');
211 my @after = $git0->qx(@log, qw(--pretty=oneline));
212 my $tip = shift @after;
213 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
214 'commit message propagated to git');
215 is_deeply(\@after, \@before, 'only one commit written to git');
216 is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
217 my $num = $smsg->{num};
218 like($num, qr/\A\d+\z/, 'numeric number in return message');
219 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
220 my $srch = $ibx->search->reopen;
221 my $mset = $srch->query('m:'.$smsg->mid, { mset => 1});
222 is($mset->size, 0, 'no longer found in Xapian');
223 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
224 is($srch->{over_ro}->get_art($num), undef,
225 'removal propagated to Over DB');
227 my $after = $git0->qx(@log1);
228 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
230 ok(index($before, $oid) > 0, 'no new blob introduced');
232 fail('failed to extract blob from log output');
234 is($im->remove($mime, 'test removal'), undef,
235 'remove is idempotent');
238 $after, 'no git history made with idempotent remove');
240 ok(!$@, '->done is idempotent');
244 ok($im->add($mime), 'add message to be purged');
245 local $SIG{__WARN__} = sub {};
246 ok(my $cmts = $im->purge($mime), 'purged message');
247 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
251 is($im->purge($mime), undef, 'no-op returns undef');
257 local $SIG{__WARN__} = sub {};
258 $mime->header_set('Subject', 'long mid');
259 $mime->header_set('Message-ID', "<$x>");
260 ok($im->add($mime), 'add excessively long Message-ID');
262 $mime->header_set('Message-ID', "<$y>");
263 $mime->header_set('References', "<$x>");
264 ok($im->add($mime), 'add excessively long References');
267 my $msgs = $ibx->search->{over_ro}->get_thread('x'x244);
268 is(2, scalar(@$msgs), 'got both messages');
269 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
270 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
271 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
276 inboxdir => "$inboxdir/non-existent/subdir",
279 -primary_address => 'test@example.com',
282 my $nope = PublicInbox::V2Writable->new($tmp);
285 ok($@, 'V2Writable fails on non-existent dir');
288 my $v2w = PublicInbox::V2Writable->new($tmp, 1);
289 ok($v2w, 'creat flag works');
290 $v2w->{parallel} = 0;
292 my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
293 open my $fh, '>>', $alt or die $!;
294 print $fh "$inboxdir/all.git/objects\n" or die $!;
295 chmod(0664, $fh) or die "fchmod: $!";
297 open $fh, '<', $alt or die $!;
298 my $before = do { local $/; <$fh> };
300 ok($v2w->git_init(3), 'init a new epoch');
301 open $fh, '<', $alt or die $!;
302 my $after = do { local $/; <$fh> };
303 ok(index($after, $before) > 0,
304 'old contents preserved after adding epoch');
305 like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
306 'first line is newest epoch');
307 my $mode = (stat($alt))[2] & 07777;
308 is($mode, 0664, sprintf('0%03o', $mode).' is 0664');