]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
nntp: add support for CAPABILITIES command
[public-inbox.git] / t / nntpd.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 foreach my $mod (qw(DBD::SQLite)) {
7         eval "require $mod";
8         plan skip_all => "$mod missing for nntpd.t" if $@;
9 }
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
12 require PublicInbox::InboxWritable;
13 use Email::Simple;
14 use IO::Socket;
15 use Socket qw(IPPROTO_TCP TCP_NODELAY);
16 use File::Temp qw/tempdir/;
17 use Net::NNTP;
18 use Sys::Hostname;
19 require './t/common.perl';
20
21 # FIXME: make easier to test both versions
22 my $version = $ENV{PI_TEST_VERSION} || 2;
23 require_git('2.6') if $version == 2;
24
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';
34 SKIP: {
35         skip "git 2.6+ required for V2Writable", 1 if $version == 1;
36         use_ok 'PublicInbox::V2Writable';
37 }
38
39 my %opts;
40 my $sock = tcp_server();
41 my $pid;
42 my $len;
43 END { kill 'TERM', $pid if defined $pid };
44
45 my $ibx = {
46         mainrepo => $mainrepo,
47         name => $group,
48         version => $version,
49         -primary_address => $addr,
50         indexlevel => 'basic',
51 };
52 $ibx = PublicInbox::Inbox->new($ibx);
53 {
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');
61         my $len;
62
63         $ibx = PublicInbox::InboxWritable->new($ibx);
64         my $im = $ibx->importer;
65
66         # ensure successful message delivery
67         {
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>
71 Cc: $addr
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>
78
79 This is a test message for El\xc3\xa9anor
80 EOF
81                 my $list_id = $addr;
82                 $list_id =~ s/@/./;
83                 $mime->header_set('List-Id', "<$list_id>");
84                 $len = length($mime->as_string);
85                 $im->add($mime);
86                 $im->done;
87                 if ($version == 1) {
88                         my $s = PublicInbox::SearchIdx->new($ibx, 1);
89                         $s->index_sync;
90                 }
91         }
92
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);
99         my $list = $n->list;
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
104         SKIP: {
105                 $n->can('starttls') or
106                         skip('Net::NNTP too old to support STARTTLS', 2);
107                 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
108                 is($n->code, 580, 'got 580 code on server w/o TLS');
109         };
110
111         %opts = (
112                 PeerAddr => $host_port,
113                 Proto => 'tcp',
114                 Type => SOCK_STREAM,
115                 Timeout => 1,
116         );
117         my $mid = '<nntp@example.com>';
118         my %xhdr = (
119                 'message-id' => $mid,
120                 subject => "Testing for El\xc3\xa9anor",
121                 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
122                 'from' => "El\xc3\xa9anor <me\@example.com>",
123                 'to' => "El\xc3\xa9anor <you\@example.com>",
124                 'cc' => $addr,
125                 'xref' => hostname . " $group:1",
126                 'references' => '<reftabsqueezed>',
127         );
128
129         my $s = IO::Socket::INET->new(%opts);
130         sysread($s, my $buf, 4096);
131         is($buf, "201 " . hostname . " ready - post via email\r\n",
132                 'got greeting');
133         $s->autoflush(1);
134
135         ok(syswrite($s, "   \r\n"), 'wrote spaces');
136         ok(syswrite($s, "\r\n"), 'wrote nothing');
137         syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
138         is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
139
140         $s = IO::Socket::INET->new(%opts);
141         sysread($s, $buf, 4096);
142         is($buf, "201 " . hostname . " ready - post via email\r\n",
143                 'got greeting');
144         $s->autoflush(1);
145
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
151         syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
152         $buf = read_til_dot($s);
153         like($buf, qr/\A231 list of /, 'newgroups OK');
154
155         while (my ($k, $v) = each %xhdr) {
156                 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
157                           "XHDR $k by message-id works");
158                 is_deeply($n->xhdr("$k 1"), { 1 => $v },
159                           "$k by article number works");
160                 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
161                           "$k by article range works");
162                 $buf = '';
163                 syswrite($s, "HDR $k $mid\r\n");
164                 $buf = read_til_dot($s);
165                 my @r = split("\r\n", $buf);
166                 like($r[0], qr/\A225 /, '225 response for HDR');
167                 is($r[1], "0 $v", 'got expected response for HDR');
168         }
169
170         {
171                 my $nogroup = Net::NNTP->new($host_port);
172                 while (my ($k, $v) = each %xhdr) {
173                         is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
174                                   "$k by message-id works without group");
175                 }
176         }
177
178         is_deeply($n->xover('1-'), {
179                 '1' => ["Testing for El\xc3\xa9anor",
180                         "El\xc3\xa9anor <me\@example.com>",
181                         'Thu, 01 Jan 1970 06:06:06 +0000',
182                         '<nntp@example.com>',
183                         '<reftabsqueezed>',
184                         $len,
185                         '1',
186                         'Xref: '. hostname . ' test-nntpd:1'] },
187                 "XOVER range works");
188
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>',
194                         '<reftabsqueezed>',
195                         $len,
196                         '1',
197                         'Xref: '. hostname . ' test-nntpd:1'] },
198                 "XOVER by article works");
199
200         is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
201         is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
202         is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
203                 'body really matches');
204         my $art = $n->article(1);
205         is(ref($art), 'ARRAY', 'got array for ARTICLE');
206         is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
207         is($n->article(999), undef, 'non-existent num');
208         is($n->article('<non-existent@example>'), undef, 'non-existent mid');
209
210         {
211                 syswrite($s, "OVER $mid\r\n");
212                 $buf = read_til_dot($s);
213                 my @r = split("\r\n", $buf);
214                 like($r[0], qr/^224 /, 'got 224 response for OVER');
215                 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
216                         "El\xc3\xa9anor <me\@example.com>\t" .
217                         "Thu, 01 Jan 1970 06:06:06 +0000\t" .
218                         "$mid\t<reftabsqueezed>\t$len\t1" .
219                         "\tXref: " . hostname . " test-nntpd:0",
220                         'OVER by Message-ID works');
221                 is($r[2], '.', 'correctly terminated response');
222         }
223
224         is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
225                  'XHDR Cc 1- works');
226         is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
227                  'XHDR References 1- works)');
228         is_deeply($n->xhdr(qw(list-id 1-)), {},
229                  'XHDR on invalid header returns empty');
230
231         my $mids = $n->newnews(0, '*');
232         is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
233         {
234                 my $t0 = time;
235                 my $date = $n->date;
236                 my $t1 = time;
237                 ok($date >= $t0, 'valid date after start');
238                 ok($date <= $t1, 'valid date before stop');
239         }
240         if ('leafnode interop') {
241                 my $for_leafnode = PublicInbox::MIME->new(<<"");
242 From: longheader\@example.com
243 To: $addr
244 Subject: none
245 Date: Fri, 02 Oct 1993 00:00:00 +0000
246
247                 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
248                 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
249                 $im->add($for_leafnode);
250                 $im->done;
251                 if ($version == 1) {
252                         my $s = PublicInbox::SearchIdx->new($ibx, 1);
253                         $s->index_sync;
254                 }
255                 my $hdr = $n->head("<$long_hdr>");
256                 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
257                 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
258                 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
259
260                 # it's possible for v2 messages to have 2+ Message-IDs,
261                 # but leafnode can't handle it
262                 if ($version != 1) {
263                         my @mids = ("<$long_hdr>", '<2mid@wtf>');
264                         $for_leafnode->header_set('Message-ID', @mids);
265                         $for_leafnode->body_set('not-a-dupe');
266                         my $warn = '';
267                         $SIG{__WARN__} = sub { $warn .= join('', @_) };
268                         $im->add($for_leafnode);
269                         $im->done;
270                         like($warn, qr/reused/, 'warned for reused MID');
271                         $hdr = $n->head('<2mid@wtf>');
272                         my @hmids = grep(/\AMessage-ID: /i, @$hdr);
273                         is(scalar(@hmids), 1, 'Single Message-ID in header');
274                         like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
275                 }
276         }
277
278         # pipelined requests:
279         {
280                 my $nreq = 90;
281                 syswrite($s, "GROUP $group\r\n");
282                 my $res = <$s>;
283                 my $rdr = fork;
284                 if ($rdr == 0) {
285                         use POSIX qw(_exit);
286                         for (1..$nreq) {
287                                 <$s> =~ /\A224 / or _exit(1);
288                                 <$s> =~ /\A1/ or _exit(2);
289                                 <$s> eq ".\r\n" or _exit(3);
290                         }
291                         _exit(0);
292                 }
293                 for (1..$nreq) {
294                         syswrite($s, "XOVER 1\r\n");
295                 }
296                 is($rdr, waitpid($rdr, 0), 'reader done');
297                 is($? >> 8, 0, 'no errors');
298         }
299         SKIP: {
300                 my @of = `lsof -p $pid 2>/dev/null`;
301                 skip('lsof broken', 1) if (!scalar(@of) || $?);
302                 my @xap = grep m!Search/Xapian!, @of;
303                 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
304         }
305         {
306                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
307                 syswrite($s, 'HDR List-id 1-');
308                 select(undef, undef, undef, 0.15);
309                 ok(kill('TERM', $pid), 'killed nntpd');
310                 select(undef, undef, undef, 0.15);
311                 syswrite($s, "\r\n");
312                 $buf = '';
313                 do {
314                         sysread($s, $buf, 4096, length($buf));
315                 } until ($buf =~ /\r\n\z/);
316                 my @r = split("\r\n", $buf);
317                 like($r[0], qr/^5\d\d /,
318                         'got 5xx response for unoptimized HDR');
319                 is(scalar @r, 1, 'only one response line');
320         }
321
322         $n = $s = undef;
323         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
324         my $eout = eval {
325                 local $/;
326                 open my $fh, '<', $err or die "open $err failed: $!";
327                 <$fh>;
328         };
329         is($?, 0, 'no error in exited process');
330         unlike($eout, qr/wide/i, 'no Wide character warnings');
331 }
332
333 done_testing();
334
335 sub read_til_dot {
336         my ($s) = @_;
337         my $buf = '';
338         do {
339                 sysread($s, $buf, 4096, length($buf));
340         } until ($buf =~ /\r\n\.\r\n\z/);
341         $buf;
342 }
343
344 1;