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