TestPosixFallocate.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/System.h>
  7. #include <LibTest/TestCase.h>
  8. TEST_CASE(posix_fallocate_basics)
  9. {
  10. char pattern[] = "/tmp/posix_fallocate.XXXXXX";
  11. auto fd = MUST(Core::System::mkstemp(pattern));
  12. VERIFY(fd >= 0);
  13. {
  14. // Valid use, grows file to new size.
  15. auto result = Core::System::posix_fallocate(fd, 0, 1024);
  16. EXPECT_EQ(result.is_error(), false);
  17. auto stat = MUST(Core::System::fstat(fd));
  18. EXPECT_EQ(stat.st_size, 1024);
  19. }
  20. {
  21. // Invalid fd (-1)
  22. auto result = Core::System::posix_fallocate(-1, 0, 1024);
  23. EXPECT_EQ(result.is_error(), true);
  24. EXPECT_EQ(result.error().code(), EBADF);
  25. }
  26. {
  27. // Invalid length (-1)
  28. auto result = Core::System::posix_fallocate(fd, 0, -1);
  29. EXPECT_EQ(result.is_error(), true);
  30. EXPECT_EQ(result.error().code(), EINVAL);
  31. }
  32. {
  33. // Invalid length (0)
  34. auto result = Core::System::posix_fallocate(fd, 0, 0);
  35. EXPECT_EQ(result.is_error(), true);
  36. EXPECT_EQ(result.error().code(), EINVAL);
  37. }
  38. {
  39. // Invalid offset (-1)
  40. auto result = Core::System::posix_fallocate(fd, -1, 1024);
  41. EXPECT_EQ(result.is_error(), true);
  42. EXPECT_EQ(result.error().code(), EINVAL);
  43. }
  44. MUST(Core::System::close(fd));
  45. }
  46. TEST_CASE(posix_fallocate_on_device_file)
  47. {
  48. auto fd = MUST(Core::System::open("/dev/zero"sv, O_RDWR));
  49. VERIFY(fd >= 0);
  50. auto result = Core::System::posix_fallocate(fd, 0, 100);
  51. EXPECT_EQ(result.is_error(), true);
  52. EXPECT_EQ(result.error().code(), ENODEV);
  53. MUST(Core::System::close(fd));
  54. }
  55. TEST_CASE(posix_fallocate_on_pipe)
  56. {
  57. auto pipefds = MUST(Core::System::pipe2(0));
  58. auto result = Core::System::posix_fallocate(pipefds[1], 0, 100);
  59. EXPECT_EQ(result.is_error(), true);
  60. EXPECT_EQ(result.error().code(), ESPIPE);
  61. MUST(Core::System::close(pipefds[0]));
  62. MUST(Core::System::close(pipefds[1]));
  63. }