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