main.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return 0;
  65. }
  66. int test_write(struct s_sandboxenv *env)
  67. {
  68. return 0;
  69. }
  70. int main()
  71. {
  72. int success = 1;
  73. struct s_sandboxenv env;
  74. int fd;
  75. t_param params;
  76. tests_init_env(&env, &params);
  77. success &= !test_open(&env, &fd);
  78. success &= !test_write(&env);
  79. tests_release_env(&env, &params);
  80. fakeclose(fd, &env);
  81. unlink("_test");
  82. exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
  83. }