1 /*
2 * Copyright (c) 2022 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 "ashmem.h"
17 #include <gtest/gtest.h>
18 #include "nlohmann/json.hpp"
19 #include <sys/mman.h>
20
21 #include "account_log_wrapper.h"
22 #define private public
23 #include "bundle_manager_adapter.h"
24 #include "bundle_manager_adapter_proxy.h"
25 #undef private
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "system_ability.h"
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::AccountSA;
33 using Json = nlohmann::json;
34
35 class BundleManagerModuleTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp(void) override;
40 void TearDown(void) override;
41
42 std::shared_ptr<BundleManagerAdapterProxy> g_bundleManagerAdapterProxyRemoteNull =
43 std::make_shared<BundleManagerAdapterProxy>(nullptr);
44 };
45
46 namespace {
47 const std::string EMPTY_BUNDLE_NAME = "";
48 const std::string INVALID_BUNDLE_NAME = "testbundlename";
49 const std::string BUNDLE_NAME = "com.ohos.launcher";
50 const int32_t FLAGS = 1;
51 const int32_t USER_ID = 1;
52 } // namespace
53
SetUpTestCase(void)54 void BundleManagerModuleTest::SetUpTestCase(void)
55 {}
56
TearDownTestCase(void)57 void BundleManagerModuleTest::TearDownTestCase(void)
58 {}
59
SetUp(void)60 void BundleManagerModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
61 {
62 testing::UnitTest *test = testing::UnitTest::GetInstance();
63 ASSERT_NE(test, nullptr);
64 const testing::TestInfo *testinfo = test->current_test_info();
65 ASSERT_NE(testinfo, nullptr);
66 string testCaseName = string(testinfo->name());
67 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
68 }
69
TearDown(void)70 void BundleManagerModuleTest::TearDown(void)
71 {}
72
73 /**
74 * @tc.name: BundleManagerProxy_GetBundleInfo_0100
75 * @tc.desc: test func failed with remote is null
76 * @tc.type: FUNC
77 * @tc.require:
78 */
79 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_GetBundleInfo_0100, TestSize.Level1)
80 {
81 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
82 BundleInfo bundleInfo;
83 bool result = g_bundleManagerAdapterProxyRemoteNull->GetBundleInfo(
84 INVALID_BUNDLE_NAME, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, USER_ID);
85 ASSERT_EQ(result, false);
86
87 Want want;
88 std::vector<AbilityInfo> abilityInfos;
89 result = g_bundleManagerAdapterProxyRemoteNull->QueryAbilityInfos(want, FLAGS, USER_ID, abilityInfos);
90 ASSERT_EQ(result, false);
91
92 std::vector<ExtensionAbilityInfo> extensionInfos;
93 result = g_bundleManagerAdapterProxyRemoteNull->QueryExtensionAbilityInfos(want, FLAGS, USER_ID, extensionInfos);
94 ASSERT_EQ(result, false);
95 }
96
97 /**
98 * @tc.name: BundleManagerProxy_GetBundleInfo_0200
99 * @tc.desc: test GetBundleInfo param is invalid
100 * @tc.type: FUNC
101 * @tc.require:
102 */
103 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_GetBundleInfo_0200, TestSize.Level1)
104 {
105 sptr<ISystemAbilityManager> systemAbilityManager =
106 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
107 ASSERT_NE(systemAbilityManager, nullptr);
108 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
109 ASSERT_NE(remoteObj, nullptr);
110 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
111 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
112
113 BundleInfo bundleInfo;
114 bool result = bundleManagerAdapterProxy->GetBundleInfo(
115 INVALID_BUNDLE_NAME, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, USER_ID);
116 ASSERT_EQ(result, false);
117 }
118
119 /**
120 * @tc.name: BundleManagerProxy_GetBundleInfo_0300
121 * @tc.desc: test GetBundleInfo param is invalid
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_GetBundleInfo_0300, TestSize.Level1)
126 {
127 sptr<ISystemAbilityManager> systemAbilityManager =
128 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
129 ASSERT_NE(systemAbilityManager, nullptr);
130 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
131 ASSERT_NE(remoteObj, nullptr);
132 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
133 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
134
135 BundleInfo bundleInfo;
136 bool result = bundleManagerAdapterProxy->GetBundleInfo(
137 EMPTY_BUNDLE_NAME, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, USER_ID);
138 ASSERT_EQ(result, false);
139 }
140
141 /**
142 * @tc.name: BundleManagerProxy_GetUidByBundleName_0100
143 * @tc.desc: test GetUidByBundleName failed with bundlename is empty
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_GetUidByBundleName_0100, TestSize.Level1)
148 {
149 sptr<ISystemAbilityManager> systemAbilityManager =
150 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151 ASSERT_NE(systemAbilityManager, nullptr);
152 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
153 ASSERT_NE(remoteObj, nullptr);
154 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
155 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
156
157 int32_t result = bundleManagerAdapterProxy->GetUidByBundleName(EMPTY_BUNDLE_NAME, USER_ID);
158 ASSERT_EQ(result, AppExecFwk::Constants::INVALID_UID);
159 }
160
161 /**
162 * @tc.name: BundleManagerProxy_QueryAbilityInfos_0200
163 * @tc.desc: test QueryAbilityInfos failed with param is invalid
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_QueryAbilityInfos_0200, TestSize.Level1)
168 {
169 sptr<ISystemAbilityManager> systemAbilityManager =
170 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
171 ASSERT_NE(systemAbilityManager, nullptr);
172 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
173 ASSERT_NE(remoteObj, nullptr);
174 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
175 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
176
177 Want want;
178 std::vector<AbilityInfo> abilityInfos;
179 bool result = bundleManagerAdapterProxy->QueryAbilityInfos(want, FLAGS, USER_ID, abilityInfos);
180 ASSERT_EQ(result, false);
181 }
182
183 /**
184 * @tc.name: BundleManagerProxy_QueryExtensionAbilityInfos_0200
185 * @tc.desc: test QueryExtensionAbilityInfos failed with param is invalid
186 * @tc.type: FUNC
187 * @tc.require:
188 */
189 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_QueryExtensionAbilityInfos_0200, TestSize.Level1)
190 {
191 sptr<ISystemAbilityManager> systemAbilityManager =
192 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
193 ASSERT_NE(systemAbilityManager, nullptr);
194 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
195 ASSERT_NE(remoteObj, nullptr);
196 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
197 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
198
199 Want want;
200 std::vector<ExtensionAbilityInfo> extensionInfos;
201 bool result = bundleManagerAdapterProxy->QueryExtensionAbilityInfos(want, FLAGS, USER_ID, extensionInfos);
202 ASSERT_EQ(result, false);
203 }
204
205 /**
206 * @tc.name: BundleManagerAdapter_GetBundleInfo_0100
207 * @tc.desc: test GetBundleInfo failed with param is invalid
208 * @tc.type: FUNC
209 * @tc.require:
210 */
211 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_GetBundleInfo_0100, TestSize.Level1)
212 {
213 BundleInfo bundleInfo;
214 bool result = BundleManagerAdapter::GetInstance()->GetBundleInfo(
215 BUNDLE_NAME, BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, USER_ID);
216 ASSERT_EQ(result, false);
217 }
218
219 /**
220 * @tc.name: BundleManagerAdapter_QueryAbilityInfos_0100
221 * @tc.desc: test QueryAbilityInfos failed with param is invalid
222 * @tc.type: FUNC
223 * @tc.require:
224 */
225 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_QueryAbilityInfos_0100, TestSize.Level1)
226 {
227 Want want;
228 std::vector<AbilityInfo> abilityInfos;
229 bool result = BundleManagerAdapter::GetInstance()->QueryAbilityInfos(want, FLAGS, USER_ID, abilityInfos);
230 ASSERT_EQ(result, false);
231 }
232
233 /**
234 * @tc.name: BundleManagerAdapter_ResetProxy_0100
235 * @tc.desc: test ResetProxy branch of remove proxy.
236 * @tc.type: FUNC
237 * @tc.require:
238 */
239 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ResetProxy_0100, TestSize.Level1)
240 {
241 auto bundleManagerAdapterSptr = BundleManagerAdapter::GetInstance();
242 ErrCode result = bundleManagerAdapterSptr->Connect();
243 ASSERT_EQ(result, ERR_OK);
244 ASSERT_NE(bundleManagerAdapterSptr->proxy_, nullptr);
245 auto sptr = bundleManagerAdapterSptr->proxy_->AsObject();
246 ASSERT_NE(nullptr, sptr);
247 bundleManagerAdapterSptr->ResetProxy(sptr);
248 ASSERT_EQ(bundleManagerAdapterSptr->proxy_, nullptr);
249 }
250
251 /**
252 * @tc.name: BundleManagerProxy_SendTransactCmd_0100
253 * @tc.desc: test SendTransactCmd failed with param is invalid
254 * @tc.type: FUNC
255 * @tc.require:
256 */
257 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_SendTransactCmd_0100, TestSize.Level1)
258 {
259 sptr<ISystemAbilityManager> systemAbilityManager =
260 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
261 ASSERT_NE(systemAbilityManager, nullptr);
262 sptr<IRemoteObject> remoteObj = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
263 ASSERT_NE(remoteObj, nullptr);
264 auto bundleManagerAdapterProxy = std::make_shared<BundleManagerAdapterProxy>(remoteObj);
265 ASSERT_NE(bundleManagerAdapterProxy, nullptr);
266
267 MessageParcel reply;
268 MessageParcel data;
269 EXPECT_EQ(bundleManagerAdapterProxy->SendTransactCmd(
270 BundleMgrInterfaceCode::QUERY_ABILITY_INFOS_MUTI_PARAM, data, reply), false);
271 }
272
273 /**
274 * @tc.name: BundleManagerProxy_SendData_0100
275 * @tc.desc: test func SendData
276 * @tc.type: FUNC
277 * @tc.require:
278 */
279 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_SendData_0100, TestSize.Level1)
280 {
281 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
282 void *buffer = nullptr;
283
284 bool result = g_bundleManagerAdapterProxyRemoteNull->SendData(buffer, 10, nullptr);
285 EXPECT_EQ(result, false);
286
287 result = g_bundleManagerAdapterProxyRemoteNull->SendData(buffer, 0, "test_data");
288 EXPECT_EQ(result, false);
289
290 // max value malloc failed
291 result = g_bundleManagerAdapterProxyRemoteNull->SendData(buffer, -1, "test_data");
292 EXPECT_EQ(result, false);
293
294 result = g_bundleManagerAdapterProxyRemoteNull->SendData(buffer, 10, "test_data");
295 EXPECT_EQ(result, true);
296 }
297
298 /**
299 * @tc.name: BundleManagerProxy_GetVectorFromParcelIntelligent_0100
300 * @tc.desc: test GetVectorFromParcelIntelligent failed with param is invalid
301 * @tc.type: FUNC
302 * @tc.require:
303 */
304 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_GetVectorFromParcelIntelligent_0100, TestSize.Level1)
305 {
306 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
307 MessageParcel data;
308 std::vector<AbilityInfo> abilityInfos;
309
310 bool result =g_bundleManagerAdapterProxyRemoteNull->GetVectorFromParcelIntelligent<AbilityInfo>(
311 BundleMgrInterfaceCode::QUERY_ABILITY_INFOS_MUTI_PARAM, data, abilityInfos);
312 EXPECT_EQ(result, false);
313 }
314
315 /**
316 * @tc.name: BundleManagerProxy_InnerGetVectorFromParcelIntelligent_0100
317 * @tc.desc: test func failed with param is invalid
318 * @tc.type: FUNC
319 * @tc.require:
320 */
321 HWTEST_F(BundleManagerModuleTest, BundleManagerProxy_InnerGetVectorFromParcelIntelligent_0100, TestSize.Level1)
322 {
323 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
324 MessageParcel reply;
325 std::vector<AbilityInfo> abilityInfos;
326
327 bool result = g_bundleManagerAdapterProxyRemoteNull->InnerGetVectorFromParcelIntelligent<AbilityInfo>(
328 reply, abilityInfos);
329 EXPECT_EQ(result, false);
330 }
331
332 /**
333 * @tc.name: BundleManagerAdapter_ParseStr_0100
334 * @tc.desc: test ParseStr
335 * @tc.type: FUNC
336 * @tc.require:
337 */
338 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ParseStr_0100, TestSize.Level1)
339 {
340 // test buf is nullptr.
341 int itemLen = 10;
342 int index = 0;
343 std::string result;
344 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseStr(nullptr, itemLen, index, result), false);
345
346 // test itemLen is invalid.
347 const char *buf = "test";
348 itemLen = -1;
349 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseStr(buf, itemLen, index, result), false);
350
351 // test index is invalid.
352 itemLen = 10;
353 index = -1;
354 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseStr(buf, itemLen, index, result), false);
355
356 // test normal case.
357 itemLen = 4;
358 index = 0;
359 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseStr(buf, itemLen, index, result), true);
360 EXPECT_EQ(result, "test");
361 }
362
363 /**
364 * @tc.name: BundleManagerAdapter_ParseExtensionAbilityInfos_0100
365 * @tc.desc: test ParseExtensionAbilityInfos
366 * @tc.type: FUNC
367 * @tc.require:
368 */
369 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ParseExtensionAbilityInfos_0100, TestSize.Level1)
370 {
371 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
372
373 std::vector<ExtensionAbilityInfo> extensionInfos;
374 // test info with normal data.
375 Json testBundleInfo = Json {
376 {"name", "test_name"},
377 {"label", "test_label"},
378 {"description", "test_description"},
379 {"type", 0},
380 {"visible", true},
381 {"uid", 123},
382 };
383 Json arrays[] = {
384 testBundleInfo,
385 };
386 Json testBundleInfo1 = Json {
387 {"extensionAbilityInfo", arrays},
388 };
389 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseExtensionAbilityInfos(testBundleInfo1, extensionInfos), true);
390 }
391
392 /**
393 * @tc.name: BundleManagerAdapter_ParseExtensionAbilityInfos_0200
394 * @tc.desc: test invalid parameter
395 * @tc.type: FUNC
396 * @tc.require:
397 */
398 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ParseExtensionAbilityInfos_0200, TestSize.Level1)
399 {
400 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
401
402 std::vector<ExtensionAbilityInfo> extensionInfos;
403 // invalid value
404 Json testBundleInfo = Json {
405 {"name", 1},
406 {"label", 1},
407 {"description", 1},
408 {"type", "testtest"},
409 {"visible", "test"},
410 {"uid", "123"},
411 };
412 Json arrays1[] = {
413 testBundleInfo,
414 };
415 // invalid JSON
416 Json arrays2[] = {
417 "invalidjsonobject",
418 };
419 Json testBundleInfo1 = Json {
420 {"extensionAbilityInfo", arrays1},
421 };
422 Json testBundleInfo2 = Json {
423 {"extensionAbilityInfo", arrays2},
424 };
425 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseExtensionAbilityInfos(testBundleInfo1, extensionInfos), true);
426 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseExtensionAbilityInfos(testBundleInfo2, extensionInfos), true);
427 }
428
429 /**
430 * @tc.name: BundleManagerAdapter_ParseExtensionInfo_0100
431 * @tc.desc: an invalid json string
432 * @tc.type: FUNC
433 * @tc.require:
434 */
435 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ParseExtensionInfo_0100, TestSize.Level1)
436 {
437 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
438 ExtensionAbilityInfo extensionInfo;
439
440 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseExtensionInfo("invalidjsonobject", extensionInfo), false);
441 }
442
443 /**
444 * @tc.name: BundleManagerAdapter_ParseExtensionInfo_0200
445 * @tc.desc: an invalid json string
446 * @tc.type: FUNC
447 * @tc.require:
448 */
449 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_ParseExtensionInfo_0200, TestSize.Level1)
450 {
451 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
452 ExtensionAbilityInfo extensionInfo;
453
454 Json testBundleInfo = Json {
455 {"invalid_name", 1},
456 {"invalid_label", 1},
457 {"invalid_description", 1},
458 {"invalid_type", "testtest"},
459 {"invalid_visible", "test"},
460 {"invalid_uid", "123"},
461 };
462 std::string testStr = testBundleInfo.dump();
463 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->ParseExtensionInfo(testStr, extensionInfo), true);
464 }
465
466 /**
467 * @tc.name: BundleManagerAdapter_QueryExtensionAbilityInfos_0100
468 * @tc.desc: QueryExtensionAbilityInfos
469 * @tc.type: FUNC
470 * @tc.require:
471 */
472 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_QueryExtensionAbilityInfos_0100, TestSize.Level1)
473 {
474 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
475 Want want;
476 int32_t flag = 1;
477 std::vector<ExtensionAbilityInfo> extensionInfos;
478 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->QueryExtensionAbilityInfos(
479 want, flag, USER_ID, extensionInfos), false);
480 }
481
482 /**
483 * @tc.name: BundleManagerAdapter_QueryExtensionAbilityInfos_0200
484 * @tc.desc: QueryExtensionAbilityInfos
485 * @tc.type: FUNC
486 * @tc.require:
487 */
488 HWTEST_F(BundleManagerModuleTest, BundleManagerAdapter_QueryExtensionAbilityInfos_0200, TestSize.Level1)
489 {
490 ASSERT_NE(g_bundleManagerAdapterProxyRemoteNull, nullptr);
491 Want want;
492 int32_t flag = 1;
493 std::vector<ExtensionAbilityInfo> extensionInfos;
494 EXPECT_EQ(g_bundleManagerAdapterProxyRemoteNull->QueryExtensionAbilityInfos(
495 want, ExtensionAbilityType::BACKUP, flag, USER_ID, extensionInfos), false);
496 }
497