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