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 #define TEST_LEN 10
20 #define TEST_FLAG_SIZE 6
21 /**
22 * @tc.name : posix_fadvise_0100
23 * @tc.desc : Correct parameters, set file preload size
24 * @tc.level : Level 0
25 */
posix_fadvise_0100(void)26 void posix_fadvise_0100(void)
27 {
28 FILE *fp = tmpfile();
29 if (!fp) {
30 EXPECT_PTRNE("posix_fadvise_0100", fp, NULL);
31 return;
32 }
33
34 int fd = fileno(fp);
35 EXPECT_TRUE("posix_fadvise_0100", fd != -1);
36 if (fd == -1) {
37 fclose(fp);
38 return;
39 }
40
41 int flags[TEST_FLAG_SIZE] = {POSIX_FADV_NORMAL,
42 POSIX_FADV_SEQUENTIAL,
43 POSIX_FADV_RANDOM,
44 POSIX_FADV_NOREUSE,
45 POSIX_FADV_WILLNEED,
46 POSIX_FADV_DONTNEED};
47 int i;
48 for (i = 0; i < TEST_FLAG_SIZE; i++) {
49 int ret = posix_fadvise(fd, 0, TEST_LEN, flags[i]);
50 EXPECT_EQ("posix_fadvise_0100", ret, 0);
51 }
52 int rev = fclose(fp);
53 EXPECT_EQ("posix_fadvise_0100", rev, 0);
54 }
55
56 /**
57 * @tc.name : posix_fadvise_0200
58 * @tc.desc : Set file preload size to -1
59 * @tc.level : Level 2
60 */
posix_fadvise_0200(void)61 void posix_fadvise_0200(void)
62 {
63 FILE *fp = tmpfile();
64 if (!fp) {
65 EXPECT_PTRNE("posix_fadvise_0200", fp, NULL);
66 return;
67 }
68
69 int fd = fileno(fp);
70 EXPECT_TRUE("posix_fadvise_0200", fd != -1);
71 if (fd == -1) {
72 fclose(fp);
73 return;
74 }
75 int ret = posix_fadvise(fd, 0, -1, POSIX_FADV_NORMAL);
76 EXPECT_EQ("posix_fadvise_0200", ret, EINVAL);
77 ret = fclose(fp);
78 EXPECT_EQ("posix_fadvise_0200", ret, 0);
79 }
80
81 /**
82 * @tc.name : posix_fadvise_0300
83 * @tc.desc : Set file preload offset to -1
84 * @tc.level : Level 0
85 */
posix_fadvise_0300(void)86 void posix_fadvise_0300(void)
87 {
88 FILE *fp = tmpfile();
89 if (!fp) {
90 EXPECT_PTRNE("posix_fadvise_0300", fp, NULL);
91 return;
92 }
93
94 int fd = fileno(fp);
95 EXPECT_TRUE("posix_fadvise_0300", fd != -1);
96 if (fd == -1) {
97 fclose(fp);
98 return;
99 }
100 int ret = posix_fadvise(fd, -1, TEST_LEN, POSIX_FADV_NORMAL);
101 EXPECT_EQ("posix_fadvise_0300", ret, 0);
102 ret = fclose(fp);
103 EXPECT_EQ("posix_fadvise_0300", ret, 0);
104 }
105
106 /**
107 * @tc.name : posix_fadvise_0400
108 * @tc.desc : set wrong file descriptor
109 * @tc.level : Level 2
110 */
posix_fadvise_0400(void)111 void posix_fadvise_0400(void)
112 {
113 int ret = posix_fadvise(-1, 0, TEST_LEN, POSIX_FADV_NORMAL);
114 EXPECT_EQ("posix_fadvise_0400", ret, EBADF);
115 }
116
117 /**
118 * @tc.name : posix_fadvise_0500
119 * @tc.desc : set wrong flag
120 * @tc.level : Level 2
121 */
posix_fadvise_0500(void)122 void posix_fadvise_0500(void)
123 {
124 FILE *fp = tmpfile();
125 if (!fp) {
126 EXPECT_PTRNE("posix_fadvise_0500", fp, NULL);
127 return;
128 }
129
130 int fd = fileno(fp);
131 EXPECT_TRUE("posix_fadvise_0500", fd != -1);
132 if (fd == -1) {
133 fclose(fp);
134 return;
135 }
136 int ret = posix_fadvise(fd, 0, TEST_LEN, -1);
137 EXPECT_EQ("posix_fadvise_0500", ret, EINVAL);
138 ret = fclose(fp);
139 EXPECT_EQ("posix_fadvise_0500", ret, 0);
140 }
141
main(void)142 int main(void)
143 {
144 posix_fadvise_0100();
145 posix_fadvise_0200();
146 posix_fadvise_0300();
147 posix_fadvise_0400();
148 posix_fadvise_0500();
149 return t_status;
150 }