shlist

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

commit f19adebd48474e36cc17ff11b7a3a1d5fd5d0473
parent 077631d4d02ef340233793601eea3c11f7adf38e
Author: kyle <kyle@getaddrinfo.net>
Date:   Fri, 16 Oct 2015 23:00:16 -0600

sl: let the test suite execute perl scripts too

Diffstat:
Mrun_tests.sh | 39++++++++++++++++++++++++---------------
Mtests/bad_msg/test.sh | 0
Mtests/lib.sh | 5-----
Dtests/msg_type_1_missing_name | 0
Dtests/msg_type_1_missing_number | 0
Dtests/new_device/net.bin | 0
Atests/new_device/test.pl | 16++++++++++++++++
Dtests/new_device/test.sh | 22----------------------
Atests/new_device_bad_phnum/test.pl | 11+++++++++++
Dtests/new_device_bad_phnum/test.sh | 24------------------------
Dtests/new_list/register.bin | 0
Atests/new_list/test.pl | 32++++++++++++++++++++++++++++++++
Dtests/new_list/test.sh | 32--------------------------------
Atests/new_list_missing_devid/test.pl | 13+++++++++++++
Atests/new_list_missing_name/test.pl | 19+++++++++++++++++++
Atests/payload_size_zero/test.pl | 10++++++++++
Dtests/payload_size_zero/test.sh | 8--------
Atests/payload_too_large/test.pl | 15+++++++++++++++
Dtests/payload_too_large/test.sh | 9---------
Atests/testlib.pm | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20 files changed, 206 insertions(+), 115 deletions(-)

