• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 
18 #include "avsession_manager.h"
19 #include "avsession_info.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS::Security::AccessToken;
29 
30 namespace OHOS {
31 namespace AVSession {
32 static HapInfoParams g_infoA = {
33     .userID = 100,
34     .bundleName = "ohos.permission_test.demoA",
35     .instIndex = 0,
36     .appIDDesc = "ohos.permission_test.demoA",
37     .isSystemApp = false
38 };
39 
40 static HapPolicyParams g_policyA = {
41     .apl = APL_NORMAL,
42     .domain = "test.domainA"
43 };
44 
45 static HapInfoParams g_infoB = {
46     .userID = 100,
47     .bundleName = "ohos.permission_test.demoB",
48     .instIndex = 0,
49     .appIDDesc = "ohos.permission_test.demoB",
50     .isSystemApp = true
51 };
52 
53 static HapPolicyParams g_policyB = {
54     .apl = APL_NORMAL,
55     .domain = "test.domainB",
56     .permList = {
57         {
58             .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
59             .bundleName = "ohos.permission_test.demoB",
60             .grantMode = 1,
61             .availableLevel = APL_NORMAL,
62             .label = "label",
63             .labelId = 1,
64             .description = "test",
65             .descriptionId = 1
66         }
67     },
68     .permStateList = {
69         {
70             .permissionName = "ohos.permission.MANAGE_MEDIA_RESOURCES",
71             .isGeneral = true,
72             .resDeviceID = { "local" },
73             .grantStatus = { PermissionState::PERMISSION_GRANTED },
74             .grantFlags = { 1 }
75         }
76     }
77 };
78 
79 class AVSessionPermissionTest : public testing::Test {
80 public:
81     static void SetUpTestCase();
82     static void TearDownTestCase();
83     void SetUp() override;
84     void TearDown() override;
85 
86     uint64_t selfTokenId_ = 0;
87     void AddPermission(const HapInfoParams& info, const HapPolicyParams& policy);
88     void DeletePermission(const HapInfoParams& info);
89 };
90 
SetUpTestCase()91 void AVSessionPermissionTest::SetUpTestCase()
92 {}
93 
TearDownTestCase()94 void AVSessionPermissionTest::TearDownTestCase()
95 {}
96 
SetUp()97 void AVSessionPermissionTest::SetUp()
98 {}
99 
TearDown()100 void AVSessionPermissionTest::TearDown()
101 {}
102 
AddPermission(const HapInfoParams & info,const HapPolicyParams & policy)103 void AVSessionPermissionTest::AddPermission(const HapInfoParams& info, const HapPolicyParams& policy)
104 {
105     AccessTokenKit::AllocHapToken(info, policy);
106     AccessTokenIDEx tokenID = AccessTokenKit::GetHapTokenIDEx(info.userID, info.bundleName, info.instIndex);
107     SetSelfTokenID(tokenID.tokenIDEx);
108     SLOGI("GetSelfTokenID:%{public}" PRId64, GetSelfTokenID());
109 }
110 
DeletePermission(const HapInfoParams & info)111 void AVSessionPermissionTest::DeletePermission(const HapInfoParams& info)
112 {
113     SetSelfTokenID(selfTokenId_);
114     auto tokenId = AccessTokenKit::GetHapTokenID(info.userID, info.bundleName, info.instIndex);
115     AccessTokenKit::DeleteToken(tokenId);
116 }
117 
118 class TestSessionListener : public SessionListener {
119 public:
OnSessionCreate(const AVSessionDescriptor & descriptor)120     void OnSessionCreate(const AVSessionDescriptor& descriptor) override
121     {
122         SLOGI("sessionId=%{public}s created", descriptor.sessionId_.c_str());
123     }
124 
OnSessionRelease(const AVSessionDescriptor & descriptor)125     void OnSessionRelease(const AVSessionDescriptor& descriptor) override
126     {
127         SLOGI("sessionId=%{public}s released", descriptor.sessionId_.c_str());
128     }
129 
OnTopSessionChange(const AVSessionDescriptor & descriptor)130     void OnTopSessionChange(const AVSessionDescriptor& descriptor) override
131     {
132         SLOGI("sessionId=%{public}s be top session", descriptor.sessionId_.c_str());
133     }
134 
OnAudioSessionChecked(const int32_t uid)135     void OnAudioSessionChecked(const int32_t uid) override
136     {
137         SLOGI("uid=%{public}d checked", uid);
138     }
139 };
140 
141 /**
142 * @tc.name: GetAllSessionDescriptorsWithNoPerm001
143 * @tc.desc: Get all session descriptors with no permission
144 * @tc.type: FUNC
145 * @tc.require: AR000H31JQ
146 */
147 HWTEST_F(AVSessionPermissionTest, GetAllSessionDescriptorsWithNoPerm001, TestSize.Level1)
148 {
149     AddPermission(g_infoA, g_policyA);
150     std::vector<AVSessionDescriptor> descriptors;
151     auto ret = AVSessionManager::GetInstance().GetAllSessionDescriptors(descriptors);
152     EXPECT_EQ(ret, ERR_NO_PERMISSION);
153     DeletePermission(g_infoA);
154 }
155 
156 /**
157 * @tc.name: GetActivatedSessionDescriptorsWithNoPerm001
158 * @tc.desc: Get all activated session descriptors with no permission
159 * @tc.type: FUNC
160 * @tc.require: AR000H31JR
161 */
162 HWTEST_F(AVSessionPermissionTest, GetActivatedSessionDescriptorsWithNoPerm001, TestSize.Level1)
163 {
164     AddPermission(g_infoA, g_policyA);
165     std::vector<AVSessionDescriptor> descriptors;
166     auto ret = AVSessionManager::GetInstance().GetActivatedSessionDescriptors(descriptors);
167     EXPECT_EQ(ret, ERR_NO_PERMISSION);
168     DeletePermission(g_infoA);
169 }
170 
171 /**
172 * @tc.name: GetSessionDescriptorsBySessionIdWithNoPerm001
173 * @tc.desc: Get session descriptors by sessionId with no permission
174 * @tc.type: FUNC
175 * @tc.require: AR000H31JR
176 */
177 HWTEST_F(AVSessionPermissionTest, GetSessionDescriptorsBySessionIdWithNoPerm001, TestSize.Level1)
178 {
179     AddPermission(g_infoA, g_policyA);
180     AVSessionDescriptor descriptor {};
181 
182     // Using "1" as the test input parameter
183     int32_t ret = AVSessionManager::GetInstance().GetSessionDescriptorsBySessionId("1", descriptor);
184     EXPECT_EQ(ret, AVSESSION_ERROR);
185     DeletePermission(g_infoA);
186 }
187 
188 /**
189 * @tc.name: GetHistoricalSessionDescriptorsWithNoPerm001
190 * @tc.desc: Get session descriptors by sessionId with no permission
191 * @tc.type: FUNC
192 * @tc.require: AR000H31JR
193 */
194 HWTEST_F(AVSessionPermissionTest, GetHistoricalSessionDescriptorsWithNoPerm001, TestSize.Level1)
195 {
196     AddPermission(g_infoA, g_policyA);
197     std::vector<AVSessionDescriptor> descriptors;
198 
199     // Using "1" as the test input parameter
200     int32_t ret = AVSessionManager::GetInstance().GetHistoricalSessionDescriptors(0, descriptors);
201     EXPECT_EQ(ret, ERR_NO_PERMISSION);
202     DeletePermission(g_infoA);
203 }
204 
205 /**
206 * @tc.name: CreateControllerWithNoPerm001
207 * @tc.desc: create session controller with no permission
208 * @tc.type: FUNC
209 * @tc.require: AR000H31JR
210 */
211 HWTEST_F(AVSessionPermissionTest, CreateControllerWithNoPerm001, TestSize.Level1)
212 {
213     AddPermission(g_infoA, g_policyA);
214     std::shared_ptr<AVSessionController> controller;
215 
216     // Using "1" as the test input parameter
217     auto ret = AVSessionManager::GetInstance().CreateController("1", controller);
218     EXPECT_EQ(ret, ERR_NO_PERMISSION);
219     DeletePermission(g_infoA);
220 }
221 
222 /**
223 * @tc.name: RegisterSessionListenerWithNoPerm001
224 * @tc.desc: register listener with no permission
225 * @tc.type: FUNC
226 * @tc.require: AR000H31JQ
227 */
228 HWTEST_F(AVSessionPermissionTest, RegisterSessionListenerWithNoPerm001, TestSize.Level1)
229 {
230     AddPermission(g_infoA, g_policyA);
231     std::shared_ptr<TestSessionListener> listener = std::make_shared<TestSessionListener>();
232     auto result = AVSessionManager::GetInstance().RegisterSessionListener(listener);
233     EXPECT_EQ(result, ERR_NO_PERMISSION);
234     DeletePermission(g_infoA);
235 }
236 
237 /**
238 * @tc.name: SendSystemMediaKeyEventWithNoPerm001
239 * @tc.desc: valid keyEvent with no permission
240 * @tc.type: FUNC
241 * @tc.require: AR000H31JR
242 */
243 HWTEST_F(AVSessionPermissionTest, SendSystemMediaKeyEventWithNoPerm001, TestSize.Level1)
244 {
245     AddPermission(g_infoA, g_policyA);
246     auto keyEvent = OHOS::MMI::KeyEvent::Create();
247     ASSERT_NE(keyEvent, nullptr);
248     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
249     keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
250     keyEvent->SetActionTime(1000);
251     auto keyItem = OHOS::MMI::KeyEvent::KeyItem();
252     keyItem.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
253     keyItem.SetDownTime(1000);
254     keyItem.SetPressed(true);
255     keyEvent->AddKeyItem(keyItem);
256 
257     auto result = AVSessionManager::GetInstance().SendSystemAVKeyEvent(*keyEvent);
258     EXPECT_EQ(result, ERR_NO_PERMISSION);
259     DeletePermission(g_infoA);
260 }
261 
262 /**
263 * @tc.name: SendSystemControlCommandWithNoPerm001
264 * @tc.desc: valid command with no permission
265 * @tc.type: FUNC
266 * @tc.require: AR000H31JR
267 */
268 HWTEST_F(AVSessionPermissionTest, SendSystemControlCommandWithNoPerm001, TestSize.Level1)
269 {
270     AddPermission(g_infoA, g_policyA);
271     AVControlCommand command;
272     command.SetCommand(AVControlCommand::SESSION_CMD_PLAY);
273     auto result = AVSessionManager::GetInstance().SendSystemControlCommand(command);
274     EXPECT_EQ(result, ERR_NO_PERMISSION);
275     DeletePermission(g_infoA);
276 }
277 
278 /**
279 * @tc.name: GetAllSessionDescriptorsWithPerm001
280 * @tc.desc: Get all session descriptors with permission
281 * @tc.type: FUNC
282 * @tc.require: AR000H31JQ
283 */
284 HWTEST_F(AVSessionPermissionTest, GetAllSessionDescriptorsWithPerm001, TestSize.Level1)
285 {
286     AddPermission(g_infoB, g_policyB);
287     OHOS::AppExecFwk::ElementName elementName;
288     elementName.SetBundleName("test.ohos.avsession");
289     elementName.SetAbilityName("test.ability");
290     auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
291     ASSERT_NE(session, nullptr);
292 
293     std::vector<AVSessionDescriptor> descriptors;
294     auto ret = AVSessionManager::GetInstance().GetAllSessionDescriptors(descriptors);
295     EXPECT_EQ(ret, AVSESSION_SUCCESS);
296     EXPECT_EQ(descriptors.size(), 1);
297     if (session != nullptr) {
298         session->Destroy();
299     }
300     DeletePermission(g_infoB);
301 }
302 
303 /**
304 * @tc.name: GetActivatedSessionDescriptorsWithPerm001
305 * @tc.desc: Get all activated session descriptors with permission
306 * @tc.type: FUNC
307 * @tc.require: AR000H31JR
308 */
309 HWTEST_F(AVSessionPermissionTest, GetActivatedSessionDescriptorsWithPerm001, TestSize.Level1)
310 {
311     AddPermission(g_infoB, g_policyB);
312     OHOS::AppExecFwk::ElementName elementName;
313     elementName.SetBundleName("test.ohos.avsession");
314     elementName.SetAbilityName("test.ability");
315     auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
316     ASSERT_NE(session, nullptr);
317     session->Activate();
318 
319     std::vector<AVSessionDescriptor> descriptors;
320     auto ret = AVSessionManager::GetInstance().GetActivatedSessionDescriptors(descriptors);
321     EXPECT_EQ(ret, AVSESSION_SUCCESS);
322     EXPECT_EQ(descriptors.size(), 1);
323 
324     if (session != nullptr) {
325         session->Destroy();
326     }
327     DeletePermission(g_infoB);
328 }
329 
330 /**
331 * @tc.name: GetHistoricalSessionDescriptorsWithNoPerm001
332 * @tc.desc: Get session descriptors by sessionId with no permission
333 * @tc.type: FUNC
334 * @tc.require: AR000H31JR
335 */
336 HWTEST_F(AVSessionPermissionTest, GetHistoricalSessionDescriptorsWithPerm001, TestSize.Level1)
337 {
338     AddPermission(g_infoB, g_policyB);
339     OHOS::AppExecFwk::ElementName elementName;
340     elementName.SetBundleName("test.ohos.avsession");
341     elementName.SetAbilityName("test.ability");
342     auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
343     ASSERT_NE(session, nullptr);
344 
345     std::vector<AVSessionDescriptor> descriptors;
346     auto ret_1 = AVSessionManager::GetInstance().GetHistoricalSessionDescriptors(0, descriptors);
347     EXPECT_EQ(ret_1, AVSESSION_SUCCESS);
348     EXPECT_EQ(descriptors.size(), 0);
349     if (session != nullptr) {
350         session->Destroy();
351     }
352     auto ret_2 = AVSessionManager::GetInstance().GetHistoricalSessionDescriptors(10, descriptors);
353     EXPECT_EQ(ret_2, AVSESSION_SUCCESS);
354     EXPECT_NE(descriptors.size(), 0);
355     DeletePermission(g_infoB);
356 }
357 
358 /**
359 * @tc.name: CreateControllerWithPerm001
360 * @tc.desc: create session controller with permission
361 * @tc.type: FUNC
362 * @tc.require: AR000H31JR
363 */
364 HWTEST_F(AVSessionPermissionTest, CreateControllerWithPerm001, TestSize.Level1)
365 {
366     AddPermission(g_infoB, g_policyB);
367     OHOS::AppExecFwk::ElementName elementName;
368     elementName.SetBundleName("test.ohos.avsession");
369     elementName.SetAbilityName("test.ability");
370     auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
371     ASSERT_NE(session, nullptr);
372 
373     std::shared_ptr<AVSessionController> controller;
374     auto ret = AVSessionManager::GetInstance().CreateController(session->GetSessionId(), controller);
375     EXPECT_EQ(ret, AVSESSION_SUCCESS);
376     EXPECT_NE(controller, nullptr);
377     if (session != nullptr) {
378         session->Destroy();
379     }
380     DeletePermission(g_infoB);
381 }
382 
383 /**
384 * @tc.name: RegisterSessionListenerWithPerm001
385 * @tc.desc: register listener with permission
386 * @tc.type: FUNC
387 * @tc.require: AR000H31JQ
388 */
389 HWTEST_F(AVSessionPermissionTest, RegisterSessionListenerWithPerm001, TestSize.Level1)
390 {
391     AddPermission(g_infoB, g_policyB);
392     std::shared_ptr<TestSessionListener> listener = std::make_shared<TestSessionListener>();
393     auto result = AVSessionManager::GetInstance().RegisterSessionListener(listener);
394     EXPECT_EQ(result, AVSESSION_SUCCESS);
395     DeletePermission(g_infoB);
396 }
397 
398 /**
399 * @tc.name: SendSystemMediaKeyEventWithPerm001
400 * @tc.desc: valid keyEvent with permission
401 * @tc.type: FUNC
402 * @tc.require: AR000H31JR
403 */
404 HWTEST_F(AVSessionPermissionTest, SendSystemMediaKeyEventWithPerm001, TestSize.Level1)
405 {
406     AddPermission(g_infoB, g_policyB);
407     auto keyEvent = OHOS::MMI::KeyEvent::Create();
408     ASSERT_NE(keyEvent, nullptr);
409     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
410     keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
411     keyEvent->SetActionTime(1000);
412     auto keyItem = OHOS::MMI::KeyEvent::KeyItem();
413     keyItem.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
414     keyItem.SetDownTime(1000);
415     keyItem.SetPressed(true);
416     keyEvent->AddKeyItem(keyItem);
417 
418     auto result = AVSessionManager::GetInstance().SendSystemAVKeyEvent(*keyEvent);
419     EXPECT_EQ(result, AVSESSION_SUCCESS);
420     DeletePermission(g_infoB);
421 }
422 
423 /**
424 * @tc.name: SendSystemControlCommandWithPerm001
425 * @tc.desc: valid command with permission
426 * @tc.type: FUNC
427 * @tc.require: AR000H31JR
428 */
429 HWTEST_F(AVSessionPermissionTest, SendSystemControlCommandWithPerm001, TestSize.Level1)
430 {
431     AddPermission(g_infoB, g_policyB);
432     AVControlCommand command;
433     command.SetCommand(AVControlCommand::SESSION_CMD_PLAY);
434     auto result = AVSessionManager::GetInstance().SendSystemControlCommand(command);
435     EXPECT_EQ(result, AVSESSION_SUCCESS);
436     DeletePermission(g_infoB);
437 }
438 } // namespace AVSession
439 } // namespace OHOS
440