• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <arpa/inet.h>
26 #include <gtest/gtest.h>
27 #include <netinet/in.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include "securec.h"
33 
34 
35 using namespace testing::ext;
36 
37 class HatsMlock2Test : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43 private:
44 };
SetUp()45 void HatsMlock2Test::SetUp()
46 {
47 }
48 
TearDown()49 void HatsMlock2Test::TearDown()
50 {
51 }
52 
SetUpTestCase()53 void HatsMlock2Test::SetUpTestCase()
54 {
55 }
56 
TearDownTestCase()57 void HatsMlock2Test::TearDownTestCase()
58 {
59 }
60 
61 /*
62  * @tc.number : SUB_KERNEL_SYSCALL_MLOCK2_0100
63  * @tc.name   : Mlock2Success_0001
64  * @tc.desc   : Mlock2 locks pages that are currently resident successfully.
65  * @tc.size   : MediumTest
66  * @tc.type   : Function
67  * @tc.level  : Level 1
68  */
69 HWTEST_F(HatsMlock2Test, Mlock2Success_0001, Function | MediumTest | Level1)
70 {
71     int ret;
72 
73     const size_t size = 1024;
74     void *addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
75     EXPECT_NE(addr, MAP_FAILED);
76 
77     ret = mlock2(addr, size, MLOCK_ONFAULT);
78     EXPECT_EQ(ret, 0);
79     EXPECT_NE(munmap(addr, size),  -1);
80 }
81 
82 /*
83  * @tc.number : SUB_KERNEL_SYSCALL_MLOCK2_0200
84  * @tc.name   : Mlock2Success_0002
85  * @tc.desc   : When flags is 0, mlock2() behaves exactly the same as mlock().
86  * @tc.size   : MediumTest
87  * @tc.type   : Function
88  * @tc.level  : Level 1
89  */
90 HWTEST_F(HatsMlock2Test, Mlock2Success_0002, Function | MediumTest | Level1)
91 {
92     int ret;
93 
94     const size_t size = 2048;
95     void *addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96     EXPECT_NE(addr, MAP_FAILED);
97 
98     // when flag is 0, the same as mlock.
99     ret = mlock2(addr, size, 0);
100     EXPECT_EQ(ret, 0);
101     EXPECT_NE(munmap(addr, size),  -1);
102 }
103 
104 /*
105  * @tc.number : SUB_KERNEL_SYSCALL_MLOCK2_0300
106  * @tc.name   : Mlock2Invalid_0003
107  * @tc.desc   : When flag is invalid, mlock2() locks failed, errno is set to EINVAL.
108  * @tc.size   : MediumTest
109  * @tc.type   : Function
110  * @tc.level  : Level 2
111  */
112 HWTEST_F(HatsMlock2Test, Mlock2Invalid_0003, Function | MediumTest | Level2)
113 {
114     int ret;
115 
116     const size_t size = 2048;
117     void *addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
118     EXPECT_NE(addr, MAP_FAILED);
119 
120     errno = 0;
121 
122     // flag 2 is invalid
123     ret = mlock2(addr, size, 2);
124     EXPECT_EQ(ret, -1);
125     EXPECT_EQ(errno, EINVAL);
126 
127     EXPECT_NE(munmap(addr, size),  -1);
128 }
129