1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include "securec.h"
28
29
30 using namespace testing::ext;
31 using namespace std;
32
33 static const int PAGE_SIZE = 1024;
34
35 class MincoreApiTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 private:
42 };
SetUp()43 void MincoreApiTest::SetUp()
44 {
45 }
TearDown()46 void MincoreApiTest::TearDown()
47 {
48 }
SetUpTestCase()49 void MincoreApiTest::SetUpTestCase()
50 {
51 }
TearDownTestCase()52 void MincoreApiTest::TearDownTestCase()
53 {
54 }
55
56 /*
57 * @tc.number : SUB_KERNEL_SYSCALL_MINCORE_0100
58 * @tc.name : MincoreCheckPageInMemorySuccess_0001
59 * @tc.desc : Mincore determine whether page are resident in memory.
60 * @tc.size : MediumTest
61 * @tc.type : Function
62 * @tc.level : Level 1
63 */
HWTEST_F(MincoreApiTest, MincoreCheckPageInMemorySuccess_0001, Function | MediumTest | Level1)64 HWTEST_F(MincoreApiTest, MincoreCheckPageInMemorySuccess_0001, Function | MediumTest | Level1)
65 {
66 int ret;
67 size_t length = PAGE_SIZE * 4;
68 unsigned char *vec = new unsigned char[(length / PAGE_SIZE) + 1]();
69
70 void *addr = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
71 EXPECT_NE(addr, MAP_FAILED);
72
73 ret = mincore(addr, length, vec);
74 EXPECT_EQ(ret, 0);
75
76 munmap(addr, length);
77 delete [] vec;
78 }
79
80 /*
81 * @tc.number : SUB_KERNEL_SYSCALL_MINCORE_0200
82 * @tc.name : MincoreCheckPageInvalidAddrFail_0002
83 * @tc.desc : Mincore determine whether page are resident in invalid memory addr fail.
84 * @tc.size : MediumTest
85 * @tc.type : Function
86 * @tc.level : Level 2
87 */
HWTEST_F(MincoreApiTest, MincoreCheckPageInMemoryFail_0002, Function | MediumTest | Level2)88 HWTEST_F(MincoreApiTest, MincoreCheckPageInMemoryFail_0002, Function | MediumTest | Level2)
89 {
90 int ret;
91 size_t length = PAGE_SIZE * 4;
92 unsigned char *vec = new unsigned char[(length / PAGE_SIZE) + 1]();
93
94 errno = 0;
95 ret = mincore(MAP_FAILED, length, vec);
96 EXPECT_EQ(ret, -1);
97 EXPECT_EQ(errno, EINVAL);
98 delete [] vec;
99 }
100
101 /*
102 * @tc.number : SUB_KERNEL_SYSCALL_MINCORE_0300
103 * @tc.name : MincoreCheckPageInvalidLengthFail_0003
104 * @tc.desc : Mincore determine whether page are resident in invalid memory addr fail.
105 * @tc.size : MediumTest
106 * @tc.type : Function
107 * @tc.level : Level 2
108 */
HWTEST_F(MincoreApiTest, MincoreCheckPageInvalidLengthFail_0003, Function | MediumTest | Level2)109 HWTEST_F(MincoreApiTest, MincoreCheckPageInvalidLengthFail_0003, Function | MediumTest | Level2)
110 {
111 int ret;
112 size_t length = PAGE_SIZE * 4;
113 unsigned char *vec = new unsigned char[(length / PAGE_SIZE) + 1]();
114 void *addr = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
115 EXPECT_NE(addr, MAP_FAILED);
116
117 errno = 0;
118 ret = mincore(MAP_FAILED, 0, vec);
119 EXPECT_EQ(ret, -1);
120 EXPECT_EQ(errno, EINVAL);
121 munmap(addr, length);
122 delete [] vec;
123 }
124