]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
t: avoid "subtest" for Perl 5.10.1 compatibility
[public-inbox.git] / t / nntpd.t
1 # Copyright (C) 2015-2018 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 File::Temp qw/tempdir/;
17 use Net::NNTP;
18 use Sys::Hostname;
19 require './t/common.perl';
20
21 # FIXME: make easier to test both versions
22 my $version = $ENV{PI_TEST_VERSION} || 2;
23 require_git('2.6') if $version == 2;
24
25 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
26 my $home = "$tmpdir/pi-home";
27 my $err = "$tmpdir/stderr.log";
28 my $out = "$tmpdir/stdout.log";
29 my $mainrepo = "$tmpdir/main.git";
30 my $group = 'test-nntpd';
31 my $addr = $group . '@example.com';
32 my $nntpd = 'blib/script/public-inbox-nntpd';
33 my $init = 'blib/script/public-inbox-init';
34 SKIP: {
35         skip "git 2.6+ required for V2Writable", 1 if $version == 1;
36         use_ok 'PublicInbox::V2Writable';
37 }
38
39 my %opts = (
40         LocalAddr => '127.0.0.1',
41         ReuseAddr => 1,
42         Proto => 'tcp',
43         Type => SOCK_STREAM,
44         Listen => 1024,
45 );
46 my $sock = IO::Socket::INET->new(%opts);
47 my $pid;
48 my $len;
49 END { kill 'TERM', $pid if defined $pid };
50
51 my $ibx = {
52         mainrepo => $mainrepo,
53         name => $group,
54         version => $version,
55         -primary_address => $addr,
56         indexlevel => 'basic',
57 };
58 $ibx = PublicInbox::Inbox->new($ibx);
59 {
60         local $ENV{HOME} = $home;
61         my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
62         push @cmd, "-V$version", '-Lbasic';
63         is(system(@cmd), 0, 'init OK');
64         is(system(qw(git config), "--file=$home/.public-inbox/config",
65                         "publicinbox.$group.newsgroup", $group),
66                 0, 'enabled newsgroup');
67         my $len;
68
69         $ibx = PublicInbox::InboxWritable->new($ibx);
70         my $im = $ibx->importer;
71
72         # ensure successful message delivery
73         {
74                 my $mime = Email::MIME->new(<<EOF);
75 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
76 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
77 Cc: $addr
78 Message-Id: <nntp\@example.com>
79 Content-Type: text/plain; charset=utf-8
80 Subject: Testing for    =?utf-8?Q?El=C3=A9anor?=
81 Date: Thu, 01 Jan 1970 06:06:06 +0000
82 Content-Transfer-Encoding: 8bit
83 References: <ref        tab     squeezed>
84
85 This is a test message for El\xc3\xa9anor
86 EOF
87                 my $list_id = $addr;
88                 $list_id =~ s/@/./;
89                 $mime->header_set('List-Id', "<$list_id>");
90                 $len = length($mime->as_string);
91                 $im->add($mime);
92                 $im->done;
93                 if ($version == 1) {
94                         my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
95                         $s->index_sync;
96                 }
97         }
98
99         ok($sock, 'sock created');
100         my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
101         $pid = spawn_listener(undef, $cmd, [ $sock ]);
102         ok(defined $pid, 'forked nntpd process successfully');
103         my $host_port = $sock->sockhost . ':' . $sock->sockport;
104         my $n = Net::NNTP->new($host_port);
105         my $list = $n->list;
106         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
107         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
108         is_deeply($n->listgroup($group), [1], 'listgroup OK');
109
110         %opts = (
111                 PeerAddr => $host_port,
112                 Proto => 'tcp',
113                 Type => SOCK_STREAM,
114                 Timeout => 1,
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 = IO::Socket::INET->new(%opts);
129         sysread($s, my $buf, 4096);
130         is($buf, "201 " . hostname . " ready - post via email\r\n",
131                 'got greeting');
132         $s->autoflush(1);
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 = IO::Socket::INET->new(%opts);
140         sysread($s, $buf, 4096);
141         is($buf, "201 " . hostname . " ready - post via email\r\n",
142                 'got greeting');
143         $s->autoflush(1);
144
145         syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
146         $buf = read_til_dot($s);
147         like($buf, qr/\A231 list of /, 'newgroups OK');
148
149         while (my ($k, $v) = each %xhdr) {
150                 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
151                           "XHDR $k by message-id works");
152                 is_deeply($n->xhdr("$k 1"), { 1 => $v },
153                           "$k by article number works");
154                 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
155                           "$k by article range works");
156                 $buf = '';
157                 syswrite($s, "HDR $k $mid\r\n");
158                 $buf = read_til_dot($s);
159                 my @r = split("\r\n", $buf);
160                 like($r[0], qr/\A225 /, '225 response for HDR');
161                 is($r[1], "0 $v", 'got expected response for HDR');
162         }
163
164         {
165                 my $nogroup = Net::NNTP->new($host_port);
166                 while (my ($k, $v) = each %xhdr) {
167                         is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
168                                   "$k by message-id works without group");
169                 }
170         }
171
172         is_deeply($n->xover('1-'), {
173                 '1' => ["Testing for El\xc3\xa9anor",
174                         "El\xc3\xa9anor <me\@example.com>",
175                         'Thu, 01 Jan 1970 06:06:06 +0000',
176                         '<nntp@example.com>',
177                         '<reftabsqueezed>',
178                         $len,
179                         '1',
180                         'Xref: '. hostname . ' test-nntpd:1'] },
181                 "XOVER range works");
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 by article works");
193
194         is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
195         is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
196         is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
197                 'body really matches');
198         my $art = $n->article(1);
199         is(ref($art), 'ARRAY', 'got array for ARTICLE');
200         is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
201         is($n->article(999), undef, 'non-existent num');
202         is($n->article('<non-existent@example>'), undef, 'non-existent mid');
203
204         {
205                 syswrite($s, "OVER $mid\r\n");
206                 $buf = read_til_dot($s);
207                 my @r = split("\r\n", $buf);
208                 like($r[0], qr/^224 /, 'got 224 response for OVER');
209                 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
210                         "El\xc3\xa9anor <me\@example.com>\t" .
211                         "Thu, 01 Jan 1970 06:06:06 +0000\t" .
212                         "$mid\t<reftabsqueezed>\t$len\t1" .
213                         "\tXref: " . hostname . " test-nntpd:0",
214                         'OVER by Message-ID works');
215                 is($r[2], '.', 'correctly terminated response');
216         }
217
218         is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
219                  'XHDR Cc 1- works');
220         is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
221                  'XHDR References 1- works)');
222         is_deeply($n->xhdr(qw(list-id 1-)), {},
223                  'XHDR on invalid header returns empty');
224
225         my $mids = $n->newnews(0, '*');
226         is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
227         {
228                 my $t0 = time;
229                 my $date = $n->date;
230                 my $t1 = time;
231                 ok($date >= $t0, 'valid date after start');
232                 ok($date <= $t1, 'valid date before stop');
233         }
234
235         # pipelined requests:
236         {
237                 my $nreq = 90;
238                 syswrite($s, "GROUP $group\r\n");
239                 my $res = <$s>;
240                 my $rdr = fork;
241                 if ($rdr == 0) {
242                         use POSIX qw(_exit);
243                         for (1..$nreq) {
244                                 <$s> =~ /\A224 / or _exit(1);
245                                 <$s> =~ /\A1/ or _exit(2);
246                                 <$s> eq ".\r\n" or _exit(3);
247                         }
248                         _exit(0);
249                 }
250                 for (1..$nreq) {
251                         syswrite($s, "XOVER 1\r\n");
252                 }
253                 is($rdr, waitpid($rdr, 0), 'reader done');
254                 is($? >> 8, 0, 'no errors');
255         }
256         SKIP: {
257                 my @of = `lsof -p $pid 2>/dev/null`;
258                 skip('lsof broken', 1) if (!scalar(@of) || $?);
259                 my @xap = grep m!Search/Xapian!, @of;
260                 is_deeply(\@xap, [], 'Xapian not loaded in nntpd');
261         }
262         {
263                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
264                 syswrite($s, 'HDR List-id 1-');
265                 select(undef, undef, undef, 0.15);
266                 ok(kill('TERM', $pid), 'killed nntpd');
267                 select(undef, undef, undef, 0.15);
268                 syswrite($s, "\r\n");
269                 $buf = '';
270                 do {
271                         sysread($s, $buf, 4096, length($buf));
272                 } until ($buf =~ /\r\n\z/);
273                 my @r = split("\r\n", $buf);
274                 like($r[0], qr/^5\d\d /,
275                         'got 5xx response for unoptimized HDR');
276                 is(scalar @r, 1, 'only one response line');
277         }
278
279         $n = $s = undef;
280         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
281         my $eout = eval {
282                 local $/;
283                 open my $fh, '<', $err or die "open $err failed: $!";
284                 <$fh>;
285         };
286         is($?, 0, 'no error in exited process');
287         unlike($eout, qr/wide/i, 'no Wide character warnings');
288 }
289
290 done_testing();
291
292 sub read_til_dot {
293         my ($s) = @_;
294         my $buf = '';
295         do {
296                 sysread($s, $buf, 4096, length($buf));
297         } until ($buf =~ /\r\n\.\r\n\z/);
298         $buf;
299 }
300
301 1;