diff --git a/run_tests.sh b/run_tests.sh @@ -1,44 +1,53 @@ #!/bin/sh -# check if server is running -if [ `pgrep -f perl\ sl` ]; then - echo "server already running, great." -else +if ! pgrep -f perl\ sl; then echo "server not running, you need to start it!" exit 1 fi # if tput is available, we can do colors! -if [ `which tput` ]; then +if which tput; then RED=$(tput setaf 9 0 0) GREEN=$(tput setaf 10 0 0) + YELLOW=$(tput setaf 11 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 +count=1 +for t in `ls tests/*/test.*`; do CWD=$(pwd) - echo -n "$(dirname $t): " + printf "%3s " $count + echo -n "$(dirname $t): $YELLOW" # XXX: put PORT in the environment - cd $(dirname $t) && sh $(basename $t) $PORT + cd $(dirname $t) && ./$(basename $t) $PORT if [ $? -ne 0 ]; then - echo "\t$RED fail$RESET" - failed=$(($failed + 1)) + echo "$RED fail$RESET" + failed=$((failed + 1)) else - echo "\t$GREEN ok$RESET" - passed=$(($passed + 1)) + echo "$GREEN ok$RESET" + passed=$((passed + 1)) fi cd $CWD + # `ps -o pid= -p $SERVER_PID` + # if [ $? -eq 0 ]; then + # echo ">>> $RED server died!$RESET" + # exit 1 + # fi + + # clean up the database between runs sqlite3 db "delete from devices" + count=$((count + 1)) done echo "\n$passed$GREEN ok$RESET $failed$RED fail$RESET" + +if [ $failed -ne 0 ]; then + exit 1 +fi diff --git a/tests/bad_msg/test.sh b/tests/bad_msg/test.sh diff --git a/tests/lib.sh b/tests/lib.sh @@ -1,10 +1,5 @@ #!/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_type_1_missing_name b/tests/msg_type_1_missing_name Binary files differ. diff --git a/tests/msg_type_1_missing_number b/tests/msg_type_1_missing_number 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.pl b/tests/new_device/test.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +my $sock = new_socket(); +send_msg($sock, 0, "4038675309"); +my ($type, $response, $length) = recv_msg($sock); +close $sock; + +# verify response length is 32 random bytes encoded with base64 +if ($length != 43) { + fail "expected response length of 43, got $length"; +} diff --git a/tests/new_device/test.sh b/tests/new_device/test.sh @@ -1,22 +0,0 @@ -#!/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.pl b/tests/new_device_bad_phnum/test.pl @@ -0,0 +1,11 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +# send a new device message with a bad phone number +my $sock = new_socket(); +send_msg($sock, 0, "403867530&"); +close($sock); diff --git a/tests/new_device_bad_phnum/test.sh b/tests/new_device_bad_phnum/test.sh @@ -1,24 +0,0 @@ -#!/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.pl b/tests/new_list/test.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +# this test: +# - gets a new device id +# - creates a new list +# - receives new list response +# - verifies received information is congruent with what was sent + +my $sock = new_socket(); +send_msg($sock, 0, "4038675309"); +my ($type, $device_id, $length) = recv_msg($sock); + +fail "got response type $type, expected 0" if ($type != 0); +fail "expected response length of 43, got $length" if ($length != 43); + +my $list_name = "this is a new list"; +send_msg($sock, 1, "$device_id\0$list_name"); +my ($type2, $list_data, $length2) = recv_msg($sock); + +fail "got response type $type, expected 1" if ($type2 != 1); + +my ($id, $name, @members) = split("\0", $list_data); +my $id_length = length($id); + +fail "bad id length $id_length != 43" if ($id_length != 43); +fail "recv'd name '$name' not equal to '$list_name'" if ($name ne $list_name); +fail "list does not have exactly 1 member" if (@members != 1); diff --git a/tests/new_list/test.sh b/tests/new_list/test.sh @@ -1,32 +0,0 @@ -#!/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/new_list_missing_devid/test.pl b/tests/new_list_missing_devid/test.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +# this test: +# - gets a new device id +# - tries to create a new list with and invalid device id + +my $sock = new_socket(); +send_msg($sock, 1, "a\0some list over here"); diff --git a/tests/new_list_missing_name/test.pl b/tests/new_list_missing_name/test.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +# this test: +# - gets a new device id +# - tries to create a new list without a name + +my $sock = new_socket(); +send_msg($sock, 0, "4038675309"); +my ($type, $device_id, $length) = recv_msg($sock); + +fail "got response type $type, expected 0" if ($type != 0); +fail "expected response length of 43, got $length" if ($length != 43); + +send_msg($sock, 1, "$device_id\0"); diff --git a/tests/payload_size_zero/test.pl b/tests/payload_size_zero/test.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +my $sock = new_socket(); +send_msg($sock, 0, ""); +close $sock; diff --git a/tests/payload_size_zero/test.sh b/tests/payload_size_zero/test.sh @@ -1,8 +0,0 @@ -#!/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.pl b/tests/payload_too_large/test.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl -I.. + +use strict; +use warnings; + +use testlib; + +my $sock = new_socket(); + +# make a long string the stupid way, 4000 bytes +my $out; +$out .= "asdf" for (1..1000); + +send_msg($sock, 0, $out); +close $sock; diff --git a/tests/payload_too_large/test.sh b/tests/payload_too_large/test.sh @@ -1,9 +0,0 @@ -#!/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 diff --git a/tests/testlib.pm b/tests/testlib.pm @@ -0,0 +1,66 @@ +package testlib; +use strict; +use warnings; + +use Exporter qw(import); +use IO::Socket; + +our @EXPORT = qw(new_socket fail send_msg recv_msg); + +sub fail { + print shift; + exit 1; +} + +sub new_socket +{ + my $sock = new IO::Socket::INET( + LocalHost => '127.0.0.1', + PeerHost => '127.0.0.1', + PeerPort => 5437, + Proto => 'tcp' + ); + + die "error: socket creation failed: $!\n" unless $sock; + return $sock; +} + +sub send_msg +{ + my ($sock, $type, $contents) = @_; + + # send away + print $sock pack("nn", $type, length($contents)); + print $sock $contents; +} + +sub recv_msg +{ + my ($sock) = @_; + + # wait for response + my ($metadata, $type, $size); + my $bread = read($sock, $metadata, 4); + unless (defined $bread) { + fail "read(): $!\n"; + } + if ($bread != 4) { + fail "read() returned $bread instead of 4!"; + } + unless (($type, $size) = unpack("nn", $metadata)) { + fail "error unpacking metadata"; + } + + # XXX: do msg type upper bounds checking here + # fail "server sent $resp_type msg instead of $type" if ($resp_type != $type); + fail "bad message size not 0 <= $size < 1024" if ($size < 0 || $size > 1023); + + my $data; + if ((my $bread = read($sock, $data, $size)) != $size) { + fail "read() returned $bread instead of $size!"; + } + + return ($type, $data, $size); +} + +1;