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 "communication_adapter/include/sa_async_handler.h"
23 #include "communication_adapter/include/sa_client.h"
24 #include "platform/time/include/time.h"
25 #include "protocol/struct_definition/aie_info_define.h"
26 #include "server_executor/include/server_executor.h"
27 #include "service_dead_cb.h"
28 #include "utils/aie_macros.h"
29 #include "utils/log/aie_log.h"
30
31 using namespace OHOS::AI;
32 using namespace testing::ext;
33
34 namespace {
35 const char * const INPUT_DATA = "inputData";
36 const char * const EXTEND_MSG = "extendMsg";
37 const char * const CONFIG_DESCRIPTION = "config information";
38
39 const int REQUEST_ID = 1;
40 const int OPERATE_ID = 2;
41 const long long CLIENT_INFO_VERSION = 1;
42 const int ALGORITHM_ASYNC_TYPE = 1;
43 const long long ALGORITHM_VERSION = 1;
44 const int WAIT_CALLBACK_TIME_MS = 2000;
45 }
46
47 class AsyncProcessFunctionTest : public testing::Test {
48 public:
49 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()50 static void SetUpTestCase() {};
51
52 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()53 static void TearDownTestCase() {};
54
55 // SetUp:Execute before each test case
SetUp()56 void SetUp() {};
57
58 // TearDown:Execute after each test case
TearDown()59 void TearDown() {};
60 };
61
62 class ClientCallback : public IClientCb {
63 public:
64 ClientCallback() = default;
65 ~ClientCallback() override = default;
OnResult(const DataInfo & result,int resultCode,int requestId)66 void OnResult(const DataInfo &result, int resultCode, int requestId) override
67 {
68 HILOGI("[Test]TestAieClientAsyncProcess execute ClientCallbackOnResult."\
69 " resultCode[%d], requestId[%d], resultData[%s], resultLength[%d].",
70 resultCode, requestId, result.data, result.length);
71 }
72 };
73
PreBuildInfo(ConfigInfo & configInfo,ClientInfo & clientInfo,AlgorithmInfo & algoInfo,bool isAsync,int requestId)74 static void PreBuildInfo(ConfigInfo &configInfo, ClientInfo &clientInfo, AlgorithmInfo &algoInfo,
75 bool isAsync, int requestId)
76 {
77 const char *str = EXTEND_MSG;
78 char *extendMsg = const_cast<char*>(str);
79 int len = strlen(str) + 1;
80
81 configInfo.description = CONFIG_DESCRIPTION;
82
83 clientInfo = {
84 .clientVersion = CLIENT_INFO_VERSION,
85 .clientId = INVALID_CLIENT_ID,
86 .sessionId = INVALID_SESSION_ID,
87 .serverUid = INVALID_UID,
88 .clientUid = INVALID_UID,
89 .extendLen = len,
90 .extendMsg = (unsigned char*)extendMsg,
91 };
92
93 algoInfo = {
94 .clientVersion = CLIENT_INFO_VERSION,
95 .isAsync = isAsync,
96 .algorithmType = ALGORITHM_ASYNC_TYPE,
97 .algorithmVersion = ALGORITHM_VERSION,
98 .isCloud = true,
99 .operateId = OPERATE_ID,
100 .requestId = requestId,
101 .extendLen = len,
102 .extendMsg = (unsigned char*)extendMsg,
103 };
104 }
105
106 /**
107 * @tc.name: TestAieClientAsyncProcess001
108 * @tc.desc: Test asynchronous execution of certain algorithm plugin.
109 * @tc.type: FUNC
110 * @tc.require: AR000F77NK
111 */
112 HWTEST_F(AsyncProcessFunctionTest, TestAieClientAsyncProcess001, TestSize.Level0)
113 {
114 HILOGI("[Test]TestAieClientAsyncProcess001 starts.");
115
116 ConfigInfo configInfo;
117 ClientInfo clientInfo;
118 AlgorithmInfo algoInfo;
119 PreBuildInfo(configInfo, clientInfo, algoInfo, true, REQUEST_ID);
120
121 ServiceDeadCb cb = ServiceDeadCb();
122 int returnInitCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
123 ASSERT_EQ(returnInitCode, RETCODE_SUCCESS);
124 ASSERT_TRUE(clientInfo.clientId > 0);
125
126 DataInfo inputInfo;
127 const char *str = INPUT_DATA;
128 char *inputData = const_cast<char*>(str);
129 int len = strlen(str) + 1;
130 inputInfo.data = (unsigned char *)inputData;
131 inputInfo.length = len;
132
133 ClientCallback callback = ClientCallback();
134 DataInfo outputInfo;
135 int returnPrepareCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
136 ASSERT_EQ(returnPrepareCode, RETCODE_SUCCESS);
137 ASSERT_NE(outputInfo.data, nullptr);
138 ASSERT_TRUE(outputInfo.length > 0);
139
140 int returnProcessCode = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
141 ASSERT_EQ(returnProcessCode, RETCODE_SUCCESS);
142
143 StepSleepMs(WAIT_CALLBACK_TIME_MS);
144
145 int returnReleaseCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
146 ASSERT_EQ(returnReleaseCode, RETCODE_SUCCESS);
147
148 int returnDestroyCode = AieClientDestroy(clientInfo);
149 ASSERT_EQ(returnDestroyCode, RETCODE_SUCCESS);
150 }
151
152 /**
153 * @tc.name: TestAieClientAsyncProcess002
154 * @tc.desc: Test asynchronous execution of certain algorithm plugin with 'inputInfo.data' being 'nullptr'.
155 * @tc.type: FUNC
156 * @tc.require: AR000F77NK
157 */
158 HWTEST_F(AsyncProcessFunctionTest, TestAieClientAsyncProcess002, TestSize.Level0)
159 {
160 HILOGI("[Test]TestAieClientAsyncProcess002 starts.");
161
162 ConfigInfo configInfo;
163 ClientInfo clientInfo;
164 AlgorithmInfo algoInfo;
165 PreBuildInfo(configInfo, clientInfo, algoInfo, true, REQUEST_ID);
166
167 ServiceDeadCb cb = ServiceDeadCb();
168 int returnInitCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
169 ASSERT_EQ(returnInitCode, RETCODE_SUCCESS);
170 ASSERT_TRUE(clientInfo.clientId > 0);
171
172 DataInfo inputInfo;
173 const char *str = INPUT_DATA;
174 char *inputData = const_cast<char*>(str);
175 int len = strlen(str) + 1;
176 inputInfo.data = (unsigned char *)inputData;
177 inputInfo.length = len;
178
179 ClientCallback callback = ClientCallback();
180 DataInfo outputInfo;
181 int returnPrepareCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
182 ASSERT_EQ(returnPrepareCode, RETCODE_SUCCESS);
183
184 inputInfo.data = nullptr;
185 inputInfo.length = 0;
186
187 int returnProcessCode = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
188 ASSERT_EQ(returnProcessCode, RETCODE_SUCCESS);
189
190 StepSleepMs(WAIT_CALLBACK_TIME_MS);
191 int returnReleaseCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
192 ASSERT_EQ(returnReleaseCode, RETCODE_SUCCESS);
193
194 int returnDestroyCode = AieClientDestroy(clientInfo);
195 ASSERT_EQ(returnDestroyCode, RETCODE_SUCCESS);
196 }
197
198 /**
199 * @tc.name: TestAieClientAsyncProcess003
200 * @tc.desc: Test asynchronous execution of certain algorithm plugin with 'isAsync' being false.
201 * @tc.type: FUNC
202 * @tc.require: AR000F77NK
203 */
204 HWTEST_F(AsyncProcessFunctionTest, TestAieClientAsyncProcess003, TestSize.Level0)
205 {
206 HILOGI("[Test]TestAieClientAsyncProcess003 starts.");
207
208 ConfigInfo configInfo;
209 ClientInfo clientInfo;
210 AlgorithmInfo algoInfo;
211 PreBuildInfo(configInfo, clientInfo, algoInfo, false, REQUEST_ID);
212
213 ServiceDeadCb cb = ServiceDeadCb();
214 int returnInitCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
215 ASSERT_EQ(returnInitCode, RETCODE_SUCCESS);
216 ASSERT_TRUE(clientInfo.clientId > 0);
217
218 DataInfo inputInfo;
219 const char *str = INPUT_DATA;
220 char *inputData = const_cast<char*>(str);
221 int len = strlen(str) + 1;
222 inputInfo.data = (unsigned char *)inputData;
223 inputInfo.length = len;
224
225 ClientCallback callback = ClientCallback();
226 DataInfo outputInfo;
227 int returnPrepareCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
228 ASSERT_EQ(returnPrepareCode, RETCODE_SUCCESS);
229 ASSERT_NE(outputInfo.data, nullptr);
230 ASSERT_TRUE(outputInfo.length > 0);
231
232 int returnProcessCode = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
233 ASSERT_NE(returnProcessCode, RETCODE_SUCCESS);
234
235 StepSleepMs(WAIT_CALLBACK_TIME_MS);
236
237 int returnReleaseCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
238 ASSERT_EQ(returnReleaseCode, RETCODE_SUCCESS);
239
240 int returnDestroyCode = AieClientDestroy(clientInfo);
241 ASSERT_EQ(returnDestroyCode, RETCODE_SUCCESS);
242 }
243