mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
Tests/Kernel: Add a very simple test for posix_fallocate()
This commit is contained in:
parent
4277e2d58f
commit
e2771db50d
Notes:
sideshowbarker
2024-07-17 03:57:55 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/e2771db50d Pull-request: https://github.com/SerenityOS/serenity/pull/16231 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/emanuele6 Reviewed-by: https://github.com/fdellwing Reviewed-by: https://github.com/skyrising Reviewed-by: https://github.com/supercomputer7 Reviewed-by: https://github.com/timschumi ✅
@ -40,6 +40,7 @@ set(LIBTEST_BASED_SOURCES
|
||||
TestEmptySharedInodeVMObject.cpp
|
||||
TestInvalidUIDSet.cpp
|
||||
TestSharedInodeVMObject.cpp
|
||||
TestPosixFallocate.cpp
|
||||
TestPrivateInodeVMObject.cpp
|
||||
TestKernelAlarm.cpp
|
||||
TestKernelFilePermissions.cpp
|
||||
|
74
Tests/Kernel/TestPosixFallocate.cpp
Normal file
74
Tests/Kernel/TestPosixFallocate.cpp
Normal file
@ -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]));
|
||||
}
|
Loading…
Reference in New Issue
Block a user