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