main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "common.h"
  2. #include "sandbox.h"
  3. #include <sys/stat.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. /*
  7. _assertNotNull(a);
  8. _assertEqual(sllist_count(a), 0);
  9. */
  10. void setArgs(struct s_sandboxenv *env, REGISTER_TYPE sysno, REGISTER_TYPE p1, REGISTER_TYPE p2, REGISTER_TYPE p3)
  11. {
  12. #ifdef __x86_64__
  13. env->syscall_no.syscall_no = env->registers.orig_rax = sysno;
  14. env->syscall_args[0] = env->registers.rdi = p1;
  15. env->syscall_args[1] = env->registers.rsi = p2;
  16. env->syscall_args[2] = env->registers.rdx = p3;
  17. #else
  18. env->syscall_no.syscall_no = env->registers.orig_eax = sysno;
  19. env->syscall_args[0] = env->registers.ebx = p1;
  20. env->syscall_args[1] = env->registers.ecx = p2;
  21. env->syscall_args[2] = env->registers.edx = p3;
  22. #endif
  23. }
  24. void setResult(struct s_sandboxenv *env, REGISTER_TYPE result)
  25. {
  26. env->syscall_no.syscall_return = result;
  27. }
  28. int fakeread(int fd, char *buf, int buflen, struct s_sandboxenv *env)
  29. {
  30. setArgs(env, (REGISTER_TYPE) __NR_read, (REGISTER_TYPE) fd, (REGISTER_TYPE) buf, (REGISTER_TYPE) buflen);
  31. ovr_read(env);
  32. return fd;
  33. }
  34. int fakelseek(int fd, off_t offset, int whence, struct s_sandboxenv *env)
  35. {
  36. setArgs(env, (REGISTER_TYPE) __NR_lseek, (REGISTER_TYPE) fd, (REGISTER_TYPE) offset, (REGISTER_TYPE) whence);
  37. ovr_lseek(env);
  38. return fd;
  39. }
  40. int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env)
  41. {
  42. int fd;
  43. setArgs(env, (REGISTER_TYPE) __NR_open, (REGISTER_TYPE) path, (REGISTER_TYPE) flags, (REGISTER_TYPE) mod);
  44. setResult(env, (fd = open(path, flags, mod)));
  45. ovr_open(env);
  46. return fd;
  47. }
  48. int fakeclose(int fd, struct s_sandboxenv *env)
  49. {
  50. setArgs(env, (REGISTER_TYPE) __NR_close, (REGISTER_TYPE) fd, 0, 0);
  51. ovr_close(env);
  52. return fd;
  53. }
  54. int fakewrite(int fd, char *buf, int len, struct s_sandboxenv *env)
  55. {
  56. setArgs(env, (REGISTER_TYPE) __NR_write, (REGISTER_TYPE) fd, (REGISTER_TYPE) buf, (REGISTER_TYPE) len);
  57. ovr_write(env);
  58. #ifdef __x86_64__
  59. if (env->registers.orig_rax == __NR_write)
  60. env->registers.rax = write(fd, buf, len);
  61. return env->registers.rax;
  62. #else
  63. if (env->registers.orig_eax == __NR_write)
  64. env->registers.eax = write(fd, buf, len);
  65. return env->registers.eax;
  66. #endif
  67. }
  68. int test_open(struct s_sandboxenv *env, int *final_fd)
  69. {
  70. t_fileinfo *fi;
  71. t_fd *fd_struct;
  72. close(open("_test", O_CREAT, 0644));
  73. _assertEqual(sllist_count(env->filetable), 0);
  74. int fd = fakeopen("_test", O_RDONLY, 0, env);
  75. _assertDiff(fd, -1);
  76. _assertEqual(env->syscall_no.syscall_return, fd);
  77. _assertEqual(sllist_count(env->filetable), 0);
  78. close(fd);
  79. fd = fakeopen("_test", O_WRONLY, 0646, env);
  80. _assertDiff(fd, -1);
  81. _assertEqual(env->syscall_no.syscall_return, fd);
  82. _assertEqual(sllist_count(env->filetable), 1);
  83. fi = (t_fileinfo *)sllist_at(env->filetable, 0);
  84. _assertEqual(sllist_count(fi->fds), 1);
  85. _assertTrue(fi->local_fd > 0);
  86. int fd2 = fakeopen("_test", O_RDONLY, 0, env);
  87. _assertTrue(fd2 > 0);
  88. _assertEqual(sllist_count(env->filetable), 1);
  89. _assertEqual(sllist_count(fi->fds), 2);
  90. fakeclose(fd2, env);
  91. _assertEqual(sllist_count(fi->fds), 1);
  92. fd_struct = sllist_at(fi->fds, 0);
  93. _assertNotNull(fd_struct);
  94. _assertEqual(fd_struct->fd, fd);
  95. _assertEqual(fd_struct->flags, O_WRONLY);
  96. _assertEqual(fd_struct->mod, 0646);
  97. *final_fd = fd;
  98. return 0;
  99. }
  100. int test_write(struct s_sandboxenv *env, int fd)
  101. {
  102. struct stat st;
  103. char buf[4];
  104. int lfd;
  105. _assertEqual(fakewrite(fd, "test", 4, env), 4);
  106. _assertEqual(stat("_test", &st), 0);
  107. _assertEqual(st.st_size, 0);
  108. //atm VBufer contains `test', file empty
  109. fakelseek(fd, 2, SEEK_SET, env);
  110. _assertEqual(fakewrite(fd, "test", 4, env), 4);
  111. _assertEqual(stat("_test", &st), 0);
  112. _assertEqual(st.st_size, 0);
  113. //atm VBufer contains `tetest', file still empty
  114. apply_fs(env);
  115. //VBuffer empty, file containing `tetest'
  116. _assertEqual(stat("_test", &st), 0);
  117. _assertEqual(st.st_size, 6);
  118. lfd = open("_test", O_RDONLY);
  119. read(lfd, buf, 6);
  120. _assertStrNEqual(buf, "tetest", 4);
  121. return 0;
  122. }
  123. int main()
  124. {
  125. int success = 1;
  126. struct s_sandboxenv env;
  127. int fd;
  128. t_param params;
  129. unlink("_test");
  130. tests_init_env(&env, &params);
  131. success &= !test_open(&env, &fd);
  132. success &= !test_write(&env, fd);
  133. fakeclose(fd, &env);
  134. tests_release_env(&env, &params);
  135. exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
  136. }