From 62a1e1f1a4ece6808a1fb63c15fdfca127d958ab Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Sat, 6 May 2017 13:01:49 +0900 Subject: sandbox: preparing for testing --- sandbox/.gitignore | 2 +- sandbox/Makefile | 18 +++++-- sandbox/sandbox.h | 1 - sandbox/t/.gitignore | 2 + sandbox/t/Makefile | 12 +++++ sandbox/t/helpers/malloc.c | 27 ++++++++++ sandbox/t/t0000-basic.sh | 17 +++++++ sandbox/t/test-utils.sh | 119 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 sandbox/t/.gitignore create mode 100644 sandbox/t/Makefile create mode 100644 sandbox/t/helpers/malloc.c create mode 100755 sandbox/t/t0000-basic.sh create mode 100644 sandbox/t/test-utils.sh diff --git a/sandbox/.gitignore b/sandbox/.gitignore index d7c76f5..0c7bf96 100644 --- a/sandbox/.gitignore +++ b/sandbox/.gitignore @@ -1,3 +1,3 @@ /sandbox /runner -/*.o +*.o diff --git a/sandbox/Makefile b/sandbox/Makefile index 2a016c0..47e10b1 100644 --- a/sandbox/Makefile +++ b/sandbox/Makefile @@ -1,21 +1,29 @@ CFLAGS ?= -std=c11 -g -O3 -D_GNU_SOURCE -D_FORTIFY_SOURCE=2 -fPIE -fstack-protector-all \ - -Wall -Wextra -Wpedantic -Wshadow -Wmissing-declarations -Wpointer-arith -Wunused-macros -Wstrict-aliasing=2 + -Wall -Wextra -Wpedantic LDFLAGS ?= -pie -Wl,-z,relro,-z,now -lseccomp DEPS := sandbox.h config.h utils.h OBJS := main.o playground.o seccomp.o cgroup.o child.o all: sandbox -%.o: %.c $(DEPS) - $(CC) -c -o $@ $< $(CFLAGS) + sandbox: $(OBJS) $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) -install: all + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +runner: sandbox install -m 6755 -o root sandbox runner + +test: runner + $(MAKE) -C t + clean: $(RM) runner sandbox $(RM) *.o -.PHONY: all install clean sandbox + +.PHONY: all clean # per-object dependencies seccomp.o: config_seccomp.h diff --git a/sandbox/sandbox.h b/sandbox/sandbox.h index 74c3b68..c38aca8 100644 --- a/sandbox/sandbox.h +++ b/sandbox/sandbox.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/sandbox/t/.gitignore b/sandbox/t/.gitignore new file mode 100644 index 0000000..4d4147f --- /dev/null +++ b/sandbox/t/.gitignore @@ -0,0 +1,2 @@ +helpers/* +!helpers/*.[ch] diff --git a/sandbox/t/Makefile b/sandbox/t/Makefile new file mode 100644 index 0000000..84b989d --- /dev/null +++ b/sandbox/t/Makefile @@ -0,0 +1,12 @@ +PROVE ?= prove +POE_TEST_OPTS ?= + +T := t0000-basic.sh +HELPERS := helpers/malloc + +all: test + +test: $(HELPERS) + $(PROVE) $(T) :: $(POE_TEST_OPTS) + +.PHONY: all test diff --git a/sandbox/t/helpers/malloc.c b/sandbox/t/helpers/malloc.c new file mode 100644 index 0000000..9a732bf --- /dev/null +++ b/sandbox/t/helpers/malloc.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + unsigned long long size; + char *ptr; + + if (argc != 2) + abort(); + errno = 0; + size = strtoull(argv[1], NULL, 10); + if (errno) + abort(); +#if ULONG_MAX > SIZE_MAX + if (size > SIZE_MAX) + abort(); +#endif + + ptr = malloc(size); + if (!ptr) + return 1; + for (size_t i = 0; i < size; i += 1024) + ptr[i] = 123; +} diff --git a/sandbox/t/t0000-basic.sh b/sandbox/t/t0000-basic.sh new file mode 100755 index 0000000..2fd0b3d --- /dev/null +++ b/sandbox/t/t0000-basic.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +# t0000-basic: +# Test the very basic functionality of poe/sandbox. + +. ./test-utils.sh + +################################################################ +# Test framework +test_expect_success 'abc' ' + echo 123 +' +test_expect_success 'abcd' ' + $SANDBOX +' + +test_done diff --git a/sandbox/t/test-utils.sh b/sandbox/t/test-utils.sh new file mode 100644 index 0000000..f2d9a92 --- /dev/null +++ b/sandbox/t/test-utils.sh @@ -0,0 +1,119 @@ +# t/test-utils.sh: Test framwork for poe/sandbox + +while test $# -ne 0 +do + case "$1" in + -v|--verbose) + POE_TEST_VERBOSE=y + shift + ;; + *) + echo "error: unknown option '$1'" >&2 + exit 1 + ;; + esac +done + +# Internal functions + +if test "x$TERM" != xdumb && test -t 1 +then + say_color_ok=$(tput setaf 2) # green + say_color_error=$(tput setaf 1) # red + say_color_info=$(tput setaf 3) # yellow + say_color_= # normal + say_color_reset=$(tput sgr0) + say() { + eval "color=\$say_color_$1" + shift + echo "$color$*$say_color_reset" + } +else + say() { + echo $2 + } +fi + +error() { + say error "error: $*" + POE_TEST_EXIT=y + exit 1 +} + +test_count=0 +test_success=0 +test_failure=0 + +test_run() { + { + eval " + test -n \"$POE_TEST_VERBOSE\" && set -x + $1" &3 2>&4 + } 2>/dev/null +} + + +# Public helper functions + +test_expect_success() { + test $# = 2 || + error "test_expect_success expects 2 arguments" + + test_count=$(($test_count+1)) + if test_run "$2" + then + test_success=$(($test_success+1)) + say "" "ok $test_count - $1" + else + test_failure=$(($test_failure+1)) + say error "not ok $test_count - $1" + printf '%s\n' "$2" | sed -e 's/^/# /' + fi +} + +test_done() { + POE_TEST_EXIT=y + + case $test_failure in + 0) + say ok "# passed all $test_count test(s)" + say "" "1..$test_count" + exit 0 + ;; + *) + say error "# failed $test_failure among $test_count test(s)" + say "" "1..$test_count" + exit 1 + ;; + esac +} + + +# Prepare for running tests + +die() { + x=$? + test -n "$POE_TEST_EXIT" && + exit $x + say error "fatal: unexpected exit with status $x" + exit 1 +} +trap 'die' EXIT + +if test -n "$POE_TEST_VERBOSE" +then + exec 3>&1 4>&2 +else + exec 3>/dev/null 4>/dev/null +fi + + +# Prepare $SANDBOX variable + +x=$(dirname $(dirname $(readlink -f "$0"))) +if test -x "$x/runner" +then + SANDBOX=$x/runner +else + error "sandbox runner binary not found in '$x'" +fi -- cgit v1.2.3