run-open.c 1.6 KB

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