• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "service_dead_cb.h"
23 #include "utils/log/aie_log.h"
24 
25 using namespace OHOS::AI;
26 using namespace testing::ext;
27 
28 namespace {
29     const char * const INPUT_CHARACTER_001 = "inputData001";
30     const char * const INPUT_CHARACTER_002 = "Data002";
31     const char * const CONFIG_DESCRIPTION = "config information";
32     const long long CLIENT_INFO_VERSION = 1;
33     const int CLIENT_ID = -1;
34     const int SESSION_ID = -1;
35     const int EXTEND_LENGTH = 10;
36     const long long ALGORITHM_INFO_CLIENT_VERSION = 2;
37     const int ALGORITHM_TYPE = 0;
38     const long long ALGORITHM_VERSION = 1;
39     const int OPERATE_ID = 2;
40     const int REQUEST_ID = 3;
41     const int CYCLENUMS = 5;
42 }
43 
44 class SyncProcessFunctionTest : public testing::Test {
45 public:
46     // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()47     static void SetUpTestCase() {};
48 
49     // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()50     static void TearDownTestCase() {};
51 
52     // SetUp:Execute before each test case
SetUp()53     void SetUp() {};
54 
55     // TearDown:Execute after each test case
TearDown()56     void TearDown() {};
57 };
58 
59 class ClientCallback : public IClientCb {
60 public:
61     ClientCallback() = default;
62     ~ClientCallback() override = default;
OnResult(const DataInfo & result,int resultCode,int requestId)63     void OnResult(const DataInfo &result, int resultCode, int requestId) override
64     {
65         HILOGI("[Test]TestAieClientSyncProcess OnResult resultCode[%d],"\
66             "requestId[%d], resultData[%s], resultLength[%d].",
67             resultCode, requestId, result.data, result.length);
68     }
69 };
70 
TestGetRightInfo(ConfigInfo & configInfo,ClientInfo & clientInfo,AlgorithmInfo & algoInfo)71 static void TestGetRightInfo(ConfigInfo &configInfo, ClientInfo &clientInfo, AlgorithmInfo &algoInfo)
72 {
73     HILOGI("[Test]Begin TestGetRightInfo");
74     const char *str = INPUT_CHARACTER_002;
75     char *inputData = const_cast<char*>(str);
76 
77     configInfo.description = CONFIG_DESCRIPTION;
78 
79     clientInfo.clientVersion = CLIENT_INFO_VERSION;
80     clientInfo.clientId = CLIENT_ID;
81     clientInfo.sessionId = SESSION_ID;
82     clientInfo.serverUid = INVALID_UID,
83     clientInfo.clientUid = INVALID_UID,
84     clientInfo.extendLen = EXTEND_LENGTH;
85     clientInfo.extendMsg = (unsigned char*)inputData;
86 
87     algoInfo.clientVersion = ALGORITHM_INFO_CLIENT_VERSION;
88     algoInfo.isAsync = false;
89     algoInfo.algorithmType = ALGORITHM_TYPE;
90     algoInfo.algorithmVersion = ALGORITHM_VERSION;
91     algoInfo.isCloud = true;
92     algoInfo.operateId = OPERATE_ID;
93     algoInfo.requestId = REQUEST_ID;
94     algoInfo.extendLen = EXTEND_LENGTH;
95     algoInfo.extendMsg = (unsigned char*)inputData;
96 
97     HILOGI("[Test]End TestGetRightInfo");
98 }
99 
100 /**
101  * @tc.name: TestAieClientSyncProcess001
102  * @tc.desc: Test sync process function: the parameter of isAsync is false and the inputInfo is not null.
103  * @tc.type: FUNC
104  * @tc.require: AR000F77NQ
105  */
106 HWTEST_F(SyncProcessFunctionTest, TestAieClientSyncProcess001, TestSize.Level0)
107 {
108     HILOGI("[Test]Begin to testAieClientSyncProcess001");
109     const char *str = INPUT_CHARACTER_001;
110     char *inputData = const_cast<char*>(str);
111     int len = strlen(str) + 1;
112 
113     ConfigInfo configInfo;
114     ClientInfo clientInfo;
115     AlgorithmInfo algoInfo;
116 
117     TestGetRightInfo(configInfo, clientInfo, algoInfo);
118 
119     DataInfo inputInfo = {
120         .data = (unsigned char*)inputData,
121         .length = len,
122     };
123 
124     ServiceDeadCb cb = ServiceDeadCb();
125     int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
126     ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
127 
128     DataInfo outputInfo = {
129         .data = nullptr,
130         .length = 0,
131     };
132     int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
133     ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
134 
135     outputInfo = {
136         .data = nullptr,
137         .length = 0,
138     };
139     int processRetCode = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
140     ASSERT_EQ(processRetCode, RETCODE_SUCCESS);
141 
142     AieClientRelease(clientInfo, algoInfo, inputInfo);
143 
144     AieClientDestroy(clientInfo);
145 }
146 
147 /**
148  * @tc.name: TestAieClientSyncProcess002
149  * @tc.desc: Test sync process function: the parameter of isAsync is false and the inputInfo is null.
150  * @tc.type: FUNC
151  * @tc.require: AR000F77NQ
152  */
153 HWTEST_F(SyncProcessFunctionTest, TestAieClientSyncProcess002, TestSize.Level0)
154 {
155     HILOGI("[Test]Begin testAieClientSyncProcess002");
156     const char *str = INPUT_CHARACTER_002;
157     char *inputData = const_cast<char*>(str);
158     int len = strlen(str) + 1;
159 
160     ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
161 
162     ClientInfo clientInfo = {
163         .clientVersion = CLIENT_INFO_VERSION,
164         .clientId = CLIENT_ID,
165         .sessionId = SESSION_ID,
166         .serverUid = INVALID_UID,
167         .clientUid = INVALID_UID,
168         .extendLen = EXTEND_LENGTH,
169         .extendMsg = (unsigned char*)inputData,
170     };
171 
172     AlgorithmInfo algoInfo = {
173         .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
174         .isAsync = false,
175         .algorithmType = ALGORITHM_TYPE,
176         .algorithmVersion = ALGORITHM_VERSION,
177         .isCloud = true,
178         .operateId = OPERATE_ID,
179         .requestId = REQUEST_ID,
180         .extendLen = EXTEND_LENGTH,
181         .extendMsg = (unsigned char*)inputData,
182     };
183 
184     ServiceDeadCb cb = ServiceDeadCb();
185     int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
186     ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
187 
188     DataInfo outputInfo = {
189         .data = nullptr,
190         .length = 0,
191     };
192 
193     DataInfo prepareInputInfo = {
194         .data = (unsigned char*)inputData,
195         .length = len,
196     };
197 
198     int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, prepareInputInfo, outputInfo, nullptr);
199     ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
200 
201     outputInfo = {
202         .data = nullptr,
203         .length = 0,
204     };
205 
206     DataInfo processInputInfo = {
207         .data = nullptr,
208         .length = 0,
209     };
210 
211     int processRetCode = AieClientSyncProcess(clientInfo, algoInfo, processInputInfo, outputInfo);
212     ASSERT_EQ(processRetCode, RETCODE_SUCCESS);
213 
214     AieClientRelease(clientInfo, algoInfo, processInputInfo);
215 
216     AieClientDestroy(clientInfo);
217 }
218 
219 /**
220  * @tc.name: TestAieClientSyncProcess003
221  * @tc.desc: Test sync process function: the parameter of isAsync is true.
222  * @tc.type: FUNC
223  * @tc.require: AR000F77NQ
224  */
225 HWTEST_F(SyncProcessFunctionTest, TestAieClientSyncProcess003, TestSize.Level0)
226 {
227     HILOGI("[Test]Begin testAieClientSyncProcess003");
228     const char *str = INPUT_CHARACTER_002;
229     char *inputData = const_cast<char*>(str);
230     int len = strlen(str) + 1;
231 
232     ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
233 
234     ClientInfo clientInfo = {
235         .clientVersion = CLIENT_INFO_VERSION,
236         .clientId = CLIENT_ID,
237         .sessionId = SESSION_ID,
238         .serverUid = INVALID_UID,
239         .clientUid = INVALID_UID,
240         .extendLen = EXTEND_LENGTH,
241         .extendMsg = (unsigned char*)inputData,
242     };
243 
244     AlgorithmInfo algoInfo = {
245         .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
246         .isAsync = true,
247         .algorithmType = ALGORITHM_TYPE,
248         .algorithmVersion = ALGORITHM_VERSION,
249         .isCloud = true,
250         .operateId = OPERATE_ID,
251         .requestId = REQUEST_ID,
252         .extendLen = EXTEND_LENGTH,
253         .extendMsg = (unsigned char*)inputData,
254     };
255 
256     DataInfo inputInfo = {
257         .data = (unsigned char*)inputData,
258         .length = len,
259     };
260 
261     ServiceDeadCb cb = ServiceDeadCb();
262     int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
263     ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
264 
265     DataInfo outputInfo = {
266         .data = nullptr,
267         .length = 0,
268     };
269     ClientCallback callback = ClientCallback();
270     int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
271     ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
272 
273     outputInfo = {
274         .data = nullptr,
275         .length = 0,
276     };
277 
278     int processRetCode = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
279     ASSERT_NE(processRetCode, RETCODE_SUCCESS);
280 
281     AieClientRelease(clientInfo, algoInfo, inputInfo);
282 
283     AieClientDestroy(clientInfo);
284 }
285 
286 /**
287  * @tc.name: TestAieClientSyncProcess004
288  * @tc.desc: Test sync process function: the parameter of isAsync is false and the test case executes
289              the method of AieClientSyncProcess five times.
290  * @tc.type: FUNC
291  * @tc.require: AR000F77NQ
292  */
293 HWTEST_F(SyncProcessFunctionTest, TestAieClientSyncProcess004, TestSize.Level0)
294 {
295     HILOGI("[Test]Begin testAieClientSyncProcess004");
296 
297     const char *str = INPUT_CHARACTER_001;
298     char *inputData = const_cast<char *>(str);
299     int len = strlen(str) + 1;
300 
301     ConfigInfo configInfo;
302     ClientInfo clientInfo;
303     AlgorithmInfo algoInfo;
304 
305     TestGetRightInfo(configInfo, clientInfo, algoInfo);
306 
307     DataInfo inputInfo = {
308         .data = (unsigned char *)inputData,
309         .length = len,
310     };
311 
312     ServiceDeadCb cb = ServiceDeadCb();
313     int initRetCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
314     ASSERT_EQ(initRetCode, RETCODE_SUCCESS);
315 
316     DataInfo outputInfo = {
317         .data = nullptr,
318         .length = 0,
319     };
320 
321     int prepareRetCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
322     ASSERT_EQ(prepareRetCode, RETCODE_SUCCESS);
323 
324     for (int i = 0; i < CYCLENUMS; ++i) {
325         HILOGI("[Test]CycleNum is [%d]", i);
326         int processRetCode = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
327         ASSERT_EQ(processRetCode, RETCODE_SUCCESS);
328     }
329 
330     AieClientRelease(clientInfo, algoInfo, inputInfo);
331 
332     AieClientDestroy(clientInfo);
333 }
334