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 <cstring>
17 #include <unistd.h>
18
19 #include "gtest/gtest.h"
20
21 #include "client_executor/include/i_aie_client.inl"
22 #include "protocol/retcode_inner/aie_retcode_inner.h"
23 #include "server_executor/include/server_executor.h"
24 #include "service_dead_cb.h"
25 #include "utils/aie_macros.h"
26 #include "utils/constants/constants.h"
27 #include "utils/log/aie_log.h"
28
29 using namespace OHOS::AI;
30 using namespace testing::ext;
31
32 namespace {
33 const int REQUEST_ID = 1;
34 const int OPERATE_ID = 2;
35 const long long CLIENT_INFO_VERSION = 1;
36 const int SESSION_ID = -1;
37 const long long ALGORITHM_INFO_CLIENT_VERSION = 1;
38 const int ALGORITHM_SYNC_TYPE = 0;
39 const int ALGORITHM_ASYNC_TYPE = 1;
40 const long long ALGORITHM_VERSION = 1;
41 const char * const CONFIG_DESCRIPTION = "Prepare config information";
42 const char * const PREPARE_INPUT_SYNC = "Sync prepare inputData";
43 const char * const PREPARE_INPUT_ASYNC = "Async prepare inputData";
44 }
45
46 class ClientCallback : public IClientCb {
47 public:
48 ClientCallback() = default;
49 ~ClientCallback() override = default;
50 void OnResult(const DataInfo &result, int resultCode, int requestId) override
51 {
52 HILOGI("[Test]TestAieClientAsyncProcess execute ClientCallbackOnResult."\
53 " resultCode[%d], requestId[%d], resultData[%s], resultLength[%d].",
54 resultCode, requestId, result.data, result.length);
55 }
56 };
57
58 class PrepareFunctionTest : public testing::Test {
59 public:
60 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()61 static void SetUpTestCase() {};
62
63 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()64 static void TearDownTestCase() {};
65
66 // SetUp:Execute before each test case
SetUp()67 void SetUp() {};
68
69 // TearDown:Execute after each test case
TearDown()70 void TearDown() {};
71 };
72
73 /**
74 * @tc.name: TestAlgorithmInfo001
75 * @tc.desc: Test preparing execution of certain synchronous plugin.
76 * @tc.type: FUNC
77 * @tc.require: AR000F77NN
78 */
HWTEST_F(PrepareFunctionTest, TestAlgorithmInfo001, TestSize.Level1)79 HWTEST_F(PrepareFunctionTest, TestAlgorithmInfo001, TestSize.Level1)
80 {
81 HILOGI("[Test]TestAlgorithmInfo001.");
82 const char *str = PREPARE_INPUT_SYNC;
83 char *inputData = const_cast<char*>(str);
84 int len = strlen(str) + 1;
85
86 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
87
88 ClientInfo clientInfo = {
89 .clientVersion = CLIENT_INFO_VERSION,
90 .clientId = INVALID_CLIENT_ID,
91 .sessionId = SESSION_ID,
92 .serverUid = INVALID_UID,
93 .clientUid = INVALID_UID,
94 .extendLen = len,
95 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
96 };
97
98 AlgorithmInfo algoInfo = {
99 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
100 .isAsync = false,
101 .algorithmType = ALGORITHM_SYNC_TYPE,
102 .algorithmVersion = ALGORITHM_VERSION,
103 .isCloud = true,
104 .operateId = OPERATE_ID,
105 .requestId = REQUEST_ID,
106 .extendLen = len,
107 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
108 };
109
110 ServiceDeadCb cb = ServiceDeadCb();
111 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
112 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
113 ASSERT_TRUE(clientInfo.clientId > 0);
114
115 DataInfo inputInfo = {
116 .data = reinterpret_cast<unsigned char*>(inputData),
117 .length = len,
118 };
119
120 DataInfo outputInfo;
121 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
122 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
123 ASSERT_NE(outputInfo.data, nullptr);
124
125 AieClientRelease(clientInfo, algoInfo, inputInfo);
126
127 AieClientDestroy(clientInfo);
128 }
129
130 /**
131 * @tc.name: TestAlgorithmInfo002
132 * @tc.desc: Test preparing execution of certain asynchronous plugin.
133 * @tc.type: FUNC
134 * @tc.require: AR000F77NN
135 */
HWTEST_F(PrepareFunctionTest, TestAlgorithmInfo002, TestSize.Level1)136 HWTEST_F(PrepareFunctionTest, TestAlgorithmInfo002, TestSize.Level1)
137 {
138 HILOGI("[Test]TestAlgorithmInfo002.");
139 const char *str = PREPARE_INPUT_ASYNC;
140 char *inputData = const_cast<char*>(str);
141 int len = strlen(str) + 1;
142
143 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
144
145 ClientInfo clientInfo = {
146 .clientVersion = CLIENT_INFO_VERSION,
147 .clientId = INVALID_CLIENT_ID,
148 .sessionId = SESSION_ID,
149 .serverUid = INVALID_UID,
150 .clientUid = INVALID_UID,
151 .extendLen = len,
152 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
153 };
154
155 AlgorithmInfo algoInfo = {
156 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
157 .isAsync = true,
158 .algorithmType = ALGORITHM_ASYNC_TYPE,
159 .algorithmVersion = ALGORITHM_VERSION,
160 .isCloud = true,
161 .operateId = OPERATE_ID,
162 .requestId = REQUEST_ID,
163 .extendLen = len,
164 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
165 };
166
167 ServiceDeadCb cb = ServiceDeadCb();
168 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
169 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
170 ASSERT_TRUE(clientInfo.clientId > 0);
171
172 DataInfo inputInfo = {
173 .data = reinterpret_cast<unsigned char*>(inputData),
174 .length = len,
175 };
176
177 DataInfo outputInfo = {
178 .data = nullptr,
179 .length = 0
180 };
181 ClientCallback callback = ClientCallback();
182 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
183 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
184 ASSERT_NE(outputInfo.data, nullptr);
185
186 AieClientRelease(clientInfo, algoInfo, inputInfo);
187
188 AieClientDestroy(clientInfo);
189 }
190
191 /**
192 * @tc.name: TestInputInfo001
193 * @tc.desc: Test preparing execution of certain synchronous plugin
194 * with 'inputInfo.data = nullptr' and 'outputInfo.data = nullptr'.
195 * @tc.type: FUNC
196 * @tc.require: AR000F77NN
197 */
HWTEST_F(PrepareFunctionTest, TestInputInfo001, TestSize.Level1)198 HWTEST_F(PrepareFunctionTest, TestInputInfo001, TestSize.Level1)
199 {
200 HILOGI("[Test]TestInputInfo001.");
201 const char *str = PREPARE_INPUT_ASYNC;
202 char *inputData = const_cast<char*>(str);
203 int len = strlen(str) + 1;
204
205 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
206
207 ClientInfo clientInfo = {
208 .clientVersion = CLIENT_INFO_VERSION,
209 .clientId = INVALID_CLIENT_ID,
210 .sessionId = SESSION_ID,
211 .serverUid = INVALID_UID,
212 .clientUid = INVALID_UID,
213 .extendLen = len,
214 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
215 };
216
217 AlgorithmInfo algoInfo = {
218 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
219 .isAsync = false,
220 .algorithmType = ALGORITHM_SYNC_TYPE,
221 .algorithmVersion = ALGORITHM_VERSION,
222 .isCloud = true,
223 .operateId = OPERATE_ID,
224 .requestId = REQUEST_ID,
225 .extendLen = len,
226 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
227 };
228
229 ServiceDeadCb cb = ServiceDeadCb();
230 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
231 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
232 ASSERT_TRUE(clientInfo.clientId > 0);
233
234 DataInfo inputInfo = {
235 .data = nullptr,
236 .length = 0,
237 };
238
239 DataInfo outputInfo = {
240 .data = nullptr,
241 .length = 0
242 };
243 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
244 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
245 ASSERT_NE(outputInfo.data, nullptr);
246
247 AieClientRelease(clientInfo, algoInfo, inputInfo);
248
249 AieClientDestroy(clientInfo);
250 }
251
252 /**
253 * @tc.name: TestInputInfo002
254 * @tc.desc: Test preparing execution of certain asynchronous plugin
255 * with 'inputInfo.data = nullptr' and 'outputInfo.data = nullptr'.
256 * @tc.type: FUNC
257 * @tc.require: AR000F77NJ
258 */
HWTEST_F(PrepareFunctionTest, TestInputInfo002, TestSize.Level1)259 HWTEST_F(PrepareFunctionTest, TestInputInfo002, TestSize.Level1)
260 {
261 HILOGI("[Test]TestInputInfo002.");
262 const char *str = PREPARE_INPUT_ASYNC;
263 char *inputData = const_cast<char*>(str);
264 int len = strlen(str) + 1;
265
266 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
267
268 ClientInfo clientInfo = {
269 .clientVersion = CLIENT_INFO_VERSION,
270 .clientId = INVALID_CLIENT_ID,
271 .sessionId = SESSION_ID,
272 .serverUid = INVALID_UID,
273 .clientUid = INVALID_UID,
274 .extendLen = len,
275 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
276 };
277
278 AlgorithmInfo algoInfo = {
279 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
280 .isAsync = true,
281 .algorithmType = ALGORITHM_ASYNC_TYPE,
282 .algorithmVersion = ALGORITHM_VERSION,
283 .isCloud = true,
284 .operateId = OPERATE_ID,
285 .requestId = REQUEST_ID,
286 .extendLen = len,
287 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
288 };
289
290 ServiceDeadCb cb = ServiceDeadCb();
291 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
292 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
293 ASSERT_TRUE(clientInfo.clientId > 0);
294
295 DataInfo inputInfo = {
296 .data = nullptr,
297 .length = 0,
298 };
299
300 DataInfo outputInfo = {
301 .data = nullptr,
302 .length = 0
303 };
304 ClientCallback callback = ClientCallback();
305 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
306 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
307 ASSERT_NE(outputInfo.data, nullptr);
308
309 AieClientRelease(clientInfo, algoInfo, inputInfo);
310
311 AieClientDestroy(clientInfo);
312 }
313
314 /**
315 * @tc.name: TestCallback001
316 * @tc.desc: Test preparing execution of certain plugin
317 * with 'isAsync = false' and 'callback != nullptr'.
318 * @tc.type: FUNC
319 * @tc.require: AR000F77NJ
320 */
HWTEST_F(PrepareFunctionTest, TestCallback001, TestSize.Level1)321 HWTEST_F(PrepareFunctionTest, TestCallback001, TestSize.Level1)
322 {
323 HILOGI("[Test]TestCallback001.");
324 const char *str = PREPARE_INPUT_ASYNC;
325 char *inputData = const_cast<char*>(str);
326 int len = strlen(str) + 1;
327
328 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
329
330 ClientInfo clientInfo = {
331 .clientVersion = CLIENT_INFO_VERSION,
332 .clientId = INVALID_CLIENT_ID,
333 .sessionId = SESSION_ID,
334 .serverUid = INVALID_UID,
335 .clientUid = INVALID_UID,
336 .extendLen = len,
337 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
338 };
339
340 AlgorithmInfo algoInfo = {
341 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
342 .isAsync = false,
343 .algorithmType = ALGORITHM_SYNC_TYPE,
344 .algorithmVersion = ALGORITHM_VERSION,
345 .isCloud = true,
346 .operateId = OPERATE_ID,
347 .requestId = REQUEST_ID,
348 .extendLen = len,
349 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
350 };
351
352 ServiceDeadCb cb = ServiceDeadCb();
353 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
354 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
355 ASSERT_TRUE(clientInfo.clientId > 0);
356
357 DataInfo inputInfo = {
358 .data = reinterpret_cast<unsigned char*>(inputData),
359 .length = len,
360 };
361
362 DataInfo outputInfo = {
363 .data = nullptr,
364 .length = 0
365 };
366 ClientCallback callback = ClientCallback();
367 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
368 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
369 ASSERT_NE(outputInfo.data, nullptr);
370
371 AieClientRelease(clientInfo, algoInfo, inputInfo);
372
373 AieClientDestroy(clientInfo);
374 }
375
376 /**
377 * @tc.name: TestCallback002
378 * @tc.desc: Test preparing execution of certain plugin
379 * with 'isAsync = true' and 'callback = nullptr'.
380 * @tc.type: FUNC
381 * @tc.require: AR000F77NJ
382 */
HWTEST_F(PrepareFunctionTest, TestCallback002, TestSize.Level1)383 HWTEST_F(PrepareFunctionTest, TestCallback002, TestSize.Level1)
384 {
385 HILOGI("[Test]TestCallback002.");
386 const char *str = PREPARE_INPUT_ASYNC;
387 char *inputData = const_cast<char*>(str);
388 int len = strlen(str) + 1;
389
390 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
391
392 ClientInfo clientInfo = {
393 .clientVersion = CLIENT_INFO_VERSION,
394 .clientId = INVALID_CLIENT_ID,
395 .sessionId = SESSION_ID,
396 .serverUid = INVALID_UID,
397 .clientUid = INVALID_UID,
398 .extendLen = len,
399 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
400 };
401
402 AlgorithmInfo algoInfo = {
403 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
404 .isAsync = true,
405 .algorithmType = ALGORITHM_ASYNC_TYPE,
406 .algorithmVersion = ALGORITHM_VERSION,
407 .isCloud = true,
408 .operateId = OPERATE_ID,
409 .requestId = REQUEST_ID,
410 .extendLen = len,
411 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
412 };
413
414 ServiceDeadCb cb = ServiceDeadCb();
415 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
416 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
417 ASSERT_TRUE(clientInfo.clientId > 0);
418
419 DataInfo inputInfo = {
420 .data = reinterpret_cast<unsigned char*>(inputData),
421 .length = len,
422 };
423
424 DataInfo outputInfo = {
425 .data = nullptr,
426 .length = 0
427 };
428 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
429 ASSERT_NE(prepareRetCode, RETCODE_SUCCESS);
430 ASSERT_EQ(outputInfo.data, nullptr);
431
432 AieClientRelease(clientInfo, algoInfo, inputInfo);
433
434 AieClientDestroy(clientInfo);
435 }
436
437 /**
438 * @tc.name: TestRegisterCallbackProxy001
439 * @tc.desc: Test preparing execution of certain plugin
440 * with 'isAsync = true' and 'callback != nullptr'.
441 * @tc.type: FUNC
442 * @tc.require: AR000F77NJ
443 */
HWTEST_F(PrepareFunctionTest, TestRegisterCallbackProxy001, TestSize.Level1)444 static HWTEST_F(PrepareFunctionTest, TestRegisterCallbackProxy001, TestSize.Level1)
445 {
446 HILOGI("[Test]TestRegisterCallbackProxy001.");
447 const char *str = PREPARE_INPUT_ASYNC;
448 char *inputData = const_cast<char*>(str);
449 int len = strlen(str) + 1;
450
451 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
452
453 ClientInfo clientInfo = {
454 .clientVersion = CLIENT_INFO_VERSION,
455 .clientId = INVALID_CLIENT_ID,
456 .sessionId = SESSION_ID,
457 .serverUid = INVALID_UID,
458 .clientUid = INVALID_UID,
459 .extendLen = len,
460 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
461 };
462
463 AlgorithmInfo algoInfo = {
464 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
465 .isAsync = true,
466 .algorithmType = ALGORITHM_ASYNC_TYPE,
467 .algorithmVersion = ALGORITHM_VERSION,
468 .isCloud = true,
469 .operateId = OPERATE_ID,
470 .requestId = REQUEST_ID,
471 .extendLen = len,
472 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
473 };
474
475 ServiceDeadCb cb = ServiceDeadCb();
476 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
477 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
478 ASSERT_TRUE(clientInfo.clientId > 0);
479
480 DataInfo inputInfo = {
481 .data = reinterpret_cast<unsigned char*>(inputData),
482 .length = len,
483 };
484
485 DataInfo outputInfo = {
486 .data = nullptr,
487 .length = 0
488 };
489 ClientCallback callback = ClientCallback();
490 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
491 ASSERT_NE(prepareRetCode, RETCODE_SUCCESS);
492 ASSERT_EQ(outputInfo.data, nullptr);
493
494 AieClientRelease(clientInfo, algoInfo, inputInfo);
495
496 AieClientDestroy(clientInfo);
497 }
498