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 }
109
DeletePermission(const HapInfoParams & info)110 void AVSessionPermissionTest::DeletePermission(const HapInfoParams& info)
111 {
112 SetSelfTokenID(selfTokenId_);
113 auto tokenId = AccessTokenKit::GetHapTokenID(info.userID, info.bundleName, info.instIndex);
114 AccessTokenKit::DeleteToken(tokenId);
115 }
116
117 class TestSessionListener : public SessionListener {
118 public:
OnSessionCreate(const AVSessionDescriptor & descriptor)119 void OnSessionCreate(const AVSessionDescriptor& descriptor) override
120 {
121 SLOGI("sessionId=%{public}s created", descriptor.sessionId_.c_str());
122 }
123
OnSessionRelease(const AVSessionDescriptor & descriptor)124 void OnSessionRelease(const AVSessionDescriptor& descriptor) override
125 {
126 SLOGI("sessionId=%{public}s released", descriptor.sessionId_.c_str());
127 }
128
OnTopSessionChange(const AVSessionDescriptor & descriptor)129 void OnTopSessionChange(const AVSessionDescriptor& descriptor) override
130 {
131 SLOGI("sessionId=%{public}s be top session", descriptor.sessionId_.c_str());
132 }
133
OnAudioSessionChecked(const int32_t uid)134 void OnAudioSessionChecked(const int32_t uid) override
135 {
136 SLOGI("uid=%{public}d checked", uid);
137 }
138 };
139
140 /**
141 * @tc.name: GetAllSessionDescriptorsWithNoPerm001
142 * @tc.desc: Get all session descriptors with no permission
143 * @tc.type: FUNC
144 * @tc.require: AR000H31JQ
145 */
146 HWTEST_F(AVSessionPermissionTest, GetAllSessionDescriptorsWithNoPerm001, TestSize.Level1)
147 {
148 AddPermission(g_infoA, g_policyA);
149 std::vector<AVSessionDescriptor> descriptors;
150 auto ret = AVSessionManager::GetInstance().GetAllSessionDescriptors(descriptors);
151 EXPECT_EQ(ret, ERR_NO_PERMISSION);
152 DeletePermission(g_infoA);
153 }
154
155 /**
156 * @tc.name: GetActivatedSessionDescriptorsWithNoPerm001
157 * @tc.desc: Get all activated session descriptors with no permission
158 * @tc.type: FUNC
159 * @tc.require: AR000H31JR
160 */
161 HWTEST_F(AVSessionPermissionTest, GetActivatedSessionDescriptorsWithNoPerm001, TestSize.Level1)
162 {
163 AddPermission(g_infoA, g_policyA);
164 std::vector<AVSessionDescriptor> descriptors;
165 auto ret = AVSessionManager::GetInstance().GetActivatedSessionDescriptors(descriptors);
166 EXPECT_EQ(ret, ERR_NO_PERMISSION);
167 DeletePermission(g_infoA);
168 }
169
170 /**
171 * @tc.name: GetSessionDescriptorsBySessionIdWithNoPerm001
172 * @tc.desc: Get session descriptors by sessionId with no permission
173 * @tc.type: FUNC
174 * @tc.require: AR000H31JR
175 */
176 HWTEST_F(AVSessionPermissionTest, GetSessionDescriptorsBySessionIdWithNoPerm001, TestSize.Level1)
177 {
178 AddPermission(g_infoA, g_policyA);
179 AVSessionDescriptor descriptor {};
180
181 // Using "1" as the test input parameter
182 int32_t ret = AVSessionManager::GetInstance().GetSessionDescriptorsBySessionId("1", descriptor);
183 SLOGI("session can get descriptor point-to itself, but wrong sessionId can not specify any session");
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 SLOGI("check descriptors count for stricter check size %{public}d", static_cast<int>(descriptors.size()));
297 EXPECT_EQ(descriptors.size() >= 0, true);
298 if (session != nullptr) {
299 session->Destroy();
300 }
301 DeletePermission(g_infoB);
302 }
303
304 /**
305 * @tc.name: GetActivatedSessionDescriptorsWithPerm001
306 * @tc.desc: Get all activated session descriptors with permission
307 * @tc.type: FUNC
308 * @tc.require: AR000H31JR
309 */
310 HWTEST_F(AVSessionPermissionTest, GetActivatedSessionDescriptorsWithPerm001, TestSize.Level1)
311 {
312 AddPermission(g_infoB, g_policyB);
313 OHOS::AppExecFwk::ElementName elementName;
314 elementName.SetBundleName("test.ohos.avsession");
315 elementName.SetAbilityName("test.ability");
316 auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
317 ASSERT_NE(session, nullptr);
318 session->Activate();
319
320 std::vector<AVSessionDescriptor> descriptors;
321 auto ret = AVSessionManager::GetInstance().GetActivatedSessionDescriptors(descriptors);
322 EXPECT_EQ(ret, AVSESSION_SUCCESS);
323 SLOGI("check descriptors count for stricter check size %{public}d", static_cast<int>(descriptors.size()));
324 EXPECT_EQ(descriptors.size() >= 0, true);
325
326 if (session != nullptr) {
327 session->Destroy();
328 }
329 DeletePermission(g_infoB);
330 }
331
332 /**
333 * @tc.name: GetHistoricalSessionDescriptorsWithNoPerm001
334 * @tc.desc: Get session descriptors by sessionId with no permission
335 * @tc.type: FUNC
336 * @tc.require: AR000H31JR
337 */
338 HWTEST_F(AVSessionPermissionTest, GetHistoricalSessionDescriptorsWithPerm001, TestSize.Level1)
339 {
340 AddPermission(g_infoB, g_policyB);
341 OHOS::AppExecFwk::ElementName elementName;
342 elementName.SetBundleName("test.ohos.avsession");
343 elementName.SetAbilityName("test.ability");
344 auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
345 ASSERT_NE(session, nullptr);
346
347 std::vector<AVSessionDescriptor> descriptors;
348 auto ret_1 = AVSessionManager::GetInstance().GetHistoricalSessionDescriptors(0, descriptors);
349 EXPECT_EQ(ret_1, AVSESSION_SUCCESS);
350 EXPECT_EQ(descriptors.size() >= 0, true);
351 if (session != nullptr) {
352 session->Destroy();
353 }
354 auto ret_2 = AVSessionManager::GetInstance().GetHistoricalSessionDescriptors(10, descriptors);
355 EXPECT_EQ(ret_2, AVSESSION_SUCCESS);
356 EXPECT_EQ(descriptors.size() >= 0, true);
357 DeletePermission(g_infoB);
358 }
359
360 /**
361 * @tc.name: CreateControllerWithPerm001
362 * @tc.desc: create session controller with permission
363 * @tc.type: FUNC
364 * @tc.require: AR000H31JR
365 */
366 HWTEST_F(AVSessionPermissionTest, CreateControllerWithPerm001, TestSize.Level1)
367 {
368 AddPermission(g_infoB, g_policyB);
369 OHOS::AppExecFwk::ElementName elementName;
370 elementName.SetBundleName("test.ohos.avsession");
371 elementName.SetAbilityName("test.ability");
372 auto session = AVSessionManager::GetInstance().CreateSession("test", AVSession::SESSION_TYPE_AUDIO, elementName);
373 ASSERT_NE(session, nullptr);
374
375 std::shared_ptr<AVSessionController> controller;
376 auto ret = AVSessionManager::GetInstance().CreateController(session->GetSessionId(), controller);
377 EXPECT_EQ(ret, AVSESSION_SUCCESS);
378 EXPECT_NE(controller, nullptr);
379 if (session != nullptr) {
380 session->Destroy();
381 }
382 DeletePermission(g_infoB);
383 }
384
385 /**
386 * @tc.name: RegisterSessionListenerWithPerm001
387 * @tc.desc: register listener with permission
388 * @tc.type: FUNC
389 * @tc.require: AR000H31JQ
390 */
391 HWTEST_F(AVSessionPermissionTest, RegisterSessionListenerWithPerm001, TestSize.Level1)
392 {
393 AddPermission(g_infoB, g_policyB);
394 std::shared_ptr<TestSessionListener> listener = std::make_shared<TestSessionListener>();
395 auto result = AVSessionManager::GetInstance().RegisterSessionListener(listener);
396 EXPECT_EQ(result, AVSESSION_SUCCESS);
397 DeletePermission(g_infoB);
398 }
399
400 /**
401 * @tc.name: SendSystemMediaKeyEventWithPerm001
402 * @tc.desc: valid keyEvent with permission
403 * @tc.type: FUNC
404 * @tc.require: AR000H31JR
405 */
406 HWTEST_F(AVSessionPermissionTest, SendSystemMediaKeyEventWithPerm001, TestSize.Level1)
407 {
408 AddPermission(g_infoB, g_policyB);
409 auto keyEvent = OHOS::MMI::KeyEvent::Create();
410 ASSERT_NE(keyEvent, nullptr);
411 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
412 keyEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
413 keyEvent->SetActionTime(1000);
414 auto keyItem = OHOS::MMI::KeyEvent::KeyItem();
415 keyItem.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_MEDIA_PLAY);
416 keyItem.SetDownTime(1000);
417 keyItem.SetPressed(true);
418 keyEvent->AddKeyItem(keyItem);
419
420 auto result = AVSessionManager::GetInstance().SendSystemAVKeyEvent(*keyEvent);
421 EXPECT_EQ(result, AVSESSION_SUCCESS);
422 DeletePermission(g_infoB);
423 }
424
425 /**
426 * @tc.name: SendSystemControlCommandWithPerm001
427 * @tc.desc: valid command with permission
428 * @tc.type: FUNC
429 * @tc.require: AR000H31JR
430 */
431 HWTEST_F(AVSessionPermissionTest, SendSystemControlCommandWithPerm001, TestSize.Level1)
432 {
433 AddPermission(g_infoB, g_policyB);
434 AVControlCommand command;
435 command.SetCommand(AVControlCommand::SESSION_CMD_PLAY);
436 auto result = AVSessionManager::GetInstance().SendSystemControlCommand(command);
437 EXPECT_EQ(result, AVSESSION_SUCCESS);
438 DeletePermission(g_infoB);
439 }
440 } // namespace AVSession
441 } // namespace OHOS
442