summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-11-27 20:14:25 +0100
committerAndreas Kling <kling@serenityos.org>2022-11-29 11:09:19 +0100
commite2771db50d704639d8b4fc3bbad2e69ba2932ca1 (patch)
tree1005df8ce7a4a1bacb20769c3067e8b088158cad
parent4277e2d58f1fc3f3600d36bf1cc71027374b8cf4 (diff)
downloadserenity-e2771db50d704639d8b4fc3bbad2e69ba2932ca1.zip
Tests/Kernel: Add a very simple test for posix_fallocate()
-rw-r--r--Tests/Kernel/CMakeLists.txt1
-rw-r--r--Tests/Kernel/TestPosixFallocate.cpp74
2 files changed, 75 insertions, 0 deletions
diff --git a/Tests/Kernel/CMakeLists.txt b/Tests/Kernel/CMakeLists.txt
index d411a8091c..139c7854ac 100644
--- a/Tests/Kernel/CMakeLists.txt
+++ b/Tests/Kernel/CMakeLists.txt
@@ -40,6 +40,7 @@ set(LIBTEST_BASED_SOURCES
TestEmptySharedInodeVMObject.cpp
TestInvalidUIDSet.cpp
TestSharedInodeVMObject.cpp
+ TestPosixFallocate.cpp
TestPrivateInodeVMObject.cpp
TestKernelAlarm.cpp
TestKernelFilePermissions.cpp
diff --git a/Tests/Kernel/TestPosixFallocate.cpp b/Tests/Kernel/TestPosixFallocate.cpp
new file mode 100644
index 0000000000..3286b68f8d
--- /dev/null
+++ b/Tests/Kernel/TestPosixFallocate.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/System.h>
+#include <LibTest/TestCase.h>
+
+TEST_CASE(posix_fallocate_basics)
+{
+ char pattern[] = "/tmp/posix_fallocate.XXXXXX";
+ auto fd = MUST(Core::System::mkstemp(pattern));
+ VERIFY(fd >= 0);
+
+ {
+ // Valid use, grows file to new size.
+ auto result = Core::System::posix_fallocate(fd, 0, 1024);
+ EXPECT_EQ(result.is_error(), false);
+
+ auto stat = MUST(Core::System::fstat(fd));
+ EXPECT_EQ(stat.st_size, 1024);
+ }
+
+ {
+ // Invalid fd (-1)
+ auto result = Core::System::posix_fallocate(-1, 0, 1024);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), EBADF);
+ }
+
+ {
+ // Invalid length (-1)
+ auto result = Core::System::posix_fallocate(fd, 0, -1);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), EINVAL);
+ }
+
+ {
+ // Invalid length (0)
+ auto result = Core::System::posix_fallocate(fd, 0, 0);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), EINVAL);
+ }
+
+ {
+ // Invalid offset (-1)
+ auto result = Core::System::posix_fallocate(fd, -1, 1024);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), EINVAL);
+ }
+
+ MUST(Core::System::close(fd));
+}
+
+TEST_CASE(posix_fallocate_on_device_file)
+{
+ auto fd = MUST(Core::System::open("/dev/zero"sv, O_RDWR));
+ VERIFY(fd >= 0);
+ auto result = Core::System::posix_fallocate(fd, 0, 100);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), ENODEV);
+ MUST(Core::System::close(fd));
+}
+
+TEST_CASE(posix_fallocate_on_pipe)
+{
+ auto pipefds = MUST(Core::System::pipe2(0));
+ auto result = Core::System::posix_fallocate(pipefds[1], 0, 100);
+ EXPECT_EQ(result.is_error(), true);
+ EXPECT_EQ(result.error().code(), ESPIPE);
+ MUST(Core::System::close(pipefds[0]));
+ MUST(Core::System::close(pipefds[1]));
+}