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);
18 # FIXME: make easier to test both versions
19 my $version = $ENV{PI_TEST_VERSION} || 1;
20 require_git('2.6') if $version == 2;
21 my $lsof = which('lsof');
22 my $fast_idle = eval { require Linux::Inotify2; 1 } //
23 eval { require IO::KQueue; 1 };
25 my ($tmpdir, $for_destroy) = tmpdir();
26 my $home = "$tmpdir/pi-home";
27 my $err = "$tmpdir/stderr.log";
28 my $out = "$tmpdir/stdout.log";
29 my $inboxdir = "$tmpdir/main.git";
30 my $group = 'test-nntpd';
31 my $addr = $group . '@example.com';
33 skip "git 2.6+ required for V2Writable", 1 if $version == 1;
34 use_ok 'PublicInbox::V2Writable';
38 my $sock = tcp_server();
43 inboxdir => $inboxdir,
46 -primary_address => $addr,
47 indexlevel => 'basic',
49 $ibx = PublicInbox::Inbox->new($ibx);
51 local $ENV{HOME} = $home;
52 my @cmd = ('-init', $group, $inboxdir, 'http://example.com/', $addr);
53 push @cmd, "-V$version", '-Lbasic';
54 ok(run_script(\@cmd), 'init OK');
55 is(xsys(qw(git config), "--file=$home/.public-inbox/config",
56 "publicinbox.$group.newsgroup", $group),
57 0, 'enabled newsgroup');
60 $ibx = PublicInbox::InboxWritable->new($ibx);
61 my $im = $ibx->importer(0);
63 # ensure successful message delivery
65 my $mime = PublicInbox::Eml->new(<<EOF);
66 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
67 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
69 Message-Id: <nntp\@example.com>
70 Content-Type: text/plain; charset=utf-8
71 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
72 Date: Thu, 01 Jan 1970 06:06:06 +0000
73 Content-Transfer-Encoding: 8bit
74 References: <ref tab squeezed>
76 This is a test message for El\xc3\xa9anor
80 $mime->header_set('List-Id', "<$list_id>");
81 my $str = $mime->as_string;
82 $str =~ s/(?<!\r)\n/\r\n/sg;
88 ok(run_script(['-index', $ibx->{inboxdir}]),
93 ok($sock, 'sock created');
94 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
95 $td = start_script($cmd, undef, { 3 => $sock });
96 my $host_port = $sock->sockhost . ':' . $sock->sockport;
97 my $n = Net::NNTP->new($host_port);
99 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
100 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
101 is_deeply($n->listgroup($group), [1], 'listgroup OK');
102 # TODO: Net::NNTP::listgroup does not support range at the moment
105 my $expect = [ qw(Subject: From: Date: Message-ID:
106 References: Bytes: Lines: Xref:full) ];
107 is_deeply($n->overview_fmt, $expect,
108 'RFC3977 8.4.2 compliant LIST OVERVIEW.FMT');
111 $n->can('starttls') or
112 skip('Net::NNTP too old to support STARTTLS', 2);
113 require_mods('IO::Socket::SSL', 2);
115 IO::Socket::SSL->VERSION(2.007);
117 IO::Socket::SSL <2.007 not supported by Net::NNTP
119 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
120 is($n->code, 580, 'got 580 code on server w/o TLS');
123 my $mid = '<nntp@example.com>';
125 'message-id' => $mid,
126 subject => "Testing for El\xc3\xa9anor",
127 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
128 'from' => "El\xc3\xa9anor <me\@example.com>",
129 'to' => "El\xc3\xa9anor <you\@example.com>",
131 'xref' => hostname . " $group:1",
132 'references' => '<reftabsqueezed>',
135 my $s = tcp_connect($sock);
136 sysread($s, my $buf, 4096);
137 is($buf, "201 " . hostname . " ready - post via email\r\n",
140 ok(syswrite($s, " \r\n"), 'wrote spaces');
141 ok(syswrite($s, "\r\n"), 'wrote nothing');
142 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
143 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
145 $s = tcp_connect($sock);
146 sysread($s, $buf, 4096);
147 is($buf, "201 " . hostname . " ready - post via email\r\n",
150 syswrite($s, "CAPABILITIES\r\n");
151 $buf = read_til_dot($s);
152 like($buf, qr/\r\nVERSION 2\r\n/s, 'CAPABILITIES works');
153 unlike($buf, qr/STARTTLS/s, 'STARTTLS not advertised');
154 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
155 if (eval { require Compress::Raw::Zlib }) {
156 like($buf, $deflate_capa, 'DEFLATE advertised');
158 unlike($buf, $deflate_capa,
159 'DEFLATE not advertised (Compress::Raw::Zlib missing)');
162 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
163 $buf = read_til_dot($s);
164 like($buf, qr/\A231 list of /, 'newgroups OK');
166 while (my ($k, $v) = each %xhdr) {
167 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
168 "XHDR $k by message-id works");
169 is_deeply($n->xhdr("$k 1"), { 1 => $v },
170 "$k by article number works");
171 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
172 "$k by article range works");
174 syswrite($s, "HDR $k $mid\r\n");
175 $buf = read_til_dot($s);
176 my @r = split("\r\n", $buf);
177 like($r[0], qr/\A225 /, '225 response for HDR');
178 is($r[1], "0 $v", 'got expected response for HDR');
182 my $nogroup = Net::NNTP->new($host_port);
183 while (my ($k, $v) = each %xhdr) {
184 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
185 "$k by message-id works without group");
189 is_deeply($n->xover('1-'), {
190 '1' => ["Testing for El\xc3\xa9anor",
191 "El\xc3\xa9anor <me\@example.com>",
192 'Thu, 01 Jan 1970 06:06:06 +0000',
193 '<nntp@example.com>',
197 'Xref: '. hostname . ' test-nntpd:1'] },
198 "XOVER range works");
200 is_deeply($n->xover('1'), {
201 '1' => ["Testing for El\xc3\xa9anor",
202 "El\xc3\xa9anor <me\@example.com>",
203 'Thu, 01 Jan 1970 06:06:06 +0000',
204 '<nntp@example.com>',
208 'Xref: '. hostname . ' test-nntpd:1'] },
209 "XOVER by article works");
211 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
212 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
213 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
214 'body really matches');
215 my $art = $n->article(1);
216 is(ref($art), 'ARRAY', 'got array for ARTICLE');
217 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
218 is($n->article(999), undef, 'non-existent num');
219 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
222 syswrite($s, "OVER $mid\r\n");
223 $buf = read_til_dot($s);
224 my @r = split("\r\n", $buf);
225 like($r[0], qr/^224 /, 'got 224 response for OVER');
226 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
227 "El\xc3\xa9anor <me\@example.com>\t" .
228 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
229 "$mid\t<reftabsqueezed>\t$len\t1" .
230 "\tXref: " . hostname . " test-nntpd:0",
231 'OVER by Message-ID works');
232 is($r[2], '.', 'correctly terminated response');
235 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
237 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
238 'XHDR References 1- works)');
239 is_deeply($n->xhdr(qw(list-id 1-)), {},
240 'XHDR on invalid header returns empty');
242 my $mids = $n->newnews(0, '*');
243 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
248 ok($date >= $t0, 'valid date after start');
249 ok($date <= $t1, 'valid date before stop');
251 if ('leafnode interop') {
252 my $for_leafnode = PublicInbox::Eml->new(<<"");
253 From: longheader\@example.com
256 Date: Fri, 02 Oct 1993 00:00:00 +0000
258 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
259 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
260 $im->add($for_leafnode);
263 ok(run_script(['-index', $ibx->{inboxdir}]),
266 my $hdr = $n->head("<$long_hdr>");
267 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
268 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
269 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
271 # it's possible for v2 messages to have 2+ Message-IDs,
272 # but leafnode can't handle it
274 my @mids = ("<$long_hdr>", '<2mid@wtf>');
275 $for_leafnode->header_set('Message-ID', @mids);
276 $for_leafnode->body_set('not-a-dupe');
278 local $SIG{__WARN__} = sub { $warn .= join('', @_) };
279 $im->add($for_leafnode);
281 like($warn, qr/reused/, 'warned for reused MID');
282 $hdr = $n->head('<2mid@wtf>');
283 my @hmids = grep(/\AMessage-ID: /i, @$hdr);
284 is(scalar(@hmids), 1, 'Single Message-ID in header');
285 like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
289 # pipelined requests:
293 syswrite($s, "GROUP $group\r\n");
298 <$s> =~ /\A224 / or _exit(1);
299 <$s> =~ /\A1/ or _exit(2);
300 <$s> eq ".\r\n" or _exit(3);
304 <$s> =~ /\A220 / or _exit(4);
305 my $dig = Digest::SHA->new(1);
306 while (my $l = <$s>) {
307 last if $l eq ".\r\n";
310 $dig = $dig->hexdigest;
314 scalar(keys(%sums)) == 1 or _exit(5);
315 (values(%sums))[0] == $nart or _exit(6);
320 syswrite($s, "XOVER 1\r\n");
322 syswrite($s, "ARTICLE 1\r\n" x $nart);
323 is($rdr, waitpid($rdr, 0), 'reader done');
324 is($? >> 8, 0, 'no errors');
326 my $noerr = { 2 => \(my $null) };
328 if ($INC{'Search/Xapian.pm'} && ($ENV{TEST_RUN_MODE}//2)) {
329 skip 'Search/Xapian.pm pre-loaded (by t/run.perl?)', 1;
331 $lsof or skip 'lsof missing', 1;
332 my @of = xqx([$lsof, '-p', $td->{pid}], undef, $noerr);
333 skip('lsof broken', 1) if (!scalar(@of) || $?);
334 my @xap = grep m!Search/Xapian!, @of;
335 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
337 # -compact requires Xapian
339 require_mods('Search::Xapian', 2);
340 which('xapian-compact') or skip 'xapian-compact missing', 2;
341 is(xsys(qw(git config), "--file=$home/.public-inbox/config",
342 "publicinbox.$group.indexlevel", 'medium'),
343 0, 'upgraded indexlevel');
344 my $ex = eml_load('t/data/0001.patch');
345 is($n->article($ex->header('Message-ID')), undef,
346 'article did not exist');
349 ok(run_script([qw(-index --reindex -c), $ibx->{inboxdir}],
350 undef, $noerr), '-compacted');
351 select(undef, undef, undef, $fast_idle ? 0.1 : 2.1);
352 $art = $n->article($ex->header('Message-ID'));
353 ok($art, 'new article retrieved after compact');
354 $lsof or skip 'lsof missing', 1;
355 ($^O =~ /\A(?:linux)\z/) or
356 skip "lsof /(deleted)/ check untested on $^O", 1;
357 my @of = xqx([$lsof, '-p', $td->{pid}], undef, $noerr);
358 is(scalar(grep(/\(deleted\)/, @of)), 0, 'no deleted files');
361 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
362 syswrite($s, 'HDR List-id 1-');
363 select(undef, undef, undef, 0.15);
364 ok($td->kill, 'killed nntpd');
365 select(undef, undef, undef, 0.15);
366 syswrite($s, "\r\n");
369 sysread($s, $buf, 4096, length($buf));
370 } until ($buf =~ /\r\n\z/);
371 my @r = split("\r\n", $buf);
372 like($r[0], qr/^5\d\d /,
373 'got 5xx response for unoptimized HDR');
374 is(scalar @r, 1, 'only one response line');
378 is($?, 0, 'no error in exited process');
380 open my $fh, '<', $err or die "open $err failed: $!";
384 unlike($eout, qr/wide/i, 'no Wide character warnings');
394 sysread($s, $buf, 4096, length($buf));
395 } until ($buf =~ /\r\n\.\r\n\z/);