summaryrefslogtreecommitdiff
path: root/lib/ccan/failtest/test/run-write.c
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-02-22 14:59:32 +1030
committerAmitay Isaacs <amitay@gmail.com>2012-03-07 13:16:16 +1100
commit361f3ea9ee577c5a3e2fed687a0b417b257c31de (patch)
tree6d356c3aa64317c609ff4e208be76e18996a55f8 /lib/ccan/failtest/test/run-write.c
parent4f5412dda687c3ff76b426842bf284d01d56a997 (diff)
downloadsamba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.gz
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.bz2
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.zip
lib/ccan: import failtest and required ccan modules for TDB2 unit tests.
New modules: failtest, list, time, read_write_all and tlist. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/ccan/failtest/test/run-write.c')
-rw-r--r--lib/ccan/failtest/test/run-write.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/ccan/failtest/test/run-write.c b/lib/ccan/failtest/test/run-write.c
new file mode 100644
index 0000000000..6a712fec53
--- /dev/null
+++ b/lib/ccan/failtest/test/run-write.c
@@ -0,0 +1,51 @@
+/* Include the C files directly. */
+#include <ccan/failtest/failtest.c>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <ccan/tap/tap.h>
+
+int main(int argc, char *argv[])
+{
+ int fd;
+ char *p;
+ char buf[] = "Hello world!";
+
+ plan_tests(5);
+ failtest_init(argc, argv);
+
+ fd = failtest_open("run-write-scratchpad", __FILE__, __LINE__,
+ O_RDWR|O_CREAT, 0600);
+ /* Child will fail, ignore. */
+ if (fd < 0)
+ failtest_exit(0);
+ if (write(fd, buf, strlen(buf)) != strlen(buf))
+ abort();
+ ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
+
+ p = failtest_malloc(100, __FILE__, __LINE__);
+ 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, __FILE__, __LINE__)
+ == 1)
+ break;
+ failtest_close(fd, __FILE__, __LINE__);
+ 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);
+ failtest_close(fd, __FILE__, __LINE__);
+
+ return exit_status();
+}