• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 <gtest/gtest.h>
17 
18 #include "accesstoken_kit.h"
19 #include "avsession_errors.h"
20 #include "avsession_log.h"
21 #include "bundle_mgr_proxy.h"
22 #define private public
23 #define protected public
24 #include "bundle_status_adapter.h"
25 #undef protected
26 #undef private
27 #include "iservice_registry.h"
28 #include "insight_intent_execute_param.h"
29 #include "nativetoken_kit.h"
30 #include "system_ability_definition.h"
31 #include "token_setproc.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::Security::AccessToken;
35 
36 namespace OHOS {
37 namespace AVSession {
38 static uint64_t g_selfTokenId = 0;
39 
40 static HapInfoParams g_info = {
41     .userID = 100,
42     .bundleName = "ohos.permission_test.demo",
43     .instIndex = 0,
44     .appIDDesc = "ohos.permission_test.demo",
45     .isSystemApp = true
46 };
47 
48 static HapPolicyParams g_policy = {
49     .apl = APL_NORMAL,
50     .domain = "test.domain",
51     .permList = {
52         {
53             .permissionName = "ohos.permission.LISTEN_BUNDLE_CHANGE",
54             .bundleName = "ohos.permission_test.demo",
55             .grantMode = 1,
56             .availableLevel = APL_NORMAL,
57             .label = "label",
58             .labelId = 1,
59             .description = "test",
60             .descriptionId = 1
61         }
62     },
63     .permStateList = {
64         {
65             .permissionName = "ohos.permission.LISTEN_BUNDLE_CHANGE",
66             .isGeneral = true,
67             .resDeviceID = { "local" },
68             .grantStatus = { PermissionState::PERMISSION_GRANTED },
69             .grantFlags = { 1 }
70         }
71     }
72 };
73 
74 class BundleStatusAdapterTest : public testing::Test {
75 public:
76     static void SetUpTestCase(void);
77     static void TearDownTestCase(void);
78     void SetUp();
79     void TearDown();
80 };
81 
SetUpTestCase()82 void BundleStatusAdapterTest::SetUpTestCase()
83 {
84     g_selfTokenId = GetSelfTokenID();
85     AccessTokenKit::AllocHapToken(g_info, g_policy);
86     AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(g_info.userID, g_info.bundleName, g_info.instIndex);
87     SetSelfTokenID(tokenID.tokenIDEx);
88 }
89 
TearDownTestCase()90 void BundleStatusAdapterTest::TearDownTestCase()
91 {
92     SetSelfTokenID(g_selfTokenId);
93     auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex);
94     AccessTokenKit::DeleteToken(tokenId);
95 }
96 
SetUp()97 void BundleStatusAdapterTest::SetUp()
98 {
99 }
100 
TearDown()101 void BundleStatusAdapterTest::TearDown()
102 {
103 }
104 
105 /**
106  * @tc.name: SubscribeBundleStatusEvent001
107  * @tc.desc: Test SubscribeBundleStatusEvent
108  * @tc.type: FUNC
109  */
110 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent001, testing::ext::TestSize.Level1)
111 {
112     SLOGI("SubscribeBundleStatusEvent001, start");
113     std::string bundleName = "test1";
__anon12a1581c0102(const std::string& capturedBundleName, int32_t userId) 114     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
115         SLOGI("SubscribeBundleStatusEvent001: get bundle name: %{public}s, userId: %{public}d",
116             capturedBundleName.c_str(), userId);
117     };
118     bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
119     EXPECT_EQ(ret, true);
120     SLOGI("SubscribeBundleStatusEvent001, end");
121 }
122 
123 /**
124  * @tc.name: SubscribeBundleStatusEvent002
125  * @tc.desc: Test SubscribeBundleStatusEvent
126  * @tc.type: FUNC
127  */
128 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent002, testing::ext::TestSize.Level0)
129 {
130     SLOGI("SubscribeBundleStatusEvent002, start");
131     BundleStatusAdapter::GetInstance().Init();
132     std::string bundleName = "";
__anon12a1581c0202(const std::string& capturedBundleName, int32_t userId) 133     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
134         SLOGI("SubscribeBundleStatusEvent002: get bundle name: %{public}s, userId: %{public}d",
135             capturedBundleName.c_str(), userId);
136     };
137     bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
138     EXPECT_EQ(ret, false);
139     SLOGI("SubscribeBundleStatusEvent002, end");
140 }
141 
142 /**
143  * @tc.name: SubscribeBundleStatusEvent003
144  * @tc.desc: Test SubscribeBundleStatusEvent
145  * @tc.type: FUNC
146  */
147 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent003, testing::ext::TestSize.Level1)
148 {
149     SLOGI("SubscribeBundleStatusEvent003, start");
150     BundleStatusAdapter::GetInstance().Init();
151     std::string bundleName = "com.ohos.sceneboard";
__anon12a1581c0302(const std::string& capturedBundleName, int32_t userId) 152     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
153         SLOGI("SubscribeBundleStatusEvent003: get bundle name: %{public}s, userId: %{public}d",
154             capturedBundleName.c_str(), userId);
155     };
156     bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
157     EXPECT_EQ(ret, true);
158     SLOGI("SubscribeBundleStatusEvent003, end");
159 }
160 
161 /**
162  * @tc.name: GetBundleNameFromUid001
163  * @tc.desc: Test GetBundleNameFromUid
164  * @tc.type: FUNC
165  */
166 static HWTEST_F(BundleStatusAdapterTest, GetBundleNameFromUid001, testing::ext::TestSize.Level0)
167 {
168     SLOGI("GetBundleNameFromUid001, start");
169     BundleStatusAdapter::GetInstance().Init();
170     const int32_t uid = 0;
171     auto bundleName = BundleStatusAdapter::GetInstance().GetBundleNameFromUid(uid);
172     EXPECT_EQ(bundleName, "");
173     SLOGI("GetBundleNameFromUid001, end");
174 }
175 
176 /**
177  * @tc.name: GetUidFromBundleName001
178  * @tc.desc: Test GetUidFromBundleName
179  * @tc.type: FUNC
180  */
181 static HWTEST_F(BundleStatusAdapterTest, GetUidFromBundleName001, testing::ext::TestSize.Level1)
182 {
183     SLOGI("GetUidFromBundleName001, start");
184     BundleStatusAdapter::GetInstance().Init();
185     const std::string bundleName = "TestBundleName";
186     auto uid = BundleStatusAdapter::GetInstance().GetUidFromBundleName(bundleName, 0);
187     EXPECT_EQ(uid, -1);
188     SLOGI("GetUidFromBundleName001, end");
189 }
190 
191 /**
192  * @tc.name: CheckBundleSupport001
193  * @tc.desc: Test CheckBundleSupport
194  * @tc.type: FUNC
195  */
196 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport001, testing::ext::TestSize.Level0)
197 {
198     SLOGI("CheckBundleSupport001, start");
199     BundleStatusAdapter::GetInstance().Init();
200     std::string profile = "";
201     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
202     EXPECT_EQ(ret, false);
203     SLOGI("CheckBundleSupport001, end");
204 }
205 
206 /**
207  * @tc.name: CheckBundleSupport002
208  * @tc.desc: Test CheckBundleSupport
209  * @tc.type: FUNC
210  */
211 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport002, testing::ext::TestSize.Level0)
212 {
213     SLOGI("CheckBundleSupport002, start");
214     BundleStatusAdapter::GetInstance().Init();
215     std::string profile = R"({
216         "insightIntents": [
217             {
218                 "intentName": "OTHER_INTENT",
219                 "uiAbility": {
220                     "executeMode": ["background"]
221                 }
222             }
223         ]
224     })";
225     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
226     EXPECT_EQ(ret, false);
227     SLOGI("CheckBundleSupport002, end");
228 }
229 
230 /**
231  * @tc.name: CheckBundleSupport003
232  * @tc.desc: Test CheckBundleSupport
233  * @tc.type: FUNC
234  */
235 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport003, testing::ext::TestSize.Level0)
236 {
237     SLOGI("CheckBundleSupport003, start");
238     BundleStatusAdapter::GetInstance().Init();
239     std::string profile = R"({
240         "insightIntents": [
241             {
242                 "intentName": "PLAY_MUSICLIST",
243                 "uiAbility": {
244                     "executeMode": ["background"]
245                 }
246             }
247         ]
248     })";
249     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
250     EXPECT_EQ(ret, false);
251     SLOGI("CheckBundleSupport003, end");
252 }
253 
254 /**
255  * @tc.name: CheckBundleSupport004
256  * @tc.desc: Test CheckBundleSupport
257  * @tc.type: FUNC
258  */
259 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport004, testing::ext::TestSize.Level0)
260 {
261     SLOGI("CheckBundleSupport004, start");
262     BundleStatusAdapter::GetInstance().Init();
263     std::string profile = R"({
264         "insightIntents": [
265             {
266                 "intentName": "PLAY_AUDIO",
267                 "uiAbility": {
268                     "executeMode": ["background"]
269                 }
270             }
271         ]
272     })";
273     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
274     EXPECT_EQ(ret, false);
275     SLOGI("CheckBundleSupport004, end");
276 }
277 
278 /**
279  * @tc.name: IsAudioPlayback001
280  * @tc.desc: Test IsAudioPlayback
281  * @tc.type: FUNC
282  */
283 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback001, testing::ext::TestSize.Level0)
284 {
285     SLOGI("IsAudioPlayback001, start");
286     BundleStatusAdapter::GetInstance().Init();
287     std::string bundleName = "";
288     std::string abilityName = "";
289     bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
290     EXPECT_EQ(ret, false);
291     SLOGI("IsAudioPlayback001, end");
292 }
293 
294 /**
295  * @tc.name: IsAudioPlayback002
296  * @tc.desc: Test IsAudioPlayback
297  * @tc.type: FUNC
298  */
299 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback002, testing::ext::TestSize.Level0)
300 {
301     SLOGI("IsAudioPlayback002, start");
302     BundleStatusAdapter::GetInstance().Init();
303     std::string bundleName = "com.ohos.screenshot";
304     std::string abilityName = "MainAbility";
__anon12a1581c0402(const std::string& capturedBundleName, int32_t userId) 305     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
306         SLOGI("SubscribeBundleStatusEvent003: get bundle name: %{public}s, userId: %{public}d",
307             capturedBundleName.c_str(), userId);
308     };
309     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
310     bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
311     EXPECT_EQ(ret, false);
312     SLOGI("IsAudioPlayback002, end");
313 }
314 
315 /**
316  * @tc.name: NotifyBundleRemoved001
317  * @tc.desc: Test NotifyBundleRemoved
318  * @tc.type: FUNC
319  */
320 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved001, testing::ext::TestSize.Level0)
321 {
322     SLOGI("NotifyBundleRemoved001, start");
323     BundleStatusAdapter::GetInstance().Init();
324     std::string bundleName = "com.ohos.screenshot";
325     std::string abilityName = "MainAbility";
__anon12a1581c0502(const std::string& capturedBundleName, int32_t userId) 326     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
327         SLOGI("SubscribeBundleStatusEvent004: get bundle name: %{public}s, userId: %{public}d",
328             capturedBundleName.c_str(), userId);
329     };
330     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
331     BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
332     EXPECT_EQ(bundleName, "com.ohos.screenshot");
333     SLOGI("NotifyBundleRemoved001, end");
334 }
335 
336 /**
337  * @tc.name: NotifyBundleRemoved002
338  * @tc.desc: Test NotifyBundleRemoved
339  * @tc.type: FUNC
340  */
341 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved002, testing::ext::TestSize.Level0)
342 {
343     SLOGI("NotifyBundleRemoved002, start");
344     BundleStatusAdapter::GetInstance().Init();
345     std::string bundleName = "com.ohos.test";
346     std::string abilityName = "MainAbility";
347     BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
348     EXPECT_EQ(bundleName, "com.ohos.test");
349     SLOGI("NotifyBundleRemoved002, end");
350 }
351 
352 /**
353  * @tc.name: IsSupportPlayIntent001
354  * @tc.desc: Test IsSupportPlayIntent
355  * @tc.type: FUNC
356  */
357 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent001, testing::ext::TestSize.Level0)
358 {
359     SLOGI("IsSupportPlayIntent001, start");
360     BundleStatusAdapter::GetInstance().Init();
361     std::string bundleName = "";
362     std::string supportModule = "";
363     std::string profile = "";
364     bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
365     EXPECT_EQ(ret, false);
366     SLOGI("IsSupportPlayIntent001, end");
367 }
368 
369 /**
370  * @tc.name: IsSupportPlayIntent002
371  * @tc.desc: Test IsSupportPlayIntent
372  * @tc.type: FUNC
373  */
374 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent002, testing::ext::TestSize.Level0)
375 {
376     SLOGI("IsSupportPlayIntent002, start");
377     BundleStatusAdapter::GetInstance().Init();
378     std::string bundleName = "com.IsSupportPlayIntent.test";
379     std::string supportModule = "";
380     std::string profile = "";
__anon12a1581c0602(const std::string& capturedBundleName, int32_t userId) 381     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
382         SLOGI("IsSupportPlayIntent002: get bundle name: %{public}s, userId: %{public}d",
383             capturedBundleName.c_str(), userId);
384     };
385     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
386     bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
387     EXPECT_EQ(ret, false);
388     SLOGI("IsSupportPlayIntent002, end");
389 }
390 
391 /**
392  * @tc.name: OnBundleStateChanged001
393  * @tc.desc: Test OnBundleStateChanged
394  * @tc.type: FUNC
395  */
396 static HWTEST_F(BundleStatusAdapterTest, OnBundleStateChanged001, testing::ext::TestSize.Level0)
397 {
398     bool ret = false;
__anon12a1581c0702(const std::string& capturedBundleName, int32_t userId) 399     auto callback = [&ret](const std::string& capturedBundleName, int32_t userId) {
400         SLOGI("OnBundleStateChanged001: get bundle name: %{public}s, userId: %{public}d",
401             capturedBundleName.c_str(), userId);
402         ret = true;
403     };
404     sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl =
405             new(std::nothrow) BundleStatusCallbackImpl(callback, 1);
406     bundleStatusCallbackImpl->OnBundleStateChanged(1, 0, "test1", "");
407     EXPECT_TRUE(ret);
408 }
409 
410 /**
411  * @tc.name: OnBundleStateChanged002
412  * @tc.desc: Test OnBundleStateChanged
413  * @tc.type: FUNC
414  */
415 static HWTEST_F(BundleStatusAdapterTest, OnBundleStateChanged002, testing::ext::TestSize.Level0)
416 {
417     bool ret = true;
__anon12a1581c0802(const std::string& capturedBundleName, int32_t userId) 418     auto callback = [&ret](const std::string& capturedBundleName, int32_t userId) {
419         SLOGI("OnBundleStateChanged002: get bundle name: %{public}s, userId: %{public}d",
420             capturedBundleName.c_str(), userId);
421         ret = false;
422     };
423     sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl = new(std::nothrow) BundleStatusCallbackImpl(callback, 1);
424     bundleStatusCallbackImpl->OnBundleStateChanged(0, 0, "test2", "");
425     EXPECT_TRUE(ret);
426 }
427 
428 /**
429  * @tc.name: GetBundleIcon001
430  * @tc.desc: fail to get icon
431  * @tc.type: FUNC
432  */
433 static HWTEST_F(BundleStatusAdapterTest, GetBundleIcon001, testing::ext::TestSize.Level0)
434 {
435     BundleStatusAdapter::GetInstance().bundleMgrProxy = nullptr;
436     BundleStatusAdapter::GetInstance().bundleResourceProxy = nullptr;
437     std::string bundleName = "test";
438     std::string abilityName = "testAbility";
439     std::string icon = "test";
440     bool ret = BundleStatusAdapter::GetInstance().GetBundleIcon(bundleName, abilityName, icon);
441     EXPECT_FALSE(ret);
442 }
443 
444 /**
445  * @tc.name: GetBundleIcon002
446  * @tc.desc: fail to get icon
447  * @tc.type: FUNC
448  */
449 static HWTEST_F(BundleStatusAdapterTest, GetBundleIcon002, testing::ext::TestSize.Level0)
450 {
451     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
452     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
453     ASSERT_TRUE(remoteObject != nullptr);
454     BundleStatusAdapter::GetInstance().bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
455     BundleStatusAdapter::GetInstance().bundleResourceProxy = nullptr;
456     std::string bundleName = "test";
457     std::string abilityName = "testAbility";
458     std::string icon = "test";
459     bool ret = BundleStatusAdapter::GetInstance().GetBundleIcon(bundleName, abilityName, icon);
460     EXPECT_FALSE(ret);
461 }
462 
463 /**
464  * @tc.name: GetBundleIcon002
465  * @tc.desc: fail to get icon
466  * @tc.type: FUNC
467  */
468 static HWTEST_F(BundleStatusAdapterTest, GetBundleIcon003, testing::ext::TestSize.Level0)
469 {
470     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
471     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
472     ASSERT_TRUE(remoteObject != nullptr);
473     auto proxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
474     BundleStatusAdapter::GetInstance().bundleMgrProxy = proxy;
475     BundleStatusAdapter::GetInstance().bundleResourceProxy = proxy->GetBundleResourceProxy();
476     std::string bundleName = "test";
477     std::string abilityName = "";
478     std::string icon = "test";
479     bool ret = BundleStatusAdapter::GetInstance().GetBundleIcon(bundleName, abilityName, icon);
480     EXPECT_FALSE(ret);
481 }
482 
483 /**
484  * @tc.name: CheckBundleSupport005
485  * @tc.desc: set intentName to PlayMusicList
486  * @tc.type: FUNC
487  */
488 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport005, testing::ext::TestSize.Level0)
489 {
490     SLOGI("CheckBundleSupport005 start");
491     BundleStatusAdapter::GetInstance().Init();
492     std::string profile = R"({
493         "insightIntents": [
494             {
495                 "intentName": "PlayMusicList",
496                 "uiAbility": {
497                     "executeMode": ["background"]
498                 }
499             }
500         ]
501     })";
502     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
503     EXPECT_EQ(ret, true);
504     SLOGI("CheckBundleSupport005 end");
505 }
506 
507 /**
508  * @tc.name: CheckBundleSupport006
509  * @tc.desc: set intentName to PlayAudio
510  * @tc.type: FUNC
511  */
512 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport006, testing::ext::TestSize.Level0)
513 {
514     SLOGI("CheckBundleSupport006 start");
515     BundleStatusAdapter::GetInstance().Init();
516     std::string profile = R"({
517         "insightIntents": [
518             {
519                 "intentName": "PlayAudio",
520                 "uiAbility": {
521                     "executeMode": ["background"]
522                 }
523             }
524         ]
525     })";
526     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
527     EXPECT_EQ(ret, true);
528     SLOGI("CheckBundleSupport006 end");
529 }
530 
531 } // namespace AVSession
532 } // namespace OHOS
533