• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <fcntl.h>
17 #include "functionalext.h"
18 
19 const int SIZE = 18;
20 const int SUCCESS = 0;
21 const int FAILED = -1;
22 
23 /**
24  * @tc.name      : fallocate_0100
25  * @tc.desc      : The parameter mode is 0, which can expand the file space.
26  * @tc.level     : Level 0
27  */
fallocate_0100()28 void fallocate_0100()
29 {
30     char str[] = "this is a fatest\n";
31     char buffer[1024] = {0};
32     int fd = open("fatest.txt", O_RDWR | O_CREAT);
33     EXPECT_TRUE("fallocate_0100", fd >= 0);
34     int retwrite = write(fd, str, sizeof(str));
35     EXPECT_EQ("fallocate_0100", retwrite, SIZE);
36     int ret = fallocate(fd, 0, 4096, 4096 * 3);
37     EXPECT_EQ("fallocate_0100", ret, SUCCESS);
38     close(fd);
39     remove("fatest.txt");
40 }
41 
42 /**
43  * @tc.name      : fallocate_0200
44  * @tc.desc      : The parameter mode is FALLOC_FL_KEEP_SIZE, which can expand the file space.
45  * @tc.level     : Level 1
46  */
fallocate_0200(void)47 void fallocate_0200(void)
48 {
49     char str[] = "this is a fatest\n";
50     char buffer[1024] = {0};
51     int fd = open("fatest.txt", O_RDWR | O_CREAT);
52     EXPECT_TRUE("fallocate_0200", fd >= 0);
53     int retwrite = write(fd, str, sizeof(str));
54     EXPECT_EQ("fallocate_0200", retwrite, SIZE);
55     int ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 4096, 4096 * 3);
56     EXPECT_EQ("fallocate_0200", ret, SUCCESS);
57     close(fd);
58     remove("fatest.txt");
59 }
60 
61 /**
62  * @tc.name      : fallocate_0300
63  * @tc.desc      : The parameter mode is FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, which can release file space.
64  * @tc.level     : Level 1
65  */
fallocate_0300(void)66 void fallocate_0300(void)
67 {
68     char str[] = "this is a fatest\n";
69     char buffer[1024] = {0};
70     int fd = open("fatest.txt", O_RDWR | O_CREAT);
71     EXPECT_TRUE("fallocate_0300", fd >= 0);
72     int retwrite = write(fd, str, sizeof(str));
73     EXPECT_EQ("fallocate_0300", retwrite, SIZE);
74     lseek(fd, 0, SEEK_SET);
75     int ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 4096, 4096 * 3);
76     EXPECT_EQ("fallocate_0300", ret, SUCCESS);
77     close(fd);
78     remove("fatest.txt");
79 }
80 
81 /**
82  * @tc.name      : fallocate_0400
83  * @tc.desc      : The parameter fd is invalid, the mode is FALLOC_FL_KEEP_SIZE, and the file space cannot be expanded
84  * @tc.level     : Level 1
85  */
fallocate_0400(void)86 void fallocate_0400(void)
87 {
88     int fd = -1;
89     int ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 4096, 4096 * 3);
90     EXPECT_EQ("fallocate_0400", ret, FAILED);
91     close(fd);
92     remove("fatest.txt");
93 }
94 
main(int argc,char * argv[])95 int main(int argc, char *argv[])
96 {
97     fallocate_0100();
98     fallocate_0200();
99     fallocate_0300();
100     fallocate_0400();
101     return t_status;
102 }