1 /*
2 * Copyright (c) 2024 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 "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "insight_intent_execute_param.h"
20 #define private public
21 #define protected public
22 #include "bundle_status_adapter.h"
23 #undef protected
24 #undef private
25
26 #include "accesstoken_kit.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::Security::AccessToken;
32
33 namespace OHOS {
34 namespace AVSession {
35 static uint64_t g_selfTokenId = 0;
36
37 static HapInfoParams g_info = {
38 .userID = 100,
39 .bundleName = "ohos.permission_test.demo",
40 .instIndex = 0,
41 .appIDDesc = "ohos.permission_test.demo",
42 .isSystemApp = true
43 };
44
45 static HapPolicyParams g_policy = {
46 .apl = APL_NORMAL,
47 .domain = "test.domain",
48 .permList = {
49 {
50 .permissionName = "ohos.permission.LISTEN_BUNDLE_CHANGE",
51 .bundleName = "ohos.permission_test.demo",
52 .grantMode = 1,
53 .availableLevel = APL_NORMAL,
54 .label = "label",
55 .labelId = 1,
56 .description = "test",
57 .descriptionId = 1
58 }
59 },
60 .permStateList = {
61 {
62 .permissionName = "ohos.permission.LISTEN_BUNDLE_CHANGE",
63 .isGeneral = true,
64 .resDeviceID = { "local" },
65 .grantStatus = { PermissionState::PERMISSION_GRANTED },
66 .grantFlags = { 1 }
67 }
68 }
69 };
70
71 class BundleStatusAdapterTest : public testing::Test {
72 public:
73 static void SetUpTestCase(void);
74 static void TearDownTestCase(void);
75 void SetUp();
76 void TearDown();
77 };
78
SetUpTestCase()79 void BundleStatusAdapterTest::SetUpTestCase()
80 {
81 g_selfTokenId = GetSelfTokenID();
82 AccessTokenKit::AllocHapToken(g_info, g_policy);
83 AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(g_info.userID, g_info.bundleName, g_info.instIndex);
84 SetSelfTokenID(tokenID.tokenIDEx);
85 }
86
TearDownTestCase()87 void BundleStatusAdapterTest::TearDownTestCase()
88 {
89 SetSelfTokenID(g_selfTokenId);
90 auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex);
91 AccessTokenKit::DeleteToken(tokenId);
92 }
93
SetUp()94 void BundleStatusAdapterTest::SetUp()
95 {
96 }
97
TearDown()98 void BundleStatusAdapterTest::TearDown()
99 {
100 }
101
102 /**
103 * @tc.name: SubscribeBundleStatusEvent001
104 * @tc.desc: Test SubscribeBundleStatusEvent
105 * @tc.type: FUNC
106 */
107 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent001, testing::ext::TestSize.Level1)
108 {
109 SLOGI("SubscribeBundleStatusEvent001, start");
110 std::string bundleName = "test1";
__anon68e2803a0102(const std::string& capturedBundleName, int32_t userId) 111 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
112 SLOGI("SubscribeBundleStatusEvent001: get bundle name: %{public}s, userId: %{public}d",
113 capturedBundleName.c_str(), userId);
114 };
115 bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
116 SLOGI("SubscribeBundleStatusEvent001 get ret:%{public}d", static_cast<int>(ret));
117 EXPECT_EQ(ret, true);
118 SLOGI("SubscribeBundleStatusEvent001, end");
119 }
120
121 /**
122 * @tc.name: SubscribeBundleStatusEvent002
123 * @tc.desc: Test SubscribeBundleStatusEvent
124 * @tc.type: FUNC
125 */
126 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent002, testing::ext::TestSize.Level1)
127 {
128 SLOGI("SubscribeBundleStatusEvent002, start");
129 BundleStatusAdapter::GetInstance().Init();
130 std::string bundleName = "";
__anon68e2803a0202(const std::string& capturedBundleName, int32_t userId) 131 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
132 SLOGI("SubscribeBundleStatusEvent002: get bundle name: %{public}s, userId: %{public}d",
133 capturedBundleName.c_str(), userId);
134 };
135 bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
136 EXPECT_EQ(ret, false);
137 SLOGI("SubscribeBundleStatusEvent002, end");
138 }
139
140 /**
141 * @tc.name: SubscribeBundleStatusEvent003
142 * @tc.desc: Test SubscribeBundleStatusEvent
143 * @tc.type: FUNC
144 */
145 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent003, testing::ext::TestSize.Level1)
146 {
147 SLOGI("SubscribeBundleStatusEvent003, start");
148 BundleStatusAdapter::GetInstance().Init();
149 std::string bundleName = "com.ohos.sceneboard";
__anon68e2803a0302(const std::string& capturedBundleName, int32_t userId) 150 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
151 SLOGI("SubscribeBundleStatusEvent003: get bundle name: %{public}s, userId: %{public}d",
152 capturedBundleName.c_str(), userId);
153 };
154 bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
155 EXPECT_EQ(ret, true);
156 SLOGI("SubscribeBundleStatusEvent003, end");
157 }
158
159 /**
160 * @tc.name: GetBundleNameFromUid001
161 * @tc.desc: Test GetBundleNameFromUid
162 * @tc.type: FUNC
163 */
164 static HWTEST_F(BundleStatusAdapterTest, GetBundleNameFromUid001, testing::ext::TestSize.Level1)
165 {
166 SLOGI("GetBundleNameFromUid001, start");
167 BundleStatusAdapter::GetInstance().Init();
168 const int32_t uid = 0;
169 BundleStatusAdapter::GetInstance().GetBundleNameFromUid(uid);
170 EXPECT_EQ(uid, 0);
171 SLOGI("GetBundleNameFromUid001, end");
172 }
173
174 /**
175 * @tc.name: CheckBundleSupport001
176 * @tc.desc: Test CheckBundleSupport
177 * @tc.type: FUNC
178 */
179 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport001, testing::ext::TestSize.Level1)
180 {
181 SLOGI("CheckBundleSupport001, start");
182 BundleStatusAdapter::GetInstance().Init();
183 std::string profile = "";
184 bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
185 EXPECT_EQ(ret, false);
186 SLOGI("CheckBundleSupport001, end");
187 }
188
189 /**
190 * @tc.name: CheckBundleSupport002
191 * @tc.desc: Test CheckBundleSupport
192 * @tc.type: FUNC
193 */
194 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport002, testing::ext::TestSize.Level1)
195 {
196 SLOGI("CheckBundleSupport002, start");
197 BundleStatusAdapter::GetInstance().Init();
198 std::string profile = R"({
199 "insightIntents": [
200 {
201 "intentName": "OTHER_INTENT",
202 "uiAbility": {
203 "executeMode": ["background"]
204 }
205 }
206 ]
207 })";
208 bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
209 EXPECT_EQ(ret, false);
210 SLOGI("CheckBundleSupport002, end");
211 }
212
213 /**
214 * @tc.name: CheckBundleSupport003
215 * @tc.desc: Test CheckBundleSupport
216 * @tc.type: FUNC
217 */
218 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport003, testing::ext::TestSize.Level1)
219 {
220 SLOGI("CheckBundleSupport003, start");
221 BundleStatusAdapter::GetInstance().Init();
222 std::string profile = R"({
223 "insightIntents": [
224 {
225 "intentName": "PLAY_MUSICLIST",
226 "uiAbility": {
227 "executeMode": ["background"]
228 }
229 }
230 ]
231 })";
232 bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
233 EXPECT_EQ(ret, false);
234 SLOGI("CheckBundleSupport003, end");
235 }
236
237 /**
238 * @tc.name: CheckBundleSupport004
239 * @tc.desc: Test CheckBundleSupport
240 * @tc.type: FUNC
241 */
242 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport004, testing::ext::TestSize.Level1)
243 {
244 SLOGI("CheckBundleSupport004, start");
245 BundleStatusAdapter::GetInstance().Init();
246 std::string profile = R"({
247 "insightIntents": [
248 {
249 "intentName": "PLAY_AUDIO",
250 "uiAbility": {
251 "executeMode": ["background"]
252 }
253 }
254 ]
255 })";
256 bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
257 EXPECT_EQ(ret, false);
258 SLOGI("CheckBundleSupport004, end");
259 }
260
261 /**
262 * @tc.name: IsAudioPlayback001
263 * @tc.desc: Test IsAudioPlayback
264 * @tc.type: FUNC
265 */
266 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback001, testing::ext::TestSize.Level1)
267 {
268 SLOGI("IsAudioPlayback001, start");
269 BundleStatusAdapter::GetInstance().Init();
270 std::string bundleName = "";
271 std::string abilityName = "";
272 bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
273 EXPECT_EQ(ret, false);
274 SLOGI("IsAudioPlayback001, end");
275 }
276
277 /**
278 * @tc.name: IsAudioPlayback002
279 * @tc.desc: Test IsAudioPlayback
280 * @tc.type: FUNC
281 */
282 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback002, testing::ext::TestSize.Level1)
283 {
284 SLOGI("IsAudioPlayback002, start");
285 BundleStatusAdapter::GetInstance().Init();
286 std::string bundleName = "com.ohos.screenshot";
287 std::string abilityName = "MainAbility";
__anon68e2803a0402(const std::string& capturedBundleName, int32_t userId) 288 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
289 SLOGI("SubscribeBundleStatusEvent003: get bundle name: %{public}s, userId: %{public}d",
290 capturedBundleName.c_str(), userId);
291 };
292 BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
293 bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
294 EXPECT_EQ(ret, false);
295 SLOGI("IsAudioPlayback002, end");
296 }
297
298 /**
299 * @tc.name: NotifyBundleRemoved001
300 * @tc.desc: Test NotifyBundleRemoved
301 * @tc.type: FUNC
302 */
303 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved001, testing::ext::TestSize.Level1)
304 {
305 SLOGI("NotifyBundleRemoved001, start");
306 BundleStatusAdapter::GetInstance().Init();
307 std::string bundleName = "com.ohos.screenshot";
308 std::string abilityName = "MainAbility";
__anon68e2803a0502(const std::string& capturedBundleName, int32_t userId) 309 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
310 SLOGI("SubscribeBundleStatusEvent004: get bundle name: %{public}s, userId: %{public}d",
311 capturedBundleName.c_str(), userId);
312 };
313 BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
314 BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
315 EXPECT_EQ(bundleName, "com.ohos.screenshot");
316 SLOGI("NotifyBundleRemoved001, end");
317 }
318
319 /**
320 * @tc.name: NotifyBundleRemoved002
321 * @tc.desc: Test NotifyBundleRemoved
322 * @tc.type: FUNC
323 */
324 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved002, testing::ext::TestSize.Level1)
325 {
326 SLOGI("NotifyBundleRemoved002, start");
327 BundleStatusAdapter::GetInstance().Init();
328 std::string bundleName = "com.ohos.test";
329 std::string abilityName = "MainAbility";
330 BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
331 EXPECT_EQ(bundleName, "com.ohos.test");
332 SLOGI("NotifyBundleRemoved002, end");
333 }
334
335 /**
336 * @tc.name: IsSupportPlayIntent001
337 * @tc.desc: Test IsSupportPlayIntent
338 * @tc.type: FUNC
339 */
340 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent001, testing::ext::TestSize.Level1)
341 {
342 SLOGI("IsSupportPlayIntent001, start");
343 BundleStatusAdapter::GetInstance().Init();
344 std::string bundleName = "";
345 std::string supportModule = "";
346 std::string profile = "";
347 bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
348 EXPECT_EQ(ret, false);
349 SLOGI("IsSupportPlayIntent001, end");
350 }
351
352 /**
353 * @tc.name: IsSupportPlayIntent002
354 * @tc.desc: Test IsSupportPlayIntent
355 * @tc.type: FUNC
356 */
357 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent002, testing::ext::TestSize.Level1)
358 {
359 SLOGI("IsSupportPlayIntent002, start");
360 BundleStatusAdapter::GetInstance().Init();
361 std::string bundleName = "com.IsSupportPlayIntent.test";
362 std::string supportModule = "";
363 std::string profile = "";
__anon68e2803a0602(const std::string& capturedBundleName, int32_t userId) 364 auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
365 SLOGI("IsSupportPlayIntent002: get bundle name: %{public}s, userId: %{public}d",
366 capturedBundleName.c_str(), userId);
367 };
368 BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
369 bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
370 EXPECT_EQ(ret, false);
371 SLOGI("IsSupportPlayIntent002, end");
372 }
373
374 /**
375 * @tc.name: OnBundleStateChanged001
376 * @tc.desc: Test OnBundleStateChanged
377 * @tc.type: FUNC
378 */
379 static HWTEST_F(BundleStatusAdapterTest, OnBundleStateChanged001, testing::ext::TestSize.Level1)
380 {
381 bool ret = false;
__anon68e2803a0702(const std::string& capturedBundleName, int32_t userId) 382 auto callback = [&ret](const std::string& capturedBundleName, int32_t userId) {
383 SLOGI("OnBundleStateChanged001: get bundle name: %{public}s, userId: %{public}d",
384 capturedBundleName.c_str(), userId);
385 ret = true;
386 };
387 sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl =
388 new(std::nothrow) BundleStatusCallbackImpl(callback, 1);
389 bundleStatusCallbackImpl->OnBundleStateChanged(1, 0, "test1", "");
390 EXPECT_TRUE(ret);
391 }
392
393 /**
394 * @tc.name: OnBundleStateChanged002
395 * @tc.desc: Test OnBundleStateChanged
396 * @tc.type: FUNC
397 */
398 static HWTEST_F(BundleStatusAdapterTest, OnBundleStateChanged002, testing::ext::TestSize.Level1)
399 {
400 bool ret = true;
__anon68e2803a0802(const std::string& capturedBundleName, int32_t userId) 401 auto callback = [&ret](const std::string& capturedBundleName, int32_t userId) {
402 SLOGI("OnBundleStateChanged002: get bundle name: %{public}s, userId: %{public}d",
403 capturedBundleName.c_str(), userId);
404 ret = false;
405 };
406 sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl = new(std::nothrow) BundleStatusCallbackImpl(callback, 1);
407 bundleStatusCallbackImpl->OnBundleStateChanged(0, 0, "test2", "");
408 EXPECT_TRUE(ret);
409 }
410 } // namespace AVSession
411 } // namespace OHOS
412