]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/httpd-corner.t
mbox: use per-message line-ending for From_ line
[public-inbox.git] / t / httpd-corner.t
index cbfc83327bddd8edd2316b226839d1b9b518e788..f25a9a9cb31d0ae5198687cdb37495d57ca9cff7 100644 (file)
@@ -16,6 +16,7 @@ use IO::Socket::UNIX;
 use Fcntl qw(:seek);
 use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
 use POSIX qw(mkfifo);
+use Carp ();
 my ($tmpdir, $for_destroy) = tmpdir();
 my $fifo = "$tmpdir/fifo";
 ok(defined mkfifo($fifo, 0777), 'created FIFO');
@@ -122,36 +123,48 @@ if ('test worker death') {
        is(scalar(grep(/CLOSE FAIL/, @$after)), 1, 'body->close not called');
 }
 
-SKIP: {
+sub check_400 {
+       my ($conn) = @_;
+       my $r = $conn->read(my $buf, 8192);
+       # ECONNRESET and $r==0 are both observed on FreeBSD 11.2
+       if (!defined($r)) {
+               ok($!{ECONNRESET}, 'ECONNRESET on read (BSD sometimes)');
+       } elsif ($r > 0) {
+               like($buf, qr!\AHTTP/1\.\d 400 !, 'got 400 response');
+       } else {
+               is($r, 0, 'got EOF (BSD sometimes)');
+       }
+       close($conn); # ensure we don't get SIGPIPE later
+}
+
+{
+       local $SIG{PIPE} = 'IGNORE';
        my $conn = conn_for($sock, 'excessive header');
-       $SIG{PIPE} = 'IGNORE';
        $conn->write("GET /callback HTTP/1.0\r\n");
        foreach my $i (1..500000) {
                last unless $conn->write("X-xxxxxJunk-$i: omg\r\n");
        }
        ok(!$conn->write("\r\n"), 'broken request');
-       ok($conn->read(my $buf, 8192), 'read response');
-       my ($head, $body) = split(/\r\n\r\n/, $buf);
-       like($head, qr/\b400\b/, 'got 400 response');
+       check_400($conn);
 }
 
 {
        my $conn = conn_for($sock, 'excessive body Content-Length');
-       $SIG{PIPE} = 'IGNORE';
        my $n = (10 * 1024 * 1024) + 1;
        $conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $n\r\n\r\n");
-       ok($conn->read(my $buf, 8192), 'read response');
+       my $r = $conn->read(my $buf, 8192);
+       ok($r > 0, 'read response');
        my ($head, $body) = split(/\r\n\r\n/, $buf);
        like($head, qr/\b413\b/, 'got 413 response');
 }
 
 {
        my $conn = conn_for($sock, 'excessive body chunked');
-       $SIG{PIPE} = 'IGNORE';
        my $n = (10 * 1024 * 1024) + 1;
        $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
        $conn->write("\r\n".sprintf("%x\r\n", $n));
-       ok($conn->read(my $buf, 8192), 'read response');
+       my $r = $conn->read(my $buf, 8192);
+       ok($r > 0, 'read response');
        my ($head, $body) = split(/\r\n\r\n/, $buf);
        like($head, qr/\b413\b/, 'got 413 response');
 }
@@ -286,6 +299,8 @@ my $len = length $str;
 is($len, 26, 'got the alphabet');
 my $check_self = sub {
        my ($conn) = @_;
+       vec(my $rbits, fileno($conn), 1) = 1;
+       select($rbits, undef, undef, 30) or Carp::confess('timed out');
        $conn->read(my $buf, 4096);
        my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
        like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
@@ -379,17 +394,20 @@ SKIP: {
 
 {
        my $conn = conn_for($sock, 'graceful termination during slow request');
-       $conn->write("PUT /sha1 HTTP/1.0\r\n");
-       delay();
-       $conn->write("Content-Length: $len\r\n");
-       delay();
-       $conn->write("\r\n");
-       is($td->kill, 1, 'started graceful shutdown');
-       delay();
+       $conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n");
+
+       # XXX ugh, want a reliable and non-intrusive way to detect
+       # that the server has started buffering our partial request so we
+       # can reliably test graceful termination.  Maybe making this and
+       # similar tests dependent on Linux strace is a possibility?
+       delay(0.1);
+
+       is($td->kill, 1, 'start graceful shutdown');
        my $n = 0;
        foreach my $c ('a'..'z') {
                $n += $conn->write($c);
        }
+       ok(kill(0, $td->{pid}), 'graceful shutdown did not kill httpd');
        is($n, $len, 'wrote alphabet');
        $check_self->($conn);
        $td->join;
@@ -410,10 +428,7 @@ SKIP: {
        ok($!, 'got error set in $!');
        is($w, undef, 'write error happened');
        ok($n > 0, 'was able to write');
-       my $r = $conn->read(my $buf, 66666);
-       ok($r > 0, 'got non-empty response');
-       like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
-
+       check_400($conn);
        $conn = conn_for($sock, '1.1 chunk trailer excessive');
        $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
        is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
@@ -424,9 +439,7 @@ SKIP: {
        }
        ok($!, 'got error set in $!');
        ok($n > 0, 'wrote part of chunk end (\r)');
-       $r = $conn->read($buf, 66666);
-       ok($r > 0, 'got non-empty response');
-       like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
+       check_400($conn);
 }
 
 {
@@ -544,7 +557,7 @@ SKIP: {
 }
 
 {
-       my $conn = conn_for($sock, '1.1 Connnection: close');
+       my $conn = conn_for($sock, '1.1 Connection: close');
        $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
        delay();
        $conn->write("Content-Length: $len\r\n\r\n$str");