From: norly Date: Wed, 19 Jun 2013 19:20:10 +0000 (+0100) Subject: Automate tests X-Git-Url: https://git.enpas.org/?p=centaur.git;a=commitdiff_plain;h=7f1a29e9e33059dfebdc24bb3ffaa3dac46b58f1 Automate tests --- diff --git a/.gitignore b/.gitignore index a76b712..4c3bcf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build +/tests/build .o .a .so diff --git a/Makefile b/Makefile index c8dee76..c93b2ab 100644 --- a/Makefile +++ b/Makefile @@ -25,11 +25,7 @@ default: $(EXE) .PHONY: check check: $(EXE) - $(error the re-layouting has broken make check for now, sorry.) - $(EXE) $(EXE) -o testexe - @cmp $(EXE) testexe - @rm testexe - @echo "Check successful." + make -C tests check .PHONY: debug @@ -53,6 +49,7 @@ clean: rm -f $(OBJS) rm -f $(TESTEXES) rm -rf $(BUILDDIR)/ + make -C tests clean .PHONY: distclean diff --git a/tests/01-noop32.test b/tests/01-noop32.test new file mode 100644 index 0000000..145bae3 --- /dev/null +++ b/tests/01-noop32.test @@ -0,0 +1,10 @@ +#!/bin/sh +source ./boilerplate.sh + +elfucli --input reference/putsmain32 \ + --output $BUILDDIR/putsmain32-cloned +test_check_retval + +cmp reference/putsmain32-cloned \ + $BUILDDIR/putsmain32-cloned +test_check_retval diff --git a/tests/03-injection32.test b/tests/03-injection32.test new file mode 100644 index 0000000..235f243 --- /dev/null +++ b/tests/03-injection32.test @@ -0,0 +1,11 @@ +#!/bin/sh +source ./boilerplate.sh + +elfucli --input reference/putsmain32 \ + --reladd reference/puts-alternative32.o \ + --output $BUILDDIR/putsmain32-with-puts-alternative32 +test_check_retval + +cmp reference/putsmain32-with-puts-alternative32 \ + $BUILDDIR/putsmain32-with-puts-alternative32 +test_check_retval diff --git a/tests/05-detour.test b/tests/05-detour.test new file mode 100644 index 0000000..c46ef2f --- /dev/null +++ b/tests/05-detour.test @@ -0,0 +1,11 @@ +#!/bin/sh +source ./boilerplate.sh + +elfucli --input $BUILDDIR/putsmainsub \ + --reladd $BUILDDIR/puts_noarg.o \ + --detour sub,puts_noarg \ + --output $BUILDDIR/putsmainsub-with-puts-noarg-detour +test_check_retval + +$BUILDDIR/putsmainsub-with-puts-noarg-detour | grep -q "puts_noarg() is returning." +test_check_retval diff --git a/tests/06-data256mb.test b/tests/06-data256mb.test new file mode 100644 index 0000000..55cfcd2 --- /dev/null +++ b/tests/06-data256mb.test @@ -0,0 +1,10 @@ +#!/bin/sh +source ./boilerplate.sh + +elfucli --input $BUILDDIR/putsmain \ + --reladd $BUILDDIR/data256mb.o \ + --output $BUILDDIR/putsmain-with-data256mb +test_check_retval + +$BUILDDIR/putsmain-with-data256mb | grep -q "puts() #2 called in main()" +test_check_retval diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..757ea21 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,37 @@ +BUILDDIR = build +SRCDIR = src + +SOURCES = $(shell find $(SRCDIR)/ -iname "*.c") +OBJS = $(patsubst $(SRCDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES)) +EXENAMES = putsmain putsmainsub +EXES = $(patsubst %, $(BUILDDIR)/%, $(EXENAMES)) + +CFLAGS = -Wall -pedantic + + + +.PHONY: testbase +testbase: $(OBJS) $(EXES) + + +.PHONY: check +check: testbase + ./runtests.sh + + +$(BUILDDIR)/putsmain: $(SRCDIR)/putsmain.c + @if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi + gcc $(CFLAGS) -o $@ $^ + +$(BUILDDIR)/putsmainsub: $(SRCDIR)/putsmainsub.c + @if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi + gcc $(CFLAGS) -o $@ $^ + +$(BUILDDIR)/%.o: $(SRCDIR)/%.c + @if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi + gcc $(CFLAGS) -c -o $@ $< + + +.PHONY: clean +clean: + rm -rf $(BUILDDIR)/ diff --git a/tests/boilerplate.sh b/tests/boilerplate.sh new file mode 100755 index 0000000..2ebafa2 --- /dev/null +++ b/tests/boilerplate.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +BUILDDIR=${BUILDDIR:-build} +mkdir -p $BUILDDIR + +function test_check_retval +{ + TEST_RETVAL=$? + + if [ $TEST_RETVAL != 0 ] + then + exit $TEST_RETVAL + fi +} diff --git a/tests/reference/puts-alternative32.o b/tests/reference/puts-alternative32.o new file mode 100644 index 0000000..cc78018 Binary files /dev/null and b/tests/reference/puts-alternative32.o differ diff --git a/tests/reference/putsmain32 b/tests/reference/putsmain32 new file mode 100755 index 0000000..9381a77 Binary files /dev/null and b/tests/reference/putsmain32 differ diff --git a/tests/reference/putsmain32-cloned b/tests/reference/putsmain32-cloned new file mode 100755 index 0000000..58328b4 Binary files /dev/null and b/tests/reference/putsmain32-cloned differ diff --git a/tests/reference/putsmain32-with-puts-alternative32 b/tests/reference/putsmain32-with-puts-alternative32 new file mode 100755 index 0000000..953ddad Binary files /dev/null and b/tests/reference/putsmain32-with-puts-alternative32 differ diff --git a/tests/runtests.sh b/tests/runtests.sh new file mode 100755 index 0000000..1cf2665 --- /dev/null +++ b/tests/runtests.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +PATH="$PATH:$PWD/../build" + +for testfile in *.test +do + testname=${testfile%*.test} + + echo -en "\033[1m\033[37m[\033[34m \033[37m]\033[0m $testname" + bash $testfile > /dev/null 2>&1 + if [ $? -ne 0 ] + then + echo -e "\r\033[1m\033[37m[\033[31mFAILED\033[37m]\033[0m $testname" + echo -e "\033[1m\033[37m[\033[34m RERUN\033[37m]\033[0m $testname" + bash $testfile + echo -e "\033[1m\033[37m[\033[34mENDRUN\033[37m]\033[0m $testname" + else + echo -e "\r\033[1m\033[37m[\033[32m OK \033[37m]\033[0m $testname" + fi +done diff --git a/tests/src/Makefile b/tests/src/Makefile new file mode 100644 index 0000000..1a2b177 --- /dev/null +++ b/tests/src/Makefile @@ -0,0 +1,22 @@ +CFLAGS := -Wall -pedantic +EXES := putsmain putsmainsub brkmain +OBJS := puts_noarg.o puts_alternative.o puts_data.o +TARGETS := $(EXES) $(OBJS) + + +all: $(TARGETS) + + +putsmain: putsmain.c + gcc $(CFLAGS) $^ -o $@ + +putsmainsub: putsmainsub.c + gcc $(CFLAGS) $^ -o $@ + +.c.o: + gcc $(CFLAGS) -c $< -o $@ + + +.PHONY: clean +clean: + rm -f $(TARGETS) diff --git a/tests/src/data256mb.c b/tests/src/data256mb.c new file mode 100644 index 0000000..2cd3849 --- /dev/null +++ b/tests/src/data256mb.c @@ -0,0 +1,2 @@ +/* 256 MB worth of data. */ +char data256mb[256*1024*1024] = {1}; diff --git a/tests/src/puts_alternative.c b/tests/src/puts_alternative.c new file mode 100644 index 0000000..21d65cb --- /dev/null +++ b/tests/src/puts_alternative.c @@ -0,0 +1,10 @@ +#include + +void puts_alternative(char *arg) +{ + puts("puts_alternative() has been reached."); + + puts(arg); + + puts("puts_alternative() is returning."); +} diff --git a/tests/src/puts_noarg.c b/tests/src/puts_noarg.c new file mode 100644 index 0000000..fe7d494 --- /dev/null +++ b/tests/src/puts_noarg.c @@ -0,0 +1,7 @@ +#include + +void puts_noarg() +{ + puts("puts_noarg() has been reached."); + puts("puts_noarg() is returning."); +} diff --git a/tests/src/putsmain.c b/tests/src/putsmain.c new file mode 100644 index 0000000..9b2ce3c --- /dev/null +++ b/tests/src/putsmain.c @@ -0,0 +1,9 @@ +#include + +int main() +{ + puts("puts() #1 called in main()"); + puts("puts() #2 called in main()"); + + return 0; +} diff --git a/tests/src/putsmainsub.c b/tests/src/putsmainsub.c new file mode 100644 index 0000000..63d4171 --- /dev/null +++ b/tests/src/putsmainsub.c @@ -0,0 +1,13 @@ +#include + +void sub() +{ + puts("sub() called."); +} + +int main() +{ + sub(); + + return 0; +}