1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/file.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include "securec.h"
28
29 using namespace testing::ext;
30 using namespace std;
31
32 static const char *TEST_FILE = "/data/local/tmp/fallocate.txt";
33 mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
34 static off_t g_offset = 0;
35 static off_t g_length = 1024;
36
37 class FallocateApiTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 private:
44 };
SetUp()45 void FallocateApiTest::SetUp()
46 {
47 }
TearDown()48 void FallocateApiTest::TearDown()
49 {
50 }
SetUpTestCase()51 void FallocateApiTest::SetUpTestCase()
52 {
53 }
TearDownTestCase()54 void FallocateApiTest::TearDownTestCase()
55 {
56 }
57
58 /*
59 * @tc.number : SUB_KERNEL_SYSCALL_FALLOCATE_0100
60 * @tc.name : FallocateValidFdSuccess_0001
61 * @tc.desc : fallocate allocate valid file space success.
62 * @tc.size : MediumTest
63 * @tc.type : Function
64 * @tc.level : Level 1
65 */
66 HWTEST_F(FallocateApiTest, FallocateValidFdSuccess_0001, Function | MediumTest | Level1)
67 {
68 int ret = -1;
69 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
70 EXPECT_TRUE(fd > 0);
71
72 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, g_offset, g_length);
73 EXPECT_EQ(ret, 0);
74
75 close(fd);
76 remove(TEST_FILE);
77 }
78
79 /*
80 * @tc.number : SUB_KERNEL_SYSCALL_FALLOCATE_0200
81 * @tc.name : FallocateInvalidFdFail_0002
82 * @tc.desc : fallocate allocate invalid fd space fail, errno EBADF.
83 * @tc.size : MediumTest
84 * @tc.type : Function
85 * @tc.level : Level 2
86 */
87 HWTEST_F(FallocateApiTest, FallocateInvalidFdFail_0002, Function | MediumTest | Level2)
88 {
89 int ret;
90 int fd = -1;
91
92 errno = 0;
93 ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, g_offset, g_length);
94 EXPECT_EQ(ret, -1);
95 EXPECT_EQ(errno, EBADF);
96 }
97
98 /*
99 * @tc.number : SUB_KERNEL_SYSCALL_FALLOCATE_0300
100 * @tc.name : FallocateInvalidBaseOrLenFail_0003
101 * @tc.desc : fallocate allocate file space with invalid offset or invalid length fail.
102 * @tc.size : MediumTest
103 * @tc.type : Function
104 * @tc.level : Level 2
105 */
106 HWTEST_F(FallocateApiTest, FallocateInvalidBaseAndLenFail_0003, Function | MediumTest | Level2)
107 {
108 int ret = -1;
109 int invalidOffset = -1;
110 int invalidLength = -1;
111
112 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
113 EXPECT_TRUE(fd > 0);
114
115 errno = 0;
116 ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, invalidOffset, g_length);
117 EXPECT_EQ(ret, -1);
118 EXPECT_EQ(errno, EINVAL);
119
120 errno = 0;
121 ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, g_offset, invalidLength);
122 EXPECT_EQ(ret, -1);
123 EXPECT_EQ(errno, EINVAL);
124
125 close(fd);
126 remove(TEST_FILE);
127 }
128