shlist

share and manage lists between multiple people
Log | Files | Refs

commit 9c1cac1c0fee573ebbb2bf9828bc6ba1c1c99bce
parent f2fa579afd1ba433e97bbf9d42725700224e3f74
Author: kyle <kyle@getaddrinfo.net>
Date:   Fri, 16 Oct 2015 01:53:09 -0600

sl: start getting a better test suite going

Diffstat:
Agen_enums.sh | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mrun_tests.sh | 44+++++++++++++++++++++++++++++++++++++++++---
Dtests/bad_msg | 2--
Atests/bad_msg/test.sh | 3+++
Atests/lib.sh | 11+++++++++++
Dtests/msg_size_zero | 0
Dtests/msg_too_large | 0
Atests/new_device/net.bin | 0
Atests/new_device/test.sh | 22++++++++++++++++++++++
Atests/new_device_bad_phnum/test.sh | 24++++++++++++++++++++++++
Atests/new_list/register.bin | 0
Atests/new_list/test.sh | 32++++++++++++++++++++++++++++++++
Atests/payload_size_zero/test.sh | 8++++++++
Atests/payload_too_large/test.sh | 9+++++++++
14 files changed, 206 insertions(+), 5 deletions(-)

diff --git a/gen_enums.sh b/gen_enums.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +MSG_TYPES="msg_new_device msg_new_list msg_update_friends msg_list_request" +MSG_TYPES="${MSG_TYPES} msg_join_list msg_leave_list msg_list_items" +MSG_TYPES="${MSG_TYPES} msg_new_list_item" + +WARN_HEADER="GENERaTED @ $(date) BY ${0}" + +OBJC_PATH="ios/shlist/MsgTypes.h" +PERL_PATH="msg_types.pl" +JAVA_PATH="android/shlist/app/src/main/java/drsocto/shlist/MsgTypes.java" +TEST_PATH="tests/net_enums.sh" + +# Objective C message type header for ios +echo "/* ${WARN_HEADER} */" > $OBJC_PATH +echo "" >> $OBJC_PATH +echo "enum MSG_TYPES {" >> $OBJC_PATH +i=0 +for msg in $MSG_TYPES; do + echo -e "\t$msg = $i," >> $OBJC_PATH + i=$((i + 1)) +done +echo "};" >> $OBJC_PATH + +# Perl source file constants for the server +echo "#!/usr/bin/env perl -w" > $PERL_PATH +echo "# ${WARN_HEADER}" >> $PERL_PATH +echo "" >> $PERL_PATH +echo "my @msg_handlers = (" >> $PERL_PATH +for msg in $MSG_TYPES; do + echo "\t\\&$msg," >> $PERL_PATH +done +echo ");" >> $PERL_PATH + +# Java message enumerations for android +echo "/* ${WARN_HEADER} */ " > $JAVA_PATH +echo "" >> $JAVA_PATH +echo "public enum MsgTypes {" >> $JAVA_PATH +i=0 +for msg in $MSG_TYPES; do + echo -e "\t$msg\t(${i})," >> $JAVA_PATH + i=$((i + 1)) +done +echo "};" >> $JAVA_PATH + +# shell constants for test suite use +echo "#!/bin/sh" > $TEST_PATH +echo "# $WARN_HEADER" >> $TEST_PATH +echo "" >> $TEST_PATH + +i=0 +for msg in $MSG_TYPES; do + hex_bytes=$(printf "%02x" $i) + echo "export $msg=00$hex_bytes" >> $TEST_PATH + i=$((i + 1)) +done diff --git a/run_tests.sh b/run_tests.sh @@ -1,6 +1,44 @@ #!/bin/sh -echo running tests -for f in `ls tests/*`; do - nc 127.0.0.1 5437 < $f +# check if server is running +if [ `pgrep -f perl\ sl` ]; then + echo "server already running, great." +else + echo "server not running, you need to start it!" + exit 1 +fi + +# if tput is available, we can do colors! +if [ `which tput` ]; then + RED=$(tput setaf 9 0 0) + GREEN=$(tput setaf 10 0 0) + RESET=$(tput sgr0) +fi + +# bring in the automatically generated message type environment variables +. tests/net_enums.sh + +export TESTS=asdf + +PORT=5437 + +passed=0 +failed=0 +for t in `ls tests/*/test.sh`; do + CWD=$(pwd) + echo -n "$(dirname $t): " + + # XXX: put PORT in the environment + cd $(dirname $t) && sh $(basename $t) $PORT + if [ $? -ne 0 ]; then + echo "\t$RED fail$RESET" + failed=$(($failed + 1)) + else + echo "\t$GREEN ok$RESET" + passed=$(($passed + 1)) + fi + cd $CWD + + sqlite3 db "delete from devices" done +echo "\n$passed$GREEN ok$RESET $failed$RED fail$RESET" diff --git a/tests/bad_msg b/tests/bad_msg @@ -1 +0,0 @@ -ºÝ -\ No newline at end of file diff --git a/tests/bad_msg/test.sh b/tests/bad_msg/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo -n "baddbedd" | xxd -r -p | nc 127.0.0.1 5437 diff --git a/tests/lib.sh b/tests/lib.sh @@ -0,0 +1,11 @@ +#!/bin/sh + + +send_msg() { + echo "$1" | xxd -r -p | nc -N 127.0.0.1 5437 > $OUT +} + +fail() { + echo -n "$1" + exit 1 +} diff --git a/tests/msg_size_zero b/tests/msg_size_zero Binary files differ. diff --git a/tests/msg_too_large b/tests/msg_too_large Binary files differ. diff --git a/tests/new_device/net.bin b/tests/new_device/net.bin Binary files differ. diff --git a/tests/new_device/test.sh b/tests/new_device/test.sh @@ -0,0 +1,22 @@ +#!/bin/sh +. ../lib.sh + +OUT=$(mktemp) +# -N disconnects after input EOF +nc -N 127.0.0.1 5437 < net.bin | tail -c +5 > $OUT +if [ $? -ne 0 ]; then + rm $OUT + fail "nc -N exited $?" +fi + +# verify that we get exactly the right number of bytes back +BYTES=$(wc -c $OUT | tr -s ' ' | cut -d ' ' -f 2) +if [ $? -ne 0 ]; then + rm $OUT + fail "wc -c exited $?" +fi +rm $OUT + +if [ $BYTES -ne 43 ]; then + fail "expected 43 bytes, got $BYTES" +fi diff --git a/tests/new_device_bad_phnum/test.sh b/tests/new_device_bad_phnum/test.sh @@ -0,0 +1,24 @@ +#!/bin/sh +. ../lib.sh + +hex_str="0000000a32333136373139383725" + +OUT=$(mktemp) +# -N disconnects after input EOF +echo -n $hex_str | xxd -r -p | nc -N 127.0.0.1 5437 | tail -c +5 > $OUT +if [ $? -ne 0 ]; then + rm $OUT + fail "nc -N exited $?" +fi + +# verify that we get exactly the right number of bytes back +BYTES=$(wc -c $OUT | tr -s ' ' | cut -d ' ' -f 2) +if [ $? -ne 0 ]; then + rm $OUT + fail "wc -c exited $?" +fi +rm $OUT + +if [ $BYTES -ne 0 ]; then + fail "expected 43 bytes, got $BYTES" +fi diff --git a/tests/new_list/register.bin b/tests/new_list/register.bin Binary files differ. diff --git a/tests/new_list/test.sh b/tests/new_list/test.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +. ../lib.sh + +DEV_ID=$(nc -N 127.0.0.1 5437 < register.bin | tail -c +5) +if [ $? -ne 0 ]; then + fail "nc -N exited $?" +fi + +# echo -n "device id is $DEV_ID" +dev_id_hex=$(echo -n $DEV_ID | xxd -p) +list_name_hex=$(echo -n "some new list that's" | xxd -p) +hex_str="${msg_new_list}0040${dev_id_hex}00${list_name_hex}" + +OUT=$(mktemp) +echo -n $hex_str | xxd -r -p | nc -N 127.0.0.1 5437 > $OUT +if [ $? -ne 0 ]; then + rm $OUT + fail "nc -N exited $?" +fi + +# 6 six_byte_file | del white | get the '6' +BYTES=$(wc -c $OUT | tr -s ' ' | cut -d ' ' -f 2) +if [ $? -ne 0 ]; then + rm $OUT + fail "wc -c exited $?" +fi +rm $OUT + +if [ $BYTES -eq 0 ]; then + fail "bytes was zero" +fi diff --git a/tests/payload_size_zero/test.sh b/tests/payload_size_zero/test.sh @@ -0,0 +1,8 @@ +#!/bin/sh +. ../lib.sh + +# send valid message type with 0 length message body +echo "00010000" | xxd -r -p | nc 127.0.0.1 5437 +if [ $? -ne 0 ]; then + fail "nc exited $?" +fi diff --git a/tests/payload_too_large/test.sh b/tests/payload_too_large/test.sh @@ -0,0 +1,9 @@ +#!/bin/sh +. ../lib.sh + +# send valid message type with 0 length message body +# we expect the other end to hang up +echo "00010F00" | xxd -r -p | nc 127.0.0.1 5437 +if [ $? -ne 0 ]; then + fail "nc exited $?" +fi