From 0004064a27605bcbf7e5735a4777b6a7beed2a1e Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Wed, 19 Jun 2019 18:53:31 +0200 Subject: [PATCH] Break up test scripts --- makefile | 18 ++++++--- test/integrationtest.sh | 83 ----------------------------------------- test/startwebserver.sh | 8 ++++ test/stopwebserver.sh | 4 ++ test/test_get.sh | 40 ++++++++++++++++++++ test/test_head.sh | 38 +++++++++++++++++++ test/tests.sh | 12 ++++++ 7 files changed, 114 insertions(+), 89 deletions(-) delete mode 100755 test/integrationtest.sh create mode 100755 test/startwebserver.sh create mode 100755 test/stopwebserver.sh create mode 100755 test/test_get.sh create mode 100755 test/test_head.sh create mode 100755 test/tests.sh diff --git a/makefile b/makefile index 4a6f192..87db8a0 100644 --- a/makefile +++ b/makefile @@ -10,26 +10,32 @@ BUILDDIRS = $(patsubst ./src/%, ./build/%, $(shell find ./src/ -type d)) BINARY_NAME = server.out BINARY_OUT = ./build/${BINARY_NAME} +BINARY_OUT_BIN = ./bin/${BINARY} -include $(DEPS) -./build/%.o: ./src/%.cpp - ${CC} ${CFLAGS} -MMD -c $< -o $@ - ${BINARY_OUT}: directories ${OBJS} ${CC} ${CFLAGS} ${OBJS} ${LFLAGS} -o $@ -.PHONY: all clean check syntax directories +./build/%.o: ./src/%.cpp + ${CC} ${CFLAGS} -MMD -c $< -o $@ + +${BINARY_OUT_BIN}: ${BINARY_OUT} + cp -u $^ $@ + +.PHONY: all clean check tests syntax directories all: ${BINARY_OUT} clean: -rm -r ./build/ -check: ${BINARY_OUT} - cp -uv $^ ./bin/ +check: ${BINARY_OUT_BIN} cd ./bin/ && ./${BINARY_NAME} +tests: ${BINARY_OUT_BIN} + cd ./test/ && ./tests.sh + syntax: ${CPPS} ${CC} ${CFLAGS} -fsyntax-only $^ diff --git a/test/integrationtest.sh b/test/integrationtest.sh deleted file mode 100755 index 65e731a..0000000 --- a/test/integrationtest.sh +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/bash - -serverBinary="server.out" -serverWorkingDirectory="../bin" -serverWwwRoot="$serverWorkingDirectory/www" - -function KillServer() -{ - echo "Stopping server..." - killall "$serverBinary" -} - -function StartServer() -{ - echo "Starting server..." - oldPwd=$PWD - until pgrep "$serverBinary"; do - cd "$serverWorkingDirectory" && ./$serverBinary & - sleep 2 - done - cd "$oldPwd" -} - -function TestGetHeader() -{ - curlHeaderOutputFile="./temp" - if curl -I $1 --output $curlHeaderOutputFile 2> /dev/null; then - - regexPattern="Content\-Type: $2" - if egrep -o "$regexPattern" $curlHeaderOutputFile; then - echo "SUCCESS GET headers for $1 has correct MIME type" - else - echo "FAILURE GET headers for $1 contains bad MIME type" - echo "expected Content-Type: $2" - echo "actual $(egrep -o \"$regexPattern\" $curlHeaderOutputFile)" - fi - - rm $curlHeaderOutputFile - else - echo "FAILURE Cannot GET headers for $1" - fi -} - -function TestGetContent() -{ - downloadedFile="./temp" - if curl "$1" --output "$downloadedFile" 2> /dev/null; then - expectedMd5="$(md5sum $2 | awk '{print $1}')" - actualMd5="$(md5sum $downloadedFile | awk '{print $1}')" - - if [ "$expectedMd5" == "$actualMd5" ]; then - echo "SUCCESS GET request content matched for $1" - else - echo "FAILURE file MD5 differs for $1" - echo "expected $truthMd5" - echo "actual $actualMd5" - fi - - rm $downloadedFile - else - echo "FAILURE Cannot GET content for $1" - fi -} - -function TestGET() -{ - truthFile="$serverWwwRoot/$1" - getUrl="http://localhost:8080/$1" - - TestGetContent $getUrl $truthFile - TestGetHeader $getUrl $2 - echo "" -} - -StartServer -TestGET "index.html" 'text/html' -TestGET "robedude.png" 'image/png' -TestGET "hope.jpg" 'image/jpeg' -TestGET "nevada.mp3" 'audio/mpeg3' -TestGET "index.css" 'text/css' -TestGET "test.js" 'application/javascript' -TestGET "subdir/index.html" 'text/html' -KillServer \ No newline at end of file diff --git a/test/startwebserver.sh b/test/startwebserver.sh new file mode 100755 index 0000000..daf00e3 --- /dev/null +++ b/test/startwebserver.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Starting webserver..." + +until pgrep "$SERVER"; do + cd "$SERVER_DIR" && ./$SERVER & + sleep 2 +done \ No newline at end of file diff --git a/test/stopwebserver.sh b/test/stopwebserver.sh new file mode 100755 index 0000000..c5ea697 --- /dev/null +++ b/test/stopwebserver.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +echo "Stopping webserver..." +killall "$SERVER" \ No newline at end of file diff --git a/test/test_get.sh b/test/test_get.sh new file mode 100755 index 0000000..fb78013 --- /dev/null +++ b/test/test_get.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +function TestGetContent() +{ + downloadedFile="./temp" + if curl "$1" --output "$downloadedFile" 2> /dev/null; then + expectedMd5="$(md5sum $2 | awk '{print $1}')" + actualMd5="$(md5sum $downloadedFile | awk '{print $1}')" + + if [ "$expectedMd5" == "$actualMd5" ]; then + echo "SUCCESS for $1" + else + echo "FAILURE MD5 differs for $1" + fi + + rm $downloadedFile + else + echo "FAILURE Cannot GET $1" + fi +} + +function TestGET() +{ + truthFile="$WWW_ROOT/$1" + getUrl="http://localhost:8080/$1" + + TestGetContent $getUrl $truthFile + echo "" +} + +echo "********" +echo "GET TEST" +echo "********" +TestGET "index.html" +TestGET "robedude.png" +TestGET "hope.jpg" +TestGET "nevada.mp3" +TestGET "index.css" +TestGET "test.js" +TestGET "subdir/index.html" \ No newline at end of file diff --git a/test/test_head.sh b/test/test_head.sh new file mode 100755 index 0000000..c40048c --- /dev/null +++ b/test/test_head.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +function TestGetHeader() +{ + curlHeaderOutputFile="./temp" + if curl -I $1 --output $curlHeaderOutputFile 2> /dev/null; then + regexPattern="Content\-Type: $2" + headerContentType="$(egrep -o "$regexPattern" $curlHeaderOutputFile)" + if [ -n "$headerContentType" ]; then + echo "SUCCESS GET headers for $1 has correct MIME type" + else + echo "FAILURE GET headers for $1 contains bad MIME type (expected but got <$headerContentType>)" + fi + + rm $curlHeaderOutputFile + else + echo "FAILURE Cannot GET headers for $1" + fi +} + +function TestHEAD() +{ + getUrl="http://localhost:8080/$1" + + TestGetHeader $getUrl $2 + echo "" +} + +echo "*********" +echo "HEAD TEST" +echo "*********" +TestHEAD "index.html" 'text/html' +TestHEAD "robedude.png" 'image/png' +TestHEAD "hope.jpg" 'image/jpeg' +TestHEAD "nevada.mp3" 'audio/mpeg3' +TestHEAD "index.css" 'text/css' +TestHEAD "test.js" 'application/javascript' +TestHEAD "subdir/index.html" 'text/html' \ No newline at end of file diff --git a/test/tests.sh b/test/tests.sh new file mode 100755 index 0000000..4babe22 --- /dev/null +++ b/test/tests.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +export SERVER="server.out" +export SERVER_DIR="../bin/" +export WWW_ROOT="../bin/www" + +./startwebserver.sh + +./test_head.sh +./test_get.sh + +./stopwebserver.sh \ No newline at end of file