Automate tests
authornorly <ny-git@enpas.org>
Wed, 19 Jun 2013 19:20:10 +0000 (20:20 +0100)
committernorly <ny-git@enpas.org>
Thu, 20 Jun 2013 21:10:23 +0000 (22:10 +0100)
19 files changed:
.gitignore
Makefile
tests/01-noop32.test [new file with mode: 0644]
tests/03-injection32.test [new file with mode: 0644]
tests/05-detour.test [new file with mode: 0644]
tests/06-data256mb.test [new file with mode: 0644]
tests/Makefile [new file with mode: 0644]
tests/boilerplate.sh [new file with mode: 0755]
tests/reference/puts-alternative32.o [new file with mode: 0644]
tests/reference/putsmain32 [new file with mode: 0755]
tests/reference/putsmain32-cloned [new file with mode: 0755]
tests/reference/putsmain32-with-puts-alternative32 [new file with mode: 0755]
tests/runtests.sh [new file with mode: 0755]
tests/src/Makefile [new file with mode: 0644]
tests/src/data256mb.c [new file with mode: 0644]
tests/src/puts_alternative.c [new file with mode: 0644]
tests/src/puts_noarg.c [new file with mode: 0644]
tests/src/putsmain.c [new file with mode: 0644]
tests/src/putsmainsub.c [new file with mode: 0644]

index a76b71259d502abab5124d7fd00b2e828210b52a..4c3bcf651774449f7ca8a2b6f08d03b1c7901a3d 100644 (file)
@@ -1,4 +1,5 @@
 /build
+/tests/build
 .o
 .a
 .so
index c8dee7616279a3a2a91c2fc104a1da46b14a53fa..c93b2abc61d0d24440717d1ce48fcd970fd07ef5 100644 (file)
--- 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 (file)
index 0000000..145bae3
--- /dev/null
@@ -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 (file)
index 0000000..235f243
--- /dev/null
@@ -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 (file)
index 0000000..c46ef2f
--- /dev/null
@@ -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 (file)
index 0000000..55cfcd2
--- /dev/null
@@ -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 (file)
index 0000000..757ea21
--- /dev/null
@@ -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 (executable)
index 0000000..2ebafa2
--- /dev/null
@@ -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 (file)
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 (executable)
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 (executable)
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 (executable)
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 (executable)
index 0000000..1cf2665
--- /dev/null
@@ -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 (file)
index 0000000..1a2b177
--- /dev/null
@@ -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 (file)
index 0000000..2cd3849
--- /dev/null
@@ -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 (file)
index 0000000..21d65cb
--- /dev/null
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+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 (file)
index 0000000..fe7d494
--- /dev/null
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+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 (file)
index 0000000..9b2ce3c
--- /dev/null
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+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 (file)
index 0000000..63d4171
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+void sub()
+{
+  puts("sub() called.");
+}
+
+int main()
+{
+  sub();
+
+  return 0;
+}