1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 foreach my $mod (qw(DBD::SQLite)) {
8 plan skip_all => "$mod missing for nntpd.t" if $@;
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
12 require PublicInbox::InboxWritable;
15 use Socket qw(IPPROTO_TCP TCP_NODELAY);
16 use File::Temp qw/tempdir/;
19 require './t/common.perl';
21 # FIXME: make easier to test both versions
22 my $version = $ENV{PI_TEST_VERSION} || 2;
23 require_git('2.6') if $version == 2;
25 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
26 my $home = "$tmpdir/pi-home";
27 my $err = "$tmpdir/stderr.log";
28 my $out = "$tmpdir/stdout.log";
29 my $mainrepo = "$tmpdir/main.git";
30 my $group = 'test-nntpd';
31 my $addr = $group . '@example.com';
32 my $nntpd = 'blib/script/public-inbox-nntpd';
33 my $init = 'blib/script/public-inbox-init';
35 skip "git 2.6+ required for V2Writable", 1 if $version == 1;
36 use_ok 'PublicInbox::V2Writable';
40 my $sock = tcp_server();
43 END { kill 'TERM', $pid if defined $pid };
46 mainrepo => $mainrepo,
49 -primary_address => $addr,
50 indexlevel => 'basic',
52 $ibx = PublicInbox::Inbox->new($ibx);
54 local $ENV{HOME} = $home;
55 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
56 push @cmd, "-V$version", '-Lbasic';
57 is(system(@cmd), 0, 'init OK');
58 is(system(qw(git config), "--file=$home/.public-inbox/config",
59 "publicinbox.$group.newsgroup", $group),
60 0, 'enabled newsgroup');
63 $ibx = PublicInbox::InboxWritable->new($ibx);
64 my $im = $ibx->importer;
66 # ensure successful message delivery
68 my $mime = Email::MIME->new(<<EOF);
69 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
70 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
72 Message-Id: <nntp\@example.com>
73 Content-Type: text/plain; charset=utf-8
74 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
75 Date: Thu, 01 Jan 1970 06:06:06 +0000
76 Content-Transfer-Encoding: 8bit
77 References: <ref tab squeezed>
79 This is a test message for El\xc3\xa9anor
83 $mime->header_set('List-Id', "<$list_id>");
84 $len = length($mime->as_string);
88 my $s = PublicInbox::SearchIdx->new($ibx, 1);
93 ok($sock, 'sock created');
94 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
95 $pid = spawn_listener(undef, $cmd, [ $sock ]);
96 ok(defined $pid, 'forked nntpd process successfully');
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);
115 require IO::Socket::SSL;
116 IO::Socket::SSL->VERSION(2.007);
117 } or skip('IO::Socket::SSL <2.007 not supported by Net::NNTP');
118 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
119 is($n->code, 580, 'got 580 code on server w/o TLS');
122 my $mid = '<nntp@example.com>';
124 'message-id' => $mid,
125 subject => "Testing for El\xc3\xa9anor",
126 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
127 'from' => "El\xc3\xa9anor <me\@example.com>",
128 'to' => "El\xc3\xa9anor <you\@example.com>",
130 'xref' => hostname . " $group:1",
131 'references' => '<reftabsqueezed>',
134 my $s = tcp_connect($sock);
135 sysread($s, my $buf, 4096);
136 is($buf, "201 " . hostname . " ready - post via email\r\n",
139 ok(syswrite($s, " \r\n"), 'wrote spaces');
140 ok(syswrite($s, "\r\n"), 'wrote nothing');
141 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
142 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
144 $s = tcp_connect($sock);
145 sysread($s, $buf, 4096);
146 is($buf, "201 " . hostname . " ready - post via email\r\n",
149 syswrite($s, "CAPABILITIES\r\n");
150 $buf = read_til_dot($s);
151 like($buf, qr/\r\nVERSION 2\r\n/s, 'CAPABILITIES works');
152 unlike($buf, qr/STARTTLS/s, 'STARTTLS not advertised');
153 my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
154 if (eval { require Compress::Raw::Zlib }) {
155 like($buf, $deflate_capa, 'DEFLATE advertised');
157 unlike($buf, $deflate_capa,
158 'DEFLATE not advertised (Compress::Raw::Zlib missing)');
161 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
162 $buf = read_til_dot($s);
163 like($buf, qr/\A231 list of /, 'newgroups OK');
165 while (my ($k, $v) = each %xhdr) {
166 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
167 "XHDR $k by message-id works");
168 is_deeply($n->xhdr("$k 1"), { 1 => $v },
169 "$k by article number works");
170 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
171 "$k by article range works");
173 syswrite($s, "HDR $k $mid\r\n");
174 $buf = read_til_dot($s);
175 my @r = split("\r\n", $buf);
176 like($r[0], qr/\A225 /, '225 response for HDR');
177 is($r[1], "0 $v", 'got expected response for HDR');
181 my $nogroup = Net::NNTP->new($host_port);
182 while (my ($k, $v) = each %xhdr) {
183 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
184 "$k by message-id works without group");
188 is_deeply($n->xover('1-'), {
189 '1' => ["Testing for El\xc3\xa9anor",
190 "El\xc3\xa9anor <me\@example.com>",
191 'Thu, 01 Jan 1970 06:06:06 +0000',
192 '<nntp@example.com>',
196 'Xref: '. hostname . ' test-nntpd:1'] },
197 "XOVER range works");
199 is_deeply($n->xover('1'), {
200 '1' => ["Testing for El\xc3\xa9anor",
201 "El\xc3\xa9anor <me\@example.com>",
202 'Thu, 01 Jan 1970 06:06:06 +0000',
203 '<nntp@example.com>',
207 'Xref: '. hostname . ' test-nntpd:1'] },
208 "XOVER by article works");
210 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
211 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
212 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
213 'body really matches');
214 my $art = $n->article(1);
215 is(ref($art), 'ARRAY', 'got array for ARTICLE');
216 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
217 is($n->article(999), undef, 'non-existent num');
218 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
221 syswrite($s, "OVER $mid\r\n");
222 $buf = read_til_dot($s);
223 my @r = split("\r\n", $buf);
224 like($r[0], qr/^224 /, 'got 224 response for OVER');
225 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
226 "El\xc3\xa9anor <me\@example.com>\t" .
227 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
228 "$mid\t<reftabsqueezed>\t$len\t1" .
229 "\tXref: " . hostname . " test-nntpd:0",
230 'OVER by Message-ID works');
231 is($r[2], '.', 'correctly terminated response');
234 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
236 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
237 'XHDR References 1- works)');
238 is_deeply($n->xhdr(qw(list-id 1-)), {},
239 'XHDR on invalid header returns empty');
241 my $mids = $n->newnews(0, '*');
242 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
247 ok($date >= $t0, 'valid date after start');
248 ok($date <= $t1, 'valid date before stop');
250 if ('leafnode interop') {
251 my $for_leafnode = PublicInbox::MIME->new(<<"");
252 From: longheader\@example.com
255 Date: Fri, 02 Oct 1993 00:00:00 +0000
257 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
258 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
259 $im->add($for_leafnode);
262 my $s = PublicInbox::SearchIdx->new($ibx, 1);
265 my $hdr = $n->head("<$long_hdr>");
266 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
267 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
268 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
270 # it's possible for v2 messages to have 2+ Message-IDs,
271 # but leafnode can't handle it
273 my @mids = ("<$long_hdr>", '<2mid@wtf>');
274 $for_leafnode->header_set('Message-ID', @mids);
275 $for_leafnode->body_set('not-a-dupe');
277 $SIG{__WARN__} = sub { $warn .= join('', @_) };
278 $im->add($for_leafnode);
280 like($warn, qr/reused/, 'warned for reused MID');
281 $hdr = $n->head('<2mid@wtf>');
282 my @hmids = grep(/\AMessage-ID: /i, @$hdr);
283 is(scalar(@hmids), 1, 'Single Message-ID in header');
284 like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
288 # pipelined requests:
291 syswrite($s, "GROUP $group\r\n");
297 <$s> =~ /\A224 / or _exit(1);
298 <$s> =~ /\A1/ or _exit(2);
299 <$s> eq ".\r\n" or _exit(3);
304 syswrite($s, "XOVER 1\r\n");
306 is($rdr, waitpid($rdr, 0), 'reader done');
307 is($? >> 8, 0, 'no errors');
310 my @of = `lsof -p $pid 2>/dev/null`;
311 skip('lsof broken', 1) if (!scalar(@of) || $?);
312 my @xap = grep m!Search/Xapian!, @of;
313 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
316 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
317 syswrite($s, 'HDR List-id 1-');
318 select(undef, undef, undef, 0.15);
319 ok(kill('TERM', $pid), 'killed nntpd');
320 select(undef, undef, undef, 0.15);
321 syswrite($s, "\r\n");
324 sysread($s, $buf, 4096, length($buf));
325 } until ($buf =~ /\r\n\z/);
326 my @r = split("\r\n", $buf);
327 like($r[0], qr/^5\d\d /,
328 'got 5xx response for unoptimized HDR');
329 is(scalar @r, 1, 'only one response line');
333 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
336 open my $fh, '<', $err or die "open $err failed: $!";
339 is($?, 0, 'no error in exited process');
340 unlike($eout, qr/wide/i, 'no Wide character warnings');
349 sysread($s, $buf, 4096, length($buf));
350 } until ($buf =~ /\r\n\.\r\n\z/);