• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <semaphore.h>
19 #include <sys/wait.h>
20 #include <sys/syscall.h>
21 
22 #include "client_map.h"
23 #include "plugin_service.ipc.h"
24 #include "service_entry.h"
25 #include "share_memory_allocator.h"
26 #include "socket_context.h"
27 #include "unix_socket_client.h"
28 #include "unix_socket_server.h"
29 
30 using namespace testing::ext;
31 
32 namespace {
33 class PluginServiceTest final : public IPluginServiceServer {
34 public:
35     int fileDescriptor_;
GetCommand(SocketContext & context,::GetCommandRequest & request,::GetCommandResponse & response)36     bool GetCommand(SocketContext& context, ::GetCommandRequest& request, ::GetCommandResponse& response) override
37     {
38         SendResponseGetCommandResponse(context, response);
39         context.SendFileDescriptor(fileDescriptor_);
40         return false;
41     }
42 };
43 
44 class PluginClientTest final : public IPluginServiceClient {
45 public:
46     int fileDescriptor_;
OnGetCommandResponse(SocketContext & context,::GetCommandResponse & response)47     bool OnGetCommandResponse(SocketContext& context, ::GetCommandResponse& response) override
48     {
49         fileDescriptor_ = context.ReceiveFileDiscriptor();
50         return true;
51     }
52 };
53 
54 class SharedMemoryAllocatorTest : public testing::Test {
55 public:
SetUpTestCase()56     static void SetUpTestCase() {}
TearDownTestCase()57     static void TearDownTestCase() {}
58 
SetUp()59     void SetUp() {}
TearDown()60     void TearDown() {}
61 };
62 
63 /**
64  * @tc.name: Service
65  * @tc.desc: Creates a memory block of the specified size.
66  * @tc.type: FUNC
67  */
68 HWTEST_F(SharedMemoryAllocatorTest, CreateMemoryBlockLocal, TestSize.Level1)
69 {
70     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("testname", 0) ==
71                 nullptr); // 创建大小为0的内存块,返回空
72     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("testname"));
73     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("testname", 1) ==
74                 nullptr); // 创建内存块大小<4096,返回空
75     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("testname"));
76     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("testname", 4096) != nullptr); // 成功创建
77     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("testname", 4096) ==
78                 nullptr); // 创建同名内存块返回空
79     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("testname"));
80     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("testname"));
81 }
82 
83 /**
84  * @tc.name: Service
85  * @tc.desc: Find memory block by name.
86  * @tc.type: FUNC
87  */
88 HWTEST_F(SharedMemoryAllocatorTest, FindMemoryBlockByName, TestSize.Level1)
89 {
90     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().FindMemoryBlockByName("err") == nullptr); // 查找不存在的内存块返回空
91 }
92 
93 /**
94  * @tc.name: Service
95  * @tc.desc: Shared memory MemoryBlockRemote test.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SharedMemoryAllocatorTest, MemoryBlockRemote, TestSize.Level1)
99 {
100     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote("err", 4096, 99) ==
101                 nullptr); // 使用不存在的文件描述符映射内存块返回空
102     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("err"));
103 
104     int fd = syscall(SYS_memfd_create, "testnameremote", 0);
105     EXPECT_GE(fd, 0);
106     int check = ftruncate(fd, 4096);
107     EXPECT_GE(check, 0);
108 
109     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote("testnameremote", 0, fd) ==
110                 nullptr); // 创建大小为0的内存块,返回空
111     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("testnameremote"));
112 
113     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote("testnameremote", 1, fd) ==
114                 nullptr); // 创建内存块大小<4096,返回空
115     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("testnameremote"));
116 
117     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote("testnameremote", 4096, fd) ==
118                 nullptr);
119     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote("testnameremote", 4096, fd) ==
120                 nullptr); // 创建正确fd的重复内存块
121     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("testnameremote"));
122     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("testnameremote")); // 重复释放内存块返回-1
123 }
124 
125 /**
126  * @tc.name: Service
127  * @tc.desc: Gets the size of the memory block with the specified name.
128  * @tc.type: FUNC
129  */
130 HWTEST_F(SharedMemoryAllocatorTest, GetDataSize, TestSize.Level1)
131 {
132     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("testname", 4096) != nullptr); // 成功创建
133     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().FindMemoryBlockByName("testname")->GetDataSize() == 0);
134     ASSERT_TRUE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("testname"));
135 }
136 
137 /**
138  * @tc.name: Service
139  * @tc.desc: Free a nonexistent memory block.
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SharedMemoryAllocatorTest, ReleaseMemoryBlockLocal, TestSize.Level1)
143 {
144     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal("or")); // 释放不存在的内存块返回-1
145 }
146 
147 /**
148  * @tc.name: Service
149  * @tc.desc: Free a nonexistent remote memory block.
150  * @tc.type: FUNC
151  */
152 HWTEST_F(SharedMemoryAllocatorTest, ReleaseMemoryBlockRemote, TestSize.Level1)
153 {
154     ASSERT_FALSE(ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote("or")); // 释放不存在的内存块返回-1
155 }
156 } // namespace
157