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/struct_definition/aie_info_define.h"
23#include "service_dead_cb.h"
24#include "utils/log/aie_log.h"
25
26using namespace OHOS::AI;
27using namespace testing::ext;
28
29namespace {
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 CLIENT_ID = -1;
34    const int SESSION_ID = -1;
35    const long long ALGORITHM_INFO_CLIENT_VERSION = 2;
36    const int ALGORITHM_TYPE_SYNC = 0;
37    const int ALGORITHM_TYPE_ASYNC = 1;
38    const long long ALGORITHM_VERSION = 1;
39    const int OPERATE_ID = 2;
40    const int REQUEST_ID = 3;
41}
42
43class DestroyFunctionTest : public testing::Test {
44public:
45    // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
46    static void SetUpTestCase() {};
47
48    // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
49    static void TearDownTestCase() {};
50
51    // SetUp:Execute before each test case
52    void SetUp() {};
53
54    // TearDown:Execute after each test case
55    void TearDown() {};
56};
57
58class ClientCallback : public IClientCb {
59public:
60    ClientCallback() = default;
61    ~ClientCallback() override = default;
62    void OnResult(const DataInfo &result, int resultCode, int requestId) override
63    {
64        HILOGI("[Test]TestAieClientDestroy001 OnResult resultCode[%d],"\
65            "requestId[%d], resultData[%s], resultLength[%d].",
66            resultCode, requestId, result.data, result.length);
67    }
68};
69
70/**
71 * @tc.name: TestAieClientDestroy001
72 * @tc.desc: Test the execution of destroying interface immediately after initializing ai server.
73 * @tc.type: FUNC
74 * @tc.require: AR000F77NL
75 */
76HWTEST_F(DestroyFunctionTest, TestAieClientDestroy001, TestSize.Level0)
77{
78    HILOGI("[Test]TestAieClientDestroy001.");
79
80    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
81
82    const char *str = INPUT_CHARACTER;
83    char *inputData = const_cast<char*>(str);
84    int len = strlen(str) + 1;
85
86    ClientInfo clientInfo = {
87        .clientVersion = CLIENT_INFO_VERSION,
88        .clientId = CLIENT_ID,
89        .sessionId = SESSION_ID,
90        .serverUid = INVALID_UID,
91        .clientUid = INVALID_UID,
92        .extendLen = len,
93        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
94    };
95
96    AlgorithmInfo algoInfo = {
97        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
98        .isAsync = false,
99        .algorithmType = ALGORITHM_TYPE_SYNC,
100        .algorithmVersion = ALGORITHM_VERSION,
101        .isCloud = true,
102        .operateId = OPERATE_ID,
103        .requestId = REQUEST_ID,
104        .extendLen = len,
105        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
106    };
107
108    ServiceDeadCb deadCb = ServiceDeadCb();
109    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &deadCb);
110    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
111    ASSERT_TRUE(clientInfo.clientId > 0);
112
113    int resultCodeDestroy = AieClientDestroy(clientInfo);
114    ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
115    ASSERT_EQ(clientInfo.clientId, INVALID_SESSION_ID);
116}
117
118/**
119 * @tc.name: TestAieClientDestroy002
120 * @tc.desc: Test the execution of destroying interface, after initializing, preparing and synchronous processing.
121 * @tc.type: FUNC
122 * @tc.require: AR000F77NL
123 */
124HWTEST_F(DestroyFunctionTest, TestAieClientDestroy002, TestSize.Level0)
125{
126    HILOGI("[Test]TestAieClientDestroy002.");
127
128    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
129
130    const char *str = INPUT_CHARACTER;
131    char *inputData = const_cast<char*>(str);
132    int len = strlen(str) + 1;
133
134    ClientInfo clientInfo = {
135        .clientVersion = CLIENT_INFO_VERSION,
136        .clientId = CLIENT_ID,
137        .sessionId = SESSION_ID,
138        .serverUid = INVALID_UID,
139        .clientUid = INVALID_UID,
140        .extendLen = len,
141        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
142    };
143
144    AlgorithmInfo algoInfo = {
145        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
146        .isAsync = false,
147        .algorithmType = ALGORITHM_TYPE_SYNC,
148        .algorithmVersion = ALGORITHM_VERSION,
149        .isCloud = true,
150        .operateId = OPERATE_ID,
151        .requestId = REQUEST_ID,
152        .extendLen = len,
153        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
154    };
155
156    DataInfo inputInfo = {
157        .data = reinterpret_cast<unsigned char*>(inputData),
158        .length = len,
159    };
160
161    ServiceDeadCb cb = ServiceDeadCb();
162    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
163    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
164    ASSERT_TRUE(clientInfo.clientId > 0);
165
166    IClientCb *callback = nullptr;
167    DataInfo outputInfo;
168    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
169    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
170    ASSERT_TRUE(clientInfo.clientId > 0);
171
172    int resultCodeSyncProcess = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
173    ASSERT_EQ(resultCodeSyncProcess, RETCODE_SUCCESS);
174    ASSERT_TRUE(clientInfo.clientId > 0);
175
176    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
177    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
178    ASSERT_TRUE(clientInfo.clientId > 0);
179
180    int resultCodeDestroy = AieClientDestroy(clientInfo);
181    ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
182    ASSERT_EQ(clientInfo.clientId, INVALID_SESSION_ID);
183}
184
185/**
186 * @tc.name: TestAieClientDestroy003
187 * @tc.desc: Test the execution of destroying interface, after initializing, preparing and asynchronous processing.
188 * @tc.type: FUNC
189 * @tc.require: AR000F77NL
190 */
191HWTEST_F(DestroyFunctionTest, TestAieClientDestroy003, TestSize.Level0)
192{
193    HILOGI("[Test]TestAieClientDestroy003.");
194
195    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
196
197    const char *str = INPUT_CHARACTER;
198    char *inputData = const_cast<char*>(str);
199    int len = strlen(str) + 1;
200
201    ClientInfo clientInfo = {
202        .clientVersion = CLIENT_INFO_VERSION,
203        .clientId = CLIENT_ID,
204        .sessionId = SESSION_ID,
205        .serverUid = INVALID_UID,
206        .clientUid = INVALID_UID,
207        .extendLen = len,
208        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
209    };
210
211    AlgorithmInfo algoInfo = {
212        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
213        .isAsync = true,
214        .algorithmType = ALGORITHM_TYPE_ASYNC,
215        .algorithmVersion = ALGORITHM_VERSION,
216        .isCloud = true,
217        .operateId = OPERATE_ID,
218        .requestId = REQUEST_ID,
219        .extendLen = len,
220        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
221    };
222
223    DataInfo inputInfo = {
224        .data = reinterpret_cast<unsigned char*>(inputData),
225        .length = len,
226    };
227
228    ServiceDeadCb cb = ServiceDeadCb();
229    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
230    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
231    ASSERT_TRUE(clientInfo.clientId > 0);
232
233    ClientCallback callback = ClientCallback();
234    DataInfo outputInfo;
235    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
236    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
237    ASSERT_TRUE(clientInfo.clientId > 0);
238
239    int resultCodeAsyncProcess = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
240    ASSERT_EQ(resultCodeAsyncProcess, RETCODE_SUCCESS);
241    ASSERT_TRUE(clientInfo.clientId > 0);
242
243    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
244    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
245    ASSERT_TRUE(clientInfo.clientId > 0);
246
247    int resultCodeDestroy = AieClientDestroy(clientInfo);
248    ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
249    ASSERT_EQ(clientInfo.clientId, INVALID_SESSION_ID);
250}
251
252/**
253 * @tc.name: TestAieClientDestroy004
254 * @tc.desc: Test the execution of destroying interface, on the condition that dead callback is nullptr
255 *           and not registered.
256 * @tc.type: FUNC
257 * @tc.require: AR000F77NL
258 */
259HWTEST_F(DestroyFunctionTest, TestAieClientDestroy004, TestSize.Level0)
260{
261    HILOGI("[Test]TestAieClientDestroy004.");
262
263    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
264
265    const char *str = INPUT_CHARACTER;
266    char *inputData = const_cast<char*>(str);
267    int len = strlen(str) + 1;
268
269    ClientInfo clientInfo = {
270        .clientVersion = CLIENT_INFO_VERSION,
271        .clientId = CLIENT_ID,
272        .sessionId = SESSION_ID,
273        .serverUid = INVALID_UID,
274        .clientUid = INVALID_UID,
275        .extendLen = len,
276        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
277    };
278
279    AlgorithmInfo algoInfo = {
280        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
281        .isAsync = true,
282        .algorithmType = ALGORITHM_TYPE_ASYNC,
283        .algorithmVersion = ALGORITHM_VERSION,
284        .isCloud = true,
285        .operateId = OPERATE_ID,
286        .requestId = REQUEST_ID,
287        .extendLen = len,
288        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
289    };
290
291    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, nullptr);
292    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
293    ASSERT_TRUE(clientInfo.clientId > 0);
294
295    int resultCodeDestroy = AieClientDestroy(clientInfo);
296    ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
297    ASSERT_EQ(clientInfo.clientId, INVALID_SESSION_ID);
298}
299
300/**
301 * @tc.name: TestAieClientDestroy005
302 * @tc.desc: Test the execution of destroying interface, after initializing, preparing.
303 * @tc.type: FUNC
304 * @tc.require: AR000F77NL
305 */
306HWTEST_F(DestroyFunctionTest, TestAieClientDestroy005, TestSize.Level0)
307{
308    HILOGI("[Test]TestAieClientDestroy005.");
309
310    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
311
312    const char *str = INPUT_CHARACTER;
313    char *inputData = const_cast<char*>(str);
314    int len = strlen(str) + 1;
315
316    ClientInfo clientInfo = {
317        .clientVersion = CLIENT_INFO_VERSION,
318        .clientId = CLIENT_ID,
319        .sessionId = SESSION_ID,
320        .serverUid = INVALID_UID,
321        .clientUid = INVALID_UID,
322        .extendLen = len,
323        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
324    };
325
326    AlgorithmInfo algoInfo = {
327        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
328        .isAsync = false,
329        .algorithmType = ALGORITHM_TYPE_SYNC,
330        .algorithmVersion = ALGORITHM_VERSION,
331        .isCloud = true,
332        .operateId = OPERATE_ID,
333        .requestId = REQUEST_ID,
334        .extendLen = len,
335        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
336    };
337
338    DataInfo inputInfo = {
339        .data = reinterpret_cast<unsigned char*>(inputData),
340        .length = len,
341    };
342
343    ServiceDeadCb cb = ServiceDeadCb();
344    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
345    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
346    ASSERT_TRUE(clientInfo.clientId > 0);
347
348    IClientCb *callback = nullptr;
349    DataInfo outputInfo;
350    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
351    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
352    ASSERT_TRUE(clientInfo.clientId > 0);
353
354    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
355    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
356    ASSERT_TRUE(clientInfo.clientId > 0);
357
358    int resultCodeDestroy = AieClientDestroy(clientInfo);
359    ASSERT_EQ(resultCodeDestroy, RETCODE_SUCCESS);
360    ASSERT_EQ(clientInfo.clientId, INVALID_SESSION_ID);
361}
362