1 /* 2 * Copyright (c) 2022 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 <cstdlib> 17 18 #include <sys/mman.h> 19 #include <sys/types.h> 20 #include <sys/stat.h> 21 #include <fcntl.h> 22 23 #include <gtest/gtest.h> 24 25 #include "cpp_type.h" 26 #include "memory_manager.h" 27 #include "test/unittest/common/file_utils.h" 28 29 using namespace testing; 30 using namespace testing::ext; 31 using namespace OHOS::NeuralNetworkRuntime; 32 namespace OHOS { 33 namespace NeuralNetworkRuntime { 34 namespace UnitTest { 35 class MemoryManagerTest : public testing::Test { 36 public: 37 MemoryManagerTest() = default; 38 ~MemoryManagerTest() = default; 39 }; 40 41 /** 42 * @tc.name: memorymanagertest_mapmemory_001 43 * @tc.desc: Verify the MapMemory function return nullptr in case of fd -1. 44 * @tc.type: FUNC 45 */ 46 HWTEST_F(MemoryManagerTest, memorymanagertest_mapmemory_001, TestSize.Level0) 47 { 48 const auto& memoryManager = MemoryManager::GetInstance(); 49 int fd = -1; 50 size_t length = 0; 51 void* result = memoryManager->MapMemory(fd, length); 52 EXPECT_EQ(nullptr, result); 53 } 54 55 /** 56 * @tc.name: memorymanagertest_mapmemory_002 57 * @tc.desc: Verify the MapMemory function return nullptr in case of length 0. 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(MemoryManagerTest, memorymanagertest_mapmemory_002, TestSize.Level0) 61 { 62 const auto& memoryManager = MemoryManager::GetInstance(); 63 int fd = 0; 64 size_t length = 0; 65 void* result = memoryManager->MapMemory(fd, length); 66 EXPECT_EQ(nullptr, result); 67 } 68 69 /** 70 * @tc.name: memorymanagertest_mapmemory_003 71 * @tc.desc: Verify the MapMemory function return nullptr in case of fd 0. 72 * @tc.type: FUNC 73 */ 74 HWTEST_F(MemoryManagerTest, memorymanagertest_mapmemory_003, TestSize.Level0) 75 { 76 const auto& memoryManager = MemoryManager::GetInstance(); 77 int fd = 0; 78 size_t length = 1; 79 void* result = memoryManager->MapMemory(fd, length); 80 EXPECT_EQ(nullptr, result); 81 } 82 83 /** 84 * @tc.name: memorymanagertest_mapmemory_004 85 * @tc.desc: Verify the MapMemory function validate mapmemory content success. 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(MemoryManagerTest, memorymanagertest_mapmemory_004, TestSize.Level0) 89 { 90 std::string data = "ABCD"; 91 const size_t dataLength = 100; 92 data.resize(dataLength, '*'); 93 94 std::string filename = "/data/log/memory-001.dat"; 95 FileUtils fileUtils(filename); 96 fileUtils.WriteFile(data); 97 98 int fd = open(filename.c_str(), O_RDWR); 99 EXPECT_NE(-1, fd); 100 101 size_t length = 4; 102 const auto& memoryManager = MemoryManager::GetInstance(); 103 char* result = static_cast<char*>(memoryManager->MapMemory(fd, length)); 104 EXPECT_NE(nullptr, result); 105 EXPECT_EQ('A', static_cast<char>(result[0])); 106 EXPECT_EQ('B', static_cast<char>(result[1])); 107 EXPECT_EQ('C', static_cast<char>(result[2])); 108 EXPECT_EQ('D', static_cast<char>(result[3])); 109 memoryManager->UnMapMemory(result); 110 close(fd); 111 } 112 113 /** 114 * @tc.name: memorymanagertest_unmapmemory_001 115 * @tc.desc: Verify the UnMapMemory function validate behavior. 116 * @tc.type: FUNC 117 */ 118 HWTEST_F(MemoryManagerTest, memorymanagertest_unmapmemory_001, TestSize.Level0) 119 { 120 const auto& memoryManager = MemoryManager::GetInstance(); 121 void* memory = nullptr; 122 EXPECT_EQ(OH_NN_INVALID_PARAMETER, memoryManager->UnMapMemory(memory)); 123 } 124 125 /** 126 * @tc.name: memorymanagertest_unmapmemory_002 127 * @tc.desc: Verify the UnMapMemory function validate behavior 128 * @tc.type: FUNC 129 */ 130 HWTEST_F(MemoryManagerTest, memorymanagertest_unmapmemory_002, TestSize.Level0) 131 { 132 const auto& memoryManager = MemoryManager::GetInstance(); 133 void* memory = malloc(10); 134 EXPECT_EQ(OH_NN_INVALID_PARAMETER, memoryManager->UnMapMemory(memory)); 135 free(memory); 136 } 137 138 /** 139 * @tc.name: memorymanagertest_unmapmemory_003 140 * @tc.desc: Verify the UnMapMemory function pairwise behavior. 141 * @tc.type: FUNC 142 */ 143 HWTEST_F(MemoryManagerTest, memorymanagertest_unmapmemory_003, TestSize.Level0) 144 { 145 std::string data = "ABCD"; 146 const size_t dataLength = 100; 147 data.resize(dataLength, '/'); 148 149 std::string filename = "/data/log/memory-001.dat"; 150 FileUtils fileUtils(filename); 151 fileUtils.WriteFile(data); 152 153 int fd = 0; 154 fd = open(filename.c_str(), O_RDWR); 155 EXPECT_NE(-1, fd); 156 157 size_t length = 10; 158 const auto& memoryManager = MemoryManager::GetInstance(); 159 void* buffer = memoryManager->MapMemory(fd, length); 160 memoryManager->UnMapMemory(buffer); 161 close(fd); 162 } 163 164 /** 165 * @tc.name: memorymanagertest_getmemory_001 166 * @tc.desc: Verify the GetMemory function return nullptr. 167 * @tc.type: FUNC 168 */ 169 HWTEST_F(MemoryManagerTest, memorymanagertest_getmemory_001, TestSize.Level0) 170 { 171 const auto& memoryManager = MemoryManager::GetInstance(); 172 void* buffer = nullptr; 173 Memory memory; 174 OH_NN_ReturnCode result = memoryManager->GetMemory(buffer, memory); 175 EXPECT_EQ(OH_NN_NULL_PTR, result); 176 } 177 178 /** 179 * @tc.name: memorymanagertest_getmemory_002 180 * @tc.desc: Verify the GetMemory function return invalid parameter. 181 * @tc.type: FUNC 182 */ 183 HWTEST_F(MemoryManagerTest, memorymanagertest_getmemory_002, TestSize.Level0) 184 { 185 const auto& memoryManager = MemoryManager::GetInstance(); 186 void* buffer = malloc(10); 187 Memory memory; 188 OH_NN_ReturnCode result = memoryManager->GetMemory(buffer, memory); 189 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result); 190 free(buffer); 191 } 192 193 /** 194 * @tc.name: memorymanagertest_getmemory_003 195 * @tc.desc: Verify the GetMemory function validate memory content success. 196 * @tc.type: FUNC 197 */ 198 HWTEST_F(MemoryManagerTest, memorymanagertest_getmemory_003, TestSize.Level0) 199 { 200 std::string data = "ABCD"; 201 const size_t dataLength = 100; 202 data.resize(dataLength, '%'); 203 204 std::string filename = "/data/log/memory-001.dat"; 205 FileUtils fileUtils(filename); 206 fileUtils.WriteFile(data); 207 208 int fd = 0; 209 fd = open(filename.c_str(), O_RDWR); 210 EXPECT_NE(-1, fd); 211 212 size_t length = 4; 213 const auto& memoryManager = MemoryManager::GetInstance(); 214 void* buffer = memoryManager->MapMemory(fd, length); 215 close(fd); 216 217 Memory memory; 218 OH_NN_ReturnCode result = memoryManager->GetMemory(buffer, memory); 219 EXPECT_EQ(OH_NN_SUCCESS, result); 220 EXPECT_NE(nullptr, memory.data); 221 222 const char* tmpData = static_cast<const char*>(memory.data); 223 EXPECT_EQ('A', static_cast<char>(tmpData[0])); 224 EXPECT_EQ('B', static_cast<char>(tmpData[1])); 225 EXPECT_EQ('C', static_cast<char>(tmpData[2])); 226 EXPECT_EQ('D', static_cast<char>(tmpData[3])); 227 memoryManager->UnMapMemory(buffer); 228 } 229 } // namespace UnitTest 230 } // namespace NeuralNetworkRuntime 231 } // namespace OHOS 232