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