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 "gtest/gtest.h"
17 #include <cstring>
18 #include <unistd.h>
19
20 #include "client_executor/include/i_aie_client.inl"
21 #include "protocol/retcode_inner/aie_retcode_inner.h"
22 #include "server_executor/include/server_executor.h"
23 #include "utils/aie_client_callback.h"
24 #include "utils/aie_client_common.h"
25 #include "utils/aie_client_const.h"
26 #include "utils/aie_macros.h"
27 #include "utils/constants/constants.h"
28 #include "utils/log/aie_log.h"
29 #include "utils/service_dead_cb.h"
30 #include "utils/utils.h"
31
32 using namespace ::testing;
33 using namespace testing::ext;
34 using namespace OHOS::AI;
35
36 class AieClientPrepareFunctionTest : public testing::Test {};
37
38 /**
39 * Tests the input parameters algorithmInfo and inputInfo in AieClientPrepare().
40 *
41 * isAsync The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
42 * isPrepareInputInfoNull Whether the input parameter InputInfo of AieClientPrepare is null or not.
43 * isPrepareCbNull Whether the cb input parameter cb of AieClientPrepare is null or not.
44 * isPrepareSuccess Whether the expected result of AieClientPrepare is successful or not.
45 * isOutputInfoDataCorrect Whether the expected OutputInfoData of AieClientPrepare is correct or not.
46 */
TestAieClientPrepare(bool isAsync,bool isPrepareInputInfoNull,bool isPrepareCbNull,bool isPrepareSuccess,bool isOutputInfoDataCorrect)47 static void TestAieClientPrepare(bool isAsync, bool isPrepareInputInfoNull, bool isPrepareCbNull,
48 bool isPrepareSuccess, bool isOutputInfoDataCorrect)
49 {
50 // Step 0: Defines variables.
51 ConfigInfo configInfo;
52 GetConfigInfo(configInfo);
53
54 ClientInfo clientInfo;
55 GetClientInfo(clientInfo);
56
57 AlgorithmInfo algorithmInfo;
58 int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
59 GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
60
61 ServiceDeadCb *cb = nullptr;
62 AIE_NEW(cb, ServiceDeadCb());
63
64 // Step 1: Invokes AieClientInit.
65 int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
66 EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
67 EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
68 EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
69 << ") is incorrect!";
70
71 // Step 2: Invokes AieClientPrepare.
72 DataInfo inputInfo = {
73 .data = nullptr,
74 .length = 0,
75 };
76 if (!isPrepareInputInfoNull) {
77 const char *str = INPUT_INFO_PREPARE;
78 char *inputData = const_cast<char*>(str);
79 int length = strlen(str) + 1;
80
81 inputInfo = {
82 .data = (unsigned char *) inputData,
83 .length = length,
84 };
85 }
86
87 DataInfo outputInfo = {
88 .data = nullptr,
89 .length = 0,
90 };
91
92 ClientCallback *callback = nullptr;
93 if (!isPrepareCbNull) {
94 AIE_NEW(callback, ClientCallback());
95 }
96 int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, inputInfo, outputInfo, callback);
97 EXPECT_EQ(isPrepareSuccess, prepareReturnCode == RETCODE_SUCCESS) << "AieClientPrepare is failed!";
98 EXPECT_EQ(isOutputInfoDataCorrect, outputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
99
100 // Step 3: Invokes AieClientRelease.
101 if (prepareReturnCode == RETCODE_SUCCESS) {
102 DataInfo releaseInputInfo = GetDataInfo(false, INPUT_INFO_RELEASE);
103 int releaseReturnCode = AieClientRelease(clientInfo, algorithmInfo, releaseInputInfo);
104 EXPECT_EQ(RETCODE_SUCCESS, releaseReturnCode) << "AieClientRelease is failed!";
105 }
106
107 // Step 4: Invokes AieClientDestroy.
108 if (initReturnCode == RETCODE_SUCCESS) {
109 int destroyReturnCode = AieClientDestroy(clientInfo);
110 EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
111 }
112
113 // Step 5: Deletes callback.
114 AIE_DELETE(cb);
115 if (!isPrepareCbNull) {
116 AIE_DELETE(callback);
117 }
118 }
119
120 /**
121 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Sync_0100
122 * @tc.name : 01. algorithmInfo中isAsync=false,inputInfo不为空,cb为空,调用AieClientPrepare返回成功
123 * @tc.desc : [C- SOFTWARE -0200]
124 */
125 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0101, Function | MediumTest | Level2)
126 {
127 HILOGI("[Test]testAieClientPrepareFunction0101.");
128 TestAieClientPrepare(false, false, true, true, true);
129 }
130
131 /**
132 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Sync_0200
133 * @tc.name : 02. algorithmInfo中isAsync=false,inputInfo为空,cb为空,调用AieClientPrepare返回成功
134 * @tc.desc : [C- SOFTWARE -0200]
135 */
136 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0102, Function | MediumTest | Level3)
137 {
138 HILOGI("[Test]testAieClientPrepareFunction0102.");
139 TestAieClientPrepare(false, true, true, true, true);
140 }
141
142 /**
143 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Sync_0300
144 * @tc.name : 03. algorithmInfo中isAsync=false,inputInfo不为空,cb不为空,调用AieClientPrepare返回成功
145 * @tc.desc : [C- SOFTWARE -0200]
146 */
147 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0103, Function | MediumTest | Level3)
148 {
149 HILOGI("[Test]testAieClientPrepareFunction0103.");
150 TestAieClientPrepare(false, false, false, true, true);
151 }
152
153 /**
154 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Sync_0400
155 * @tc.name : 04. algorithmInfo中isAsync=false,inputInfo为空,cb不为空,调用AieClientPrepare返回成功
156 * @tc.desc : [C- SOFTWARE -0200]
157 */
158 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0104, Function | MediumTest | Level3)
159 {
160 HILOGI("[Test]testAieClientPrepareFunction0104.");
161 TestAieClientPrepare(false, true, false, true, true);
162 }
163
164 /**
165 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Async_0100
166 * @tc.name : 01. algorithmInfo中isAsync=true,inputInfo不为空,cb不为空,调用AieClientPrepare返回成功
167 * @tc.desc : [C- SOFTWARE -0200]
168 */
169 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0201, Function | MediumTest | Level3)
170 {
171 HILOGI("[Test]testAieClientPrepareFunction0201.");
172 TestAieClientPrepare(true, false, false, true, true);
173 }
174
175 /**
176 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Async_0200
177 * @tc.name : 02. algorithmInfo中isAsync=true,inputInfo为空,cb不为空,调用AieClientPrepare返回成功
178 * @tc.desc : [C- SOFTWARE -0200]
179 */
180 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0202, Function | MediumTest | Level3)
181 {
182 HILOGI("[Test]testAieClientPrepareFunction0202.");
183 TestAieClientPrepare(true, true, false, true, true);
184 }
185
186 /**
187 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Async_0300
188 * @tc.name : 03. algorithmInfo中isAsync=true,inputInfo不为空,cb为空,调用AieClientPrepare返回失败
189 * @tc.desc : [C- SOFTWARE -0200]
190 */
191 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0203, Function | MediumTest | Level3)
192 {
193 HILOGI("[Test]testAieClientPrepareFunction0203.");
194 TestAieClientPrepare(true, false, true, false, false);
195 }
196
197 /**
198 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientPrepare_Async_0400
199 * @tc.name : 04. algorithmInfo中isAsync=true,inputInfo为空,cb为空,调用AieClientPrepare返回失败
200 * @tc.desc : [C- SOFTWARE -0200]
201 */
202 HWTEST_F(AieClientPrepareFunctionTest, testAieClientPrepareFunction0204, Function | MediumTest | Level3)
203 {
204 HILOGI("[Test]testAieClientPrepareFunction0204.");
205 TestAieClientPrepare(true, true, true, false, false);
206 }