]> git.ozlabs.org Git - ccan-lca-2011.git/blobdiff - ccan/oserver/test/run.c
lca2011: use failtest
[ccan-lca-2011.git] / ccan / oserver / test / run.c
index f6c5b45762ff30a413fd840baaa1577e1321c671..b8e9dc3042c67312aade84acc9a4a95abaabf1a3 100644 (file)
@@ -1,51 +1,54 @@
-#include <ccan/oserver/oserver.h>
+#include <ccan/failtest/failtest_override.h>
 #include <ccan/oserver/oserver.c>
+#include <ccan/oserver/oserver.h>
 #include <ccan/str/str.h>
-#include <ccan/array_size/array_size.h>
+#include <ccan/foreach/foreach.h>
 #include <ccan/tap/tap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <ccan/failtest/failtest.h>
 #include <fcntl.h>
 #include <string.h>
-#include <sys/wait.h>
+#include <ccan/failtest/failtest_undo.h>
+
+static void exit_test(void)
+{
+       failtest_exit(exit_status());
+}
 
-int main(void)
+int main(int argc, char *argv[])
 {
-       int fd, i;
-       int status;
+       int fd;
        char buf[200];
-       const char *input[] = { "This is a test\n",
-                               "This is a test\r",
-                               "This is a test\r\n",
-                               "This is a test\nWith extra",
-                               "This is a test\rWith extra",
-                               "This is a test\r\nWith extra" };
+       const char *input;
 
        /* This is how many tests you plan to run */
-       plan_tests(4 * ARRAY_SIZE(input));
-
-       for (i = 0; i < ARRAY_SIZE(input); i++) {
+       plan_tests(3 * 6);
+       failtest_init(argc, argv);
+       tap_fail_callback = exit_test;
+
+       foreach_ptr(input,
+                   "This is a test\n",
+                   "This is a test\r",
+                   "This is a test\r\n",
+                   "This is a test\nWith extra",
+                   "This is a test\rWith extra",
+                   "This is a test\r\nWith extra") {
                fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600);
 
-               write(fd, input[i], strlen(input[i]));
+               write(fd, input, strlen(input));
                lseek(fd, 0, SEEK_SET);
 
-               if (fork() == 0)
-                       oserver_serve(fd);
-
-               wait(&status);
-
-               ok1(WIFEXITED(status));
-               ok1(WEXITSTATUS(status) == 0);
+               ok1(oserver_serve(fd));
 
                lseek(fd, 0, SEEK_SET);
                buf[read(fd, buf, sizeof(buf)-1)] = '\0';
 
-               ok1(strncmp(buf, input[i], strlen("This is a test")) == 0);
+               ok1(strncmp(buf, input, strlen("This is a test")) == 0);
                ok1(streq(buf + strlen("This is a test") + 1,
                          "Louder, like this: 'THIS IS A TEST'\r\n"));
        }
-               
+
        /* This exits depending on whether all tests passed */
-       return exit_status();
+       failtest_exit(exit_status());
 }