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 ReleaseFunctionTest : 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]TestAlgorithmInfo002 OnResult resultCode[%d], requestId[%d], resultData[%s], resultLength[%d].",
65            resultCode, requestId, result.data, result.length);
66    }
67};
68
69/**
70 * @tc.name: TestAieClientRelease001
71 * @tc.desc: Test release function: the registered callback is empty and test case does not execute
72             the method of AieClientSyncProcess.
73 * @tc.type: FUNC
74 * @tc.require: AR000F77NO
75 */
76HWTEST_F(ReleaseFunctionTest, TestAieClientRelease001, TestSize.Level0)
77{
78    HILOGI("[Test]TestAieClientRelease001.");
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    DataInfo inputInfo = {
109        .data = reinterpret_cast<unsigned char*>(inputData),
110        .length = len,
111    };
112
113    ServiceDeadCb deadCb = ServiceDeadCb();
114    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &deadCb);
115    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
116    ASSERT_TRUE(clientInfo.clientId > 0);
117
118    IClientCb *callback = nullptr;
119    DataInfo outputInfo;
120    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
121    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
122    ASSERT_TRUE(clientInfo.clientId > 0);
123
124    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
125    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
126    ASSERT_TRUE(clientInfo.clientId > 0);
127
128    AieClientDestroy(clientInfo);
129}
130
131/**
132 * @tc.name: TestAieClientRelease002
133 * @tc.desc: Test release function: the registered callback is not empty and test case does not execute
134             the method of AieClientSyncProcess.
135 * @tc.type: FUNC
136 * @tc.require: AR000F77NO
137 */
138HWTEST_F(ReleaseFunctionTest, TestAieClientRelease002, TestSize.Level0)
139{
140    HILOGI("[Test]TestAieClientRelease002.");
141
142    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
143
144    const char *str = INPUT_CHARACTER;
145    char *inputData = const_cast<char*>(str);
146    int len = strlen(str) + 1;
147
148    ClientInfo clientInfo = {
149        .clientVersion = CLIENT_INFO_VERSION,
150        .clientId = CLIENT_ID,
151        .sessionId = SESSION_ID,
152        .serverUid = INVALID_UID,
153        .clientUid = INVALID_UID,
154        .extendLen = len,
155        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
156    };
157
158    AlgorithmInfo algoInfo = {
159        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
160        .isAsync = true,
161        .algorithmType = ALGORITHM_TYPE_ASYNC,
162        .algorithmVersion = ALGORITHM_VERSION,
163        .isCloud = true,
164        .operateId = OPERATE_ID,
165        .requestId = REQUEST_ID,
166        .extendLen = len,
167        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
168    };
169
170    DataInfo inputInfo = {
171        .data = reinterpret_cast<unsigned char*>(inputData),
172        .length = len,
173    };
174
175    ServiceDeadCb cb = ServiceDeadCb();
176    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
177    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
178    ASSERT_TRUE(clientInfo.clientId > 0);
179
180    ClientCallback callback = ClientCallback();
181    DataInfo outputInfo;
182    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
183    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
184    ASSERT_TRUE(clientInfo.clientId > 0);
185
186    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
187    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
188    ASSERT_TRUE(clientInfo.clientId > 0);
189
190    AieClientDestroy(clientInfo);
191}
192
193/**
194 * @tc.name: TestAieClientRelease003
195 * @tc.desc: Test release function: the registered callback is empty and test case executes
196             the method of AieClientSyncProcess.
197 * @tc.type: FUNC
198 * @tc.require: AR000F77NO
199 */
200HWTEST_F(ReleaseFunctionTest, TestAieClientRelease003, TestSize.Level0)
201{
202    HILOGI("[Test]TestAieClientRelease003.");
203
204    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
205
206    const char *str = INPUT_CHARACTER;
207    char *inputData = const_cast<char*>(str);
208    int len = strlen(str) + 1;
209
210    ClientInfo clientInfo = {
211        .clientVersion = CLIENT_INFO_VERSION,
212        .clientId = CLIENT_ID,
213        .sessionId = SESSION_ID,
214        .serverUid = INVALID_UID,
215        .clientUid = INVALID_UID,
216        .extendLen = len,
217        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
218    };
219
220    AlgorithmInfo algoInfo = {
221        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
222        .isAsync = false,
223        .algorithmType = ALGORITHM_TYPE_SYNC,
224        .algorithmVersion = ALGORITHM_VERSION,
225        .isCloud = true,
226        .operateId = OPERATE_ID,
227        .requestId = REQUEST_ID,
228        .extendLen = len,
229        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
230    };
231
232    DataInfo inputInfo = {
233        .data = reinterpret_cast<unsigned char*>(inputData),
234        .length = len,
235    };
236
237    ServiceDeadCb cb = ServiceDeadCb();
238    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
239    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
240    ASSERT_TRUE(clientInfo.clientId > 0);
241
242    IClientCb *callback = nullptr;
243    DataInfo outputInfo;
244    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, callback);
245    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
246    ASSERT_TRUE(clientInfo.clientId > 0);
247
248    int resultCodeSyncProcess = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
249    ASSERT_EQ(resultCodeSyncProcess, RETCODE_SUCCESS);
250    ASSERT_TRUE(clientInfo.clientId > 0);
251
252    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
253    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
254    ASSERT_TRUE(clientInfo.clientId > 0);
255
256    AieClientDestroy(clientInfo);
257}
258
259/**
260 * @tc.name: TestAieClientRelease004
261 * @tc.desc: Test release function: the registered callback is not empty and test case executes
262             the method of AieClientSyncProcess.
263 * @tc.type: FUNC
264 * @tc.require: AR000F77NO
265 */
266HWTEST_F(ReleaseFunctionTest, TestAieClientRelease004, TestSize.Level0)
267{
268    HILOGI("[Test]TestAieClientRelease004.");
269
270    ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
271
272    const char *str = INPUT_CHARACTER;
273    char *inputData = const_cast<char*>(str);
274    int len = strlen(str) + 1;
275
276    ClientInfo clientInfo = {
277        .clientVersion = CLIENT_INFO_VERSION,
278        .clientId = CLIENT_ID,
279        .sessionId = SESSION_ID,
280        .serverUid = INVALID_UID,
281        .clientUid = INVALID_UID,
282        .extendLen = len,
283        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
284    };
285
286    AlgorithmInfo algoInfo = {
287        .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
288        .isAsync = true,
289        .algorithmType = ALGORITHM_TYPE_ASYNC,
290        .algorithmVersion = ALGORITHM_VERSION,
291        .isCloud = true,
292        .operateId = OPERATE_ID,
293        .requestId = REQUEST_ID,
294        .extendLen = len,
295        .extendMsg = reinterpret_cast<unsigned char*>(inputData),
296    };
297
298    DataInfo inputInfo = {
299        .data = reinterpret_cast<unsigned char*>(inputData),
300        .length = len,
301    };
302
303    ServiceDeadCb cb = ServiceDeadCb();
304    int resultCodeInit = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
305    ASSERT_EQ(resultCodeInit, RETCODE_SUCCESS);
306    ASSERT_TRUE(clientInfo.clientId > 0);
307
308    ClientCallback callback = ClientCallback();
309    DataInfo outputInfo;
310    int resultCodePrepare = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
311    ASSERT_EQ(resultCodePrepare, RETCODE_SUCCESS);
312    ASSERT_TRUE(clientInfo.clientId > 0);
313
314    int resultCodeAsyncProcess = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
315    ASSERT_EQ(resultCodeAsyncProcess, RETCODE_SUCCESS);
316    ASSERT_TRUE(clientInfo.clientId > 0);
317
318    int resultCodeRelease = AieClientRelease(clientInfo, algoInfo, inputInfo);
319    ASSERT_EQ(resultCodeRelease, RETCODE_SUCCESS);
320    ASSERT_TRUE(clientInfo.clientId > 0);
321
322    AieClientDestroy(clientInfo);
323}
324