From 5c3fca7e54e1cb435921ff6cc2c99185a39450a5 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sat, 21 May 2011 19:50:23 +0300 Subject: [PATCH] raw: Add run_as_child_process function. Allows running a test function in the context of another process. --- src/raw/test/Makefile | 2 +- src/raw/test/test.h | 6 ++++++ src/raw/test/test_common.c | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/raw/test/test_common.c diff --git a/src/raw/test/Makefile b/src/raw/test/Makefile index 793a511..fba469b 100644 --- a/src/raw/test/Makefile +++ b/src/raw/test/Makefile @@ -10,7 +10,7 @@ test: test.o test_sw_socket.o test_sw_bind.o test_sw_getsockname.o \ test_sw_sendto.o test_sw_recvfrom.o \ test_sw_sendmsg.o test_sw_recvmsg.o \ test_sw_setsockopt.o test_sw_getsockopt.o \ - test_sw_close.o test_dummy.o + test_sw_close.o test_dummy.o test_common.o \ ../swift_raw.o ../swift_list.o clean: diff --git a/src/raw/test/test.h b/src/raw/test/test.h index a830632..fe5c281 100644 --- a/src/raw/test/test.h +++ b/src/raw/test/test.h @@ -14,6 +14,12 @@ extern "C" { #include #include +/* Test function type. */ +typedef void (*test_fn)(void); + +/* Run test function f in another process. */ +void run_as_child_process(test_fn f); + /* * uncommend EXIT_IF_FAIL macro in order to stop test execution * at first failed test diff --git a/src/raw/test/test_common.c b/src/raw/test/test_common.c new file mode 100644 index 0000000..1a7b406 --- /dev/null +++ b/src/raw/test/test_common.c @@ -0,0 +1,44 @@ +/* + * Test suite functions. + * + * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro + */ + +#include +#include +#include + +#include +#include +#include + +#include "util.h" +#include "test.h" + +/* Run test function f in another process. */ +void run_as_child_process(test_fn f) +{ + pid_t pid; + int status; + int rc; + + pid = fork(); + switch (pid) { + case -1: /* error */ + ERR("fork"); + exit(EXIT_FAILURE); + + case 0: /* child process */ + /* Run test function. */ + f(); + exit(EXIT_SUCCESS); + break; + + default: /* parent process */ + break; + } + + /* Wait for child process. */ + rc = waitpid(pid, &status, 0); + DIE(rc < 0, "waitpid"); +} -- 2.20.1