main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "common.h"
  2. #include "sandbox.h"
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. /*
  6. _assertNotNull(a);
  7. _assertEqual(sllist_count(a), 0);
  8. */
  9. int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env)
  10. {
  11. int fd;
  12. #ifdef __x86_64__
  13. env->syscall_no.syscall_no = env->registers.orig_rax = (unsigned long long) __NR_open;
  14. env->syscall_no.syscall_return = fd = open(path, flags, mod);
  15. env->syscall_args[0] = env->registers.rdi = (unsigned long long) path;
  16. env->syscall_args[1] = env->registers.rsi = flags;
  17. env->syscall_args[2] = env->registers.rdx = mod;
  18. #else
  19. # error "non-x86 unsupported"
  20. #endif
  21. ovr_open(env);
  22. return fd;
  23. }
  24. int fakeclose(int fd, struct s_sandboxenv *env)
  25. {
  26. #ifdef __x86_64__
  27. env->syscall_no.syscall_no = env->registers.orig_rax = (unsigned long long) __NR_close;
  28. env->syscall_args[0] = env->registers.rdi = fd;
  29. #else
  30. # error "non-x86 unsupported"
  31. #endif
  32. ovr_close(env);
  33. return fd;
  34. }
  35. int test_open(struct s_sandboxenv *env, int *final_fd)
  36. {
  37. t_fileinfo *fi;
  38. t_fd *fd_struct;
  39. close(open("_test", O_CREAT, 0644));
  40. _assertEqual(sllist_count(env->filetable), 0);
  41. int fd = fakeopen("_test", O_RDONLY, 0, env);
  42. _assertDiff(fd, -1);
  43. _assertEqual(env->syscall_no.syscall_return, fd);
  44. _assertEqual(sllist_count(env->filetable), 0);
  45. close(fd);
  46. fd = fakeopen("_test", O_WRONLY, 0646, env);
  47. _assertDiff(fd, -1);
  48. _assertEqual(env->syscall_no.syscall_return, fd);
  49. _assertEqual(sllist_count(env->filetable), 1);
  50. fi = (t_fileinfo *)sllist_at(env->filetable, 0);
  51. _assertEqual(sllist_count(fi->fds), 1);
  52. _assertTrue(fi->local_fd > 0);
  53. int fd2 = fakeopen("_test", O_RDONLY, 0, env);
  54. _assertTrue(fd2 > 0);
  55. _assertEqual(sllist_count(env->filetable), 1);
  56. _assertEqual(sllist_count(fi->fds), 2);
  57. fakeclose(fd2, env);
  58. _assertEqual(sllist_count(fi->fds), 1);
  59. fd_struct = sllist_at(fi->fds, 0);
  60. _assertNotNull(fd_struct);
  61. _assertEqual(fd_struct->fd, fd);
  62. _assertEqual(fd_struct->flags, O_WRONLY);
  63. _assertEqual(fd_struct->mod, 0646);
  64. *final_fd = fd;
  65. return 0;
  66. }
  67. int test_write(struct s_sandboxenv *env)
  68. {
  69. return 0;
  70. }
  71. int main()
  72. {
  73. int success = 1;
  74. struct s_sandboxenv env;
  75. int fd;
  76. t_param params;
  77. tests_init_env(&env, &params);
  78. success &= !test_open(&env, &fd);
  79. success &= !test_write(&env);
  80. fakeclose(fd, &env);
  81. tests_release_env(&env, &params);
  82. unlink("_test");
  83. exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
  84. }