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