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