]> git.ozlabs.org Git - ccan/blob - ccan/failtest/test/run-open.c
failtest: replay --failpath correctly on really failing opens.
[ccan] / ccan / failtest / test / run-open.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, pfd[2], err;
13         char buf[] = "Hello world!";
14         struct stat st;
15
16         plan_tests(12);
17
18         if (pipe(pfd))
19                 abort();
20         fd = failtest_open("run-open-scratchpad", "run-open.c", 1,
21                            O_RDWR|O_CREAT, 0600);
22         if (fd == -1) {
23                 /* We are the child: write error code for parent to check. */
24                 err = errno;
25                 if (write(pfd[1], &err, sizeof(err)) != sizeof(err))
26                         abort();
27                 failtest_exit(0);
28         }
29         /* Check it is read-write. */
30         ok1(write(fd, buf, strlen(buf)) == strlen(buf));
31         lseek(fd, SEEK_SET, 0);
32         ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
33         ok1(strcmp(buf, "Hello world!") == 0);
34
35         /* Check name and perms. */
36         ok1(stat("run-open-scratchpad", &st) == 0);
37         ok1(st.st_size == strlen(buf));
38         ok1(S_ISREG(st.st_mode));
39         ok1((st.st_mode & 0777) == 0600);
40
41         /* Check child got correct errno. */
42         ok1(read(pfd[0], &err, sizeof(err)) == sizeof(err));
43         ok1(err == EACCES);
44
45         /* Clean up. */
46         failtest_close(fd, "run-open.c", 1);
47         close(pfd[0]);
48         close(pfd[1]);
49
50         /* Two-arg open. */
51         if (pipe(pfd) != 0)
52                 abort();
53         fd = failtest_open("run-open-scratchpad", "run-open.c", 1, O_RDONLY);
54         if (fd == -1) {
55                 /* We are the child: write error code for parent to check. */
56                 err = errno;
57                 if (write(pfd[1], &err, sizeof(err)) != sizeof(err))
58                         abort();
59                 failtest_exit(0);
60         }
61         /* Check it is read-only. */
62         ok1(write(fd, buf, strlen(buf)) == -1);
63         ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
64         ok1(strcmp(buf, "Hello world!") == 0);
65         /* Clean up. */
66         failtest_close(fd, "run-open.c", 1);
67         close(pfd[0]);
68         close(pfd[1]);
69
70         return exit_status();
71 }