s3:smb2_sesssetup: add support for SMB 2.24/3.00 signing
[Samba/gebeck_regimport.git] / lib / ccan / failtest / test / run-open.c
blob0166506480d897ea96f4b6ba33a5615109fb28ee
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>
10 int main(void)
12 int fd, pfd[2], err;
13 char buf[] = "Hello world!";
14 struct stat st;
16 plan_tests(12);
17 failtest_init(0, NULL);
19 if (pipe(pfd))
20 abort();
21 fd = failtest_open("run-open-scratchpad", "run-open.c", 1,
22 O_RDWR|O_CREAT, 0600);
23 if (fd == -1) {
24 /* We are the child: write error code for parent to check. */
25 err = errno;
26 if (write(pfd[1], &err, sizeof(err)) != sizeof(err))
27 abort();
28 failtest_exit(0);
30 /* Check it is read-write. */
31 ok1(write(fd, buf, strlen(buf)) == strlen(buf));
32 lseek(fd, SEEK_SET, 0);
33 ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
34 ok1(strcmp(buf, "Hello world!") == 0);
36 /* Check name and perms. */
37 ok1(stat("run-open-scratchpad", &st) == 0);
38 ok1(st.st_size == strlen(buf));
39 ok1(S_ISREG(st.st_mode));
40 ok1((st.st_mode & 0777) == 0600);
42 /* Check child got correct errno. */
43 ok1(read(pfd[0], &err, sizeof(err)) == sizeof(err));
44 ok1(err == EACCES);
46 /* Clean up. */
47 failtest_close(fd, "run-open.c", 1);
48 close(pfd[0]);
49 close(pfd[1]);
51 /* Two-arg open. */
52 if (pipe(pfd) != 0)
53 abort();
54 fd = failtest_open("run-open-scratchpad", "run-open.c", 1, O_RDONLY);
55 if (fd == -1) {
56 /* We are the child: write error code for parent to check. */
57 err = errno;
58 if (write(pfd[1], &err, sizeof(err)) != sizeof(err))
59 abort();
60 failtest_exit(0);
62 /* Check it is read-only. */
63 ok1(write(fd, buf, strlen(buf)) == -1);
64 ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
65 ok1(strcmp(buf, "Hello world!") == 0);
66 /* Clean up. */
67 failtest_close(fd, "run-open.c", 1);
68 close(pfd[0]);
69 close(pfd[1]);
71 return exit_status();