]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
t/nntpd: fix lsof check w/ TEST_RUN_MODE=0
[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::SearchIdx;
10 require PublicInbox::Msgmap;
11 require PublicInbox::InboxWritable;
12 use Email::Simple;
13 use IO::Socket;
14 use Socket qw(IPPROTO_TCP TCP_NODELAY);
15 use Net::NNTP;
16 use Sys::Hostname;
17
18 # FIXME: make easier to test both versions
19 my $version = $ENV{PI_TEST_VERSION} || 2;
20 require_git('2.6') if $version == 2;
21
22 my ($tmpdir, $for_destroy) = tmpdir();
23 my $home = "$tmpdir/pi-home";
24 my $err = "$tmpdir/stderr.log";
25 my $out = "$tmpdir/stdout.log";
26 my $inboxdir = "$tmpdir/main.git";
27 my $group = 'test-nntpd';
28 my $addr = $group . '@example.com';
29 SKIP: {
30         skip "git 2.6+ required for V2Writable", 1 if $version == 1;
31         use_ok 'PublicInbox::V2Writable';
32 }
33
34 my %opts;
35 my $sock = tcp_server();
36 my $td;
37 my $len;
38
39 my $ibx = {
40         inboxdir => $inboxdir,
41         name => $group,
42         version => $version,
43         -primary_address => $addr,
44         indexlevel => 'basic',
45 };
46 $ibx = PublicInbox::Inbox->new($ibx);
47 {
48         local $ENV{HOME} = $home;
49         my @cmd = ('-init', $group, $inboxdir, 'http://example.com/', $addr);
50         push @cmd, "-V$version", '-Lbasic';
51         ok(run_script(\@cmd), 'init OK');
52         is(xsys(qw(git config), "--file=$home/.public-inbox/config",
53                         "publicinbox.$group.newsgroup", $group),
54                 0, 'enabled newsgroup');
55         my $len;
56
57         $ibx = PublicInbox::InboxWritable->new($ibx);
58         my $im = $ibx->importer;
59
60         # ensure successful message delivery
61         {
62                 my $mime = Email::MIME->new(<<EOF);
63 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
64 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
65 Cc: $addr
66 Message-Id: <nntp\@example.com>
67 Content-Type: text/plain; charset=utf-8
68 Subject: Testing for    =?utf-8?Q?El=C3=A9anor?=
69 Date: Thu, 01 Jan 1970 06:06:06 +0000
70 Content-Transfer-Encoding: 8bit
71 References: <ref        tab     squeezed>
72
73 This is a test message for El\xc3\xa9anor
74 EOF
75                 my $list_id = $addr;
76                 $list_id =~ s/@/./;
77                 $mime->header_set('List-Id', "<$list_id>");
78                 $len = length($mime->as_string);
79                 $im->add($mime);
80                 $im->done;
81                 if ($version == 1) {
82                         my $s = PublicInbox::SearchIdx->new($ibx, 1);
83                         $s->index_sync;
84                 }
85         }
86
87         ok($sock, 'sock created');
88         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
89         $td = start_script($cmd, undef, { 3 => $sock });
90         my $host_port = $sock->sockhost . ':' . $sock->sockport;
91         my $n = Net::NNTP->new($host_port);
92         my $list = $n->list;
93         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
94         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
95         is_deeply($n->listgroup($group), [1], 'listgroup OK');
96         # TODO: Net::NNTP::listgroup does not support range at the moment
97
98         {
99                 my $expect = [ qw(Subject: From: Date: Message-ID:
100                                 References: Bytes: Lines: Xref:full) ];
101                 is_deeply($n->overview_fmt, $expect,
102                         'RFC3977 8.4.2 compliant LIST OVERVIEW.FMT');
103         }
104         SKIP: {
105                 $n->can('starttls') or
106                         skip('Net::NNTP too old to support STARTTLS', 2);
107                 require_mods('IO::Socket::SSL', 2);
108                 eval {
109                         IO::Socket::SSL->VERSION(2.007);
110                 } or skip(<<EOF, 2);
111 IO::Socket::SSL <2.007 not supported by Net::NNTP
112 EOF
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                         local $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                 if ($INC{'Search/Xapian.pm'} && ($ENV{TEST_RUN_MODE}//2)) {
306                         skip 'Search/Xapian.pm pre-loaded (by t/run.perl?)', 1;
307                 }
308                 my $lsof = which('lsof') or skip 'lsof missing', 1;
309                 my $rdr = { 2 => \(my $null) };
310                 my @of = xqx([$lsof, '-p', $td->{pid}], undef, $rdr);
311                 skip('lsof broken', 1) if (!scalar(@of) || $?);
312                 my @xap = grep m!Search/Xapian!, @of;
313                 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
314         }
315         {
316                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
317                 syswrite($s, 'HDR List-id 1-');
318                 select(undef, undef, undef, 0.15);
319                 ok($td->kill, 'killed nntpd');
320                 select(undef, undef, undef, 0.15);
321                 syswrite($s, "\r\n");
322                 $buf = '';
323                 do {
324                         sysread($s, $buf, 4096, length($buf));
325                 } until ($buf =~ /\r\n\z/);
326                 my @r = split("\r\n", $buf);
327                 like($r[0], qr/^5\d\d /,
328                         'got 5xx response for unoptimized HDR');
329                 is(scalar @r, 1, 'only one response line');
330         }
331
332         $n = $s = undef;
333         $td->join;
334         my $eout = eval {
335                 local $/;
336                 open my $fh, '<', $err or die "open $err failed: $!";
337                 <$fh>;
338         };
339         is($?, 0, 'no error in exited process');
340         unlike($eout, qr/wide/i, 'no Wide character warnings');
341 }
342
343 $td = undef;
344 done_testing();
345
346 sub read_til_dot {
347         my ($s) = @_;
348         my $buf = '';
349         do {
350                 sysread($s, $buf, 4096, length($buf));
351         } until ($buf =~ /\r\n\.\r\n\z/);
352         $buf;
353 }
354
355 1;