TestIo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/Types.h>
  9. #include <LibCore/File.h>
  10. #include <LibTest/TestCase.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <sys/uio.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #define EXPECT_ERROR_2(err, syscall, arg1, arg2) \
  20. do { \
  21. rc = syscall(arg1, arg2); \
  22. EXPECT(rc < 0); \
  23. EXPECT_EQ(errno, err); \
  24. if (rc >= 0 || errno != err) { \
  25. warnln(__FILE__ ":{}: Expected " #err ": " #syscall "({}, {}), got rc={}, errno={}", __LINE__, arg1, arg2, rc, errno); \
  26. } \
  27. } while (0)
  28. #define EXPECT_ERROR_3(err, syscall, arg1, arg2, arg3) \
  29. do { \
  30. rc = syscall(arg1, arg2, arg3); \
  31. EXPECT(rc < 0); \
  32. EXPECT_EQ(errno, err); \
  33. if (rc >= 0 || errno != err) { \
  34. warnln(__FILE__ ":{}: Expected " #err ": " #syscall "({}, {}, {}), got rc={}, errno={}", __LINE__, arg1, arg2, arg3, rc, errno); \
  35. } \
  36. } while (0)
  37. TEST_CASE(read_from_directory)
  38. {
  39. char buffer[BUFSIZ];
  40. int fd = open("/", O_DIRECTORY | O_RDONLY);
  41. VERIFY(fd >= 0);
  42. int rc;
  43. EXPECT_ERROR_3(EISDIR, read, fd, buffer, sizeof(buffer));
  44. rc = close(fd);
  45. VERIFY(rc == 0);
  46. }
  47. TEST_CASE(write_to_directory)
  48. {
  49. char str[] = "oh frick";
  50. int fd = open("/", O_DIRECTORY | O_RDONLY);
  51. if (fd < 0)
  52. perror("open");
  53. VERIFY(fd >= 0);
  54. int rc;
  55. EXPECT_ERROR_3(EBADF, write, fd, str, sizeof(str));
  56. rc = close(fd);
  57. VERIFY(rc == 0);
  58. }
  59. TEST_CASE(read_from_writeonly)
  60. {
  61. char buffer[BUFSIZ];
  62. int fd = open("/tmp/xxxx123", O_CREAT | O_WRONLY);
  63. VERIFY(fd >= 0);
  64. int rc;
  65. EXPECT_ERROR_3(EBADF, read, fd, buffer, sizeof(buffer));
  66. rc = close(fd);
  67. VERIFY(rc == 0);
  68. rc = unlink("/tmp/xxxx123");
  69. VERIFY(rc == 0);
  70. }
  71. TEST_CASE(write_to_readonly)
  72. {
  73. char str[] = "hello";
  74. int fd = open("/tmp/abcd123", O_CREAT | O_RDONLY);
  75. VERIFY(fd >= 0);
  76. int rc;
  77. EXPECT_ERROR_3(EBADF, write, fd, str, sizeof(str));
  78. rc = close(fd);
  79. VERIFY(rc == 0);
  80. rc = unlink("/tmp/abcd123");
  81. VERIFY(rc == 0);
  82. }
  83. TEST_CASE(read_past_eof)
  84. {
  85. char buffer[BUFSIZ];
  86. int fd = open("/home/anon/README.md", O_RDONLY);
  87. if (fd < 0)
  88. perror("open");
  89. VERIFY(fd >= 0);
  90. int rc;
  91. rc = lseek(fd, 99999, SEEK_SET);
  92. if (rc < 0)
  93. perror("lseek");
  94. rc = read(fd, buffer, sizeof(buffer));
  95. if (rc < 0)
  96. perror("read");
  97. if (rc > 0)
  98. warnln("read {} bytes past EOF", rc);
  99. rc = close(fd);
  100. VERIFY(rc == 0);
  101. }
  102. TEST_CASE(ftruncate_readonly)
  103. {
  104. int fd = open("/tmp/trunctest", O_RDONLY | O_CREAT, 0666);
  105. VERIFY(fd >= 0);
  106. int rc;
  107. EXPECT_ERROR_2(EBADF, ftruncate, fd, 0);
  108. rc = close(fd);
  109. VERIFY(rc == 0);
  110. rc = unlink("/tmp/trunctest");
  111. VERIFY(rc == 0);
  112. }
  113. TEST_CASE(ftruncate_negative)
  114. {
  115. int fd = open("/tmp/trunctest", O_RDWR | O_CREAT, 0666);
  116. VERIFY(fd >= 0);
  117. int rc;
  118. EXPECT_ERROR_2(EINVAL, ftruncate, fd, -1);
  119. rc = close(fd);
  120. VERIFY(rc == 0);
  121. rc = unlink("/tmp/trunctest");
  122. VERIFY(rc == 0);
  123. }
  124. TEST_CASE(mmap_directory)
  125. {
  126. int fd = open("/tmp", O_RDONLY | O_DIRECTORY);
  127. VERIFY(fd >= 0);
  128. auto* ptr = mmap(nullptr, 4096, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
  129. EXPECT_EQ(ptr, MAP_FAILED);
  130. if (ptr != MAP_FAILED) {
  131. warnln("Boo! mmap() of a directory succeeded!");
  132. }
  133. EXPECT_EQ(errno, ENODEV);
  134. if (errno != ENODEV) {
  135. warnln("Boo! mmap() of a directory gave errno={} instead of ENODEV!", errno);
  136. return;
  137. }
  138. close(fd);
  139. }
  140. TEST_CASE(tmpfs_read_past_end)
  141. {
  142. int fd = open("/tmp/x", O_RDWR | O_CREAT | O_TRUNC, 0600);
  143. VERIFY(fd >= 0);
  144. int rc = ftruncate(fd, 1);
  145. VERIFY(rc == 0);
  146. rc = lseek(fd, 4096, SEEK_SET);
  147. VERIFY(rc == 4096);
  148. char buffer[16];
  149. int nread = read(fd, buffer, sizeof(buffer));
  150. if (nread != 0) {
  151. warnln("Expected 0-length read past end of file in /tmp");
  152. }
  153. rc = close(fd);
  154. VERIFY(rc == 0);
  155. rc = unlink("/tmp/x");
  156. VERIFY(rc == 0);
  157. }
  158. TEST_CASE(sysfs_read_past_uptime_end)
  159. {
  160. int fd = open("/sys/kernel/uptime", O_RDONLY);
  161. VERIFY(fd >= 0);
  162. int rc = lseek(fd, 4096, SEEK_SET);
  163. VERIFY(rc == 4096);
  164. char buffer[16];
  165. int nread = read(fd, buffer, sizeof(buffer));
  166. if (nread != 0) {
  167. warnln("Expected 0-length read past end of file in /proc");
  168. }
  169. close(fd);
  170. }
  171. TEST_CASE(open_create_device)
  172. {
  173. int fd = open("/tmp/fakedevice", (O_RDWR | O_CREAT), (S_IFCHR | 0600));
  174. VERIFY(fd >= 0);
  175. struct stat st;
  176. int rc = fstat(fd, &st);
  177. EXPECT(rc >= 0);
  178. if (rc < 0) {
  179. perror("stat");
  180. }
  181. EXPECT_EQ(st.st_mode, 0100600);
  182. if (st.st_mode != 0100600) {
  183. warnln("Expected mode 0100600 after attempt to create a device node with open(O_CREAT), mode={:o}", st.st_mode);
  184. }
  185. rc = unlink("/tmp/fakedevice");
  186. EXPECT_EQ(rc, 0);
  187. close(fd);
  188. EXPECT_EQ(rc, 0);
  189. }
  190. TEST_CASE(unlink_symlink)
  191. {
  192. int rc = symlink("/proc/2/foo", "/tmp/linky");
  193. EXPECT(rc >= 0);
  194. if (rc < 0) {
  195. perror("symlink");
  196. }
  197. auto target_or_error = Core::File::read_link("/tmp/linky");
  198. EXPECT(!target_or_error.is_error());
  199. auto target = target_or_error.release_value();
  200. EXPECT_EQ(target, "/proc/2/foo");
  201. rc = unlink("/tmp/linky");
  202. EXPECT(rc >= 0);
  203. if (rc < 0) {
  204. perror("unlink");
  205. warnln("Expected unlink() of a symlink into an unreadable directory to succeed!");
  206. }
  207. }
  208. TEST_CASE(tmpfs_eoverflow)
  209. {
  210. int fd = open("/tmp/x", O_RDWR | O_CREAT);
  211. EXPECT(fd >= 0);
  212. off_t rc = lseek(fd, INT64_MAX, SEEK_SET);
  213. EXPECT_EQ(rc, INT64_MAX);
  214. char buffer[16] {};
  215. char empty_buffer[16] {};
  216. rc = read(fd, buffer, sizeof(buffer));
  217. EXPECT_EQ(rc, -1);
  218. EXPECT_EQ(errno, EOVERFLOW);
  219. [[maybe_unused]] auto ignored = strlcpy(buffer, "abcdefghijklmno", sizeof(buffer) - 1);
  220. rc = write(fd, buffer, sizeof(buffer));
  221. EXPECT_EQ(rc, -1);
  222. EXPECT_EQ(errno, EOVERFLOW);
  223. if (rc >= 0 || errno != EOVERFLOW) {
  224. warnln("Expected EOVERFLOW when trying to write past INT64_MAX");
  225. }
  226. // ok now, write something to it, and try again
  227. rc = lseek(fd, 0, SEEK_SET);
  228. EXPECT_EQ(rc, 0);
  229. rc = write(fd, buffer, sizeof(buffer));
  230. EXPECT_EQ(rc, 16);
  231. rc = lseek(fd, INT64_MAX, SEEK_SET);
  232. EXPECT_EQ(rc, INT64_MAX);
  233. memset(buffer, 0, sizeof(buffer));
  234. rc = read(fd, buffer, sizeof(buffer));
  235. EXPECT_EQ(rc, -1);
  236. EXPECT_EQ(errno, EOVERFLOW);
  237. if (rc >= 0 || errno != EOVERFLOW) {
  238. warnln("Expected EOVERFLOW when trying to read past INT64_MAX");
  239. }
  240. EXPECT_EQ(0, memcmp(buffer, empty_buffer, sizeof(buffer)));
  241. rc = close(fd);
  242. EXPECT_EQ(rc, 0);
  243. rc = unlink("/tmp/x");
  244. EXPECT_EQ(rc, 0);
  245. }
  246. TEST_CASE(tmpfs_massive_file)
  247. {
  248. int fd = open("/tmp/x", O_RDWR | O_CREAT);
  249. EXPECT(fd >= 0);
  250. off_t rc = lseek(fd, INT32_MAX, SEEK_SET);
  251. EXPECT_EQ(rc, INT32_MAX);
  252. char buffer[16] {};
  253. rc = read(fd, buffer, sizeof(buffer));
  254. EXPECT_EQ(rc, 0);
  255. [[maybe_unused]] auto ignored = strlcpy(buffer, "abcdefghijklmno", sizeof(buffer) - 1);
  256. rc = write(fd, buffer, sizeof(buffer));
  257. EXPECT_EQ(rc, 16);
  258. // ok now, write something to it, and try again
  259. rc = lseek(fd, 0, SEEK_SET);
  260. EXPECT_EQ(rc, 0);
  261. rc = write(fd, buffer, sizeof(buffer));
  262. EXPECT_EQ(rc, 16);
  263. rc = lseek(fd, INT32_MAX, SEEK_SET);
  264. EXPECT_EQ(rc, INT32_MAX);
  265. memset(buffer, 0, sizeof(buffer));
  266. rc = read(fd, buffer, sizeof(buffer));
  267. EXPECT_EQ(rc, 16);
  268. EXPECT(buffer != "abcdefghijklmno"sv);
  269. rc = close(fd);
  270. EXPECT_EQ(rc, 0);
  271. rc = unlink("/tmp/x");
  272. EXPECT_EQ(rc, 0);
  273. }
  274. TEST_CASE(rmdir_dot)
  275. {
  276. int rc = mkdir("/home/anon/rmdir-test-1", 0700);
  277. EXPECT_EQ(rc, 0);
  278. rc = rmdir("/home/anon/rmdir-test-1/.");
  279. EXPECT_NE(rc, 0);
  280. EXPECT_EQ(errno, EINVAL);
  281. rc = chdir("/home/anon/rmdir-test-1");
  282. EXPECT_EQ(rc, 0);
  283. rc = rmdir(".");
  284. VERIFY(rc != 0);
  285. EXPECT_EQ(errno, EINVAL);
  286. rc = rmdir("/home/anon/rmdir-test-1");
  287. EXPECT_EQ(rc, 0);
  288. }
  289. TEST_CASE(rmdir_dot_dot)
  290. {
  291. int rc = mkdir("/home/anon/rmdir-test-2", 0700);
  292. EXPECT_EQ(rc, 0);
  293. rc = mkdir("/home/anon/rmdir-test-2/foo", 0700);
  294. EXPECT_EQ(rc, 0);
  295. rc = rmdir("/home/anon/rmdir-test-2/foo/..");
  296. EXPECT_NE(rc, 0);
  297. EXPECT_EQ(errno, ENOTEMPTY);
  298. rc = rmdir("/home/anon/rmdir-test-2/foo");
  299. EXPECT_EQ(rc, 0);
  300. rc = rmdir("/home/anon/rmdir-test-2");
  301. EXPECT_EQ(rc, 0);
  302. }
  303. TEST_CASE(rmdir_someone_elses_directory_in_my_sticky_directory)
  304. {
  305. // NOTE: This test only works when run as root, since it has to chown a directory to someone else.
  306. if (getuid() != 0)
  307. return;
  308. // Create /tmp/sticky-dir a sticky directory owned by 12345:12345
  309. // Then, create /tmp/sticky-dir/notmine, a normal directory owned by 23456:23456
  310. // Then, fork and seteuid to 12345, and try to rmdir the "notmine" directory. This should succeed.
  311. // In the parent, waitpid on the child, and finally rmdir /tmp/sticky-dir
  312. int rc = mkdir("/tmp/sticky-dir", 01777);
  313. EXPECT_EQ(rc, 0);
  314. rc = chown("/tmp/sticky-dir", 12345, 12345);
  315. EXPECT_EQ(rc, 0);
  316. rc = mkdir("/tmp/sticky-dir/notmine", 0700);
  317. EXPECT_EQ(rc, 0);
  318. rc = chown("/tmp/sticky-dir/notmine", 23456, 23456);
  319. EXPECT_EQ(rc, 0);
  320. int pid = fork();
  321. EXPECT(pid >= 0);
  322. if (pid == 0) {
  323. // We are in the child.
  324. rc = seteuid(12345);
  325. EXPECT_EQ(rc, 0);
  326. rc = rmdir("/tmp/sticky-dir/notmine");
  327. EXPECT_EQ(rc, 0);
  328. _exit(0);
  329. }
  330. int status = 0;
  331. waitpid(pid, &status, 0);
  332. rc = rmdir("/tmp/sticky-dir");
  333. EXPECT_EQ(rc, 0);
  334. }
  335. TEST_CASE(rmdir_while_inside_dir)
  336. {
  337. int rc = mkdir("/home/anon/testdir", 0700);
  338. VERIFY(rc == 0);
  339. rc = chdir("/home/anon/testdir");
  340. VERIFY(rc == 0);
  341. rc = rmdir("/home/anon/testdir");
  342. VERIFY(rc == 0);
  343. int fd = open("x", O_CREAT | O_RDWR, 0600);
  344. EXPECT(fd < 0);
  345. EXPECT_EQ(errno, ENOENT);
  346. if (fd >= 0 || errno != ENOENT) {
  347. warnln("Expected ENOENT when trying to create a file inside a deleted directory. Got {} with errno={}", fd, errno);
  348. }
  349. rc = chdir("/home/anon");
  350. VERIFY(rc == 0);
  351. }
  352. TEST_CASE(writev)
  353. {
  354. int pipefds[2];
  355. int rc = pipe(pipefds);
  356. EXPECT(rc == 0);
  357. iovec iov[2];
  358. iov[0].iov_base = const_cast<void*>((void const*)"Hello");
  359. iov[0].iov_len = 5;
  360. iov[1].iov_base = const_cast<void*>((void const*)"Friends");
  361. iov[1].iov_len = 7;
  362. int nwritten = writev(pipefds[1], iov, 2);
  363. EXPECT_EQ(nwritten, 12);
  364. if (nwritten < 0) {
  365. perror("writev");
  366. }
  367. if (nwritten != 12) {
  368. warnln("Didn't write 12 bytes to pipe with writev");
  369. }
  370. char buffer[32] {};
  371. int nread = read(pipefds[0], buffer, sizeof(buffer));
  372. EXPECT_EQ(nread, 12);
  373. EXPECT_EQ(buffer, "HelloFriends"sv);
  374. if (nread != 12 || memcmp(buffer, "HelloFriends", 12)) {
  375. warnln("Didn't read the expected data from pipe after writev");
  376. VERIFY_NOT_REACHED();
  377. }
  378. close(pipefds[0]);
  379. close(pipefds[1]);
  380. }
  381. TEST_CASE(rmdir_root)
  382. {
  383. int rc = rmdir("/");
  384. EXPECT_EQ(rc, -1);
  385. EXPECT_EQ(errno, EBUSY);
  386. if (rc != -1 || errno != EBUSY) {
  387. warnln("rmdir(/) didn't fail with EBUSY");
  388. }
  389. }
  390. TEST_CASE(open_silly_things)
  391. {
  392. int rc = -1;
  393. EXPECT_ERROR_2(ENOTDIR, open, "/dev/zero", (O_DIRECTORY | O_RDONLY));
  394. EXPECT_ERROR_2(EINVAL, open, "/dev/zero", (O_DIRECTORY | O_CREAT | O_RDWR));
  395. EXPECT_ERROR_2(EEXIST, open, "/dev/zero", (O_CREAT | O_EXCL | O_RDWR));
  396. EXPECT_ERROR_2(EINVAL, open, "/tmp/abcdef", (O_DIRECTORY | O_CREAT | O_RDWR));
  397. EXPECT_ERROR_2(EACCES, open, "/sys/kernel/processes", (O_RDWR));
  398. EXPECT_ERROR_2(ENOENT, open, "/boof/baaf/nonexistent", (O_CREAT | O_RDWR));
  399. EXPECT_ERROR_2(EISDIR, open, "/tmp", (O_DIRECTORY | O_RDWR));
  400. EXPECT_ERROR_2(EPERM, link, "/", "/home/anon/lolroot");
  401. }