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