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 <sys/stat.h>
18 #include "functionalext.h"
19
20 #define TEST_LEN 10
21
22 /**
23 * @tc.name : posix_fallocate_0100
24 * @tc.desc : Allocate a specified amount of storage space for a file
25 * @tc.level : Level 0
26 */
posix_fallocate_0100(void)27 void posix_fallocate_0100(void)
28 {
29 struct stat sb;
30 FILE *fp = tmpfile();
31 if (!fp) {
32 EXPECT_PTRNE("posix_fallocate_0100", fp, NULL);
33 return;
34 }
35
36 int fd = fileno(fp);
37 EXPECT_TRUE("posix_fallocate_0100", fd != -1);
38 if (fd == -1) {
39 fclose(fp);
40 return;
41 }
42 int ret = posix_fallocate(fd, 0, TEST_LEN);
43 EXPECT_EQ("posix_fallocate_0100", ret, 0);
44 ret = fstat(fd, &sb);
45 EXPECT_EQ("posix_fallocate_0100", ret, 0);
46 EXPECT_EQ("posix_fallocate_0100", sb.st_size, TEST_LEN);
47 ret = fclose(fp);
48 EXPECT_EQ("posix_fallocate_0100", ret, 0);
49 }
50
51 /**
52 * @tc.name : posix_fallocate_0200
53 * @tc.desc : Allocate -1 byte of storage for the file
54 * @tc.level : Level 2
55 */
posix_fallocate_0200(void)56 void posix_fallocate_0200(void)
57 {
58 FILE *fp = tmpfile();
59 if (!fp) {
60 EXPECT_PTRNE("posix_fallocate_0200", fp, NULL);
61 return;
62 }
63
64 int fd = fileno(fp);
65 EXPECT_TRUE("posix_fallocate_0200", fd != -1);
66 if (fd == -1) {
67 fclose(fp);
68 return;
69 }
70 int ret = posix_fallocate(fd, 0, -1);
71 EXPECT_EQ("posix_fallocate_0200", ret, EINVAL);
72 ret = fclose(fp);
73 EXPECT_EQ("posix_fallocate_0200", ret, 0);
74 }
75
76 /**
77 * @tc.name : posix_fallocate_0300
78 * @tc.desc : At file offset -1 byte, allocate 10 bytes of storage for the file
79 * @tc.level : Level 2
80 */
posix_fallocate_0300(void)81 void posix_fallocate_0300(void)
82 {
83 FILE *fp = tmpfile();
84 if (!fp) {
85 EXPECT_PTRNE("posix_fallocate_0300", fp, NULL);
86 return;
87 }
88
89 int fd = fileno(fp);
90 EXPECT_TRUE("posix_fallocate_0300", fd != -1);
91 if (fd == -1) {
92 fclose(fp);
93 return;
94 }
95 int ret = posix_fallocate(fd, -1, TEST_LEN);
96 EXPECT_EQ("posix_fallocate_0300", ret, EINVAL);
97 ret = fclose(fp);
98 EXPECT_EQ("posix_fallocate_0300", ret, 0);
99 }
100
101 /**
102 * @tc.name : posix_fallocate_0400
103 * @tc.desc : Bad file handle provided, allocating 10 bytes of storage for file
104 * @tc.level : Level 2
105 */
posix_fallocate_0400(void)106 void posix_fallocate_0400(void)
107 {
108 int ret = posix_fallocate(-1, 0, TEST_LEN);
109 EXPECT_EQ("posix_fallocate_0400", ret, EBADF);
110 }
111
main(void)112 int main(void)
113 {
114 posix_fallocate_0100();
115 posix_fallocate_0200();
116 posix_fallocate_0300();
117 posix_fallocate_0400();
118 return t_status;
119 }