1 # Copyright (C) 2018-2019 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 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13 plan skip_all => "$mod missing for nntpd.t" if $@;
15 local $ENV{HOME} = abs_path('t');
16 use_ok 'PublicInbox::V2Writable';
18 my ($inboxdir, $for_destroy) = tmpdir();
20 inboxdir => $inboxdir,
21 name => 'test-v2writable',
23 -primary_address => 'test@example.com',
25 $ibx = PublicInbox::Inbox->new($ibx);
26 my $mime = PublicInbox::MIME->create(
28 From => 'a@example.com',
29 To => 'test@example.com',
30 Subject => 'this is a subject',
31 'Message-ID' => '<a-mid@b>',
32 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
34 body => "hello world\n",
37 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
38 is($im->{shards}, 1, 'one shard when forced');
39 ok($im->add($mime), 'ordinary message added');
40 foreach my $f ("$inboxdir/msgmap.sqlite3",
41 glob("$inboxdir/xap*/*"),
42 glob("$inboxdir/xap*/*/*")) {
44 my ($bn) = (split(m!/!, $f))[-1];
45 is($st[2] & 07777, -f _ ? 0660 : 0770,
46 "default sharedRepository respected for $bn");
51 if ('ensure git configs are correct') {
52 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
53 qw(core.sharedRepository 0644));
54 is(system(@cmd), 0, "set sharedRepository in all.git");
55 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
56 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
57 is($v, '0644', 'child repo inherited core.sharedRepository');
58 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
59 is($v, 'true', 'child repo inherited repack.writeBitmaps');
64 local $SIG{__WARN__} = sub { push @warn, @_ };
65 is($im->add($mime), undef, 'obvious duplicate rejected');
66 is(scalar(@warn), 0, 'no warning about resent message');
69 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
70 is($im->add($mime), undef, 'secondary MID ignored if first matches');
71 my $sec = PublicInbox::MIME->new($mime->as_string);
72 $sec->header_set('Date');
73 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
74 ok($im->add($sec), 'secondary MID used if data is different');
75 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
76 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
77 is_deeply([ '<a-mid@b>', '<c@d>' ],
78 [ $sec->header_obj->header_raw('Message-Id') ],
79 'no new Message-Id added');
81 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
83 $mime->header_set('Message-Id', '<a-mid@b>');
84 $mime->body_set('different');
85 ok($im->add($mime), 'reused mid ok');
86 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
87 my @mids = $mime->header_obj->header_raw('Message-Id');
88 is($mids[0], '<a-mid@b>', 'original mid not changed');
89 like($mids[1], $sane_mid, 'new MID added');
90 is(scalar(@mids), 2, 'only one new MID added');
93 $mime->header_set('Message-Id', '<a-mid@b>');
94 $mime->body_set('this one needs a random mid');
95 my $hdr = $mime->header_obj;
96 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
97 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
98 my $fake = PublicInbox::MIME->new($mime->as_string);
99 $fake->header_set('Message-Id', "<$gen>");
100 ok($im->add($fake), 'fake added easily');
101 is_deeply(\@warn, [], 'no warnings from a faker');
102 ok($im->add($mime), 'random MID made');
103 like(join(' ', @warn), qr/using random/, 'warned about using random');
104 @mids = $mime->header_obj->header_raw('Message-Id');
105 is($mids[0], '<a-mid@b>', 'original mid not changed');
106 like($mids[1], $sane_mid, 'new MID added');
107 is(scalar(@mids), 2, 'only one new MID added');
110 $mime->header_set('Message-Id');
111 ok($im->add($mime), 'random MID made for MID free message');
112 @mids = $mime->header_obj->header_raw('Message-Id');
113 like($mids[0], $sane_mid, 'mid was generated');
114 is(scalar(@mids), 1, 'new generated');
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, undef) = $ibx->over->recent;
124 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
125 my $srch = $ibx->search;
126 my $mset1 = $srch->reopen->query('m:abcde@1', { mset => 1 });
127 is($mset1->size, 1, 'message found by first MID');
128 my $mset2 = $srch->reopen->query('m:abcde@2', { mset => 1 });
129 is($mset2->size, 1, 'message found by second MID');
130 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
131 'same document') if ($mset1->size);
133 my $alt = $srch->reopen->query('m:alt-id-for-nntp', { mset => 1 });
134 is($alt->size, 1, 'message found by alt MID (NNTP)');
135 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
136 'same document') if ($mset1->size);
137 $mime->header_set('X-Alt-Message-Id');
140 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
141 my $msgs = $ibx->over->get_thread($mid);
142 my $key = join(' ', sort(map { $_->{num} } @$msgs));
145 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
146 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
151 my $err = "$inboxdir/stderr.log";
152 my $out = "$inboxdir/stdout.log";
153 my $group = 'inbox.comp.test.v2writable';
154 my $pi_config = "$inboxdir/pi_config";
155 open my $fh, '>', $pi_config or die "open: $!\n";
157 [publicinbox "test-v2writable"]
160 address = test\@example.com
164 close $fh or die "close: $!\n";
165 my $sock = tcp_server();
166 ok($sock, 'sock created');
168 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
169 my $env = { PI_CONFIG => $pi_config };
170 my $td = start_script($cmd, $env, { 3 => $sock });
171 my $host_port = $sock->sockhost . ':' . $sock->sockport;
172 my $n = Net::NNTP->new($host_port);
174 my $x = $n->xover('1-');
176 foreach my $num (sort { $a <=> $b } keys %$x) {
177 my $mid = $x->{$num}->[3];
178 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
179 is_deeply($n->xhdr('Message-ID', $num),
180 { $num => $mid }, "XHDR lookup OK on num $num");
181 is_deeply($n->xhdr('Message-ID', $mid),
182 { $mid => $mid }, "XHDR lookup OK on MID $num");
185 foreach my $mid (@{$n->newnews(0, $group)}) {
186 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
188 is_deeply([sort keys %nn], [sort keys %uniq]);
191 foreach my $num (@{$n->listgroup($group)}) {
192 is($lg{$num}++, 0, "num is unique in LISTGROUP");
194 is_deeply([sort keys %lg], [sort keys %$x],
195 'XOVER and LISTGROUPS return the same article numbers');
197 my $xref = $n->xhdr('Xref', '1-');
198 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
200 my $mids = $n->xhdr('Message-ID', '1-');
201 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
203 my $rover = $n->xrover('1-');
204 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
207 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
208 my @before = $git0->qx(@log, qw(--pretty=oneline));
209 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
210 $im = PublicInbox::V2Writable->new($ibx, {nproc => 2});
211 is($im->{shards}, 1, 'detected single shard from previous');
212 my $smsg = $im->remove($mime, 'test removal');
214 my @after = $git0->qx(@log, qw(--pretty=oneline));
215 my $tip = shift @after;
216 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
217 'commit message propagated to git');
218 is_deeply(\@after, \@before, 'only one commit written to git');
219 is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
220 my $num = $smsg->{num};
221 like($num, qr/\A\d+\z/, 'numeric number in return message');
222 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
223 my $srch = $ibx->search->reopen;
224 my $mset = $srch->query('m:'.$smsg->mid, { mset => 1});
225 is($mset->size, 0, 'no longer found in Xapian');
226 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
227 is($srch->{over_ro}->get_art($num), undef,
228 'removal propagated to Over DB');
230 my $after = $git0->qx(@log1);
231 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
233 ok(index($before, $oid) > 0, 'no new blob introduced');
235 fail('failed to extract blob from log output');
237 is($im->remove($mime, 'test removal'), undef,
238 'remove is idempotent');
241 $after, 'no git history made with idempotent remove');
243 ok(!$@, '->done is idempotent');
247 ok($im->add($mime), 'add message to be purged');
248 local $SIG{__WARN__} = sub {};
249 ok(my $cmts = $im->purge($mime), 'purged message');
250 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
254 is($im->purge($mime), undef, 'no-op returns undef');
260 local $SIG{__WARN__} = sub {};
261 $mime->header_set('Subject', 'long mid');
262 $mime->header_set('Message-ID', "<$x>");
263 ok($im->add($mime), 'add excessively long Message-ID');
265 $mime->header_set('Message-ID', "<$y>");
266 $mime->header_set('References', "<$x>");
267 ok($im->add($mime), 'add excessively long References');
270 my $msgs = $ibx->search->{over_ro}->get_thread('x'x244);
271 is(2, scalar(@$msgs), 'got both messages');
272 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
273 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
274 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
279 inboxdir => "$inboxdir/non-existent/subdir",
282 -primary_address => 'test@example.com',
285 my $nope = PublicInbox::V2Writable->new($tmp);
288 ok($@, 'V2Writable fails on non-existent dir');