1 # Copyright (C) 2015-2018 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 LocalAddr => '127.0.0.1',
46 my $sock = IO::Socket::INET->new(%opts);
49 END { kill 'TERM', $pid if defined $pid };
52 mainrepo => $mainrepo,
55 -primary_address => $addr,
56 indexlevel => 'basic',
58 $ibx = PublicInbox::Inbox->new($ibx);
60 local $ENV{HOME} = $home;
61 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
62 push @cmd, "-V$version", '-Lbasic';
63 is(system(@cmd), 0, 'init OK');
64 is(system(qw(git config), "--file=$home/.public-inbox/config",
65 "publicinbox.$group.newsgroup", $group),
66 0, 'enabled newsgroup');
69 $ibx = PublicInbox::InboxWritable->new($ibx);
70 my $im = $ibx->importer;
72 # ensure successful message delivery
74 my $mime = Email::MIME->new(<<EOF);
75 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
76 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
78 Message-Id: <nntp\@example.com>
79 Content-Type: text/plain; charset=utf-8
80 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
81 Date: Thu, 01 Jan 1970 06:06:06 +0000
82 Content-Transfer-Encoding: 8bit
83 References: <ref tab squeezed>
85 This is a test message for El\xc3\xa9anor
89 $mime->header_set('List-Id', "<$list_id>");
90 $len = length($mime->as_string);
94 my $s = PublicInbox::SearchIdx->new($ibx, 1);
99 ok($sock, 'sock created');
100 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
101 $pid = spawn_listener(undef, $cmd, [ $sock ]);
102 ok(defined $pid, 'forked nntpd process successfully');
103 my $host_port = $sock->sockhost . ':' . $sock->sockport;
104 my $n = Net::NNTP->new($host_port);
106 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
107 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
108 is_deeply($n->listgroup($group), [1], 'listgroup OK');
109 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
110 is($n->code, 580, 'got 580 code on server w/o TLS');
113 PeerAddr => $host_port,
118 my $mid = '<nntp@example.com>';
120 'message-id' => $mid,
121 subject => "Testing for El\xc3\xa9anor",
122 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
123 'from' => "El\xc3\xa9anor <me\@example.com>",
124 'to' => "El\xc3\xa9anor <you\@example.com>",
126 'xref' => hostname . " $group:1",
127 'references' => '<reftabsqueezed>',
130 my $s = IO::Socket::INET->new(%opts);
131 sysread($s, my $buf, 4096);
132 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 = IO::Socket::INET->new(%opts);
142 sysread($s, $buf, 4096);
143 is($buf, "201 " . hostname . " ready - post via email\r\n",
147 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
148 $buf = read_til_dot($s);
149 like($buf, qr/\A231 list of /, 'newgroups OK');
151 while (my ($k, $v) = each %xhdr) {
152 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
153 "XHDR $k by message-id works");
154 is_deeply($n->xhdr("$k 1"), { 1 => $v },
155 "$k by article number works");
156 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
157 "$k by article range works");
159 syswrite($s, "HDR $k $mid\r\n");
160 $buf = read_til_dot($s);
161 my @r = split("\r\n", $buf);
162 like($r[0], qr/\A225 /, '225 response for HDR');
163 is($r[1], "0 $v", 'got expected response for HDR');
167 my $nogroup = Net::NNTP->new($host_port);
168 while (my ($k, $v) = each %xhdr) {
169 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
170 "$k by message-id works without group");
174 is_deeply($n->xover('1-'), {
175 '1' => ["Testing for El\xc3\xa9anor",
176 "El\xc3\xa9anor <me\@example.com>",
177 'Thu, 01 Jan 1970 06:06:06 +0000',
178 '<nntp@example.com>',
182 'Xref: '. hostname . ' test-nntpd:1'] },
183 "XOVER range works");
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 by article works");
196 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
197 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
198 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
199 'body really matches');
200 my $art = $n->article(1);
201 is(ref($art), 'ARRAY', 'got array for ARTICLE');
202 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
203 is($n->article(999), undef, 'non-existent num');
204 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
207 syswrite($s, "OVER $mid\r\n");
208 $buf = read_til_dot($s);
209 my @r = split("\r\n", $buf);
210 like($r[0], qr/^224 /, 'got 224 response for OVER');
211 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
212 "El\xc3\xa9anor <me\@example.com>\t" .
213 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
214 "$mid\t<reftabsqueezed>\t$len\t1" .
215 "\tXref: " . hostname . " test-nntpd:0",
216 'OVER by Message-ID works');
217 is($r[2], '.', 'correctly terminated response');
220 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
222 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
223 'XHDR References 1- works)');
224 is_deeply($n->xhdr(qw(list-id 1-)), {},
225 'XHDR on invalid header returns empty');
227 my $mids = $n->newnews(0, '*');
228 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
233 ok($date >= $t0, 'valid date after start');
234 ok($date <= $t1, 'valid date before stop');
236 if ('leafnode interop') {
237 my $for_leafnode = PublicInbox::MIME->new(<<"");
238 From: longheader\@example.com
241 Date: Fri, 02 Oct 1993 00:00:00 +0000
243 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
244 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
245 $im->add($for_leafnode);
248 my $s = PublicInbox::SearchIdx->new($ibx, 1);
251 my $hdr = $n->head("<$long_hdr>");
252 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
253 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
254 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
256 # it's possible for v2 messages to have 2+ Message-IDs,
257 # but leafnode can't handle it
259 my @mids = ("<$long_hdr>", '<2mid@wtf>');
260 $for_leafnode->header_set('Message-ID', @mids);
261 $for_leafnode->body_set('not-a-dupe');
263 $SIG{__WARN__} = sub { $warn .= join('', @_) };
264 $im->add($for_leafnode);
266 like($warn, qr/reused/, 'warned for reused MID');
267 $hdr = $n->head('<2mid@wtf>');
268 my @hmids = grep(/\AMessage-ID: /i, @$hdr);
269 is(scalar(@hmids), 1, 'Single Message-ID in header');
270 like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
274 # pipelined requests:
277 syswrite($s, "GROUP $group\r\n");
283 <$s> =~ /\A224 / or _exit(1);
284 <$s> =~ /\A1/ or _exit(2);
285 <$s> eq ".\r\n" or _exit(3);
290 syswrite($s, "XOVER 1\r\n");
292 is($rdr, waitpid($rdr, 0), 'reader done');
293 is($? >> 8, 0, 'no errors');
296 my @of = `lsof -p $pid 2>/dev/null`;
297 skip('lsof broken', 1) if (!scalar(@of) || $?);
298 my @xap = grep m!Search/Xapian!, @of;
299 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
302 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
303 syswrite($s, 'HDR List-id 1-');
304 select(undef, undef, undef, 0.15);
305 ok(kill('TERM', $pid), 'killed nntpd');
306 select(undef, undef, undef, 0.15);
307 syswrite($s, "\r\n");
310 sysread($s, $buf, 4096, length($buf));
311 } until ($buf =~ /\r\n\z/);
312 my @r = split("\r\n", $buf);
313 like($r[0], qr/^5\d\d /,
314 'got 5xx response for unoptimized HDR');
315 is(scalar @r, 1, 'only one response line');
319 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
322 open my $fh, '<', $err or die "open $err failed: $!";
325 is($?, 0, 'no error in exited process');
326 unlike($eout, qr/wide/i, 'no Wide character warnings');
335 sysread($s, $buf, 4096, length($buf));
336 } until ($buf =~ /\r\n\.\r\n\z/);