]> git.ozlabs.org Git - ccan/blob - ccan/failtest/test/run-write.c
failtest: generic cleanup hooks
[ccan] / ccan / failtest / test / run-write.c
1 #include <stdlib.h>
2 #include <setjmp.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <assert.h>
6 #include <ccan/tap/tap.h>
7 /* Include the C files directly. */
8 #include <ccan/failtest/failtest.c>
9
10 int main(void)
11 {
12         int fd;
13         char *p;
14         char buf[] = "Hello world!";
15
16         plan_tests(5);
17
18         fd = open("run-write-scratchpad", O_RDWR|O_CREAT, 0600);
19         write(fd, buf, strlen(buf));
20         ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
21
22         p = failtest_malloc(100, "run-write.c", 1);
23         if (!p) {
24                 /* We are the child.  Do a heap of writes. */
25                 unsigned int i;
26
27                 for (i = 0; i < strlen(buf)+1; i++)
28                         if (failtest_write(fd, "x", 1, "run-write.c", 1) == 1)
29                                 break;
30                 failtest_exit(0);
31         }
32
33         /* Seek pointer should be left alone! */
34         ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
35         /* Length should be restored. */
36         ok1(lseek(fd, 0, SEEK_END) == strlen(buf));
37         lseek(fd, 0, SEEK_SET);
38         ok1(read(fd, buf, strlen(buf)) == strlen("Hello world!"));
39         ok1(strcmp(buf, "Hello world!") == 0);
40
41         return exit_status();
42 }