1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Spawn qw(which);
8 require_mods(qw(DBD::SQLite));
9 require PublicInbox::InboxWritable;
12 use Socket qw(IPPROTO_TCP TCP_NODELAY);
17 use_ok 'PublicInbox::Msgmap';
19 # FIXME: make easier to test both versions
20 my $version = $ENV{PI_TEST_VERSION} || 1;
21 require_git('2.6') if $version == 2;
22 my $lsof = which('lsof');
23 my $fast_idle = eval { require Linux::Inotify2; 1 } //
24 eval { require IO::KQueue; 1 };
26 my ($tmpdir, $for_destroy) = tmpdir();
27 my $home = "$tmpdir/pi-home";
28 my $err = "$tmpdir/stderr.log";
29 my $out = "$tmpdir/stdout.log";
30 my $inboxdir = "$tmpdir/main.git";
31 my $group = 'test-nntpd';
32 my $addr = $group . '@example.com';
34 skip "git 2.6+ required for V2Writable", 1 if $version == 1;
35 use_ok 'PublicInbox::V2Writable';
39 my $sock = tcp_server();
44 inboxdir => $inboxdir,
47 -primary_address => $addr,
48 indexlevel => 'basic',
50 $ibx = PublicInbox::Inbox->new($ibx);
52 local $ENV{HOME} = $home;
53 my @cmd = ('-init', $group, $inboxdir, 'http://example.com/', $addr);
54 push @cmd, "-V$version", '-Lbasic';
55 ok(run_script(\@cmd), 'init OK');
56 is(xsys(qw(git config), "--file=$home/.public-inbox/config",
57 "publicinbox.$group.newsgroup", $group),
58 0, 'enabled newsgroup');
61 $ibx = PublicInbox::InboxWritable->new($ibx);
62 my $im = $ibx->importer(0);
64 # ensure successful message delivery
66 my $mime = PublicInbox::Eml->new(<<EOF);
67 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
68 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
70 Message-Id: <nntp\@example.com>
71 Content-Type: text/plain; charset=utf-8
72 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
73 Date: Thu, 01 Jan 1970 06:06:06 +0000
74 Content-Transfer-Encoding: 8bit
75 References: <ref tab squeezed>
77 This is a test message for El\xc3\xa9anor
81 $mime->header_set('List-Id', "<$list_id>");
82 my $str = $mime->as_string;
83 $str =~ s/(?<!\r)\n/\r\n/sg;
89 ok(run_script(['-index', $ibx->{inboxdir}]),
94 ok($sock, 'sock created');
95 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
96 $td = start_script($cmd, undef, { 3 => $sock });
97 my $host_port = $sock->sockhost . ':' . $sock->sockport;
98 my $n = Net::NNTP->new($host_port);
100 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
101 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
102 is_deeply($n->listgroup($group), [1], 'listgroup OK');
103 # TODO: Net::NNTP::listgroup does not support range at the moment
106 my $expect = [ qw(Subject: From: Date: Message-ID:
107 References: Bytes: Lines: Xref:full) ];
108 is_deeply($n->overview_fmt, $expect,
109 'RFC3977 8.4.2 compliant LIST OVERVIEW.FMT');
112 $n->can('starttls') or
113 skip('Net::NNTP too old to support STARTTLS', 2);
114 require_mods('IO::Socket::SSL', 2);
115 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
116 is($n->code, 580, 'got 580 code on server w/o TLS');
119 my $mid = '<nntp@example.com>';
121 'message-id' => $mid,
122 subject => "Testing for El\xc3\xa9anor",
123 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
124 'from' => "El\xc3\xa9anor <me\@example.com>",
125 'to' => "El\xc3\xa9anor <you\@example.com>",
127 'xref' => hostname . " $group:1",
128 'references' => '<reftabsqueezed>',
131 my $s = tcp_connect($sock);
132 sysread($s, my $buf, 4096);
133 is($buf, "201 " . hostname . " ready - post via email\r\n",
136 ok(syswrite($s, " \r\n"), 'wrote spaces');
137 ok(syswrite($s, "\r\n"), 'wrote nothing');
138 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
139 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
141 $s = tcp_connect($sock);
142 sysread($s, $buf, 4096);
143 is($buf, "201 " . hostname . " ready - post via email\r\n",
146 syswrite($s, "CAPABILITIES\r\n");
147 $buf = read_til_dot($s);
148 like($buf, qr/\r\nVERSION 2\r\n/s, 'CAPABILITIES works');
149 unlike($buf, qr/STARTTLS/s, 'STARTTLS not advertised');
150 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
151 if (eval { require Compress::Raw::Zlib }) {
152 like($buf, $deflate_capa, 'DEFLATE advertised');
154 unlike($buf, $deflate_capa,
155 'DEFLATE not advertised (Compress::Raw::Zlib missing)');
158 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
159 $buf = read_til_dot($s);
160 like($buf, qr/\A231 list of /, 'newgroups OK');
162 while (my ($k, $v) = each %xhdr) {
163 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
164 "XHDR $k by message-id works");
165 is_deeply($n->xhdr("$k 1"), { 1 => $v },
166 "$k by article number works");
167 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
168 "$k by article range works");
170 syswrite($s, "HDR $k $mid\r\n");
171 $buf = read_til_dot($s);
172 my @r = split("\r\n", $buf);
173 like($r[0], qr/\A225 /, '225 response for HDR');
174 is($r[1], "0 $v", 'got expected response for HDR');
178 my $nogroup = Net::NNTP->new($host_port);
179 while (my ($k, $v) = each %xhdr) {
180 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
181 "$k by message-id works without group");
185 is_deeply($n->xover('1-'), {
186 '1' => ["Testing for El\xc3\xa9anor",
187 "El\xc3\xa9anor <me\@example.com>",
188 'Thu, 01 Jan 1970 06:06:06 +0000',
189 '<nntp@example.com>',
193 'Xref: '. hostname . ' test-nntpd:1'] },
194 "XOVER range works");
196 is_deeply($n->xover('1'), {
197 '1' => ["Testing for El\xc3\xa9anor",
198 "El\xc3\xa9anor <me\@example.com>",
199 'Thu, 01 Jan 1970 06:06:06 +0000',
200 '<nntp@example.com>',
204 'Xref: '. hostname . ' test-nntpd:1'] },
205 "XOVER by article works");
207 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
208 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
209 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
210 'body really matches');
211 my $art = $n->article(1);
212 is(ref($art), 'ARRAY', 'got array for ARTICLE');
213 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
214 is($n->article(999), undef, 'non-existent num');
215 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
218 syswrite($s, "OVER $mid\r\n");
219 $buf = read_til_dot($s);
220 my @r = split("\r\n", $buf);
221 like($r[0], qr/^224 /, 'got 224 response for OVER');
222 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
223 "El\xc3\xa9anor <me\@example.com>\t" .
224 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
225 "$mid\t<reftabsqueezed>\t$len\t1" .
226 "\tXref: " . hostname . " test-nntpd:0",
227 'OVER by Message-ID works');
228 is($r[2], '.', 'correctly terminated response');
231 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
233 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
234 'XHDR References 1- works)');
235 is_deeply($n->xhdr(qw(list-id 1-)), {},
236 'XHDR on invalid header returns empty');
238 my $mids = $n->newnews(0, '*');
239 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
244 ok($date >= $t0, 'valid date after start');
245 ok($date <= $t1, 'valid date before stop');
247 if ('leafnode interop') {
248 my $for_leafnode = PublicInbox::Eml->new(<<"");
249 From: longheader\@example.com
252 Date: Fri, 02 Oct 1993 00:00:00 +0000
254 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
255 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
256 $im->add($for_leafnode);
259 ok(run_script(['-index', $ibx->{inboxdir}]),
262 my $hdr = $n->head("<$long_hdr>");
263 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
264 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
265 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
267 # it's possible for v2 messages to have 2+ Message-IDs,
268 # but leafnode can't handle it
270 my @mids = ("<$long_hdr>", '<2mid@wtf>');
271 $for_leafnode->header_set('Message-ID', @mids);
272 $for_leafnode->body_set('not-a-dupe');
274 local $SIG{__WARN__} = sub { $warn .= join('', @_) };
275 $im->add($for_leafnode);
277 like($warn, qr/reused/, 'warned for reused MID');
278 $hdr = $n->head('<2mid@wtf>');
279 my @hmids = grep(/\AMessage-ID: /i, @$hdr);
280 is(scalar(@hmids), 1, 'Single Message-ID in header');
281 like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
285 # pipelined requests:
289 syswrite($s, "GROUP $group\r\n");
294 <$s> =~ /\A224 / or _exit(1);
295 <$s> =~ /\A1/ or _exit(2);
296 <$s> eq ".\r\n" or _exit(3);
300 <$s> =~ /\A220 / or _exit(4);
301 my $dig = Digest::SHA->new(1);
302 while (my $l = <$s>) {
303 last if $l eq ".\r\n";
306 $dig = $dig->hexdigest;
310 scalar(keys(%sums)) == 1 or _exit(5);
311 (values(%sums))[0] == $nart or _exit(6);
316 syswrite($s, "XOVER 1\r\n");
318 syswrite($s, "ARTICLE 1\r\n" x $nart);
319 is($rdr, waitpid($rdr, 0), 'reader done');
320 is($? >> 8, 0, 'no errors');
322 my $noerr = { 2 => \(my $null) };
324 if ($INC{'Search/Xapian.pm'} && ($ENV{TEST_RUN_MODE}//2)) {
325 skip 'Search/Xapian.pm pre-loaded (by t/run.perl?)', 1;
327 $lsof or skip 'lsof missing', 1;
328 my @of = xqx([$lsof, '-p', $td->{pid}], undef, $noerr);
329 skip('lsof broken', 1) if (!scalar(@of) || $?);
330 my @xap = grep m!Search/Xapian!, @of;
331 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
333 # -compact requires Xapian
335 require_mods('Search::Xapian', 2);
336 which('xapian-compact') or skip 'xapian-compact missing', 2;
337 is(xsys(qw(git config), "--file=$home/.public-inbox/config",
338 "publicinbox.$group.indexlevel", 'medium'),
339 0, 'upgraded indexlevel');
340 my $ex = eml_load('t/data/0001.patch');
341 is($n->article($ex->header('Message-ID')), undef,
342 'article did not exist');
346 my $f = $ibx->mm->{dbh}->sqlite_db_filename;
347 my $tmp = "$tmpdir/tmp.sqlite3";
348 $ibx->mm->{dbh}->sqlite_backup_to_file($tmp);
350 rename($tmp, $f) or BAIL_OUT "rename($tmp, $f): $!";
352 ok(run_script([qw(-index --reindex -c), $ibx->{inboxdir}],
353 undef, $noerr), '-compacted');
354 select(undef, undef, undef, $fast_idle ? 0.1 : 2.1);
355 $art = $n->article($ex->header('Message-ID'));
356 ok($art, 'new article retrieved after compact');
357 $lsof or skip 'lsof missing', 1;
358 ($^O =~ /\A(?:linux)\z/) or
359 skip "lsof /(deleted)/ check untested on $^O", 1;
360 my @of = xqx([$lsof, '-p', $td->{pid}], undef, $noerr);
361 is(scalar(grep(/\(deleted\)/, @of)), 0, 'no deleted files');
363 SKIP: { test_watch($tmpdir, $sock, $group) };
365 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
366 syswrite($s, 'HDR List-id 1-');
367 select(undef, undef, undef, 0.15);
368 ok($td->kill, 'killed nntpd');
369 select(undef, undef, undef, 0.15);
370 syswrite($s, "\r\n");
373 sysread($s, $buf, 4096, length($buf));
374 } until ($buf =~ /\r\n\z/);
375 my @r = split("\r\n", $buf);
376 like($r[0], qr/^5\d\d /,
377 'got 5xx response for unoptimized HDR');
378 is(scalar @r, 1, 'only one response line');
382 is($?, 0, 'no error in exited process');
384 open my $fh, '<', $err or die "open $err failed: $!";
388 unlike($eout, qr/wide/i, 'no Wide character warnings');
398 sysread($s, $buf, 4096, length($buf));
399 } until ($buf =~ /\r\n\.\r\n\z/);
404 my ($tmpdir, $sock, $group) = @_;
405 use_ok 'PublicInbox::WatchMaildir';
406 use_ok 'PublicInbox::InboxIdle';
407 use_ok 'PublicInbox::Config';
408 require_git('1.8.5', 1) or skip('git 1.8.5+ needed for --urlmatch', 4);
409 my $old_env = { HOME => $ENV{HOME} };
410 my $home = "$tmpdir/watch_home";
411 mkdir $home or BAIL_OUT $!;
412 mkdir "$home/.public-inbox" or BAIL_OUT $!;
413 local $ENV{HOME} = $home;
414 my $name = 'watchnntp';
415 my $addr = "i1\@example.com";
416 my $url = "http://example.com/i1";
417 my $inboxdir = "$tmpdir/watchnntp";
418 my $cmd = ['-init', '-V1', '-Lbasic', $name, $inboxdir, $url, $addr];
419 my ($ihost, $iport) = ($sock->sockhost, $sock->sockport);
420 my $nntpurl = "nntp://$ihost:$iport/$group";
421 run_script($cmd) or BAIL_OUT("init $name");
422 xsys(qw(git config), "--file=$home/.public-inbox/config",
423 "publicinbox.$name.watch",
424 $nntpurl) == 0 or BAIL_OUT "git config $?";
425 # try again with polling
426 xsys(qw(git config), "--file=$home/.public-inbox/config",
427 'nntp.PollInterval', 0.11) == 0
428 or BAIL_OUT "git config $?";
429 my $cfg = PublicInbox::Config->new;
430 PublicInbox::DS->Reset;
431 my $ii = PublicInbox::InboxIdle->new($cfg);
432 my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
433 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
434 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
435 my $watcherr = "$tmpdir/watcherr";
436 open my $err_wr, '>', $watcherr or BAIL_OUT $!;
437 open my $err, '<', $watcherr or BAIL_OUT $!;
438 my $w = start_script(['-watch'], undef, { 2 => $err_wr });
440 diag 'waiting for initial fetch...';
441 PublicInbox::DS->EventLoop;
442 diag 'inbox unlocked on initial fetch';
445 is($?, 0, 'no error in exited -watch process');
446 $cfg->each_inbox(sub { shift->unsubscribe_unlock('ident') });
448 PublicInbox::DS->Reset;
449 my @err = grep(!/^I:/, <$err>);
450 is(@err, 0, 'no warnings/errors from -watch'.join(' ', @err));
451 my @ls = xqx(['git', "--git-dir=$inboxdir", qw(ls-tree -r HEAD)]);
452 isnt(scalar(@ls), 0, 'imported something');