]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
Merge remote-tracking branch 'origin/danga-bundle'
[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 Search::Xapian PublicInbox::DS)) {
7         eval "require $mod";
8         plan skip_all => "$mod missing for nntpd.t" if $@;
9 }
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
12 use Cwd;
13 use Email::Simple;
14 use IO::Socket;
15 use Socket qw(SO_KEEPALIVE 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_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 use_ok 'PublicInbox::Import';
35 use_ok 'PublicInbox::Inbox';
36 use_ok 'PublicInbox::Git';
37 SKIP: {
38         skip "git 2.6+ required for V2Writable", 1 if $version == 1;
39         use_ok 'PublicInbox::V2Writable';
40 }
41
42 my %opts = (
43         LocalAddr => '127.0.0.1',
44         ReuseAddr => 1,
45         Proto => 'tcp',
46         Type => SOCK_STREAM,
47         Listen => 1024,
48 );
49 my $sock = IO::Socket::INET->new(%opts);
50 my $pid;
51 my $len;
52 END { kill 'TERM', $pid if defined $pid };
53
54 my $ibx = {
55         mainrepo => $mainrepo,
56         name => $group,
57         version => $version,
58         -primary_address => $addr,
59 };
60 $ibx = PublicInbox::Inbox->new($ibx);
61 {
62         local $ENV{HOME} = $home;
63         my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
64         push @cmd, "-V$version";
65         is(system(@cmd), 0, 'init OK');
66         is(system(qw(git config), "--file=$home/.public-inbox/config",
67                         "publicinbox.$group.newsgroup", $group),
68                 0, 'enabled newsgroup');
69         my $len;
70
71         my $im;
72         if ($version == 2) {
73                 $im = PublicInbox::V2Writable->new($ibx);
74         } elsif ($version == 1) {
75                 my $git = PublicInbox::Git->new($mainrepo);
76                 $im = PublicInbox::Import->new($git, 'test', $addr);
77         } else {
78                 die "unsupported version: $version";
79         }
80
81         # ensure successful message delivery
82         {
83                 my $mime = Email::MIME->new(<<EOF);
84 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
85 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
86 Cc: $addr
87 Message-Id: <nntp\@example.com>
88 Content-Type: text/plain; charset=utf-8
89 Subject: Testing for    =?utf-8?Q?El=C3=A9anor?=
90 Date: Thu, 01 Jan 1970 06:06:06 +0000
91 Content-Transfer-Encoding: 8bit
92 References: <ref        tab     squeezed>
93
94 This is a test message for El\xc3\xa9anor
95 EOF
96                 my $list_id = $addr;
97                 $list_id =~ s/@/./;
98                 $mime->header_set('List-Id', "<$list_id>");
99                 $len = length($mime->as_string);
100                 $im->add($mime);
101                 $im->done;
102                 if ($version == 1) {
103                         my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
104                         $s->index_sync;
105                 }
106         }
107
108         ok($sock, 'sock created');
109         my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
110         $pid = spawn_listener(undef, $cmd, [ $sock ]);
111         ok(defined $pid, 'forked nntpd process successfully');
112         my $host_port = $sock->sockhost . ':' . $sock->sockport;
113         my $n = Net::NNTP->new($host_port);
114         my $list = $n->list;
115         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
116         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
117         is_deeply($n->listgroup($group), [1], 'listgroup OK');
118
119         %opts = (
120                 PeerAddr => $host_port,
121                 Proto => 'tcp',
122                 Type => SOCK_STREAM,
123                 Timeout => 1,
124         );
125         my $mid = '<nntp@example.com>';
126         my %xhdr = (
127                 'message-id' => $mid,
128                 subject => "Testing for El\xc3\xa9anor",
129                 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
130                 'from' => "El\xc3\xa9anor <me\@example.com>",
131                 'to' => "El\xc3\xa9anor <you\@example.com>",
132                 'cc' => $addr,
133                 'xref' => hostname . " $group:1",
134                 'references' => '<reftabsqueezed>',
135         );
136
137         my $s = IO::Socket::INET->new(%opts);
138         sysread($s, my $buf, 4096);
139         is($buf, "201 " . hostname . " ready - post via email\r\n",
140                 'got greeting');
141         $s->autoflush(1);
142
143         ok(syswrite($s, "   \r\n"), 'wrote spaces');
144         ok(syswrite($s, "\r\n"), 'wrote nothing');
145         syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
146         is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
147
148         $s = IO::Socket::INET->new(%opts);
149         sysread($s, $buf, 4096);
150         is($buf, "201 " . hostname . " ready - post via email\r\n",
151                 'got greeting');
152         $s->autoflush(1);
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
244         # pipelined requests:
245         {
246                 my $nreq = 90;
247                 syswrite($s, "GROUP $group\r\n");
248                 my $res = <$s>;
249                 my $rdr = fork;
250                 if ($rdr == 0) {
251                         use POSIX qw(_exit);
252                         for (1..$nreq) {
253                                 <$s> =~ /\A224 / or _exit(1);
254                                 <$s> =~ /\A1/ or _exit(2);
255                                 <$s> eq ".\r\n" or _exit(3);
256                         }
257                         _exit(0);
258                 }
259                 for (1..$nreq) {
260                         syswrite($s, "XOVER 1\r\n");
261                 }
262                 is($rdr, waitpid($rdr, 0), 'reader done');
263                 is($? >> 8, 0, 'no errors');
264         }
265         {
266                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
267                 syswrite($s, 'HDR List-id 1-');
268                 select(undef, undef, undef, 0.15);
269                 ok(kill('TERM', $pid), 'killed nntpd');
270                 select(undef, undef, undef, 0.15);
271                 syswrite($s, "\r\n");
272                 $buf = '';
273                 do {
274                         sysread($s, $buf, 4096, length($buf));
275                 } until ($buf =~ /\r\n\z/);
276                 my @r = split("\r\n", $buf);
277                 like($r[0], qr/^5\d\d /,
278                         'got 5xx response for unoptimized HDR');
279                 is(scalar @r, 1, 'only one response line');
280         }
281
282         $n = $s = undef;
283         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
284         my $eout = eval {
285                 local $/;
286                 open my $fh, '<', $err or die "open $err failed: $!";
287                 <$fh>;
288         };
289         is($?, 0, 'no error in exited process');
290         unlike($eout, qr/wide/i, 'no Wide character warnings');
291 }
292
293 done_testing();
294
295 sub read_til_dot {
296         my ($s) = @_;
297         my $buf = '';
298         do {
299                 sysread($s, $buf, 4096, length($buf));
300         } until ($buf =~ /\r\n\.\r\n\z/);
301         $buf;
302 }
303
304 1;