• 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 <stdio.h>
17 #include <fcntl.h>
18 #include <sys/mman.h>
19 
20 #include "functionalext.h"
21 
22 const char *path = "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      : 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  */
madvise_0100(void)46 void madvise_0100(void)
47 {
48     size_t length = 4096;
49     int fd = openfile();
50     EXPECT_NE("madvise_0100", fd, ERREXPECT);
51     void *ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
52     EXPECT_PTRNE("madvise_0100", ptr, MAP_FAILED);
53     if (ptr == MAP_FAILED || ptr == NULL) {
54         return;
55     }
56     int ret = madvise(ptr, length, MADV_NORMAL);
57     EXPECT_EQ("madvise_0100", ret, CMPFLAG);
58     munmap(ptr, length);
59     close(fd);
60     clearfile();
61 }
62 
63 /**
64  * @tc.name      : 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  */
madvise_0200(void)68 void madvise_0200(void)
69 {
70     size_t length = 4096;
71     int fd = openfile();
72 
73     EXPECT_NE("madvise_0200", fd, ERREXPECT);
74     void *ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75     EXPECT_PTRNE("madvise_0200", ptr, MAP_FAILED);
76     if (ptr == MAP_FAILED || ptr == NULL) {
77         return;
78     }
79 
80     size_t errorlen = -1;
81     int ret = madvise(ptr, errorlen, MADV_NORMAL);
82     EXPECT_EQ("madvise_0200", ret, ERREXPECT);
83 
84     munmap(ptr, length);
85     close(fd);
86     clearfile();
87 }
88 
main(void)89 int main(void)
90 {
91     madvise_0100();
92     madvise_0200();
93 
94     return t_status;
95 }
96