1 /* 2 * Copyright (c) 2024 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 <gtest/gtest.h> 17 #include "edm_errors.h" 18 #include "hilog_wrapper.h" 19 #include "ddk_api.h" 20 #include "ddk_types.h" 21 22 using namespace testing::ext; 23 24 #define PARAM_0 0 25 #define BUFF_LENTH 10 26 #define PORT_READ 0x01 27 #define PORT_WRITE 0x02 28 #define PORT_ILLEGAL 0x08 29 30 namespace OHOS { 31 namespace ExternalDeviceManager { 32 33 class DdkBaseTest : public testing::Test { 34 public: SetUp()35 void SetUp() override 36 { 37 EDM_LOGD(MODULE_BASE_DDK, "DdkBaseTest SetUp"); 38 } TearDown()39 void TearDown() override 40 { 41 EDM_LOGD(MODULE_BASE_DDK, "DdkBaseTest TearDown"); 42 } 43 }; 44 45 /** 46 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0100 47 * @tc.name: OH_DDK_CreateAshmem_001 48 * @tc.desc: Test functions to OH_DDK_CreateAshmem 49 * @tc.desc: Operation success 50 * @tc.type: FUNC 51 */ 52 HWTEST_F(DdkBaseTest, OH_DDK_CreateAshmem_001, Function | MediumTest | Level1) 53 { 54 DDK_Ashmem *ashmem = nullptr; 55 const uint8_t name[100] = "TestAshmem"; 56 int32_t bufferLen = BUFF_LENTH; 57 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 58 EXPECT_EQ(ret, 0); 59 } 60 61 /** 62 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0200 63 * @tc.name: OH_DDK_CreateAshmem_002 64 * @tc.desc: Test functions to OH_DDK_CreateAshmem 65 * @tc.desc: Invalid parameter, bufferLen = 0 66 * @tc.type: FUNC 67 */ 68 HWTEST_F(DdkBaseTest, OH_DDK_CreateAshmem_002, Function | MediumTest | Level1) 69 { 70 DDK_Ashmem *ashmem = nullptr; 71 const uint8_t name[100] = "TestAshmem"; 72 uint32_t bufferLen = PARAM_0; 73 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 74 EXPECT_EQ(ret, DDK_INVALID_PARAMETER); 75 } 76 77 /** 78 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0300 79 * @tc.name: OH_DDK_CreateAshmem_003 80 * @tc.desc: Test functions to OH_DDK_CreateAshmem 81 * @tc.desc: Invalid parameter, name = nullptr 82 * @tc.type: FUNC 83 */ 84 HWTEST_F(DdkBaseTest, OH_DDK_CreateAshmem_003, Function | MediumTest | Level1) 85 { 86 DDK_Ashmem *ashmem = nullptr; 87 const uint8_t *name = nullptr; 88 uint32_t bufferLen = BUFF_LENTH; 89 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 90 EXPECT_EQ(ret, DDK_INVALID_PARAMETER); 91 } 92 93 /** 94 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0400 95 * @tc.name: OH_DDK_MapAshmem_001 96 * @tc.desc: Test functions to OH_DDK_MapAshmem 97 * @tc.desc: Operation success 98 * @tc.type: FUNC 99 */ 100 HWTEST_F(DdkBaseTest, OH_DDK_MapAshmem_001, Function | MediumTest | Level1) 101 { 102 DDK_Ashmem *ashmem = nullptr; 103 const uint8_t name[100] = "TestAshmem"; 104 uint32_t bufferLen = BUFF_LENTH; 105 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 106 EXPECT_EQ(ret, DDK_SUCCESS); 107 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 108 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 109 EXPECT_EQ(ret, DDK_SUCCESS); 110 } 111 112 /** 113 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0500 114 * @tc.name: OH_DDK_MapAshmem_002 115 * @tc.desc: Test functions to OH_DDK_MapAshmem 116 * @tc.desc: Null pointer exception, shared memory not created 117 * @tc.type: FUNC 118 */ 119 HWTEST_F(DdkBaseTest, OH_DDK_MapAshmem_002, Function | MediumTest | Level1) 120 { 121 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 122 auto ret = OH_DDK_MapAshmem(nullptr, ashmemMapType); 123 EXPECT_EQ(ret, DDK_NULL_PTR); 124 } 125 126 /** 127 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0600 128 * @tc.name: OH_DDK_MapAshmem_003 129 * @tc.desc: Test functions to OH_DDK_MapAshmem 130 * @tc.desc: Operation failed, ashmemFd = 0 131 * @tc.type: FUNC 132 */ 133 HWTEST_F(DdkBaseTest, OH_DDK_MapAshmem_003, Function | MediumTest | Level1) 134 { 135 DDK_Ashmem *ashmem = nullptr; 136 const uint8_t name[100] = "TestAshmem"; 137 uint32_t bufferLen = BUFF_LENTH; 138 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 139 EXPECT_EQ(ret, DDK_SUCCESS); 140 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 141 ashmem->ashmemFd = 0; 142 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 143 EXPECT_EQ(ret, DDK_FAILURE); 144 } 145 146 /** 147 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0700 148 * @tc.name: OH_DDK_MapAshmem_004 149 * @tc.desc: Test functions to OH_DDK_MapAshmem 150 * @tc.desc: Invalid operation, ashmemMapType = 0x80 151 * @tc.type: FUNC 152 */ 153 HWTEST_F(DdkBaseTest, OH_DDK_MapAshmem_004, Function | MediumTest | Level1) 154 { 155 DDK_Ashmem *ashmem = nullptr; 156 const uint8_t name[100] = "TestAshmem"; 157 uint32_t bufferLen = BUFF_LENTH; 158 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 159 EXPECT_EQ(ret, DDK_SUCCESS); 160 const uint8_t ashmemMapType = PORT_ILLEGAL; 161 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 162 EXPECT_EQ(ret, DDK_INVALID_OPERATION); 163 } 164 165 /** 166 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0800 167 * @tc.name: OH_DDK_UnMapAshmem_001 168 * @tc.desc: Test functions to OH_DDK_UnmapAshmem 169 * @tc.desc: Operation success 170 * @tc.type: FUNC 171 */ 172 HWTEST_F(DdkBaseTest, OH_DDK_UnMapAshmem_001, Function | MediumTest | Level1) 173 { 174 DDK_Ashmem *ashmem = nullptr; 175 const uint8_t name[100] = "TestAshmem"; 176 uint32_t bufferLen = BUFF_LENTH; 177 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 178 EXPECT_EQ(ret, DDK_SUCCESS); 179 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 180 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 181 EXPECT_EQ(ret, DDK_SUCCESS); 182 ret = OH_DDK_UnmapAshmem(ashmem); 183 EXPECT_EQ(ret, DDK_SUCCESS); 184 } 185 186 /** 187 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_0900 188 * @tc.name: OH_DDK_UnMapAshmem_002 189 * @tc.desc: Test functions to OH_DDK_UnmapAshmem 190 * @tc.desc: Null pointer exception, shared memory not created and mapped 191 * @tc.type: FUNC 192 */ 193 HWTEST_F(DdkBaseTest, OH_DDK_UnMapAshmem_002, Function | MediumTest | Level1) 194 { 195 DDK_Ashmem *ashmem = nullptr; 196 const uint8_t name[100] = "TestAshmem"; 197 uint32_t bufferLen = BUFF_LENTH; 198 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 199 EXPECT_EQ(ret, DDK_SUCCESS); 200 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 201 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 202 EXPECT_EQ(ret, DDK_SUCCESS); 203 ret = OH_DDK_UnmapAshmem(nullptr); 204 EXPECT_EQ(ret, DDK_NULL_PTR); 205 } 206 207 /** 208 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_1000 209 * @tc.name: OH_DDK_DestroyAshmem_001 210 * @tc.desc: Test functions to OH_DDK_DestroyAshmem 211 * @tc.desc: Operation success 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(DdkBaseTest, OH_DDK_DestroyAshmem_001, Function | MediumTest | Level1) 215 { 216 DDK_Ashmem *ashmem = nullptr; 217 const uint8_t name[100] = "TestAshmem"; 218 uint32_t bufferLen = BUFF_LENTH; 219 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 220 EXPECT_EQ(ret, DDK_SUCCESS); 221 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 222 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 223 EXPECT_EQ(ret, DDK_SUCCESS); 224 ret = OH_DDK_DestroyAshmem(ashmem); 225 EXPECT_EQ(ret, DDK_SUCCESS); 226 } 227 228 /** 229 * @tc.number: SUB_Driver_Ext_BaseDdkAPI_1100 230 * @tc.name: OH_DDK_DestroyAshmem_002 231 * @tc.desc: Test functions to OH_DDK_DestroyAshmem 232 * @tc.desc: Null pointer exception, shared memory not created and mapped 233 * @tc.type: FUNC 234 */ 235 HWTEST_F(DdkBaseTest, OH_DDK_DestroyAshmem_002, Function | MediumTest | Level1) 236 { 237 DDK_Ashmem *ashmem = nullptr; 238 const uint8_t name[100] = "TestAshmem"; 239 uint32_t bufferLen = BUFF_LENTH; 240 auto ret = OH_DDK_CreateAshmem(name, bufferLen, &ashmem); 241 EXPECT_EQ(ret, DDK_SUCCESS); 242 const uint8_t ashmemMapType = PORT_READ | PORT_WRITE; 243 ret = OH_DDK_MapAshmem(ashmem, ashmemMapType); 244 EXPECT_EQ(ret, DDK_SUCCESS); 245 ret = OH_DDK_DestroyAshmem(nullptr); 246 EXPECT_EQ(ret, DDK_NULL_PTR); 247 } 248 } // ExternalDeviceManager 249 } // OHOS 250