main.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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)
  36. {
  37. t_fileinfo *fi;
  38. close(open("_test", O_CREAT, 0644));
  39. _assertEqual(sllist_count(env->filetable), 0);
  40. int fd = fakeopen("_test", O_RDONLY, 0, env);
  41. _assertDiff(fd, -1);
  42. _assertEqual(env->syscall_no.syscall_return, fd);
  43. _assertEqual(sllist_count(env->filetable), 0);
  44. close(fd);
  45. fd = fakeopen("_test", O_WRONLY, 0, env);
  46. _assertDiff(fd, -1);
  47. _assertEqual(env->syscall_no.syscall_return, fd);
  48. _assertEqual(sllist_count(env->filetable), 1);
  49. fi = (t_fileinfo *)sllist_at(env->filetable, 0);
  50. _assertEqual(sllist_count(fi->fds), 1);
  51. _assertTrue(fi->local_fd > 0);
  52. int fd2 = fakeopen("_test", O_RDONLY, 0, env);
  53. _assertTrue(fd2 > 0);
  54. _assertEqual(sllist_count(env->filetable), 1);
  55. _assertEqual(sllist_count(fi->fds), 2);
  56. fakeclose(fd2, env);
  57. _assertEqual(sllist_count(fi->fds), 1);
  58. fakeclose(fd, env);
  59. unlink("_test");
  60. return 0;
  61. }
  62. int test_write(struct s_sandboxenv *env)
  63. {
  64. return 0;
  65. }
  66. int main()
  67. {
  68. int success = 1;
  69. struct s_sandboxenv env;
  70. t_param params;
  71. tests_init_env(&env, &params);
  72. success &= !test_open(&env);
  73. success &= !test_write(&env);
  74. tests_release_env(&env, &params);
  75. exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
  76. }