]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.t
366e56cb5d3f04b2f00b5e9ee3e6ae983a6d55f0
[public-inbox.git] / t / httpd-corner.t
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # note: our HTTP server should be standalone and capable of running
4 # generic Rack apps.
5 use strict;
6 use warnings;
7 use Test::More;
8
9 foreach my $mod (qw(Plack::Util Plack::Request Plack::Builder Danga::Socket
10                         HTTP::Parser::XS HTTP::Date HTTP::Status)) {
11         eval "require $mod";
12         plan skip_all => "$mod missing for httpd-corner.t" if $@;
13 }
14
15 use Digest::SHA qw(sha1_hex);
16 use File::Temp qw/tempdir/;
17 use Cwd qw/getcwd/;
18 use IO::Socket;
19 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
20 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
21 my $tmpdir = tempdir(CLEANUP => 1);
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $httpd = 'blib/script/public-inbox-httpd';
25 my $psgi = getcwd()."/t/httpd-corner.psgi";
26 my %opts = (
27         LocalAddr => '127.0.0.1',
28         ReuseAddr => 1,
29         Proto => 'tcp',
30         Type => SOCK_STREAM,
31         Listen => 1024,
32 );
33 my $sock = IO::Socket::INET->new(%opts);
34 my $pid;
35 END { kill 'TERM', $pid if defined $pid };
36 {
37         ok($sock, 'sock created');
38         $! = 0;
39         my $fl = fcntl($sock, F_GETFD, 0);
40         ok(! $!, 'no error from fcntl(F_GETFD)');
41         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
42         $pid = fork;
43         if ($pid == 0) {
44                 use POSIX qw(dup2);
45                 # pretend to be systemd
46                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
47                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
48                 $ENV{LISTEN_PID} = $$;
49                 $ENV{LISTEN_FDS} = 1;
50                 exec $httpd, '-W0', "--stdout=$out", "--stderr=$err", $psgi;
51                 die "FAIL: $!\n";
52         }
53         ok(defined $pid, 'forked httpd process successfully');
54         $! = 0;
55         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
56         ok(! $!, 'no error from fcntl(F_SETFD)');
57 }
58
59 sub conn_for {
60         my ($sock, $msg) = @_;
61         my $conn = IO::Socket::INET->new(
62                                 PeerAddr => $sock->sockhost,
63                                 PeerPort => $sock->sockport,
64                                 Proto => 'tcp',
65                                 Type => SOCK_STREAM);
66         ok($conn, "connected for $msg");
67         $conn->autoflush(1);
68         setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1);
69         return $conn;
70 }
71
72 sub delay { select(undef, undef, undef, shift || rand(0.02)) }
73
74 my $str = 'abcdefghijklmnopqrstuvwxyz';
75 my $len = length $str;
76 is($len, 26, 'got the alphabet');
77 my $check_self = sub {
78         my ($conn) = @_;
79         $conn->read(my $buf, 4096);
80         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
81         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
82         is($body, sha1_hex($str), 'read expected body');
83 };
84
85 SKIP: {
86         use POSIX qw(dup2);
87         use IO::File;
88         my $have_curl = 0;
89         foreach my $p (split(':', $ENV{PATH})) {
90                 -x "$p/curl" or next;
91                 $have_curl = 1;
92                 last;
93         }
94         my $ntest = 2;
95         $have_curl or skip('curl(1) missing', $ntest);
96         my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
97         my ($r, $w);
98         pipe($r, $w) or die "pipe: $!";
99         my $tout = IO::File->new_tmpfile or die "new_tmpfile: $!";
100         my $pid = fork;
101         defined $pid or die "fork: $!";
102         my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
103         if ($pid == 0) {
104                 dup2(fileno($r), 0) or die "redirect stdin failed: $!\n";
105                 dup2(fileno($tout), 1) or die "redirect stdout failed: $!\n";
106                 exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
107         }
108         $w->autoflush(1);
109         foreach my $c ('a'..'z') {
110                 print $w $c or die "failed to write to curl: $!";
111                 delay();
112         }
113         close $w or die "close write pipe: $!";
114         close $r or die "close read pipe: $!";
115         my $kid = waitpid $pid, 0;
116         is($?, 0, 'curl exited successfully');
117         $tout->sysseek(0, SEEK_SET);
118         $tout->sysread(my $buf, 100);
119         is($buf, sha1_hex($str), 'read expected body');
120 }
121
122 {
123         my $conn = conn_for($sock, '1.1 pipeline together');
124         $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .
125                         "PUT /sha1 HTTP/1.1\r\n\r\n");
126         my $buf = '';
127         my @r;
128         until (scalar(@r) >= 2) {
129                 my $r = $conn->sysread(my $tmp, 4096);
130                 die $! unless defined $r;
131                 die "EOF <$buf>" unless $r;
132                 $buf .= $tmp;
133                 @r = ($buf =~ /\r\n\r\n([a-f0-9]{40})/g);
134         }
135         is(2, scalar @r, 'got 2 responses');
136         my $i = 3;
137         foreach my $hex (@r) {
138                 is($hex, sha1_hex(''), "read expected body $i");
139                 $i++;
140         }
141 }
142
143 # various DoS attacks against the chunk parser:
144 {
145         local $SIG{PIPE} = 'IGNORE';
146         my $conn = conn_for($sock, '1.1 chunk header excessive');
147         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
148         my $n = 0;
149         my $w;
150         while ($w = $conn->write('ffffffff')) {
151                 $n += $w;
152         }
153         ok($!, 'got error set in $!');
154         is($w, undef, 'write error happened');
155         ok($n > 0, 'was able to write');
156         my $r = $conn->read(my $buf, 66666);
157         ok($r > 0, 'got non-empty response');
158         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
159
160         $conn = conn_for($sock, '1.1 chunk trailer excessive');
161         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
162         is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
163         delay();
164         $n = 0;
165         while ($w = $conn->write("\r")) {
166                 $n += $w;
167         }
168         ok($!, 'got error set in $!');
169         ok($n > 0, 'wrote part of chunk end (\r)');
170         $r = $conn->read($buf, 66666);
171         ok($r > 0, 'got non-empty response');
172         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
173 }
174
175 {
176         my $conn = conn_for($sock, '1.1 chunked close trickle');
177         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
178         $conn->write("Transfer-encoding: chunked\r\n\r\n");
179         foreach my $x ('a'..'z') {
180                 delay();
181                 $conn->write('1');
182                 delay();
183                 $conn->write("\r");
184                 delay();
185                 $conn->write("\n");
186                 delay();
187                 $conn->write($x);
188                 delay();
189                 $conn->write("\r");
190                 delay();
191                 $conn->write("\n");
192         }
193         $conn->write('0');
194         delay();
195         $conn->write("\r");
196         delay();
197         $conn->write("\n");
198         delay();
199         $conn->write("\r");
200         delay();
201         $conn->write("\n");
202         delay();
203         $check_self->($conn);
204 }
205
206 {
207         my $conn = conn_for($sock, '1.1 chunked close');
208         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
209         my $xlen = sprintf('%x', $len);
210         $conn->write("Transfer-Encoding: chunked\r\n\r\n$xlen\r\n" .
211                 "$str\r\n0\r\n\r\n");
212         $check_self->($conn);
213 }
214
215 {
216         my $conn = conn_for($sock, 'chunked body + pipeline');
217         $conn->write("PUT /sha1 HTTP/1.1\r\n" .
218                         "Transfer-Encoding: chunked\r\n");
219         delay();
220         $conn->write("\r\n1\r\n");
221         delay();
222         $conn->write('a');
223         delay();
224         $conn->write("\r\n0\r\n\r\nPUT /sha1 HTTP/1.1\r\n");
225         delay();
226
227         my $buf = '';
228         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
229                 $conn->sysread(my $tmp, 4096);
230                 $buf .= $tmp;
231         }
232         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
233         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
234         is($body, sha1_hex('a'), 'read expected body');
235
236         $conn->write("Connection: close\r\n");
237         $conn->write("Content-Length: $len\r\n\r\n$str");
238         $check_self->($conn);
239 }
240
241 {
242         my $conn = conn_for($sock, 'trickle header, one-shot body + pipeline');
243         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
244                         "Connection: keep-alive\r\n");
245         delay();
246         $conn->write("Content-Length: $len\r\n\r\n${str}PUT");
247         my $buf = '';
248         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
249                 $conn->sysread(my $tmp, 4096);
250                 $buf .= $tmp;
251         }
252         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
253         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
254         is($body, sha1_hex($str), 'read expected body');
255
256         $conn->write(" /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n$str");
257         $check_self->($conn);
258 }
259
260 {
261         my $conn = conn_for($sock, 'trickle body');
262         $conn->write("PUT /sha1 HTTP/1.0\r\n");
263         $conn->write("Content-Length: $len\r\n\r\n");
264         my $beg = substr($str, 0, 10);
265         my $end = substr($str, 10);
266         is($beg . $end, $str, 'substr setup correct');
267         delay();
268         $conn->write($beg);
269         delay();
270         $conn->write($end);
271         $check_self->($conn);
272 }
273
274 {
275         my $conn = conn_for($sock, 'one-shot write');
276         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
277                         "Content-Length: $len\r\n\r\n$str");
278         $check_self->($conn);
279 }
280
281 {
282         my $conn = conn_for($sock, 'trickle header, one-shot body');
283         $conn->write("PUT /sha1 HTTP/1.0\r\n");
284         delay();
285         $conn->write("Content-Length: $len\r\n\r\n$str");
286         $check_self->($conn);
287 }
288
289 {
290         my $conn = conn_for($sock, '1.1 Connnection: close');
291         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
292         delay();
293         $conn->write("Content-Length: $len\r\n\r\n$str");
294         $check_self->($conn);
295 }
296
297 {
298         my $conn = conn_for($sock, '1.1 pipeline start');
299         $conn->write("PUT /sha1 HTTP/1.1\r\n\r\nPUT");
300         my $buf = '';
301         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
302                 $conn->sysread(my $tmp, 4096);
303                 $buf .= $tmp;
304         }
305         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
306         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
307         is($body, sha1_hex(''), 'read expected body');
308
309         # 2nd request
310         $conn->write(" /sha1 HTTP/1.1\r\n\r\n");
311         $buf = '';
312         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
313                 $conn->sysread(my $tmp, 4096);
314                 $buf .= $tmp;
315         }
316         ($head, $body) = split(/\r\n\r\n/, $buf, 2);
317         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
318         is($body, sha1_hex(''), 'read expected body #2');
319 }
320
321 done_testing();
322
323 1;