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 <stdlib.h>
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include "functionalext.h"
22 #include "filepath_util.h"
23
24 #define TEST_SIZE 4096
25
26 /**
27 * @tc.name : mincore_0100
28 * @tc.desc : Determine whether pages are resident in memory
29 * @tc.level : Level 0
30 */
mincore_0100(void)31 void mincore_0100(void)
32 {
33 struct stat st;
34 char pathname[PATH_MAX] = {0};
35 FILE_ABSOLUTE_PATH("mincore", pathname);
36
37 int ret = stat(pathname, &st);
38 EXPECT_EQ("mincore_0100", ret, CMPFLAG);
39 if (ret != 0) {
40 return;
41 }
42
43 int fd = open(pathname, O_RDONLY);
44 EXPECT_NE("mincore_0100", fd, ERREXPECT);
45 if (fd == -1) {
46 return;
47 }
48
49 void *start = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
50 close(fd);
51 EXPECT_PTRNE("mincore_0100", start, NULL);
52 if (!start) {
53 return;
54 }
55
56 unsigned char vec[TEST_SIZE];
57 memset(vec, 0x0, sizeof(vec));
58 ret = mincore(start, TEST_SIZE, vec);
59 EXPECT_EQ("mincore_0100", ret, CMPFLAG);
60
61 munmap(start, st.st_size);
62 }
63
64 /**
65 * @tc.name : mincore_0200
66 * @tc.desc : Determine whether pages are resident in memory
67 * @tc.level : Level 2
68 */
mincore_0200(void)69 void mincore_0200(void)
70 {
71 char *memory = (char *)malloc(TEST_SIZE);
72 if (memory == NULL) {
73 EXPECT_PTRNE("mincore_0200", memory, NULL);
74 return;
75 }
76
77 unsigned char vec[TEST_SIZE];
78 memset(vec, 0x0, sizeof(vec));
79 int ret = mincore(memory, TEST_SIZE, vec);
80 EXPECT_EQ("mincore_0200", ret, ERREXPECT);
81
82 ret = mincore(memory, TEST_SIZE, NULL);
83 EXPECT_EQ("mincore_0200", ret, ERREXPECT);
84 free(memory);
85 memory = NULL;
86 }
87
main(void)88 int main(void)
89 {
90 mincore_0100();
91 mincore_0200();
92 return t_status;
93 }