raw: Add run_as_child_process function.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 21 May 2011 16:50:23 +0000 (19:50 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 21 May 2011 16:50:25 +0000 (19:50 +0300)
Allows running a test function in the context of another process.

src/raw/test/Makefile
src/raw/test/test.h
src/raw/test/test_common.c [new file with mode: 0644]

index 793a511..fba469b 100644 (file)
@@ -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:
index a830632..fe5c281 100644 (file)
@@ -14,6 +14,12 @@ extern "C" {
 #include <stdio.h>
 #include <string.h>
 
+/* 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 (file)
index 0000000..1a7b406
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Test suite functions.
+ *
+ * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#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");
+}