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 using namespace testing::ext;
35 static const char* TEST_FILE = "/data/local/tmp/test_file.txt";
36
37 class HatsMlockTest : 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 HatsMlockTest::SetUp()
46 {
47 }
48
TearDown()49 void HatsMlockTest::TearDown()
50 {
51 }
52
SetUpTestCase()53 void HatsMlockTest::SetUpTestCase()
54 {
55 }
56
TearDownTestCase()57 void HatsMlockTest::TearDownTestCase()
58 {
59 }
60
61 /*
62 * @tc.number : SUB_KERNEL_SYSCALL_MLOCK_0100
63 * @tc.name : MlockSuccess_0001
64 * @tc.desc : Mlock locks address successfully.
65 * @tc.size : MediumTest
66 * @tc.type : Function
67 * @tc.level : Level 1
68 */
HWTEST_F(HatsMlockTest, MlockSuccess_0001, Function | MediumTest | Level1)69 HWTEST_F(HatsMlockTest, MlockSuccess_0001, Function | MediumTest | Level1)
70 {
71 int size = 4096;
72 int fd = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
73 EXPECT_TRUE(fd > -1);
74
75 ftruncate(fd, size);
76
77 void *addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
78 EXPECT_NE(addr, MAP_FAILED);
79
80 int ret = mlock(addr, size);
81 EXPECT_EQ(ret, 0);
82
83 ret = munlock(addr, size);
84 EXPECT_EQ(ret, 0);
85
86 munmap(addr, size);
87 close(fd);
88 }
89
90 /*
91 * @tc.number : SUB_KERNEL_SYSCALL_MLOCK_0200
92 * @tc.name : MlockallSuccess_0002
93 * @tc.desc : mlockall sets flag MCL_CURRENT successfully.
94 * @tc.size : MediumTest
95 * @tc.type : Function
96 * @tc.level : Level 1
97 */
HWTEST_F(HatsMlockTest, MlockallSuccess_0002, Function | MediumTest | Level1)98 HWTEST_F(HatsMlockTest, MlockallSuccess_0002, Function | MediumTest | Level1)
99 {
100 int ret = mlockall(MCL_CURRENT | MCL_FUTURE);
101 EXPECT_EQ(ret, 0);
102
103 ret = munlockall();
104 EXPECT_EQ(ret, 0);
105 }
106