From 2eb7b1662c291ab535ee5dabf5d96194ca6483d4 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 5 Jun 2023 10:12:34 +0000 Subject: [PATCH 05/23] port t0002-parser-error.sh to Perl 5 Another socat dependency down... --- t/integration.t | 33 +++++++++++++++ t/lib.perl | 9 +++- t/t0002-parser-error.sh | 94 ----------------------------------------- 3 files changed, 41 insertions(+), 95 deletions(-) delete mode 100755 t/t0002-parser-error.sh diff --git a/t/integration.t b/t/integration.t index ee22e7e..503b7eb 100644 --- a/t/integration.t +++ b/t/integration.t @@ -85,6 +85,39 @@ SKIP: { is($res->{content}, 'Goodbye', 'write-on-close body read'); } +if ('bad requests') { + $c = start_req($srv, 'GET /env_dump HTTP/1/1'); + ($status, $hdr) = slurp_hdr($c); + like($status, qr!\AHTTP/1\.[01] 400 \b!, 'got 400 on bad request'); + + $c = tcp_connect($srv); + print $c 'GET /' or die $!; + my $buf = join('', (0..9), 'ab'); + for (0..1023) { print $c $buf or die $! } + print $c " HTTP/1.0\r\n\r\n" or die $!; + ($status, $hdr) = slurp_hdr($c); + like($status, qr!\AHTTP/1\.[01] 414 \b!, + '414 on REQUEST_PATH > (12 * 1024)'); + + $c = tcp_connect($srv); + print $c 'GET /hello-world?a' or die $!; + $buf = join('', (0..9)); + for (0..1023) { print $c $buf or die $! } + print $c " HTTP/1.0\r\n\r\n" or die $!; + ($status, $hdr) = slurp_hdr($c); + like($status, qr!\AHTTP/1\.[01] 414 \b!, + '414 on QUERY_STRING > (10 * 1024)'); + + $c = tcp_connect($srv); + print $c 'GET /hello-world#a' or die $!; + $buf = join('', (0..9), 'a'..'f'); + for (0..63) { print $c $buf or die $! } + print $c " HTTP/1.0\r\n\r\n" or die $!; + ($status, $hdr) = slurp_hdr($c); + like($status, qr!\AHTTP/1\.[01] 414 \b!, '414 on FRAGMENT > (1024)'); +} + + # ... more stuff here undef $ar; my @log = slurp("$tmpdir/err.log"); diff --git a/t/lib.perl b/t/lib.perl index 12deaf8..7d712b5 100644 --- a/t/lib.perl +++ b/t/lib.perl @@ -10,7 +10,7 @@ use POSIX qw(dup2 _exit setpgid :signal_h SEEK_SET F_SETFD); use File::Temp 0.19 (); # 0.19 for ->newdir our ($tmpdir, $errfh); our @EXPORT = qw(unicorn slurp tcp_server tcp_connect unicorn $tmpdir $errfh - SEEK_SET tcp_host_port); + SEEK_SET tcp_host_port start_req); my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1); @@ -59,6 +59,13 @@ sub tcp_connect { $s; } +sub start_req { + my ($srv, @req) = @_; + my $c = tcp_connect($srv); + print $c @req, "\r\n\r\n" or die "print: $!"; + $c; +} + sub slurp { open my $fh, '<', $_[0] or die "open($_[0]): $!"; local $/; diff --git a/t/t0002-parser-error.sh b/t/t0002-parser-error.sh deleted file mode 100755 index 9dc1cd2..0000000 --- a/t/t0002-parser-error.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/sh -. ./test-lib.sh -t_plan 11 "parser error test" - -t_begin "setup and startup" && { - unicorn_setup - unicorn -D env.ru -c $unicorn_config - unicorn_wait_start -} - -t_begin "send a bad request" && { - ( - printf 'GET / HTTP/1/1\r\nHost: example.com\r\n\r\n' - cat $fifo > $tmp & - wait - echo ok > $ok - ) | socat - TCP:$listen > $fifo - test xok = x$(cat $ok) -} - -dbgcat tmp - -t_begin "response should be a 400" && { - grep -F 'HTTP/1.1 400 Bad Request' $tmp -} - -t_begin "send a huge Request URI (REQUEST_PATH > (12 * 1024))" && { - rm -f $tmp - cat $fifo > $tmp & - ( - set -e - trap 'echo ok > $ok' EXIT - printf 'GET /' - for i in $(awk $fifo || : - test xok = x$(cat $ok) - wait -} - -t_begin "response should be a 414 (REQUEST_PATH)" && { - grep -F 'HTTP/1.1 414 ' $tmp -} - -t_begin "send a huge Request URI (QUERY_STRING > (10 * 1024))" && { - rm -f $tmp - cat $fifo > $tmp & - ( - set -e - trap 'echo ok > $ok' EXIT - printf 'GET /hello-world?a' - for i in $(awk $fifo || : - test xok = x$(cat $ok) - wait -} - -t_begin "response should be a 414 (QUERY_STRING)" && { - grep -F 'HTTP/1.1 414 ' $tmp -} - -t_begin "send a huge Request URI (FRAGMENT > 1024)" && { - rm -f $tmp - cat $fifo > $tmp & - ( - set -e - trap 'echo ok > $ok' EXIT - printf 'GET /hello-world#a' - for i in $(awk $fifo || : - test xok = x$(cat $ok) - wait -} - -t_begin "response should be a 414 (FRAGMENT)" && { - grep -F 'HTTP/1.1 414 ' $tmp -} - -t_begin "server stderr should be clean" && check_stderr - -t_begin "term signal sent" && kill $unicorn_pid - -t_done