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