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 <cstdlib>
17 #include <cstring>
18 #include <ctime>
19 #include <string>
20
21 #include "gtest/gtest.h"
22
23 #include "client_executor/include/i_aie_client.inl"
24 #include "service_dead_cb.h"
25
26 using namespace OHOS::AI;
27 using namespace testing::ext;
28
29 namespace {
30 const char * const INPUT_CHARACTER = "inputData";
31 const char * const CONFIG_DESCRIPTION = "config information";
32 const long long CLIENT_INFO_VERSION = 1;
33 const int EXTEND_LENGTH = 10;
34 const long long ALGORITHM_INFO_CLIENT_VERSION = 2;
35 const int ALGORITHM_TYPE = 66;
36 const long long ALGORITHM_VERSION = 2;
37 const int OPERATE_ID = 2;
38 const int REQUEST_ID = 3;
39 const int CHAR_TYPE = 4;
40 const char MIN_UPPER_CASE_CHAR = 'A';
41 const char MIN_LOWER_CASE_CHAR = 'a';
42 const char MIN_NUMERIC_CHAR = '0';
43 const char TRAILING_CHAR = '\0';
44 const int NUMBER_OF_ALPHABETS = 26;
45 const int NUMBER_OF_DIGITS = 10;
46 const int CHAR_TYPE_UPPER_CASE = 1;
47 const int CHAR_TYPE_LOWER_CASE = 2;
48 const int CHAR_TYPE_WHITE_SPACE = 3;
49 const char WHITE_SPACE = ' ';
50 }
51
52 class InitFunctionTest : public testing::Test {
53 public:
54 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()55 static void SetUpTestCase() {};
56
57 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()58 static void TearDownTestCase() {};
59
60 // SetUp:Execute before each test case
SetUp()61 void SetUp() {};
62
63 // TearDown:Execute after each test case
TearDown()64 void TearDown() {};
65 };
66
RandStr(const int len,char * str)67 static void RandStr(const int len, char *str)
68 {
69 srand(time(nullptr));
70 int i;
71 for (i = 0; i < len - 1; ++i) {
72 switch (rand() % CHAR_TYPE) {
73 case CHAR_TYPE_UPPER_CASE:
74 str[i] = MIN_UPPER_CASE_CHAR + rand() % NUMBER_OF_ALPHABETS;
75 break;
76 case CHAR_TYPE_LOWER_CASE:
77 str[i] = MIN_LOWER_CASE_CHAR + rand() % NUMBER_OF_ALPHABETS;
78 break;
79 case CHAR_TYPE_WHITE_SPACE:
80 str[i] = WHITE_SPACE;
81 break;
82 default:
83 str[i] = MIN_NUMERIC_CHAR + rand() % NUMBER_OF_DIGITS;
84 break;
85 }
86 }
87
88 str[i] = TRAILING_CHAR;
89 }
90
91 /**
92 * @tc.name: TestAieClientInitConfigInfo001
93 * @tc.desc: Test initial execution of certain algorithm plugin.
94 * @tc.type: FUNC
95 * @tc.require: AR000F77NM
96 */
97 HWTEST_F(InitFunctionTest, TestAieClientInitConfigInfo001, TestSize.Level1)
98 {
99 HILOGI("[Test]Begin TestAieClientInitConfigInfo001.");
100 const char *str = INPUT_CHARACTER;
101 char *inputData = const_cast<char*>(str);
102
103 char configDesc[21];
104 RandStr(21, configDesc);
105 ConfigInfo configInfo {.description = configDesc};
106
107 ClientInfo clientInfo = {
108 .clientVersion = CLIENT_INFO_VERSION,
109 .clientId = -1,
110 .sessionId = -1,
111 .serverUid = INVALID_UID,
112 .clientUid = INVALID_UID,
113 .extendLen = EXTEND_LENGTH,
114 .extendMsg = (unsigned char*)inputData,
115 };
116
117 AlgorithmInfo algoInfo = {
118 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
119 .isAsync = true,
120 .algorithmType = ALGORITHM_TYPE,
121 .algorithmVersion = ALGORITHM_VERSION,
122 .isCloud = true,
123 .operateId = OPERATE_ID,
124 .requestId = REQUEST_ID,
125 .extendLen = EXTEND_LENGTH,
126 .extendMsg = (unsigned char*)inputData,
127 };
128
129 ServiceDeadCb cb = ServiceDeadCb();
130 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
131 ASSERT_EQ(initResult, RETCODE_SUCCESS);
132 ASSERT_TRUE(clientInfo.clientId > 0);
133 ASSERT_TRUE(clientInfo.sessionId > 0);
134
135 int destroyResult = AieClientDestroy(clientInfo);
136 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
137 }
138
139 /**
140 * @tc.name: TestAieClientInitConfigInfo002
141 * @tc.desc: Test initial execution of certain algorithm plugin with over-size config message.
142 * @tc.type: FUNC
143 * @tc.require: AR000F77NM
144 */
145 HWTEST_F(InitFunctionTest, TestAieClientInitConfigInfo002, TestSize.Level1)
146 {
147 HILOGI("[Test]Begin TestAieClientInitConfigInfo002.");
148 const char *str = INPUT_CHARACTER;
149 char *inputData = const_cast<char*>(str);
150
151 char configDesc[129];
152 RandStr(129, configDesc);
153 ConfigInfo configInfo {.description = configDesc};
154
155 ClientInfo clientInfo = {
156 .clientVersion = CLIENT_INFO_VERSION,
157 .clientId = -1,
158 .sessionId = -1,
159 .serverUid = INVALID_UID,
160 .clientUid = INVALID_UID,
161 .extendLen = EXTEND_LENGTH,
162 .extendMsg = (unsigned char*)inputData,
163 };
164
165 AlgorithmInfo algoInfo = {
166 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
167 .isAsync = true,
168 .algorithmType = ALGORITHM_TYPE,
169 .algorithmVersion = ALGORITHM_VERSION,
170 .isCloud = true,
171 .operateId = OPERATE_ID,
172 .requestId = REQUEST_ID,
173 .extendLen = EXTEND_LENGTH,
174 .extendMsg = (unsigned char*)inputData,
175 };
176
177 ServiceDeadCb cb = ServiceDeadCb();
178 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
179 ASSERT_EQ(initResult, RETCODE_SUCCESS);
180 ASSERT_TRUE(clientInfo.clientId > 0);
181 ASSERT_TRUE(clientInfo.sessionId > 0);
182
183 int destroyResult = AieClientDestroy(clientInfo);
184 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
185 }
186
187 /**
188 * @tc.name: TestAieClientInitConfigInfo003
189 * @tc.desc: Test initial execution of certain algorithm plugin with much shorter(length = 1) config message.
190 * @tc.type: FUNC
191 * @tc.require: AR000F77NM
192 */
193 HWTEST_F(InitFunctionTest, TestAieClientInitConfigInfo003, TestSize.Level1)
194 {
195 HILOGI("[Test]Begin TestAieClientInitConfigInfo003.");
196 const char *str = INPUT_CHARACTER;
197 char *inputData = const_cast<char*>(str);
198
199 char configDesc[1];
200 RandStr(1, configDesc);
201 ConfigInfo configInfo {.description = configDesc};
202
203 ClientInfo clientInfo = {
204 .clientVersion = CLIENT_INFO_VERSION,
205 .clientId = -1,
206 .sessionId = -1,
207 .serverUid = INVALID_UID,
208 .clientUid = INVALID_UID,
209 .extendLen = EXTEND_LENGTH,
210 .extendMsg = (unsigned char*)inputData,
211 };
212
213 AlgorithmInfo algoInfo = {
214 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
215 .isAsync = true,
216 .algorithmType = ALGORITHM_TYPE,
217 .algorithmVersion = ALGORITHM_VERSION,
218 .isCloud = true,
219 .operateId = OPERATE_ID,
220 .requestId = REQUEST_ID,
221 .extendLen = EXTEND_LENGTH,
222 .extendMsg = (unsigned char*)inputData,
223 };
224
225 ServiceDeadCb cb = ServiceDeadCb();
226 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
227 ASSERT_EQ(initResult, RETCODE_SUCCESS);
228 ASSERT_TRUE(clientInfo.clientId > 0);
229 ASSERT_TRUE(clientInfo.sessionId > 0);
230
231 int destroyResult = AieClientDestroy(clientInfo);
232 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
233 }
234
235 /**
236 * @tc.name: TestAieClientInitClientInfo001
237 * @tc.desc: Test initial execution of certain algorithm plugin with 'clientId' being -1.
238 * @tc.type: FUNC
239 * @tc.require: AR000F77NM
240 */
241 HWTEST_F(InitFunctionTest, TestAieClientInitClientInfo001, TestSize.Level1)
242 {
243 HILOGI("[Test]Begin TestAieClientInitClientInfo001.");
244 const char *str = INPUT_CHARACTER;
245 char *inputData = const_cast<char*>(str);
246
247 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
248
249 ClientInfo clientInfo = {
250 .clientVersion = CLIENT_INFO_VERSION,
251 .clientId = -1,
252 .sessionId = -1,
253 .serverUid = INVALID_UID,
254 .clientUid = INVALID_UID,
255 .extendLen = EXTEND_LENGTH,
256 .extendMsg = (unsigned char*)inputData,
257 };
258
259 AlgorithmInfo algoInfo = {
260 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
261 .isAsync = true,
262 .algorithmType = ALGORITHM_TYPE,
263 .algorithmVersion = ALGORITHM_VERSION,
264 .isCloud = true,
265 .operateId = OPERATE_ID,
266 .requestId = REQUEST_ID,
267 .extendLen = EXTEND_LENGTH,
268 .extendMsg = (unsigned char*)inputData,
269 };
270
271 ServiceDeadCb cb = ServiceDeadCb();
272 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
273 ASSERT_EQ(initResult, RETCODE_SUCCESS);
274
275 int destroyResult = AieClientDestroy(clientInfo);
276 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
277 }
278
279 /**
280 * @tc.name: TestAieClientInitAlgoInfo001
281 * @tc.desc: Test initial execution of certain algorithm plugin with client
282 * with 'clientId' being -1 and 'isAsync' being true.
283 * @tc.type: FUNC
284 * @tc.require: AR000F77NM
285 */
286 HWTEST_F(InitFunctionTest, TestAieClientInitAlgoInfo001, TestSize.Level1)
287 {
288 HILOGI("[Test]Begin TestAieClientInitAlgoInfo001.");
289 const char *str = INPUT_CHARACTER;
290 char *inputData = const_cast<char*>(str);
291
292 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
293
294 ClientInfo clientInfo = {
295 .clientVersion = CLIENT_INFO_VERSION,
296 .clientId = -1,
297 .sessionId = -1,
298 .serverUid = INVALID_UID,
299 .clientUid = INVALID_UID,
300 .extendLen = EXTEND_LENGTH,
301 .extendMsg = (unsigned char*)inputData,
302 };
303
304 AlgorithmInfo algoInfo = {
305 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
306 .isAsync = true,
307 .algorithmType = ALGORITHM_TYPE,
308 .algorithmVersion = ALGORITHM_VERSION,
309 .isCloud = true,
310 .operateId = OPERATE_ID,
311 .requestId = REQUEST_ID,
312 .extendLen = EXTEND_LENGTH,
313 .extendMsg = (unsigned char*)inputData,
314 };
315
316 ServiceDeadCb cb = ServiceDeadCb();
317 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
318 ASSERT_EQ(initResult, RETCODE_SUCCESS);
319
320 int destroyResult = AieClientDestroy(clientInfo);
321 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
322 }
323
324 /**
325 * @tc.name: TestAieClientInitAlgoInfo002
326 * @tc.desc: Test initial execution of certain algorithm plugin with client
327 * with 'clientId' being -1 and 'isAsync' being false.
328 * @tc.type: FUNC
329 * @tc.require: AR000F77NM
330 */
331 HWTEST_F(InitFunctionTest, TestAieClientInitAlgoInfo002, TestSize.Level1)
332 {
333 HILOGI("[Test]Begin TestAieClientInitAlgoInfo002.");
334 const char *str = INPUT_CHARACTER;
335 char *inputData = const_cast<char*>(str);
336
337 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
338
339 ClientInfo clientInfo = {
340 .clientVersion = CLIENT_INFO_VERSION,
341 .clientId = -1,
342 .sessionId = -1,
343 .serverUid = INVALID_UID,
344 .clientUid = INVALID_UID,
345 .extendLen = EXTEND_LENGTH,
346 .extendMsg = (unsigned char*)inputData,
347 };
348
349 AlgorithmInfo algoInfo = {
350 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
351 .isAsync = false,
352 .algorithmType = ALGORITHM_TYPE,
353 .algorithmVersion = ALGORITHM_VERSION,
354 .isCloud = true,
355 .operateId = OPERATE_ID,
356 .requestId = REQUEST_ID,
357 .extendLen = EXTEND_LENGTH,
358 .extendMsg = (unsigned char*)inputData,
359 };
360
361 ServiceDeadCb cb = ServiceDeadCb();
362 int initResult = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
363 ASSERT_EQ(initResult, RETCODE_SUCCESS);
364
365 int destroyResult = AieClientDestroy(clientInfo);
366 ASSERT_EQ(destroyResult, RETCODE_SUCCESS);
367 }
368