diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/lib.go | 2 | ||||
-rwxr-xr-x | tests/cli-opts.sh | 45 | ||||
-rwxr-xr-x | tests/integration.sh | 69 | ||||
-rw-r--r-- | tests/lib.sh | 119 |
5 files changed, 237 insertions, 2 deletions
@@ -63,8 +63,10 @@ check-unit: tests/lib_test.bin integration-tests = \ tests/cli-opts.sh \ + tests/integration.sh \ -$(integration-tests): $(NAME).bin ALWAYS +$(integration-tests): $(NAME).bin +$(integration-tests): ALWAYS sh $@ check-integration: $(integration-tests) @@ -114,7 +114,7 @@ func Start(toAddr string, listener net.Listener) { "dial-connection", "err", err, ) - return + os.Exit(1) } defer connTo.Close() diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index 92b70ea..fa337fc 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -1,2 +1,47 @@ #!/bin/sh set -eu + +. tests/lib.sh + + +test_needs_2_arguments() { + testing 'needs 2 arguments' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin FROM-ADDR 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin TO-ADDR 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + test_ok +} + + +test_needs_2_arguments diff --git a/tests/integration.sh b/tests/integration.sh new file mode 100755 index 0000000..4c59d3f --- /dev/null +++ b/tests/integration.sh @@ -0,0 +1,69 @@ +#!/bin/sh +set -eu + +. tests/lib.sh + + +test_exits_when_upstream_errors() { + testing 'exits when upstream errors' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR" s1.socket client.txt' EXIT + + rm -f s1.socket + ./binder.bin localhost:1234 s1.socket 1>"$OUT" 2>"$ERR" & + while ! lsof -s TCP:LISTEN -i :1234 > /dev/null; do + true + done + + echo request | socat tcp-connect:localhost:1234 stdio > client.txt + + assert_fgrep_stream '$OUT' "$OUT" 'listen-start' + assert_fgrep_stream '$OUT' "$OUT" 'active-connections' + assert_fgrep_stream '$OUT' "$OUT" 'dial-connection' + assert_empty_stderr + assert_empty_stream 'client.txt' client.txt + rm -f "$OUT" "$ERR" s1.socket client.txt + + test_ok +} + +test_works_from_client_to_server() { + testing 'works from client to server' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR" s2.socket client.txt server.txt' EXIT + + rm -f s2.socket + ./binder.bin localhost:1234 s2.socket 1>"$OUT" 2>"$ERR" & + pid=$! + while ! lsof -s TCP:LISTEN -i :1234 > /dev/null; do + true + done + + echo response | socat unix-listen:s2.socket stdio > server.txt & + while [ ! -S s2.socket ]; do + true + done + + echo request | socat tcp-connect:localhost:1234 stdio > client.txt + kill $pid + wait + + assert_fgrep_stream '$OUT' "$OUT" 'listen-start' + assert_fgrep_stream '$OUT' "$OUT" 'active-connections' + assert_empty_stderr + assert_fgrep_stream 'client.txt' client.txt 'response' + assert_fgrep_stream 'server.txt' server.txt 'request' + rm -f "$OUT" "$ERR" s2.socket client.txt server.txt + + test_ok +} + + +test_exits_when_upstream_errors +test_works_from_client_to_server diff --git a/tests/lib.sh b/tests/lib.sh new file mode 100644 index 0000000..07ecbef --- /dev/null +++ b/tests/lib.sh @@ -0,0 +1,119 @@ +#!/bin/sh + +end="\033[0m" +red="\033[0;31m" +green="\033[0;32m" +yellow="\033[0;33m" + +N= +OUT= +ERR= +STATUS= + +ERROR() { + # shellcheck disable=2059 + printf "${red}ERROR${end}" +} + +print_debug_info() { + # shellcheck disable=2016 + printf 'LINENO: %s\n$OUT: %s\n$ERR: %s\n' \ + "$N" "$OUT" "$ERR" >&2 +} + +assert_status() { + if [ "$STATUS" != "$1" ]; then + printf '\n%s: Bad status.\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$STATUS" >&2 + print_debug_info + exit 1 + fi +} + +assert_usage() { + if ! grep -Fq 'Usage' "$1"; then + echo 'Expected to find "Usage" text, it was missing:' >&2 + cat "$1" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stream() { + if [ -s "$2" ]; then + FMT='\n%s: Expected %s (%s) to be empty, but has content:\n%s\n' + # shellcheck disable=2059 + printf "$FMT" \ + "$(ERROR)" "$1" "$2" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stdout() { + assert_empty_stream STDOUT "$OUT" +} + +assert_empty_stderr() { + assert_empty_stream STDERR "$ERR" +} + +assert_stream() { + if [ "$(cat "$2")" != "$3" ]; then + printf '\n%s: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_stdout() { + assert_stream STDOUT "$OUT" "$1" +} + +assert_stderr() { + assert_stream STDERR "$ERR" "$1" +} + +assert_grep_stream() { + if ! grep -qE "$3" "$2"; then + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_grep_stdout() { + assert_grep_stream STDOUT "$OUT" "$1" +} + +assert_grep_stderr() { + assert_grep_stream STDERR "$ERR" "$1" +} + +assert_fgrep_stream() { + if ! grep -Fq -- "$3" "$2"; then + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_fgrep_stdout() { + assert_fgrep_stream STDOUT "$OUT" "$1" +} + +assert_fgrep_stderr() { + assert_fgrep_stream STDERR "$ERR" "$1" +} + +testing() { + printf "${yellow}testing${end}: %s..." "$1" >&2 +} + +test_ok() { + # shellcheck disable=2059 + printf " ${green}OK${end}.\n" >&2 +} |