git » 5medias » commit a60ae05

test: Add some tests

author Alberto Bertogli
2018-06-09 09:17:16 UTC
committer Alberto Bertogli
2018-06-09 10:14:16 UTC
parent a05f2903d4c18e5dead6d1ce817ce6e96ccf4ccf

test: Add some tests

This patch adds some integration/end to end tests.

.gitlab-ci.yml +18 -0
5medias.go +4 -1
test/README +6 -0
test/build.sh +10 -0
test/common.sh +28 -0
test/s1-http.sh +21 -0
test/s2-http+auth.sh +29 -0
test/test.sh +17 -0

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..b200d3d
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,18 @@
+# gitlab test integration
+
+image: golang:latest
+
+stages:
+  - test
+
+before_script:
+  - apt-get update -qq && apt-get install -y -qq busybox curl
+  - mkdir -p /go/src/blitiri.com.ar/go/5medias
+  - cp -r $CI_PROJECT_DIR/* /go/src/blitiri.com.ar/go/5medias
+  - cd /go/src/blitiri.com.ar/go/5medias
+  - go get .
+
+test:
+  stage: test
+  script:
+    - ./test/test.sh
diff --git a/5medias.go b/5medias.go
index 541a962..12fb56c 100644
--- a/5medias.go
+++ b/5medias.go
@@ -27,6 +27,9 @@ var (
 	addr     = flag.String("addr", ":1080", "address to listen on")
 	username = flag.String("username", "", "username to expect")
 	password = flag.String("password", "", "password to expect")
+
+	allowLoopback = flag.Bool("allow_loopback", false,
+		"allow loopback connections")
 )
 
 func main() {
@@ -85,7 +88,7 @@ func (c *Conn) Handle() {
 	}
 	defer dstConn.Close()
 
-	if dstConn.LocalAddr().(*net.TCPAddr).IP.IsLoopback() {
+	if dstConn.LocalAddr().(*net.TCPAddr).IP.IsLoopback() && !*allowLoopback {
 		c.Logf("loopback connection denied")
 		c.reply(2) // Connection not allowed by ruleset.
 		return
diff --git a/test/README b/test/README
new file mode 100644
index 0000000..b337a07
--- /dev/null
+++ b/test/README
@@ -0,0 +1,6 @@
+
+Test dependencies:
+
+ - busybox
+ - curl
+
diff --git a/test/build.sh b/test/build.sh
new file mode 100755
index 0000000..5059d1a
--- /dev/null
+++ b/test/build.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+set -e
+source "$(dirname ${0})/common.sh"
+
+if [ "${RACE}" == "1" ]; then
+	export GOFLAGS="$GOFLAGS -race"
+fi
+
+( cd ../; go build $GOFLAGS -tags="$GOTAGS" )
diff --git a/test/common.sh b/test/common.sh
new file mode 100644
index 0000000..254eb81
--- /dev/null
+++ b/test/common.sh
@@ -0,0 +1,28 @@
+# To be sourced from individual tests.
+
+if [ "$V" == "1" ]; then
+	set -v
+fi
+
+export TBASE="$(realpath `dirname ${0}`)"
+cd ${TBASE}
+
+
+# Set traps to kill our subprocesses when we exit (for any reason).
+trap ":" TERM      # Avoid the EXIT handler from killing bash.
+trap "exit 2" INT  # Ctrl-C, make sure we fail in that case.
+trap "kill 0" EXIT # Kill children on exit.
+
+# Generate some random test content.
+# Do this for each test to avoid accidental passes.
+dd if=/dev/urandom of=.random bs=1k count=20 status=none
+
+
+# Wait until there's something listening on the given port.
+function wait_until_ready() {
+	PORT=$1
+
+	while ! bash -c "true < /dev/tcp/localhost/$PORT" 2>/dev/null ; do
+		sleep 0.1
+	done
+}
diff --git a/test/s1-http.sh b/test/s1-http.sh
new file mode 100755
index 0000000..0afa6b1
--- /dev/null
+++ b/test/s1-http.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+set -e
+source "$(dirname ${0})/common.sh"
+
+busybox httpd -f -p 9001 > .http.log 2>&1 &
+../5medias --allow_loopback 2> .5medias.log &
+
+wait_until_ready 9001
+wait_until_ready 1080
+
+curl -s -S --preproxy "socks5://localhost:1080" \
+		http://localhost:9001/.random \
+		> .curl.out
+
+if ! diff -q .random .curl.out; then
+	echo proxied content differs
+	exit 1
+fi
+
+echo success
diff --git a/test/s2-http+auth.sh b/test/s2-http+auth.sh
new file mode 100755
index 0000000..6f84614
--- /dev/null
+++ b/test/s2-http+auth.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+set -e
+source "$(dirname ${0})/common.sh"
+
+busybox httpd -f -p 9001 > .http.log 2>&1 &
+../5medias --allow_loopback --username=user --password=test 2> .5medias.log &
+
+wait_until_ready 9001
+wait_until_ready 1080
+
+curl -s -S --preproxy "socks5://user:test@localhost:1080/" \
+		http://localhost:9001/.random \
+		> .curl.out
+
+if ! diff -q .random .curl.out; then
+	echo proxied content differs
+	exit 1
+fi
+
+if curl -s --preproxy "socks5://localhost:1080/" \
+		http://localhost:9001/.random \
+		> /dev/null
+then
+	echo proxy worked without auth
+	exit 1
+fi
+
+echo success
diff --git a/test/test.sh b/test/test.sh
new file mode 100755
index 0000000..220456b
--- /dev/null
+++ b/test/test.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+set -e
+
+cd "$(realpath `dirname ${0}`)"
+
+echo build
+setsid -w ./build.sh
+echo
+
+for i in s*.sh; do
+	echo $i
+	if ! setsid -w ./$i; then
+		echo "FAILED"
+		exit 1
+	fi
+	echo
+done