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 <stdio.h>
18 #include <sys/mman.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21
22 const char *path = "posix_madvise_function_test.c";
23
openfile(void)24 static int openfile(void)
25 {
26 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
27 if (fd < 0) {
28 t_error("open file failed\n");
29 return -1;
30 }
31 return fd;
32 }
33
clearfile(void)34 static void clearfile(void)
35 {
36 if (access(path, F_OK) == 0) {
37 remove(path);
38 }
39 }
40
41 /**
42 * @tc.name : posix_madvise_0100
43 * @tc.desc : Verify that the return value result of creating a reasonable data page is as expected
44 * @tc.level : Level 1
45 */
posix_madvise_0100(void)46 void posix_madvise_0100(void)
47 {
48 size_t length = 4096;
49
50 int fd = openfile();
51 EXPECT_NE("posix_madvise_0100", fd, ERREXPECT);
52
53 void *ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
54 EXPECT_PTRNE("posix_madvise_0100", ptr, MAP_FAILED);
55 if (ptr != MAP_FAILED) {
56 EXPECT_EQ("posix_madvise_0100", posix_madvise(ptr, length, MADV_NORMAL), CMPFLAG);
57 }
58 munmap(ptr, length);
59 close(fd);
60 clearfile();
61 }
62
63 /**
64 * @tc.name : posix_madvise_0200
65 * @tc.desc : Verify that the return value result of creating a super long data page is as expected
66 * @tc.level : Level 2
67 */
posix_madvise_0200(void)68 void posix_madvise_0200(void)
69 {
70 size_t length = 4096;
71 int fd = openfile();
72
73 EXPECT_NE("posix_madvise_0200", fd, ERREXPECT);
74 void *ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75 EXPECT_PTRNE("posix_madvise_0200", ptr, MAP_FAILED);
76 if (ptr != MAP_FAILED) {
77 size_t errorlen = -1;
78 int ret = posix_madvise(ptr, errorlen, MADV_NORMAL);
79 EXPECT_GT("posix_madvise_0200", ret, CMPFLAG);
80 }
81 munmap(ptr, length);
82 close(fd);
83 clearfile();
84 }
85
86 /**
87 * @tc.name : posix_madvise_0300
88 * @tc.desc : When the advice parameter is passed to MADV_DONTNEED
89 * the function exits directly and returns 0
90 * @tc.level : Level 2
91 */
posix_madvise_0300(void)92 void posix_madvise_0300(void)
93 {
94 void *ptr = NULL;
95 size_t len = 1024;
96 int ret = posix_madvise(ptr, len, MADV_DONTNEED);
97 EXPECT_EQ("posix_madvise_0300", ret, CMPFLAG);
98 }
99
main(void)100 int main(void)
101 {
102 posix_madvise_0100();
103 posix_madvise_0200();
104 posix_madvise_0300();
105
106 return t_status;
107 }
108