]> git.ozlabs.org Git - ccan/blob - ccan/failtest/test/run-write.c
d3d7c6041871c96884e28c488a0f86a1982a9f24
[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 = failtest_open("run-write-scratchpad", "run-write.c", 1,
19                            O_RDWR|O_CREAT, 0600);
20         /* Child will fail, ignore. */
21         if (fd < 0)
22                 failtest_exit(0);
23         write(fd, buf, strlen(buf));
24         ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
25
26         p = failtest_malloc(100, "run-write.c", 1);
27         if (!p) {
28                 /* We are the child.  Do a heap of writes. */
29                 unsigned int i;
30
31                 for (i = 0; i < strlen(buf)+1; i++)
32                         if (failtest_write(fd, "x", 1, "run-write.c", 1) == 1)
33                                 break;
34                 failtest_exit(0);
35         }
36
37         /* Seek pointer should be left alone! */
38         ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
39         /* Length should be restored. */
40         ok1(lseek(fd, 0, SEEK_END) == strlen(buf));
41         lseek(fd, 0, SEEK_SET);
42         ok1(read(fd, buf, strlen(buf)) == strlen("Hello world!"));
43         ok1(strcmp(buf, "Hello world!") == 0);
44
45         return exit_status();
46 }