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