diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2012-02-22 14:59:32 +1030 |
---|---|---|
committer | Amitay Isaacs <amitay@gmail.com> | 2012-03-07 13:16:16 +1100 |
commit | 361f3ea9ee577c5a3e2fed687a0b417b257c31de (patch) | |
tree | 6d356c3aa64317c609ff4e208be76e18996a55f8 /lib/ccan/failtest/test/run-open.c | |
parent | 4f5412dda687c3ff76b426842bf284d01d56a997 (diff) | |
download | samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.gz samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.bz2 samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.zip |
lib/ccan: import failtest and required ccan modules for TDB2 unit tests.
New modules: failtest, list, time, read_write_all and tlist.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/ccan/failtest/test/run-open.c')
-rw-r--r-- | lib/ccan/failtest/test/run-open.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/ccan/failtest/test/run-open.c b/lib/ccan/failtest/test/run-open.c new file mode 100644 index 0000000000..0166506480 --- /dev/null +++ b/lib/ccan/failtest/test/run-open.c @@ -0,0 +1,72 @@ +/* Include the C files directly. */ +#include <ccan/failtest/failtest.c> +#include <stdlib.h> +#include <setjmp.h> +#include <stdio.h> +#include <stdarg.h> +#include <assert.h> +#include <ccan/tap/tap.h> + +int main(void) +{ + int fd, pfd[2], err; + char buf[] = "Hello world!"; + struct stat st; + + plan_tests(12); + failtest_init(0, NULL); + + if (pipe(pfd)) + abort(); + fd = failtest_open("run-open-scratchpad", "run-open.c", 1, + O_RDWR|O_CREAT, 0600); + if (fd == -1) { + /* We are the child: write error code for parent to check. */ + err = errno; + if (write(pfd[1], &err, sizeof(err)) != sizeof(err)) + abort(); + failtest_exit(0); + } + /* Check it is read-write. */ + ok1(write(fd, buf, strlen(buf)) == strlen(buf)); + lseek(fd, SEEK_SET, 0); + ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!")); + ok1(strcmp(buf, "Hello world!") == 0); + + /* Check name and perms. */ + ok1(stat("run-open-scratchpad", &st) == 0); + ok1(st.st_size == strlen(buf)); + ok1(S_ISREG(st.st_mode)); + ok1((st.st_mode & 0777) == 0600); + + /* Check child got correct errno. */ + ok1(read(pfd[0], &err, sizeof(err)) == sizeof(err)); + ok1(err == EACCES); + + /* Clean up. */ + failtest_close(fd, "run-open.c", 1); + close(pfd[0]); + close(pfd[1]); + + /* Two-arg open. */ + if (pipe(pfd) != 0) + abort(); + fd = failtest_open("run-open-scratchpad", "run-open.c", 1, O_RDONLY); + if (fd == -1) { + /* We are the child: write error code for parent to check. */ + err = errno; + if (write(pfd[1], &err, sizeof(err)) != sizeof(err)) + abort(); + failtest_exit(0); + } + /* Check it is read-only. */ + ok1(write(fd, buf, strlen(buf)) == -1); + ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!")); + ok1(strcmp(buf, "Hello world!") == 0); + /* Clean up. */ + failtest_close(fd, "run-open.c", 1); + close(pfd[0]); + close(pfd[1]); + + return exit_status(); +} |