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 File::Temp qw/tempdir/;
9 require './t/common.perl';
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13 plan skip_all => "$mod missing for nntpd.t" if $@;
15 use_ok 'PublicInbox::V2Writable';
17 my $inboxdir = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 inboxdir => $inboxdir,
20 name => 'test-v2writable',
22 -primary_address => 'test@example.com',
24 $ibx = PublicInbox::Inbox->new($ibx);
25 my $mime = PublicInbox::MIME->create(
27 From => 'a@example.com',
28 To => 'test@example.com',
29 Subject => 'this is a subject',
30 'Message-ID' => '<a-mid@b>',
31 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
33 body => "hello world\n",
36 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
37 is($im->{shards}, 1, 'one shard when forced');
38 ok($im->add($mime), 'ordinary message added');
39 foreach my $f ("$inboxdir/msgmap.sqlite3",
40 glob("$inboxdir/xap*/*"),
41 glob("$inboxdir/xap*/*/*")) {
43 my ($bn) = (split(m!/!, $f))[-1];
44 is($st[2] & 07777, -f _ ? 0660 : 0770,
45 "default sharedRepository respected for $bn");
50 if ('ensure git configs are correct') {
51 my @cmd = (qw(git config), "--file=$inboxdir/all.git/config",
52 qw(core.sharedRepository 0644));
53 is(system(@cmd), 0, "set sharedRepository in all.git");
54 $git0 = PublicInbox::Git->new("$inboxdir/git/0.git");
55 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
56 is($v, '0644', 'child repo inherited core.sharedRepository');
57 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
58 is($v, 'true', 'child repo inherited repack.writeBitmaps');
63 local $SIG{__WARN__} = sub { push @warn, @_ };
64 is($im->add($mime), undef, 'obvious duplicate rejected');
65 is(scalar(@warn), 0, 'no warning about resent message');
68 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
69 is($im->add($mime), undef, 'secondary MID ignored if first matches');
70 my $sec = PublicInbox::MIME->new($mime->as_string);
71 $sec->header_set('Date');
72 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
73 ok($im->add($sec), 'secondary MID used if data is different');
74 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
75 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
76 is_deeply([ '<a-mid@b>', '<c@d>' ],
77 [ $sec->header_obj->header_raw('Message-Id') ],
78 'no new Message-Id added');
80 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
82 $mime->header_set('Message-Id', '<a-mid@b>');
83 $mime->body_set('different');
84 ok($im->add($mime), 'reused mid ok');
85 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
86 my @mids = $mime->header_obj->header_raw('Message-Id');
87 is($mids[0], '<a-mid@b>', 'original mid not changed');
88 like($mids[1], $sane_mid, 'new MID added');
89 is(scalar(@mids), 2, 'only one new MID added');
92 $mime->header_set('Message-Id', '<a-mid@b>');
93 $mime->body_set('this one needs a random mid');
94 my $hdr = $mime->header_obj;
95 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
96 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
97 my $fake = PublicInbox::MIME->new($mime->as_string);
98 $fake->header_set('Message-Id', "<$gen>");
99 ok($im->add($fake), 'fake added easily');
100 is_deeply(\@warn, [], 'no warnings from a faker');
101 ok($im->add($mime), 'random MID made');
102 like(join(' ', @warn), qr/using random/, 'warned about using random');
103 @mids = $mime->header_obj->header_raw('Message-Id');
104 is($mids[0], '<a-mid@b>', 'original mid not changed');
105 like($mids[1], $sane_mid, 'new MID added');
106 is(scalar(@mids), 2, 'only one new MID added');
109 $mime->header_set('Message-Id');
110 ok($im->add($mime), 'random MID made for MID free message');
111 @mids = $mime->header_obj->header_raw('Message-Id');
112 like($mids[0], $sane_mid, 'mid was generated');
113 is(scalar(@mids), 1, 'new generated');
117 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
118 $mime->header_set('X-Alt-Message-Id', '<alt-id-for-nntp>');
119 $mime->header_set('References', '<zz-mid@b>');
120 ok($im->add($mime), 'message with multiple Message-ID');
122 my ($total, undef) = $ibx->over->recent;
123 is($ibx->mm->num_highwater, $total, 'got expected highwater value');
124 my $srch = $ibx->search;
125 my $mset1 = $srch->reopen->query('m:abcde@1', { mset => 1 });
126 is($mset1->size, 1, 'message found by first MID');
127 my $mset2 = $srch->reopen->query('m:abcde@2', { mset => 1 });
128 is($mset2->size, 1, 'message found by second MID');
129 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
130 'same document') if ($mset1->size);
132 my $alt = $srch->reopen->query('m:alt-id-for-nntp', { mset => 1 });
133 is($alt->size, 1, 'message found by alt MID (NNTP)');
134 is((($alt->items)[0])->get_docid, (($mset1->items)[0])->get_docid,
135 'same document') if ($mset1->size);
136 $mime->header_set('X-Alt-Message-Id');
139 for my $mid (qw(abcde@1 abcde@2 alt-id-for-nntp)) {
140 my $msgs = $ibx->over->get_thread($mid);
141 my $key = join(' ', sort(map { $_->{num} } @$msgs));
144 is(scalar(keys(%uniq)), 1, 'all alt Message-ID queries give same smsg');
145 is_deeply([values(%uniq)], [3], '3 queries, 3 results');
150 my $err = "$inboxdir/stderr.log";
151 my $out = "$inboxdir/stdout.log";
152 my $group = 'inbox.comp.test.v2writable';
153 my $pi_config = "$inboxdir/pi_config";
154 open my $fh, '>', $pi_config or die "open: $!\n";
156 [publicinbox "test-v2writable"]
159 address = test\@example.com
163 close $fh or die "close: $!\n";
164 my $sock = tcp_server();
165 ok($sock, 'sock created');
168 END { kill 'TERM', $pid if defined $pid };
169 my $nntpd = 'blib/script/public-inbox-nntpd';
170 my $cmd = [ $nntpd, '-W0', "--stdout=$out", "--stderr=$err" ];
171 $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [ $sock ]);
172 my $host_port = $sock->sockhost . ':' . $sock->sockport;
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");
182 is_deeply($n->xhdr('Message-ID', $mid),
183 { $mid => $mid }, "XHDR lookup OK on MID $num");
186 foreach my $mid (@{$n->newnews(0, $group)}) {
187 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
189 is_deeply([sort keys %nn], [sort keys %uniq]);
192 foreach my $num (@{$n->listgroup($group)}) {
193 is($lg{$num}++, 0, "num is unique in LISTGROUP");
195 is_deeply([sort keys %lg], [sort keys %$x],
196 'XOVER and LISTGROUPS return the same article numbers');
198 my $xref = $n->xhdr('Xref', '1-');
199 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
201 my $mids = $n->xhdr('Message-ID', '1-');
202 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
204 my $rover = $n->xrover('1-');
205 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
208 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
209 my @before = $git0->qx(@log, qw(--pretty=oneline));
210 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
211 $im = PublicInbox::V2Writable->new($ibx, {nproc => 2});
212 is($im->{shards}, 1, 'detected single shard from previous');
213 my $smsg = $im->remove($mime, 'test removal');
215 my @after = $git0->qx(@log, qw(--pretty=oneline));
216 my $tip = shift @after;
217 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
218 'commit message propagated to git');
219 is_deeply(\@after, \@before, 'only one commit written to git');
220 is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
221 my $num = $smsg->{num};
222 like($num, qr/\A\d+\z/, 'numeric number in return message');
223 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
224 my $srch = $ibx->search->reopen;
225 my $mset = $srch->query('m:'.$smsg->mid, { mset => 1});
226 is($mset->size, 0, 'no longer found in Xapian');
227 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
228 is($srch->{over_ro}->get_art($num), undef,
229 'removal propagated to Over DB');
231 my $after = $git0->qx(@log1);
232 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
234 ok(index($before, $oid) > 0, 'no new blob introduced');
236 fail('failed to extract blob from log output');
238 is($im->remove($mime, 'test removal'), undef,
239 'remove is idempotent');
242 $after, 'no git history made with idempotent remove');
244 ok(!$@, '->done is idempotent');
248 ok($im->add($mime), 'add message to be purged');
249 local $SIG{__WARN__} = sub {};
250 ok(my $cmts = $im->purge($mime), 'purged message');
251 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
255 is($im->purge($mime), undef, 'no-op returns undef');
261 local $SIG{__WARN__} = sub {};
262 $mime->header_set('Subject', 'long mid');
263 $mime->header_set('Message-ID', "<$x>");
264 ok($im->add($mime), 'add excessively long Message-ID');
266 $mime->header_set('Message-ID', "<$y>");
267 $mime->header_set('References', "<$x>");
268 ok($im->add($mime), 'add excessively long References');
271 my $msgs = $ibx->search->{over_ro}->get_thread('x'x244);
272 is(2, scalar(@$msgs), 'got both messages');
273 is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
274 is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
275 is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
280 inboxdir => "$inboxdir/non-existent/subdir",
283 -primary_address => 'test@example.com',
286 my $nope = PublicInbox::V2Writable->new($tmp);
289 ok($@, 'V2Writable fails on non-existent dir');