]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/test/run-write.c
failtest: new module.
[ccan] / ccan / failtest / test / run-write.c
diff --git a/ccan/failtest/test/run-write.c b/ccan/failtest/test/run-write.c
new file mode 100644 (file)
index 0000000..5425517
--- /dev/null
@@ -0,0 +1,42 @@
+#include <stdlib.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <ccan/tap/tap.h>
+/* Include the C files directly. */
+#include <ccan/failtest/failtest.c>
+
+int main(void)
+{
+       int fd;
+       char *p;
+       char buf[] = "Hello world!";
+
+       plan_tests(5);
+
+       fd = open("run-write-scratchpad", O_RDWR|O_CREAT, 0600);
+       write(fd, buf, strlen(buf));
+       ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
+
+       p = failtest_malloc(100, "run-write.c", 1);
+       if (!p) {
+               /* We are the child.  Do a heap of writes. */
+               unsigned int i;
+
+               for (i = 0; i < strlen(buf)+1; i++)
+                       if (failtest_write(fd, "x", 1, "run-write.c", 1) == 1)
+                               break;
+               failtest_exit(0);
+       }
+
+       /* Seek pointer should be left alone! */
+       ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
+       /* Length should be restored. */
+       ok1(lseek(fd, 0, SEEK_END) == strlen(buf));
+       lseek(fd, 0, SEEK_SET);
+       ok1(read(fd, buf, strlen(buf)) == strlen("Hello world!"));
+       ok1(strcmp(buf, "Hello world!") == 0);
+
+       return exit_status();
+}