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 "utils/aie_client_callback.h"
23 #include "utils/aie_client_common.h"
24 #include "utils/aie_client_const.h"
25 #include "utils/aie_macros.h"
26 #include "utils/log/aie_log.h"
27 #include "utils/service_dead_cb.h"
28 #include "utils/utils.h"
29
30 using namespace ::testing;
31 using namespace testing::ext;
32 using namespace OHOS::AI;
33
34 namespace {
35 const int g_setGetFirst = 1;
36 const int g_setGetSecond = 2;
37 const int g_setGetThird = 3;
38 const int INT_LENGTH = 65535;
39 }
40
41 class AieClientSetOptionFunctionTest : public testing::Test {};
42
43 /**
44 * Tests TestAieClientSetOption.
45 * Invoke sequence: AieClientInit-AieClientPrepare-AieClientSetOption-AieClientGetOption.
46 *
47 * isAsync The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
48 * setAndGetTimes The attempts for invoking AieClientSetOption and AieClientGetOption.
49 */
TestAieClientSetOption(bool isAsync,int setAndGetTimes)50 static void TestAieClientSetOption(bool isAsync, int setAndGetTimes)
51 {
52 // Step 0: Defines variables.
53 ConfigInfo configInfo;
54 GetConfigInfo(configInfo);
55
56 ClientInfo clientInfo;
57 GetClientInfo(clientInfo);
58
59 AlgorithmInfo algorithmInfo;
60 int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
61 GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
62
63 ServiceDeadCb *cb = nullptr;
64 AIE_NEW(cb, ServiceDeadCb());
65
66 // Step 1: Invokes AieClientInit.
67 int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
68 EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
69 EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
70 EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
71 << ") is incorrect!";
72
73 // Step 2: Invokes AieClientPrepare.
74 bool isPrepareInputInfoNull = GetRandomBool();
75 DataInfo prepareInputInfo = GetDataInfo(isPrepareInputInfoNull, INPUT_INFO_PREPARE);
76 DataInfo prepareOutputInfo = GetDataInfo();
77
78 IClientCb *callback = nullptr;
79 if (isAsync) {
80 AIE_NEW(callback, ClientCallback());
81 }
82 int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, prepareInputInfo, prepareOutputInfo, callback);
83 EXPECT_EQ(RETCODE_SUCCESS, prepareReturnCode) << "AieClientPrepare is failed!";
84 EXPECT_EQ(true, prepareOutputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
85
86 // Step 3: Invokes AieClientSetOption and AieClientGetOption.
87 int optionType = GetRandomInt(INT_LENGTH);
88 for (int i = 0; i < setAndGetTimes; i++) {
89 // Step 3.1: Invokes AieClientSetOption.
90 std::string setOptionString = std::string(INPUT_INFO_SET_OPTION) + "_" + std::to_string(i + 1);
91 char *setOptionInputData = const_cast<char*>(setOptionString.c_str());
92 int setOptionLength = strlen(setOptionInputData) + 1;
93
94 DataInfo setOptionInputInfo = {
95 .data = (unsigned char*)setOptionInputData,
96 .length = setOptionLength,
97 };
98
99 int setOptionReturnCode = AieClientSetOption(clientInfo, optionType, setOptionInputInfo);
100 EXPECT_EQ(RETCODE_SUCCESS, setOptionReturnCode) << "AieClientSetOption is failed!";
101
102 // Step 3.2: Invokes AieClientGetOption.
103 bool isGetOptionInputInfoNull = GetRandomBool();
104 DataInfo getOptionInputInfo = GetDataInfo(isGetOptionInputInfoNull, INPUT_INFO_GET_OPTION);
105 DataInfo getOptionOutputInfo = GetDataInfo();
106
107 int getOptionReturnCode = AieClientGetOption(clientInfo, optionType, getOptionInputInfo, getOptionOutputInfo);
108 EXPECT_EQ(RETCODE_SUCCESS, getOptionReturnCode) << "AieClientSetOption is failed!";
109
110 std::string setOptionInputDataToString = "";
111 setOptionInputDataToString.append(reinterpret_cast<const char*>(setOptionInputInfo.data));
112 std::string getOptionOutputInfoToString = "";
113 getOptionOutputInfoToString.append(reinterpret_cast<const char*>(getOptionOutputInfo.data));
114 EXPECT_EQ(setOptionInputDataToString, getOptionOutputInfoToString) << "GetOption outputInfo is incorrect!";
115 }
116
117 // Step 4: Invokes AieClientRelease.
118 if (prepareReturnCode == RETCODE_SUCCESS) {
119 bool isReleaseInputInfoNull = GetRandomBool();
120 DataInfo releaseInputInfo = GetDataInfo(isReleaseInputInfoNull, INPUT_INFO_RELEASE);
121 int releaseReturnCode = AieClientRelease(clientInfo, algorithmInfo, releaseInputInfo);
122 EXPECT_EQ(RETCODE_SUCCESS, releaseReturnCode) << "AieClientRelease is failed!";
123 }
124
125 // Step5: Invoke AieClientDestroy().
126 if (initReturnCode == RETCODE_SUCCESS) {
127 int destroyReturnCode = AieClientDestroy(clientInfo);
128 EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
129 }
130 // Step 6: Deletes callback.
131 AIE_DELETE(cb);
132 AIE_DELETE(callback);
133 }
134
135 /**
136 * Tests TestAieClientSetOption.
137 * Invoke sequence: AieClientInit-AieClientSetOption-AieClientGetOption.
138 *
139 * isAsync The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
140 * isPerformSetOption Whether to invoke AieClientSetOption or not.
141 */
TestAieClientSetOptionWithoutPrepare(bool isAsync,bool isPerformSetOption)142 static void TestAieClientSetOptionWithoutPrepare(bool isAsync, bool isPerformSetOption)
143 {
144 // Step 0: Defines variables.
145 ConfigInfo configInfo;
146 GetConfigInfo(configInfo);
147
148 ClientInfo clientInfo;
149 GetClientInfo(clientInfo);
150
151 AlgorithmInfo algorithmInfo;
152 int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
153 GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
154
155 ServiceDeadCb *cb = nullptr;
156 AIE_NEW(cb, ServiceDeadCb());
157
158 // Step 1: Invokes AieClientInit.
159 int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
160 EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
161 EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
162 EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
163 << ") is incorrect!";
164
165 // Step 3: Invokes AieClientSetOption.
166 int optionType = 1;
167 if (isPerformSetOption) {
168 const char *setOptionString = INPUT_INFO_SET_OPTION;
169 char *setOptionInputData = const_cast<char*>(setOptionString);
170 int setOptionLength = strlen(setOptionString) + 1;
171 DataInfo inputInfo2 = {
172 .data = (unsigned char*)setOptionInputData,
173 .length = setOptionLength,
174 };
175 int setOptionReturnCode = AieClientSetOption(clientInfo, optionType, inputInfo2);
176 EXPECT_EQ(true, setOptionReturnCode != RETCODE_SUCCESS) << "AieClientSetOption is failed!";
177 }
178
179 // Step 4: Invokes AieClientGetOption.
180 bool isGetOptionInputInfoNull = GetRandomBool();
181 DataInfo getOptionInputInfo = GetDataInfo(isGetOptionInputInfoNull, INPUT_INFO_GET_OPTION);
182 DataInfo getOptionOutputInfo = GetDataInfo();
183
184 int getOptionReturnCode = AieClientGetOption(clientInfo, optionType, getOptionInputInfo, getOptionOutputInfo);
185 EXPECT_EQ(true, getOptionReturnCode != RETCODE_SUCCESS) << "AieClientSetOption is failed!";
186 EXPECT_EQ(true, getOptionOutputInfo.data == nullptr) << "GetOption outputInfo is incorrect!";
187
188 // Step5: Invoke AieClientDestroy().
189 if (initReturnCode == RETCODE_SUCCESS) {
190 int destroyReturnCode = AieClientDestroy(clientInfo);
191 EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
192 }
193 // Step 6: Deletes callback.
194 AIE_DELETE(cb);
195 }
196
197 /**
198 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSetOption_01_0100
199 * @tc.name : 01. 加载算法插件之后,执行1次设置参数值和获取参数值,成功
200 * @tc.desc : [C- SOFTWARE -0200]
201 */
202 HWTEST_F(AieClientSetOptionFunctionTest, testAieClientSetOptionFunction0101, Function | MediumTest | Level2)
203 {
204 HILOGI("[Test]testAieClientSetOptionFunction0101.");
205 bool isAsync = GetRandomBool();
206 TestAieClientSetOption(isAsync, g_setGetFirst);
207 }
208
209 /**
210 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSetOption_01_0200
211 * @tc.name : 02. 加载算法插件之后,执行2次设置参数值和获取参数值,成功
212 * @tc.desc : [C- SOFTWARE -0200]
213 */
214 HWTEST_F(AieClientSetOptionFunctionTest, testAieClientSetOptionFunction0102, Function | MediumTest | Level3)
215 {
216 HILOGI("[Test]testAieClientSetOptionFunction0102.");
217 bool isAsync = GetRandomBool();
218 TestAieClientSetOption(isAsync, g_setGetSecond);
219 }
220
221 /**
222 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSetOption_01_0300
223 * @tc.name : 03. 加载算法插件之后,执行3次设置参数值和获取参数值,成功
224 * @tc.desc : [C- SOFTWARE -0200]
225 */
226 HWTEST_F(AieClientSetOptionFunctionTest, testAieClientSetOptionFunction0103, Function | MediumTest | Level3)
227 {
228 HILOGI("[Test]testAieClientSetOptionFunction0103.");
229 bool isAsync = GetRandomBool();
230 TestAieClientSetOption(isAsync, g_setGetThird);
231 }
232
233 /**
234 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSetOption_02_0100
235 * @tc.name : 01. 不加载算法插件,设置参数值,获取参数值,失败
236 * @tc.desc : [C- SOFTWARE -0200]
237 */
238 HWTEST_F(AieClientSetOptionFunctionTest, testAieClientSetOptionFunction0201, Function | MediumTest | Level3)
239 {
240 HILOGI("[Test]testAieClientSetOptionFunction0201.");
241 bool isAsync = GetRandomBool();
242 TestAieClientSetOptionWithoutPrepare(isAsync, true);
243 }
244
245 /**
246 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSetOption_02_0200
247 * @tc.name : 02. 不加载算法插件,获取参数值,失败
248 * @tc.desc : [C- SOFTWARE -0200]
249 */
250 HWTEST_F(AieClientSetOptionFunctionTest, testAieClientSetOptionFunction0202, Function | MediumTest | Level3)
251 {
252 HILOGI("[Test]testAieClientSetOptionFunction0202.");
253 bool isAsync = GetRandomBool();
254 TestAieClientSetOptionWithoutPrepare(isAsync, false);
255 }