]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
30f3fb9c761863b20760b0557c590b87e1ea480a
[public-inbox.git] / t / nntpd.t
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>
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 Net::NNTP;
17 use Sys::Hostname;
18 use PublicInbox::TestCommon;
19
20 # FIXME: make easier to test both versions
21 my $version = $ENV{PI_TEST_VERSION} || 2;
22 require_git('2.6') if $version == 2;
23
24 my ($tmpdir, $for_destroy) = tmpdir();
25 my $home = "$tmpdir/pi-home";
26 my $err = "$tmpdir/stderr.log";
27 my $out = "$tmpdir/stdout.log";
28 my $inboxdir = "$tmpdir/main.git";
29 my $group = 'test-nntpd';
30 my $addr = $group . '@example.com';
31 SKIP: {
32         skip "git 2.6+ required for V2Writable", 1 if $version == 1;
33         use_ok 'PublicInbox::V2Writable';
34 }
35
36 my %opts;
37 my $sock = tcp_server();
38 my $td;
39 my $len;
40
41 my $ibx = {
42         inboxdir => $inboxdir,
43         name => $group,
44         version => $version,
45         -primary_address => $addr,
46         indexlevel => 'basic',
47 };
48 $ibx = PublicInbox::Inbox->new($ibx);
49 {
50         local $ENV{HOME} = $home;
51         my @cmd = ('-init', $group, $inboxdir, 'http://example.com/', $addr);
52         push @cmd, "-V$version", '-Lbasic';
53         ok(run_script(\@cmd), 'init OK');
54         is(system(qw(git config), "--file=$home/.public-inbox/config",
55                         "publicinbox.$group.newsgroup", $group),
56                 0, 'enabled newsgroup');
57         my $len;
58
59         $ibx = PublicInbox::InboxWritable->new($ibx);
60         my $im = $ibx->importer;
61
62         # ensure successful message delivery
63         {
64                 my $mime = Email::MIME->new(<<EOF);
65 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
66 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
67 Cc: $addr
68 Message-Id: <nntp\@example.com>
69 Content-Type: text/plain; charset=utf-8
70 Subject: Testing for    =?utf-8?Q?El=C3=A9anor?=
71 Date: Thu, 01 Jan 1970 06:06:06 +0000
72 Content-Transfer-Encoding: 8bit
73 References: <ref        tab     squeezed>
74
75 This is a test message for El\xc3\xa9anor
76 EOF
77                 my $list_id = $addr;
78                 $list_id =~ s/@/./;
79                 $mime->header_set('List-Id', "<$list_id>");
80                 $len = length($mime->as_string);
81                 $im->add($mime);
82                 $im->done;
83                 if ($version == 1) {
84                         my $s = PublicInbox::SearchIdx->new($ibx, 1);
85                         $s->index_sync;
86                 }
87         }
88
89         ok($sock, 'sock created');
90         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
91         $td = start_script($cmd, undef, { 3 => $sock });
92         my $host_port = $sock->sockhost . ':' . $sock->sockport;
93         my $n = Net::NNTP->new($host_port);
94         my $list = $n->list;
95         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
96         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
97         is_deeply($n->listgroup($group), [1], 'listgroup OK');
98         # TODO: Net::NNTP::listgroup does not support range at the moment
99
100         {
101                 my $expect = [ qw(Subject: From: Date: Message-ID:
102                                 References: Bytes: Lines: Xref:full) ];
103                 is_deeply($n->overview_fmt, $expect,
104                         'RFC3977 8.4.2 compliant LIST OVERVIEW.FMT');
105         }
106         SKIP: {
107                 $n->can('starttls') or
108                         skip('Net::NNTP too old to support STARTTLS', 2);
109                 eval {
110                         require IO::Socket::SSL;
111                         IO::Socket::SSL->VERSION(2.007);
112                 } or skip('IO::Socket::SSL <2.007 not supported by Net::NNTP');
113                 ok(!$n->starttls, 'STARTTLS fails when unconfigured');
114                 is($n->code, 580, 'got 580 code on server w/o TLS');
115         };
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 = tcp_connect($sock);
130         sysread($s, my $buf, 4096);
131         is($buf, "201 " . hostname . " ready - post via email\r\n",
132                 'got greeting');
133
134         ok(syswrite($s, "   \r\n"), 'wrote spaces');
135         ok(syswrite($s, "\r\n"), 'wrote nothing');
136         syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
137         is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
138
139         $s = tcp_connect($sock);
140         sysread($s, $buf, 4096);
141         is($buf, "201 " . hostname . " ready - post via email\r\n",
142                 'got greeting');
143
144         syswrite($s, "CAPABILITIES\r\n");
145         $buf = read_til_dot($s);
146         like($buf, qr/\r\nVERSION 2\r\n/s, 'CAPABILITIES works');
147         unlike($buf, qr/STARTTLS/s, 'STARTTLS not advertised');
148         my $deflate_capa = qr/\r\nCOMPRESS DEFLATE\r\n/;
149         if (eval { require Compress::Raw::Zlib }) {
150                 like($buf, $deflate_capa, 'DEFLATE advertised');
151         } else {
152                 unlike($buf, $deflate_capa,
153                         'DEFLATE not advertised (Compress::Raw::Zlib missing)');
154         }
155
156         syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
157         $buf = read_til_dot($s);
158         like($buf, qr/\A231 list of /, 'newgroups OK');
159
160         while (my ($k, $v) = each %xhdr) {
161                 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
162                           "XHDR $k by message-id works");
163                 is_deeply($n->xhdr("$k 1"), { 1 => $v },
164                           "$k by article number works");
165                 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
166                           "$k by article range works");
167                 $buf = '';
168                 syswrite($s, "HDR $k $mid\r\n");
169                 $buf = read_til_dot($s);
170                 my @r = split("\r\n", $buf);
171                 like($r[0], qr/\A225 /, '225 response for HDR');
172                 is($r[1], "0 $v", 'got expected response for HDR');
173         }
174
175         {
176                 my $nogroup = Net::NNTP->new($host_port);
177                 while (my ($k, $v) = each %xhdr) {
178                         is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
179                                   "$k by message-id works without group");
180                 }
181         }
182
183         is_deeply($n->xover('1-'), {
184                 '1' => ["Testing for El\xc3\xa9anor",
185                         "El\xc3\xa9anor <me\@example.com>",
186                         'Thu, 01 Jan 1970 06:06:06 +0000',
187                         '<nntp@example.com>',
188                         '<reftabsqueezed>',
189                         $len,
190                         '1',
191                         'Xref: '. hostname . ' test-nntpd:1'] },
192                 "XOVER range works");
193
194         is_deeply($n->xover('1'), {
195                 '1' => ["Testing for El\xc3\xa9anor",
196                         "El\xc3\xa9anor <me\@example.com>",
197                         'Thu, 01 Jan 1970 06:06:06 +0000',
198                         '<nntp@example.com>',
199                         '<reftabsqueezed>',
200                         $len,
201                         '1',
202                         'Xref: '. hostname . ' test-nntpd:1'] },
203                 "XOVER by article works");
204
205         is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
206         is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
207         is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
208                 'body really matches');
209         my $art = $n->article(1);
210         is(ref($art), 'ARRAY', 'got array for ARTICLE');
211         is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
212         is($n->article(999), undef, 'non-existent num');
213         is($n->article('<non-existent@example>'), undef, 'non-existent mid');
214
215         {
216                 syswrite($s, "OVER $mid\r\n");
217                 $buf = read_til_dot($s);
218                 my @r = split("\r\n", $buf);
219                 like($r[0], qr/^224 /, 'got 224 response for OVER');
220                 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
221                         "El\xc3\xa9anor <me\@example.com>\t" .
222                         "Thu, 01 Jan 1970 06:06:06 +0000\t" .
223                         "$mid\t<reftabsqueezed>\t$len\t1" .
224                         "\tXref: " . hostname . " test-nntpd:0",
225                         'OVER by Message-ID works');
226                 is($r[2], '.', 'correctly terminated response');
227         }
228
229         is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
230                  'XHDR Cc 1- works');
231         is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
232                  'XHDR References 1- works)');
233         is_deeply($n->xhdr(qw(list-id 1-)), {},
234                  'XHDR on invalid header returns empty');
235
236         my $mids = $n->newnews(0, '*');
237         is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
238         {
239                 my $t0 = time;
240                 my $date = $n->date;
241                 my $t1 = time;
242                 ok($date >= $t0, 'valid date after start');
243                 ok($date <= $t1, 'valid date before stop');
244         }
245         if ('leafnode interop') {
246                 my $for_leafnode = PublicInbox::MIME->new(<<"");
247 From: longheader\@example.com
248 To: $addr
249 Subject: none
250 Date: Fri, 02 Oct 1993 00:00:00 +0000
251
252                 my $long_hdr = 'for-leafnode-'.('y'x200).'@example.com';
253                 $for_leafnode->header_set('Message-ID', "<$long_hdr>");
254                 $im->add($for_leafnode);
255                 $im->done;
256                 if ($version == 1) {
257                         my $s = PublicInbox::SearchIdx->new($ibx, 1);
258                         $s->index_sync;
259                 }
260                 my $hdr = $n->head("<$long_hdr>");
261                 my $expect = qr/\AMessage-ID: /i . qr/\Q<$long_hdr>\E/;
262                 ok(scalar(grep(/$expect/, @$hdr)), 'Message-ID not folded');
263                 ok(scalar(grep(/^Path:/, @$hdr)), 'Path: header found');
264
265                 # it's possible for v2 messages to have 2+ Message-IDs,
266                 # but leafnode can't handle it
267                 if ($version != 1) {
268                         my @mids = ("<$long_hdr>", '<2mid@wtf>');
269                         $for_leafnode->header_set('Message-ID', @mids);
270                         $for_leafnode->body_set('not-a-dupe');
271                         my $warn = '';
272                         $SIG{__WARN__} = sub { $warn .= join('', @_) };
273                         $im->add($for_leafnode);
274                         $im->done;
275                         like($warn, qr/reused/, 'warned for reused MID');
276                         $hdr = $n->head('<2mid@wtf>');
277                         my @hmids = grep(/\AMessage-ID: /i, @$hdr);
278                         is(scalar(@hmids), 1, 'Single Message-ID in header');
279                         like($hmids[0], qr/: <2mid\@wtf>/, 'got expected mid');
280                 }
281         }
282
283         # pipelined requests:
284         {
285                 my $nreq = 90;
286                 syswrite($s, "GROUP $group\r\n");
287                 my $res = <$s>;
288                 my $rdr = fork;
289                 if ($rdr == 0) {
290                         use POSIX qw(_exit);
291                         for (1..$nreq) {
292                                 <$s> =~ /\A224 / or _exit(1);
293                                 <$s> =~ /\A1/ or _exit(2);
294                                 <$s> eq ".\r\n" or _exit(3);
295                         }
296                         _exit(0);
297                 }
298                 for (1..$nreq) {
299                         syswrite($s, "XOVER 1\r\n");
300                 }
301                 is($rdr, waitpid($rdr, 0), 'reader done');
302                 is($? >> 8, 0, 'no errors');
303         }
304         SKIP: {
305                 my @of = `lsof -p $td->{pid} 2>/dev/null`;
306                 skip('lsof broken', 1) if (!scalar(@of) || $?);
307                 my @xap = grep m!Search/Xapian!, @of;
308                 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
309         }
310         {
311                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
312                 syswrite($s, 'HDR List-id 1-');
313                 select(undef, undef, undef, 0.15);
314                 ok($td->kill, 'killed nntpd');
315                 select(undef, undef, undef, 0.15);
316                 syswrite($s, "\r\n");
317                 $buf = '';
318                 do {
319                         sysread($s, $buf, 4096, length($buf));
320                 } until ($buf =~ /\r\n\z/);
321                 my @r = split("\r\n", $buf);
322                 like($r[0], qr/^5\d\d /,
323                         'got 5xx response for unoptimized HDR');
324                 is(scalar @r, 1, 'only one response line');
325         }
326
327         $n = $s = undef;
328         $td->join;
329         my $eout = eval {
330                 local $/;
331                 open my $fh, '<', $err or die "open $err failed: $!";
332                 <$fh>;
333         };
334         is($?, 0, 'no error in exited process');
335         unlike($eout, qr/wide/i, 'no Wide character warnings');
336 }
337
338 $td = undef;
339 done_testing();
340
341 sub read_til_dot {
342         my ($s) = @_;
343         my $buf = '';
344         do {
345                 sysread($s, $buf, 4096, length($buf));
346         } until ($buf =~ /\r\n\.\r\n\z/);
347         $buf;
348 }
349
350 1;