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 "service_dead_cb.h"
24 #include "utils/aie_macros.h"
25 #include "utils/log/aie_log.h"
26
27 using namespace OHOS::AI;
28 using namespace testing::ext;
29
30 namespace {
31 const int REQUEST_ID = 1;
32 const int OPERATE_ID = 2;
33 const long long CLIENT_INFO_VERSION = 1;
34 const long long ALGORITHM_INFO_CLIENT_VERSION = 1;
35 const int ALGORITHM_SYNC_TYPE = 0;
36 const int ALGORITHM_ASYNC_TYPE = 1;
37 const long long ALGORITHM_VERSION = 1;
38 const int RAND = 2;
39 const char * const CONFIG_DESCRIPTION = "Prepare config information";
40 const char * const PREPARE_INPUT_SYNC = "Sync prepare inputData";
41 const char * const EXTEND_INFORMATION = "Extended information";
42 const char * const SET_OPTION_INPUT = "First set option inputData";
43 const char * const SET_OPTION_DATA = "Second set option inputData";
44 }
45
46 class OptionFunctionTest : public testing::Test {
47 public:
48 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()49 static void SetUpTestCase()
50 {
51 srand(time(nullptr));
52 };
53
54 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()55 static void TearDownTestCase() {};
56
57 // SetUp:Execute before each test case
SetUp()58 void SetUp() {};
59
60 // TearDown:Execute after each test case
TearDown()61 void TearDown() {};
62 };
63
64 class ClientCallback : public IClientCb {
65 public:
66 ClientCallback() = default;
67 ~ClientCallback() override = default;
OnResult(const DataInfo & result,int resultCode,int requestId)68 void OnResult(const DataInfo &result, int resultCode, int requestId) override
69 {
70 HILOGI("[Test]TestSetOption/GetOption OnResult resultCode[%d], requestId[%d], resultData[%s].",
71 resultCode, requestId, result.data);
72 }
73 };
74
GetConfigInfo(ConfigInfo & configInfo)75 static void GetConfigInfo(ConfigInfo &configInfo)
76 {
77 configInfo = {.description = CONFIG_DESCRIPTION};
78 }
79
GetClientInfo(ClientInfo & clientInfo)80 static void GetClientInfo(ClientInfo &clientInfo)
81 {
82 const char *str = EXTEND_INFORMATION;
83 char *inputData = const_cast<char*>(str);
84 int len = strlen(str) + 1;
85
86 clientInfo = {
87 .clientVersion = CLIENT_INFO_VERSION,
88 .clientId = INVALID_CLIENT_ID,
89 .sessionId = INVALID_SESSION_ID,
90 .serverUid = INVALID_UID,
91 .clientUid = INVALID_UID,
92 .extendLen = len,
93 .extendMsg = (unsigned char*)inputData,
94 };
95 }
96
GetSyncAlgorithmInfo(AlgorithmInfo & algoInfo)97 static void GetSyncAlgorithmInfo(AlgorithmInfo &algoInfo)
98 {
99 const char *str = EXTEND_INFORMATION;
100 char *inputData = const_cast<char*>(str);
101 int len = strlen(str) + 1;
102
103 algoInfo = {
104 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
105 .isAsync = false,
106 .algorithmType = ALGORITHM_SYNC_TYPE,
107 .algorithmVersion = ALGORITHM_VERSION,
108 .isCloud = true,
109 .operateId = OPERATE_ID,
110 .requestId = REQUEST_ID,
111 .extendLen = len,
112 .extendMsg = (unsigned char*)inputData,
113 };
114 }
115
GetAsyncAlgorithmInfo(AlgorithmInfo & algoInfo)116 static void GetAsyncAlgorithmInfo(AlgorithmInfo &algoInfo)
117 {
118 const char *str = EXTEND_INFORMATION;
119 char *inputData = const_cast<char*>(str);
120 int len = strlen(str) + 1;
121
122 algoInfo = {
123 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
124 .isAsync = true,
125 .algorithmType = ALGORITHM_ASYNC_TYPE,
126 .algorithmVersion = ALGORITHM_VERSION,
127 .isCloud = true,
128 .operateId = OPERATE_ID,
129 .requestId = REQUEST_ID,
130 .extendLen = len,
131 .extendMsg = (unsigned char*)inputData,
132 };
133 }
134
135 /**
136 * @tc.name: TestOption001
137 * @tc.desc: Test get/set option function: after loading the plugin,
138 the test case sets the parameter value and gets the parameter value successfully.
139 * @tc.type: FUNC
140 * @tc.require: AR000F77NP
141 */
142 HWTEST_F(OptionFunctionTest, TestOption001, TestSize.Level0)
143 {
144 HILOGI("[Test]TestOption001.");
145 ConfigInfo configInfo;
146 GetConfigInfo(configInfo);
147
148 ClientInfo clientInfo;
149 GetClientInfo(clientInfo);
150
151 // Randomly generate synchronous or asynchronous algorithm information.
152 AlgorithmInfo algoInfo;
153 if (rand() % RAND == 0) {
154 HILOGI("[Test]The current algorithm information is asynchronous.");
155 GetAsyncAlgorithmInfo(algoInfo);
156 } else {
157 HILOGI("[Test]The current algorithm information is synchronous.");
158 GetSyncAlgorithmInfo(algoInfo);
159 }
160 ServiceDeadCb cb = ServiceDeadCb();
161 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
162 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
163 ASSERT_TRUE(clientInfo.clientId > 0);
164
165 const char *str = PREPARE_INPUT_SYNC;
166 char *inputData = const_cast<char*>(str);
167 int len = strlen(str) + 1;
168
169 DataInfo inputInfo = {
170 .data = (unsigned char*)inputData,
171 .length = len,
172 };
173
174 DataInfo outputInfo = {
175 .data = nullptr,
176 .length = 0
177 };
178
179 ClientCallback *callback = nullptr;
180 if (algoInfo.isAsync) {
181 AIE_NEW(callback, ClientCallback());
182 }
183 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
184 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
185 ASSERT_NE(outputInfo.data, nullptr);
186 ASSERT_TRUE(outputInfo.length > 0);
187
188 str = SET_OPTION_INPUT;
189 inputData = const_cast<char*>(str);
190 len = strlen(str) + 1;
191
192 inputInfo = {
193 .data = (unsigned char*)inputData,
194 .length = len,
195 };
196 int optionType = 0;
197 int setOptionRetCode = AieClientSetOption(clientInfo, optionType, inputInfo);
198 ASSERT_EQ(setOptionRetCode, RETCODE_SUCCESS);
199
200 outputInfo = {
201 .data = nullptr,
202 .length = 0
203 };
204 int getRetCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
205 ASSERT_EQ(getRetCode, RETCODE_SUCCESS);
206 ASSERT_EQ(outputInfo.length, inputInfo.length);
207 AIE_DELETE(callback);
208
209 AieClientRelease(clientInfo, algoInfo, inputInfo);
210
211 AieClientDestroy(clientInfo);
212 }
213
214 /**
215 * @tc.name: TestOption002
216 * @tc.desc: Test get/set option function: after loading the plugin,
217 the test case sets the parameter value and gets the parameter value successfully for many times.
218 * @tc.type: FUNC
219 * @tc.require: AR000F77NP
220 */
221 HWTEST_F(OptionFunctionTest, TestOption002, TestSize.Level0)
222 {
223 HILOGI("[Test]TestOption002.");
224 ConfigInfo configInfo;
225 GetConfigInfo(configInfo);
226
227 ClientInfo clientInfo;
228 GetClientInfo(clientInfo);
229
230 // Randomly generate synchronous or asynchronous algorithm information.
231 AlgorithmInfo algoInfo;
232 if (rand() % RAND == 0) {
233 HILOGI("[Test]The current algorithm information is asynchronous.");
234 GetAsyncAlgorithmInfo(algoInfo);
235 } else {
236 HILOGI("[Test]The current algorithm information is synchronous.");
237 GetSyncAlgorithmInfo(algoInfo);
238 }
239 ServiceDeadCb cb = ServiceDeadCb();
240 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
241 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
242 ASSERT_TRUE(clientInfo.clientId > 0);
243
244 const char *str = PREPARE_INPUT_SYNC;
245 char *inputData = const_cast<char*>(str);
246 int len = strlen(str) + 1;
247 DataInfo inputInfo = {
248 .data = (unsigned char*)inputData,
249 .length = len,
250 };
251
252 DataInfo outputInfo = {
253 .data = nullptr,
254 .length = 0
255 };
256
257 ClientCallback *callback = nullptr;
258 if (algoInfo.isAsync) {
259 AIE_NEW(callback, ClientCallback());
260 }
261
262 printf("%d!!!!!!!!!!!\n", clientInfo.clientId);
263 fflush(stdout);
264
265 int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
266 ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
267 ASSERT_NE(outputInfo.data, nullptr);
268 ASSERT_TRUE(outputInfo.length > 0);
269
270 str = SET_OPTION_INPUT;
271 inputData = const_cast<char*>(str);
272 len = strlen(str) + 1;
273 inputInfo = {
274 .data = (unsigned char*)inputData,
275 .length = len,
276 };
277 int optionType = 0;
278 int setOptionRetCode = AieClientSetOption(clientInfo, optionType, inputInfo);
279 ASSERT_EQ(setOptionRetCode, RETCODE_SUCCESS);
280
281 int getOptionRetCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
282 ASSERT_EQ(getOptionRetCode, RETCODE_SUCCESS);
283
284 str = SET_OPTION_DATA;
285 inputData = const_cast<char*>(str);
286 len = strlen(str) + 1;
287 inputInfo = {
288 .data = (unsigned char*)inputData,
289 .length = len,
290 };
291
292 int setRetCode = AieClientSetOption(clientInfo, optionType, inputInfo);
293 ASSERT_EQ(setRetCode, RETCODE_SUCCESS);
294
295 int getRetCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
296 ASSERT_EQ(getRetCode, RETCODE_SUCCESS);
297 ASSERT_EQ(outputInfo.length, inputInfo.length);
298 AIE_DELETE(callback);
299
300 AieClientRelease(clientInfo, algoInfo, inputInfo);
301
302 AieClientDestroy(clientInfo);
303 }
304
305 /**
306 * @tc.name: TestOption003
307 * @tc.desc: Test get/set option function: the test case sets the parameter value and gets the parameter value
308 which does not load the plugin.
309 * @tc.type: FUNC
310 * @tc.require: AR000F77NP
311 */
312 HWTEST_F(OptionFunctionTest, TestOption003, TestSize.Level0)
313 {
314 HILOGI("[Test]TestOption003.");
315 ConfigInfo configInfo;
316 GetConfigInfo(configInfo);
317
318 ClientInfo clientInfo;
319 GetClientInfo(clientInfo);
320
321 // Randomly generate synchronous or asynchronous algorithm information.
322 AlgorithmInfo algoInfo;
323 if (rand() % RAND == 0) {
324 HILOGI("[Test]The current algorithm information is asynchronous.");
325 GetAsyncAlgorithmInfo(algoInfo);
326 } else {
327 HILOGI("[Test]The current algorithm information is synchronous.");
328 GetSyncAlgorithmInfo(algoInfo);
329 }
330 ServiceDeadCb cb = ServiceDeadCb();
331 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
332 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
333 ASSERT_TRUE(clientInfo.clientId > 0);
334
335 const char *str = SET_OPTION_INPUT;
336 char *inputData = const_cast<char*>(str);
337 int len = strlen(str) + 1;
338
339 DataInfo inputInfo = {
340 .data = (unsigned char*)inputData,
341 .length = len,
342 };
343 int optionType = 0;
344 int setOptionRetCode = AieClientSetOption(clientInfo, optionType, inputInfo);
345 ASSERT_NE(setOptionRetCode, RETCODE_SUCCESS);
346
347 DataInfo outputInfo = {
348 .data = nullptr,
349 .length = 0
350 };
351 int getOptionRetCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
352 ASSERT_NE(getOptionRetCode, RETCODE_SUCCESS);
353 ASSERT_EQ(outputInfo.data, nullptr);
354 ASSERT_EQ(outputInfo.length, 0);
355
356 AieClientRelease(clientInfo, algoInfo, inputInfo);
357
358 AieClientDestroy(clientInfo);
359 }
360
361 /**
362 * @tc.name: TestOption004
363 * @tc.desc: Test get/set option function: the test case gets the parameter value
364 which does not set the parameter value and load the plugin.
365 * @tc.type: FUNC
366 * @tc.require: AR000F77NP
367 */
368 HWTEST_F(OptionFunctionTest, TestOption004, TestSize.Level0)
369 {
370 HILOGI("[Test]TestOption004.");
371
372 ConfigInfo configInfo;
373 GetConfigInfo(configInfo);
374
375 ClientInfo clientInfo;
376 GetClientInfo(clientInfo);
377
378 // Randomly generate synchronous or asynchronous algorithm information.
379 AlgorithmInfo algoInfo;
380 if (rand() % RAND == 0) {
381 HILOGI("[Test]The current algorithm information is asynchronous.");
382 GetAsyncAlgorithmInfo(algoInfo);
383 } else {
384 HILOGI("[Test]The current algorithm information is synchronous.");
385 GetSyncAlgorithmInfo(algoInfo);
386 }
387 ServiceDeadCb cb = ServiceDeadCb();
388 int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
389 ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
390 ASSERT_TRUE(clientInfo.clientId > 0);
391
392 int optionType = 0;
393
394 const char *str = SET_OPTION_DATA;
395 char *inputData = const_cast<char*>(str);
396 int len = strlen(str) + 1;
397
398 DataInfo inputInfo = {
399 .data = (unsigned char*)inputData,
400 .length = len,
401 };
402
403 DataInfo outputInfo = {
404 .data = nullptr,
405 .length = 0
406 };
407 int getOptionRetCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
408 ASSERT_NE(getOptionRetCode, RETCODE_SUCCESS);
409 ASSERT_EQ(outputInfo.data, nullptr);
410 ASSERT_EQ(outputInfo.length, 0);
411
412 AieClientRelease(clientInfo, algoInfo, inputInfo);
413
414 AieClientDestroy(clientInfo);
415 }
416