]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
update copyrights for 2018
[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 $maindir = "$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::Git';
31
32 my %opts = (
33         LocalAddr => '127.0.0.1',
34         ReuseAddr => 1,
35         Proto => 'tcp',
36         Type => SOCK_STREAM,
37         Listen => 1024,
38 );
39 my $sock = IO::Socket::INET->new(%opts);
40 my $pid;
41 my $len;
42 END { kill 'TERM', $pid if defined $pid };
43 {
44         local $ENV{HOME} = $home;
45         system($init, $group, $maindir, 'http://example.com/', $addr);
46         is(system(qw(git config), "--file=$home/.public-inbox/config",
47                         "publicinbox.$group.newsgroup", $group),
48                 0, 'enabled newsgroup');
49         my $len;
50
51         # ensure successful message delivery
52         {
53                 my $mime = Email::MIME->new(<<EOF);
54 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
55 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
56 Cc: $addr
57 Message-Id: <nntp\@example.com>
58 Content-Type: text/plain; charset=utf-8
59 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
60 Date: Thu, 01 Jan 1970 06:06:06 +0000
61 Content-Transfer-Encoding: 8bit
62
63 This is a test message for El\xc3\xa9anor
64 EOF
65                 my $list_id = $addr;
66                 $list_id =~ s/@/./;
67                 $mime->header_set('List-Id', "<$list_id>");
68                 $len = length($mime->as_string);
69                 my $git = PublicInbox::Git->new($maindir);
70                 my $im = PublicInbox::Import->new($git, 'test', $addr);
71                 $im->add($mime);
72                 $im->done;
73                 my $s = PublicInbox::SearchIdx->new($maindir, 1);
74                 $s->index_sync;
75         }
76
77         ok($sock, 'sock created');
78         $! = 0;
79         my $fl = fcntl($sock, F_GETFD, 0);
80         ok(! $!, 'no error from fcntl(F_GETFD)');
81         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
82         $pid = fork;
83         if ($pid == 0) {
84                 use POSIX qw(dup2);
85                 # pretend to be systemd
86                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
87                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
88                 $ENV{LISTEN_PID} = $$;
89                 $ENV{LISTEN_FDS} = 1;
90                 exec $nntpd, "--stdout=$out", "--stderr=$err";
91                 die "FAIL: $!\n";
92         }
93         ok(defined $pid, 'forked nntpd process successfully');
94         $! = 0;
95         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
96         ok(! $!, 'no error from fcntl(F_SETFD)');
97         my $host_port = $sock->sockhost . ':' . $sock->sockport;
98         my $n = Net::NNTP->new($host_port);
99         my $list = $n->list;
100         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
101         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
102
103         %opts = (
104                 PeerAddr => $host_port,
105                 Proto => 'tcp',
106                 Type => SOCK_STREAM,
107                 Timeout => 1,
108         );
109         my $mid = '<nntp@example.com>';
110         my %xhdr = (
111                 'message-id' => $mid,
112                 subject => "Testing for El\xc3\xa9anor",
113                 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
114                 'from' => "El\xc3\xa9anor <me\@example.com>",
115                 'to' => "El\xc3\xa9anor <you\@example.com>",
116                 'cc' => $addr,
117                 'xref' => "example.com $group:1"
118         );
119
120         my $s = IO::Socket::INET->new(%opts);
121         sysread($s, my $buf, 4096);
122         is($buf, "201 server ready - post via email\r\n", 'got greeting');
123         $s->autoflush(1);
124
125         syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
126         is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
127
128         $s = IO::Socket::INET->new(%opts);
129         sysread($s, $buf, 4096);
130         is($buf, "201 server ready - post via email\r\n", 'got greeting');
131         $s->autoflush(1);
132
133         syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
134         $buf = read_til_dot($s);
135         like($buf, qr/\A231 list of /, 'newgroups OK');
136
137         while (my ($k, $v) = each %xhdr) {
138                 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
139                           "XHDR $k by message-id works");
140                 is_deeply($n->xhdr("$k 1"), { 1 => $v },
141                           "$k by article number works");
142                 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
143                           "$k by article range works");
144                 $buf = '';
145                 syswrite($s, "HDR $k $mid\r\n");
146                 $buf = read_til_dot($s);
147                 my @r = split("\r\n", $buf);
148                 like($r[0], qr/\A225 /, '225 response for HDR');
149                 is($r[1], "0 $v", 'got expected response for HDR');
150         }
151
152         {
153                 my $nogroup = Net::NNTP->new($host_port);
154                 while (my ($k, $v) = each %xhdr) {
155                         is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
156                                   "$k by message-id works without group");
157                 }
158         }
159
160         is_deeply($n->xover('1-'), {
161                 '1' => ["Testing for El\xc3\xa9anor",
162                         "El\xc3\xa9anor <me\@example.com>",
163                         'Thu, 01 Jan 1970 06:06:06 +0000',
164                         '<nntp@example.com>',
165                         '',
166                         $len,
167                         '1' ] }, "XOVER range works");
168
169         is_deeply($n->xover('1'), {
170                 '1' => ["Testing for El\xc3\xa9anor",
171                         "El\xc3\xa9anor <me\@example.com>",
172                         'Thu, 01 Jan 1970 06:06:06 +0000',
173                         '<nntp@example.com>',
174                         '',
175                         $len,
176                         '1' ] }, "XOVER by article works");
177
178         is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
179         is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
180         is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
181                 'body really matches');
182         my $art = $n->article(1);
183         is(ref($art), 'ARRAY', 'got array for ARTICLE');
184         is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
185         is($n->article(999), undef, 'non-existent num');
186         is($n->article('<non-existent@example>'), undef, 'non-existent mid');
187
188         {
189                 syswrite($s, "OVER $mid\r\n");
190                 $buf = read_til_dot($s);
191                 my @r = split("\r\n", $buf);
192                 like($r[0], qr/^224 /, 'got 224 response for OVER');
193                 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
194                         "El\xc3\xa9anor <me\@example.com>\t" .
195                         "Thu, 01 Jan 1970 06:06:06 +0000\t" .
196                         "$mid\t\t$len\t1", 'OVER by Message-ID works');
197                 is($r[2], '.', 'correctly terminated response');
198         }
199
200         is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
201                  'XHDR Cc 1- works');
202         is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
203                  'XHDR References 1- works (empty string)');
204         is_deeply($n->xhdr(qw(list-id 1-)), {},
205                  'XHDR on invalid header returns empty');
206
207         {
208                 my $t0 = time;
209                 my $date = $n->date;
210                 my $t1 = time;
211                 ok($date >= $t0, 'valid date after start');
212                 ok($date <= $t1, 'valid date before stop');
213         }
214
215         {
216                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
217                 syswrite($s, 'HDR List-id 1-');
218                 select(undef, undef, undef, 0.15);
219                 ok(kill('TERM', $pid), 'killed nntpd');
220                 select(undef, undef, undef, 0.15);
221                 syswrite($s, "\r\n");
222                 $buf = '';
223                 do {
224                         sysread($s, $buf, 4096, length($buf));
225                 } until ($buf =~ /\r\n\z/);
226                 my @r = split("\r\n", $buf);
227                 like($r[0], qr/^5\d\d /,
228                         'got 5xx response for unoptimized HDR');
229                 is(scalar @r, 1, 'only one response line');
230         }
231
232         $n = $s = undef;
233         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
234         my $eout = eval {
235                 local $/;
236                 open my $fh, '<', $err or die "open $err failed: $!";
237                 <$fh>;
238         };
239         is($?, 0, 'no error in exited process');
240         unlike($eout, qr/wide/i, 'no Wide character warnings');
241 }
242
243 done_testing();
244
245 sub read_til_dot {
246         my ($s) = @_;
247         my $buf = '';
248         do {
249                 sysread($s, $buf, 4096, length($buf));
250         } until ($buf =~ /\r\n\.\r\n\z/);
251         $buf;
252 }
253
254 1;