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 <future>
17 #include <unistd.h>
18
19 #include "gtest/gtest.h"
20 #include "securec.h"
21
22 #include "client_executor/include/i_aie_client.inl"
23 #include "platform/time/include/time.h"
24 #include "service_dead_cb.h"
25 #include "utils/constants/constants.h"
26 #include "utils/log/aie_log.h"
27
28 using namespace OHOS::AI;
29 using namespace testing::ext;
30
31 namespace {
32 const char * const CONFIG_DESCRIPTION = "config information";
33 constexpr int ALGORITHM_TYPE_SYNC = 0; // identity for sync algorithm, determines ALGORITHM_TYPE_ID_LIST[algorithmType]
34 constexpr long long ALGORITHM_VERSION = 1;
35 #ifdef __LINUX__
36 constexpr uint32_t LONG_MEMORY_LENGTH = 2U * 1024U * 1024U; // 2 MB long data
37 #else // liteos device may not have enough remaining memory.
38 constexpr uint32_t LONG_MEMORY_LENGTH = 1U * 1024U * 1024U; // 1 MB long data
39 #endif
40 constexpr char DUMP_CONTENT = 'm'; // randomly chosen to stuff inputInfo.
41
FreeDataInfo(DataInfo * dataInfo)42 void FreeDataInfo(DataInfo *dataInfo)
43 {
44 if (dataInfo != nullptr && dataInfo->data != nullptr) {
45 free(dataInfo->data);
46 dataInfo->data = nullptr;
47 dataInfo->length = 0;
48 }
49 }
50
FreeAlgorithmInfo(AlgorithmInfo * algorithmInfo)51 void FreeAlgorithmInfo(AlgorithmInfo *algorithmInfo)
52 {
53 if (algorithmInfo != nullptr && algorithmInfo->extendMsg != nullptr) {
54 free(algorithmInfo->extendMsg);
55 algorithmInfo->extendMsg = nullptr;
56 algorithmInfo->extendLen = 0;
57 }
58 }
59
FreeClientInfo(ClientInfo * clientInfo)60 void FreeClientInfo(ClientInfo *clientInfo)
61 {
62 if (clientInfo != nullptr && clientInfo->extendMsg != nullptr) {
63 free(clientInfo->extendMsg);
64 clientInfo->extendMsg = nullptr;
65 clientInfo->extendLen = 0;
66 }
67 }
68
69 class ClientCallback : public IClientCb {
70 public:
ClientCallback()71 ClientCallback()
72 {
73 AIE_NEW(promiseResult_, std::promise<DataInfo>);
74 }
75
~ClientCallback()76 ~ClientCallback() override
77 {
78 AIE_DELETE(promiseResult_);
79 }
80
OnResult(const DataInfo & result,int resultCode,int requestId)81 void OnResult(const DataInfo &result, int resultCode, int requestId) override
82 {
83 HILOGI("[Test]ShareMemoryFunctionTest OnResult retCode[%d], requestId[%d], resultLength[%d].",
84 resultCode, requestId, result.length);
85 DataInfo outputInfo {};
86 promiseResult_->set_value(outputInfo);
87 }
88
GetResult() const89 DataInfo GetResult() const
90 {
91 return promiseResult_->get_future().get();
92 }
93
94 private:
95 std::promise<DataInfo> *promiseResult_ {nullptr};
96 };
97 } // anonymous namespace
98
99 class ShareMemoryFunctionTest : public testing::Test {
100 public:
101 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()102 static void SetUpTestCase() {};
103
104 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()105 static void TearDownTestCase() {};
106
107 // SetUp:Execute before each test case
SetUp()108 void SetUp() override
109 {
110 clientInfo_ = {
111 .clientVersion = 0,
112 .clientId = INVALID_CLIENT_ID,
113 .sessionId = INVALID_SESSION_ID,
114 .serverUid = 0,
115 .clientUid = 0,
116 .extendLen = 0,
117 .extendMsg = nullptr,
118 };
119
120 algorithmInfo_ = {
121 .clientVersion = 0,
122 .isAsync = false,
123 .algorithmType = ALGORITHM_TYPE_SYNC,
124 .algorithmVersion = ALGORITHM_VERSION,
125 .isCloud = false,
126 .operateId = 0,
127 .requestId = 0,
128 .extendLen = 0,
129 .extendMsg = nullptr,
130 };
131
132 auto extMsg = reinterpret_cast<unsigned char*>(malloc(LONG_MEMORY_LENGTH));
133 ASSERT_NE(extMsg, nullptr);
134 ASSERT_EQ(
135 memset_s(extMsg, LONG_MEMORY_LENGTH, DUMP_CONTENT, LONG_MEMORY_LENGTH), EOK);
136 inputInfo_ = {
137 .data = extMsg,
138 .length = LONG_MEMORY_LENGTH,
139 };
140
141 outputInfo_ = {
142 .data = nullptr,
143 .length = 0,
144 };
145 }
146
147 // TearDown:Execute after each test case
TearDown()148 void TearDown() override
149 {
150 FreeClientInfo(&clientInfo_);
151 FreeAlgorithmInfo(&algorithmInfo_);
152 FreeDataInfo(&inputInfo_);
153 FreeDataInfo(&outputInfo_);
154 };
155
156 public:
157 ClientInfo clientInfo_ {};
158 AlgorithmInfo algorithmInfo_ {};
159 DataInfo inputInfo_ {};
160 DataInfo outputInfo_ {};
161 ConfigInfo configInfo_ {.description = CONFIG_DESCRIPTION};
162 ServiceDeadCb cb_ = ServiceDeadCb();
163 };
164
165 /**
166 * @tc.name: TestAieClientShareMemoryInitDestroy
167 * @tc.desc: Test the execution of Init/Destroy function with share memory.
168 * @tc.type: FUNC
169 * @tc.require: AR000F77NL
170 */
171 HWTEST_F(ShareMemoryFunctionTest, TestAieClientShareMemoryInitDestroy, TestSize.Level0)
172 {
173 HILOGI("[Test]TestAieClientShareMemoryInitDestroy.");
174
175 int resultCodeInit = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, &cb_);
176 ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
177 ASSERT_NE(clientInfo_.clientId, INVALID_CLIENT_ID);
178
179 int resultCodeDestroy = AieClientDestroy(clientInfo_);
180 ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
181 ASSERT_EQ(clientInfo_.clientId, INVALID_CLIENT_ID);
182 }
183
184 /**
185 * @tc.name: TestAieClientShareMemoryPrepareRelease
186 * @tc.desc: Test the execution of prepare/release function with share memory.
187 * @tc.type: FUNC
188 * @tc.require: AR000F77NL
189 */
190 HWTEST_F(ShareMemoryFunctionTest, TestAieClientShareMemoryPrepareRelease, TestSize.Level0)
191 {
192 HILOGI("[Test]TestAieClientShareMemoryPrepareRelease.");
193
194 int resultCodeInit = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, &cb_);
195 ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
196 ASSERT_NE(clientInfo_.clientId, INVALID_CLIENT_ID);
197
198 int resultCodePrepare = AieClientPrepare(clientInfo_, algorithmInfo_, inputInfo_, outputInfo_, nullptr);
199 ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
200 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
201 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
202 FreeDataInfo(&outputInfo_);
203
204 DataInfo dummy {};
205 int resultCodeRelease = AieClientRelease(clientInfo_, algorithmInfo_, dummy);
206 ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
207
208 int resultCodeDestroy = AieClientDestroy(clientInfo_);
209 ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
210 ASSERT_EQ(clientInfo_.clientId, INVALID_CLIENT_ID);
211 }
212
213 /**
214 * @tc.name: TestAieClientShareMemorySyncProcess
215 * @tc.desc: Test the execution of SyncProcess function with share memory.
216 * @tc.type: FUNC
217 * @tc.require: AR000F77NL
218 */
219 HWTEST_F(ShareMemoryFunctionTest, TestAieClientShareMemorySyncProcess, TestSize.Level0)
220 {
221 HILOGI("[Test]TestAieClientShareMemorySyncProcess.");
222
223 int resultCodeInit = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, &cb_);
224 ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
225 ASSERT_NE(clientInfo_.clientId, INVALID_CLIENT_ID);
226
227 int resultCodePrepare = AieClientPrepare(clientInfo_, algorithmInfo_, inputInfo_, outputInfo_, nullptr);
228 ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
229 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
230 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
231 FreeDataInfo(&outputInfo_);
232
233 int resultCodeSyncProcess = AieClientSyncProcess(clientInfo_, algorithmInfo_, inputInfo_, outputInfo_);
234 ASSERT_EQ(resultCodeSyncProcess, RETCODE_SUCCESS);
235 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
236 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
237 FreeDataInfo(&outputInfo_);
238
239 DataInfo dummy {};
240 int resultCodeRelease = AieClientRelease(clientInfo_, algorithmInfo_, dummy);
241 ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
242
243 int resultCodeDestroy = AieClientDestroy(clientInfo_);
244 ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
245 ASSERT_EQ(clientInfo_.clientId, INVALID_CLIENT_ID);
246 }
247
248 /**
249 * @tc.name: TestAieClientShareMemorySetGetOption
250 * @tc.desc: Test the execution of SetOption/GetOption function with share memory.
251 * @tc.type: FUNC
252 * @tc.require: AR000F77NL
253 */
254 HWTEST_F(ShareMemoryFunctionTest, TestAieClientShareMemorySetGetOption, TestSize.Level0)
255 {
256 HILOGI("[Test]TestAieClientShareMemorySetGetOption.");
257
258 int resultCodeInit = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, &cb_);
259 ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
260 ASSERT_NE(clientInfo_.clientId, INVALID_CLIENT_ID);
261
262 int resultCodePrepare = AieClientPrepare(clientInfo_, algorithmInfo_, inputInfo_, outputInfo_, nullptr);
263 ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
264 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
265 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
266 FreeDataInfo(&outputInfo_);
267
268 int dummyOptionType = 0;
269 int resultCodeSetOption = AieClientSetOption(clientInfo_, dummyOptionType, inputInfo_);
270 ASSERT_EQ(resultCodeSetOption, RETCODE_SUCCESS);
271
272 DataInfo dummy {};
273 int resultCodeGetOption = AieClientGetOption(clientInfo_, dummyOptionType, dummy, outputInfo_);
274 ASSERT_EQ(resultCodeGetOption, RETCODE_SUCCESS);
275 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
276 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
277 FreeDataInfo(&outputInfo_);
278
279 resultCodeSetOption = AieClientSetOption(clientInfo_, dummyOptionType, inputInfo_);
280 ASSERT_EQ(resultCodeSetOption, RETCODE_SUCCESS);
281
282 resultCodeGetOption = AieClientGetOption(clientInfo_, dummyOptionType, dummy, outputInfo_);
283 ASSERT_EQ(resultCodeGetOption, RETCODE_SUCCESS);
284 ASSERT_EQ(outputInfo_.length, inputInfo_.length);
285 ASSERT_EQ(memcmp(outputInfo_.data, inputInfo_.data, inputInfo_.length), 0);
286 FreeDataInfo(&outputInfo_);
287
288 int resultCodeRelease = AieClientRelease(clientInfo_, algorithmInfo_, dummy);
289 ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
290
291 int resultCodeDestroy = AieClientDestroy(clientInfo_);
292 ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
293 ASSERT_EQ(clientInfo_.clientId, INVALID_CLIENT_ID);
294 }
295