]> git.ozlabs.org Git - ccan-lca-2011.git/blobdiff - ccan/oserver/test/run.c
lca2011: test
[ccan-lca-2011.git] / ccan / oserver / test / run.c
diff --git a/ccan/oserver/test/run.c b/ccan/oserver/test/run.c
new file mode 100644 (file)
index 0000000..3a93be8
--- /dev/null
@@ -0,0 +1,40 @@
+#include <ccan/oserver/oserver.h>
+#include <ccan/oserver/oserver.c>
+#include <ccan/str/str.h>
+#include <ccan/tap/tap.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/wait.h>
+
+int main(void)
+{
+       int fd;
+       int status;
+       char buf[200];
+
+       /* This is how many tests you plan to run */
+       plan_tests(3);
+
+       fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600);
+       write(fd, "This is a test\n", sizeof("This is a test\n"));
+       lseek(fd, 0, SEEK_SET);
+
+       if (fork() == 0)
+               oserver_serve(fd);
+
+       wait(&status);
+
+       ok1(WIFEXITED(status));
+       ok1(WEXITSTATUS(status) == 0);
+
+       lseek(fd, 0, SEEK_SET);
+       buf[read(fd, buf, sizeof(buf)-1)] = '\0';
+
+       ok1(streq(buf, "This is a test\n"
+                 "Louder, like this: 'THIS IS A TEST'\r\n"));
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}