From 8df5d237856d365a7a49835274e58e8c8bb281b1 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 27 Jul 2017 14:16:17 +1200 Subject: Add more tests, pull simple test runner from other project --- Makefile | 4 +-- test.sh | 34 ------------------- test/001-error-no-args.test/run.sh | 8 +++++ test/001-error-no-args.test/stderr.expected | 2 ++ test/002-test-key-name.test/run.sh | 34 +++++++++++++++++++ test/003-test-invalid-char.test/run.sh | 9 ++++++ test/003-test-invalid-char.test/stderr.expected | 2 ++ test/run-tests.sh | 43 +++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 36 deletions(-) delete mode 100755 test.sh create mode 100755 test/001-error-no-args.test/run.sh create mode 100644 test/001-error-no-args.test/stderr.expected create mode 100755 test/002-test-key-name.test/run.sh create mode 100644 test/003-test-invalid-char.test/run.sh create mode 100644 test/003-test-invalid-char.test/stderr.expected create mode 100755 test/run-tests.sh diff --git a/Makefile b/Makefile index 4f5566f..c06ac86 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ sand-leek: sand-leek.o onion_base32.o key_update.o clean: rm -vf sand-leek *.o -test: - ./test.sh +test: all + @./test/run-tests.sh .PHONY: all clean test diff --git a/test.sh b/test.sh deleted file mode 100755 index 2b24176..0000000 --- a/test.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -set -e - -key="$(mktemp)" -stderr="$(mktemp)" - -# Four character search should be a < 1 second CPU burst for CI runner -./sand-leek -s site > $key 2>$stderr - -found="$(tr '\r' '\n' < $stderr | grep Found | cut -d ' ' -f 2)" - -echo "sand-leek says it found $found..." - -# Trick adapted to py3 from https://swehack.org/viewtopic.php?f=37&p=6978 -real="$( \ - openssl rsa -in $key -pubout -outform DER \ - | tail -c +23 \ - | shasum \ - | head -c 20 \ - | python -c "import base64,sys,codecs; print(base64.b32encode(codecs.decode(sys.stdin.readline().strip('\n'), 'hex')).decode().lower())").onion" - - -echo "Key analysis shows it's for ${real}" - -if [ "$found" = "$real" ] ; then - echo "It's a match, I'm happy" - rm $key - rm $stderr - exit 0 -else - echo "Error: No match" - exit 1 -fi diff --git a/test/001-error-no-args.test/run.sh b/test/001-error-no-args.test/run.sh new file mode 100755 index 0000000..4e67e1d --- /dev/null +++ b/test/001-error-no-args.test/run.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +${EXECUTABLE} +if [ $? -eq 0 ]; then + exit 1 +else + exit 0 +fi diff --git a/test/001-error-no-args.test/stderr.expected b/test/001-error-no-args.test/stderr.expected new file mode 100644 index 0000000..32481e5 --- /dev/null +++ b/test/001-error-no-args.test/stderr.expected @@ -0,0 +1,2 @@ +usage: ../../sand-leek [-t threads] -s search +searches for keys for onion addresses beginning with `search` diff --git a/test/002-test-key-name.test/run.sh b/test/002-test-key-name.test/run.sh new file mode 100755 index 0000000..e4b4acb --- /dev/null +++ b/test/002-test-key-name.test/run.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set -e + +key="$(mktemp)" +stderr="$(mktemp)" + +# Four character search should be a < 1 second CPU burst for CI runner +${EXECUTABLE} -s site > $key 2>$stderr + +found="$(tr '\r' '\n' < $stderr | grep Found | cut -d ' ' -f 2)" + +echo "sand-leek says it found $found..." + +# Trick adapted to py3 from https://swehack.org/viewtopic.php?f=37&p=6978 +real="$( \ + openssl rsa -in $key -pubout -outform DER \ + | tail -c +23 \ + | shasum \ + | head -c 20 \ + | python -c "import base64,sys,codecs; print(base64.b32encode(codecs.decode(sys.stdin.readline().strip('\n'), 'hex')).decode().lower())").onion" + + +echo "Key analysis shows it's for ${real}" + +if [ "$found" = "$real" ] ; then + echo "It's a match, I'm happy" + rm $key + rm $stderr + exit 0 +else + echo "Error: No match" + exit 1 +fi diff --git a/test/003-test-invalid-char.test/run.sh b/test/003-test-invalid-char.test/run.sh new file mode 100644 index 0000000..a59b826 --- /dev/null +++ b/test/003-test-invalid-char.test/run.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +${EXECUTABLE} -s foovalid1not + +if [ $? -eq 0 ]; then + exit 1 +else + exit 0 +fi diff --git a/test/003-test-invalid-char.test/stderr.expected b/test/003-test-invalid-char.test/stderr.expected new file mode 100644 index 0000000..f1652b2 --- /dev/null +++ b/test/003-test-invalid-char.test/stderr.expected @@ -0,0 +1,2 @@ +Error: search contains non-base-32 character(s): 1 +I cannot search for something that will never occur diff --git a/test/run-tests.sh b/test/run-tests.sh new file mode 100755 index 0000000..07647dd --- /dev/null +++ b/test/run-tests.sh @@ -0,0 +1,43 @@ +#!/bin/sh + + +fail() +{ + echo -e '[\e[1;31mFAIL\e[0m] '$i: $@ + exit 1 +} + +pass() +{ + echo -e '[\e[0;32mPASS\e[0m] '$i +} + +check_expected() +{ + [ -z $1 ] && echo WARN: check_expected called with no argument + if [ -f $1.expected ] ; then + diff $1.expected $1.tmp >/dev/null + if [ $? -ne 0 ] ; then + fail "$1 didn't match expected" + fi + fi +} + +pushd $(dirname $0) >/dev/null +EXECUTABLE="../../sand-leek" +for i in *.test ; do + pushd ${i} >/dev/null + ( . ./run.sh ) 2>stderr.tmp >stdout.tmp + if [ $? -ne 0 ] ; then + fail "script had non-zero return code" + fi + + check_expected stdout + check_expected stderr + + pass $i + + rm std{err,out}.tmp + popd >/dev/null +done +popd >/dev/null -- cgit v1.1