1 /*
2 * Copyright (c) 2025 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 <iostream>
17 #include <chrono>
18 #include <thread>
19 #include <vector>
20
21 #include <gtest/gtest.h>
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "listen_ability_client.h"
25 #include "my_rawdata.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Idl {
32
33 class IdlSaUnitTest : public testing::Test {
34 public:
IdlSaUnitTest()35 IdlSaUnitTest() {}
36
~IdlSaUnitTest()37 virtual ~IdlSaUnitTest() {}
38
39 static void SetUpTestCase();
40
41 static void TearDownTestCase();
42
43 void SetUp();
44
45 void TearDown();
46 };
47
SetUpTestCase()48 void IdlSaUnitTest::SetUpTestCase() {}
49
TearDownTestCase()50 void IdlSaUnitTest::TearDownTestCase() {}
51
SetUp()52 void IdlSaUnitTest::SetUp() {}
53
TearDown()54 void IdlSaUnitTest::TearDown() {}
55
56 /*
57 * @tc.name: IdlSaProxyTest001
58 * @tc.desc: sync func run
59 * @tc.type: FUNC
60 */
61 HWTEST_F(IdlSaUnitTest, IdlSaProxyTest001, TestSize.Level1)
62 {
63 std::cout << "IdlSaProxyTest001 start" << std::endl;
64
65 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
66 if (client_ == nullptr) {
67 std::cout << "client is nullptr" << std::endl;
68 return;
69 }
70
71 // sync func
72 double retDouble;
73 int32_t ret = client_->TestSaCallSa(100, retDouble);
74 std::cout << "TestSaCallSa client" << std::endl;
75 EXPECT_EQ(ret, ERR_OK);
76
77 int32_t times;
78 ret = client_->TestGetIpcSendRequestTimes(times);
79 std::cout << "TestGetIpcSendRequestTimes client" << std::endl;
80 EXPECT_EQ(ret, ERR_OK);
81 EXPECT_EQ(times, 1);
82
83 std::this_thread::sleep_for(std::chrono::seconds(2));
84 delete client_;
85 }
86
87 /*
88 * @tc.name: IdlSaProxyTest002
89 * @tc.desc: async func run
90 * @tc.type: FUNC
91 */
92 HWTEST_F(IdlSaUnitTest, IdlSaProxyTest002, TestSize.Level1)
93 {
94 std::cout << "IdlSaProxyTest002 start" << std::endl;
95
96 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
97 if (client_ == nullptr) {
98 std::cout << "client is nullptr" << std::endl;
99 return;
100 }
101
102 // async func
103 int32_t ret = client_->LoadSystemAbility(3);
104 client_->AddVolume(100);
105 EXPECT_EQ(ret, ERR_OK);
106
107 std::cout << "AddVolume client" << std::endl;
108 std::this_thread::sleep_for(std::chrono::seconds(2));
109 delete client_;
110 }
111
112 /*
113 * @tc.name: IdlSaRemoveDeathTest001
114 * @tc.desc: RemoveDeathRecipient test
115 * @tc.type: FUNC
116 */
117 HWTEST_F(IdlSaUnitTest, IdlSaRemoveDeathTest001, TestSize.Level1)
118 {
119 std::cout << "IdlSaRemoveDeathTest001 start" << std::endl;
120
121 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
122 if (client_ == nullptr) {
123 std::cout << "client is nullptr" << std::endl;
124 return;
125 }
126
__anon5ff104aa0102(bool status) 127 auto cb = [](bool status) {
128 std::cout << "LoadSystemAbility cb status: " << status << std::endl;
129 };
130
__anon5ff104aa0202() 131 auto cbDied = []() {
132 std::cout << "LoadSystemAbility cbDied." << std::endl;
133 };
134 client_->RegisterOnRemoteDiedCallback(cbDied);
135 int32_t ret = client_->LoadSystemAbility(cb);
136 EXPECT_EQ(ret, ERR_OK);
137
138 std::this_thread::sleep_for(std::chrono::minutes(2));
139 delete client_;
140 }
141
142 /*
143 * @tc.name: IdlSaLoadTest001
144 * @tc.desc: sync load sa
145 * @tc.type: FUNC
146 */
147 HWTEST_F(IdlSaUnitTest, IdlSaLoadTest001, TestSize.Level1)
148 {
149 std::cout << "IdlSaLoadTest001 start" << std::endl;
150
151 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
152 if (client_ == nullptr) {
153 std::cout << "client is nullptr" << std::endl;
154 return;
155 }
156
157 int32_t ret = client_->LoadSystemAbility(3);
158 EXPECT_EQ(ret, ERR_OK);
159
160 std::this_thread::sleep_for(std::chrono::seconds(2));
161 delete client_;
162 }
163
164 /*
165 * @tc.name: IdlSaLoadTest002
166 * @tc.desc: async load sa
167 * @tc.type: FUNC
168 */
169 HWTEST_F(IdlSaUnitTest, IdlSaLoadTest002, TestSize.Level1)
170 {
171 std::cout << "IdlSaLoadTest002 start" << std::endl;
172
173 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
174 if (client_ == nullptr) {
175 std::cout << "client is nullptr" << std::endl;
176 return;
177 }
178
__anon5ff104aa0302(bool status) 179 auto cb = [](bool status) {
180 std::cout << "LoadSystemAbility cb status: " << status << std::endl;
181 };
182
183 int32_t ret = client_->LoadSystemAbility(cb);
184 EXPECT_EQ(ret, ERR_OK);
185
186 std::this_thread::sleep_for(std::chrono::seconds(2));
187 delete client_;
188 }
189
190 /*
191 * @tc.name: IdlOverLoadTest001
192 * @tc.desc: async load sa
193 * @tc.type: FUNC
194 */
195 HWTEST_F(IdlSaUnitTest, IdlOverLoadTest001, TestSize.Level1)
196 {
197 std::cout << "IdlOverLoadTest001 start" << std::endl;
198
199 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
200 if (client_ == nullptr) {
201 std::cout << "client is nullptr" << std::endl;
202 return;
203 }
204
205 std::unordered_map<int32_t, int32_t> outApp;
206 int32_t outParam = 9;
207 outApp[4] = 11;
208 int32_t ret = client_->overloadfun(outParam);
209 #ifdef DEVICE
210 ret = client_->overloadfun(outApp);
211 #endif
212 std::cout << "TestOverLoad" << std::endl;
213 EXPECT_EQ(outParam, 10);
214 EXPECT_EQ(outApp[4], 7);
215 EXPECT_EQ(ret, ERR_OK);
216
217 std::this_thread::sleep_for(std::chrono::seconds(2));
218 delete client_;
219 }
220
221 /*
222 * @tc.name: IdlSaCustomTest001
223 * @tc.desc: async load sa
224 * @tc.type: FUNC
225 */
226 HWTEST_F(IdlSaUnitTest, IdlSaCustomTest001, TestSize.Level1)
227 {
228 std::cout << "IdlSaCustomTest001 start" << std::endl;
229
230 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
231 if (client_ == nullptr) {
232 std::cout << "client is nullptr" << std::endl;
233 return;
234 }
235
236 FooEnum in1 = FooEnum::ENUM_ONE, out1 = FooEnum::ENUM_ONE, inout1 = FooEnum::ENUM_ONE, result1 = FooEnum::ENUM_ONE;
237 int32_t ret = client_->enum_test_func(in1, out1, inout1, result1);
238 std::cout << "TestSaCallSa enum_test_func" << std::endl;
239 EXPECT_EQ(static_cast<int>(out1), 2);
240 EXPECT_EQ(ret, ERR_OK);
241
242 FooStruct in2 = { 1, "ExampleName", FooEnum::ENUM_ONE };
243 FooStruct inout2 = { 2, "AnotherName", FooEnum::ENUM_TWO };
244 FooStruct result2 = { 3, "ResultName", FooEnum::ENUM_NESTING};
245 RequestInfo out2 = {
246 {1, 2},
247 {{"key1", "value1"}, {"key2", "value2"}} // optionalData
248 };
249
250 ret = client_->struct_test_func(in2, out2, inout2, result2);
251 std::cout << "TestSaCallSa struct_test_func" << std::endl;
252 EXPECT_EQ(out2.initData[2], 3);
253 EXPECT_EQ(ret, ERR_OK);
254
255 FooUnion in3, out3, inout3, result3;
256 in3.enumType = FooEnum::ENUM_ONE;
257 ret = client_->union_test_func(in3, out3, inout3, result3);
258 std::cout << "TestSaCallSa union_test_func" << std::endl;
259 EXPECT_EQ(static_cast<int>(out3.enumType), 2);
260 EXPECT_EQ(ret, ERR_OK);
261
262 std::vector<std::string> quick;
263 bool isTrue = true;
264 ret = client_->ApplyQuickFix(quick, isTrue);
265 std::cout << "TestSaCallSa ApplyQuickFix" << std::endl;
266 EXPECT_EQ(ret, ERR_OK);
267
268 std::unordered_map<int32_t, FooStruct> inApp, outApp;
269 inApp[0].id = 11;
270 inApp[0].name = "shiyi";
271 ret = client_->GetAllAppSuspendState(inApp, outApp);
272 std::cout << "TestSaCallSa GetAllAppSuspendState" << std::endl;
273 EXPECT_EQ(outApp[1].id, 999);
274 EXPECT_EQ(outApp[1].name, "MapTest");
275 EXPECT_EQ(ret, ERR_OK);
276
277 std::this_thread::sleep_for(std::chrono::seconds(2));
278 delete client_;
279 }
280
281 /*
282 * @tc.name: IdlSaCustomTest001
283 * @tc.desc: async load sa
284 * @tc.type: FUNC
285 */
286 HWTEST_F(IdlSaUnitTest, IdlRawDataTest001, TestSize.Level1)
287 {
288 std::cout << "IdlRawDataTest001 start" << std::endl;
289
290 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
291 if (client_ == nullptr) {
292 std::cout << "client is nullptr" << std::endl;
293 return;
294 }
295
296 MyRawdata in, out, inout, result;
297 const char sampleData[] = "Hello!";
298 out.data = sampleData;
299 out.size = sizeof(sampleData);
300 in.data = sampleData;
301 in.size = sizeof(sampleData);
302 inout.data = sampleData;
303 inout.size = sizeof(sampleData);
304 int32_t ret = client_->rawdata_test_func(in, out, inout, result);
305 std::cout << "TestRawData" << std::endl;
306 EXPECT_STREQ(static_cast<const char*>(out.data), "Hello, world!");
307 EXPECT_EQ(out.size, 14);
308 EXPECT_STREQ(static_cast<const char*>(inout.data), "world!");
309 EXPECT_EQ(inout.size, 7);
310 EXPECT_EQ(ret, ERR_OK);
311
312 std::this_thread::sleep_for(std::chrono::seconds(2));
313 delete client_;
314 }
315
316 } // namespace idl
317 } // namespace OHOS