1 /*
2 * Copyright (c) 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 "ability_bundle_event_callback.h"
19 #include "ability_connect_manager.h"
20 #include "ability_manager_service.h"
21 #include "ability_util.h"
22 #include "connection_data.h"
23 #include "interceptor/kiosk_interceptor.h"
24 #include "mock_ability_connect_callback.h"
25 #include "mock_ipc_skeleton.h"
26 #include "mock_my_flag.h"
27 #include "mock_my_status.h"
28 #include "mock_parameters.h"
29 #include "mock_permission_verification.h"
30 #include "mock_test_object.h"
31 #include "mock_scene_board_judgement.h"
32 #include "remote_on_listener_stub_mock.h"
33 #include "session/host/include/session.h"
34 #include "system_ability_definition.h"
35
36 using namespace testing;
37 using namespace testing::ext;
38 using namespace OHOS::AppExecFwk;
39 using OHOS::AppExecFwk::AbilityType;
40 constexpr char KIOSK_WHITE_LIST[] = "KioskWhitelist";
41
42 namespace OHOS {
43 namespace AAFwk {
44 class KioskManagerTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50
51 std::shared_ptr<AbilityRecord> MockAbilityRecord(AbilityType);
52 sptr<Token> MockToken(AbilityType);
53 AbilityRequest GenerateAbilityRequest(const std::string& deviceName, const std::string& abilityName,
54 const std::string& appName, const std::string& bundleName, const std::string& moduleName);
55
56 std::shared_ptr<AbilityRecord> abilityRecord;
57 sptr<SessionInfo> MockSessionInfo(int32_t persistentId);
58 };
59
SetUpTestCase()60 void KioskManagerTest::SetUpTestCase() {}
61
TearDownTestCase()62 void KioskManagerTest::TearDownTestCase() {}
63
SetUp()64 void KioskManagerTest::SetUp() {}
65
TearDown()66 void KioskManagerTest::TearDown() {}
67
MockAbilityRecord(AbilityType abilityType)68 std::shared_ptr<AbilityRecord> KioskManagerTest::MockAbilityRecord(AbilityType abilityType)
69 {
70 AbilityRequest abilityRequest;
71 abilityRequest.appInfo.bundleName = "com.test.demo";
72 abilityRequest.abilityInfo.name = "MainAbility";
73 abilityRequest.abilityInfo.type = abilityType;
74 return AbilityRecord::CreateAbilityRecord(abilityRequest);
75 }
76
MockToken(AbilityType abilityType)77 sptr<Token> KioskManagerTest::MockToken(AbilityType abilityType)
78 {
79 abilityRecord = MockAbilityRecord(abilityType);
80 if (!abilityRecord) {
81 return nullptr;
82 }
83 return abilityRecord->GetToken();
84 }
85
MockSessionInfo(int32_t persistentId)86 sptr<SessionInfo> KioskManagerTest::MockSessionInfo(int32_t persistentId)
87 {
88 sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
89 if (!sessionInfo) {
90 TAG_LOGE(AAFwkTag::TEST, "sessionInfo is nullptr");
91 return nullptr;
92 }
93 sessionInfo->persistentId = persistentId;
94 return sessionInfo;
95 }
96
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName,const std::string & moduleName)97 AbilityRequest KioskManagerTest::GenerateAbilityRequest(const std::string& deviceName,
98 const std::string& abilityName, const std::string& appName, const std::string& bundleName,
99 const std::string& moduleName)
100 {
101 ElementName element(deviceName, bundleName, abilityName, moduleName);
102 Want want;
103 want.SetElement(element);
104
105 AbilityInfo abilityInfo;
106 abilityInfo.visible = true;
107 abilityInfo.applicationName = appName;
108 abilityInfo.type = AbilityType::SERVICE;
109 abilityInfo.name = abilityName;
110 abilityInfo.bundleName = bundleName;
111 abilityInfo.moduleName = moduleName;
112 abilityInfo.deviceId = deviceName;
113 ApplicationInfo appinfo;
114 appinfo.name = appName;
115 abilityInfo.applicationInfo = appinfo;
116 AbilityRequest abilityRequest;
117 abilityRequest.want = want;
118 abilityRequest.abilityInfo = abilityInfo;
119 abilityRequest.appInfo = appinfo;
120 abilityInfo.process = bundleName;
121
122 return abilityRequest;
123 }
124
125 /*
126 * Feature: KioskManager
127 * Function: IsInKioskMode
128 * SubFunction: NA
129 * FunctionPoints: KioskManager IsInKioskMode
130 */
131 HWTEST_F(KioskManagerTest, IsInKioskMode_001, TestSize.Level1) {
132 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
133 bool result = KioskManager::GetInstance().IsInKioskModeInner();
134 EXPECT_EQ(result, false);
135
136 result = KioskManager::GetInstance().IsInKioskMode();
137 EXPECT_EQ(result, false);
138 }
139
140 /*
141 * Feature: KioskManager
142 * Function: IsInKioskMode
143 * SubFunction: NA
144 * FunctionPoints: KioskManager IsInKioskMode
145 */
146 HWTEST_F(KioskManagerTest, IsInKioskMode_002, TestSize.Level1) {
147 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
148 bool result = KioskManager::GetInstance().IsInKioskModeInner();
149 EXPECT_EQ(result, true);
150
151 result = KioskManager::GetInstance().IsInKioskMode();
152 EXPECT_EQ(result, true);
153 }
154
155 /*
156 * Feature: KioskManager
157 * Function: IsInWhiteList
158 * SubFunction: NA
159 * FunctionPoints: KioskManager IsInWhiteList
160 */
161 HWTEST_F(KioskManagerTest, IsInWhiteList_001, TestSize.Level1) {
162 std::string bundleName = "com.test.example";
163 KioskManager::GetInstance().whitelist_.clear();
164 bool result = KioskManager::GetInstance().IsInWhiteListInner(bundleName);
165 EXPECT_EQ(result, false);
166
167 result = KioskManager::GetInstance().IsInWhiteList(bundleName);
168 EXPECT_EQ(result, false);
169 }
170
171 /*
172 * Feature: KioskManager
173 * Function: IsInWhiteList
174 * SubFunction: NA
175 * FunctionPoints: KioskManager IsInWhiteList
176 */
177 HWTEST_F(KioskManagerTest, IsInWhiteList_002, TestSize.Level1) {
178 std::string bundleName = "com.test.example";
179 KioskManager::GetInstance().whitelist_.emplace(bundleName);
180 bool result = KioskManager::GetInstance().IsInWhiteListInner(bundleName);
181 EXPECT_EQ(result, true);
182
183 result = KioskManager::GetInstance().IsInWhiteList(bundleName);
184 EXPECT_EQ(result, true);
185 }
186
187 /*
188 * Feature: KioskManager
189 * Function: ExitKioskMode
190 * SubFunction: NA
191 * FunctionPoints: KioskManager ExitKioskMode
192 */
193 HWTEST_F(KioskManagerTest, ExitKioskMode_001, TestSize.Level1) {
194 MyStatus::GetInstance().paramGetBoolParameter_ = false;
195 auto callerToken = MockToken(AbilityType::PAGE);
196 auto result = KioskManager::GetInstance().ExitKioskMode(callerToken);
197 EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT);
198 }
199
200 /*
201 * Feature: KioskManager
202 * Function: ExitKioskMode
203 * SubFunction: NA
204 * FunctionPoints: KioskManager ExitKioskMode
205 */
206 HWTEST_F(KioskManagerTest, ExitKioskMode_002, TestSize.Level1) {
207 MyStatus::GetInstance().paramGetBoolParameter_ = true;
208 auto result = KioskManager::GetInstance().ExitKioskMode(nullptr);
209 EXPECT_EQ(result, INVALID_PARAMETERS_ERR);
210 }
211
212 /*
213 * Feature: KioskManager
214 * Function: ExitKioskMode
215 * SubFunction: NA
216 * FunctionPoints: KioskManager ExitKioskModeInner
217 */
218 HWTEST_F(KioskManagerTest, ExitKioskModeInner_001, TestSize.Level1) {
219 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
220 auto callerToken = MockToken(AbilityType::PAGE);
221 std::string bundleName = "com.test.example";
222 auto result = KioskManager::GetInstance().ExitKioskModeInner(bundleName, callerToken);
223 EXPECT_EQ(result, ERR_NOT_IN_KIOSK_MODE);
224 }
225
226 /*
227 * Feature: KioskManager
228 * Function: GetKioskStatus
229 * SubFunction: NA
230 * FunctionPoints: KioskManager GetKioskStatus
231 */
232 HWTEST_F(KioskManagerTest, GetKioskStatus_001, TestSize.Level1) {
233 MyStatus::GetInstance().paramGetBoolParameter_ = false;
234 KioskStatus kioskStatus;
235 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
236 EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT);
237 }
238
239 /*
240 * Feature: KioskManager
241 * Function: GetKioskStatus
242 * SubFunction: NA
243 * FunctionPoints: KioskManager GetKioskStatus
244 */
245 HWTEST_F(KioskManagerTest, GetKioskStatus_002, TestSize.Level1) {
246 MyStatus::GetInstance().paramGetBoolParameter_ = true;
247 MyFlag::flag_ = false;
248 MyFlag::saFlag_ = false;
249 MyFlag::permissionFlag_ = false;
250 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
251 KioskStatus kioskStatus;
252 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
253 EXPECT_EQ(result, ERR_NOT_SYSTEM_APP);
254 }
255
256 /*
257 * Feature: KioskManager
258 * Function: GetKioskStatus
259 * SubFunction: NA
260 * FunctionPoints: KioskManager GetKioskStatus
261 */
262 HWTEST_F(KioskManagerTest, GetKioskStatus_003, TestSize.Level1) {
263 MyStatus::GetInstance().paramGetBoolParameter_ = true;
264 MyFlag::flag_ = false;
265 MyFlag::saFlag_ = true;
266 MyFlag::permissionFlag_ = false;
267 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
268 KioskStatus kioskStatus;
269 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
270 EXPECT_EQ(result, ERR_NOT_SYSTEM_APP);
271 }
272
273 /*
274 * Feature: KioskManager
275 * Function: GetKioskStatus
276 * SubFunction: NA
277 * FunctionPoints: KioskManager GetKioskStatus
278 */
279 HWTEST_F(KioskManagerTest, GetKioskStatus_004, TestSize.Level1) {
280 MyStatus::GetInstance().paramGetBoolParameter_ = true;
281 MyFlag::flag_ = false;
282 MyFlag::saFlag_ = true;
283 MyFlag::permissionFlag_ = true;
284 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
285 KioskStatus kioskStatus;
286 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
287 EXPECT_EQ(result, ERR_OK);
288 }
289
290 /*
291 * Feature: KioskManager
292 * Function: GetKioskStatus
293 * SubFunction: NA
294 * FunctionPoints: KioskManager GetKioskStatus
295 */
296 HWTEST_F(KioskManagerTest, GetKioskStatus_005, TestSize.Level1) {
297 MyStatus::GetInstance().paramGetBoolParameter_ = true;
298 MyFlag::flag_ = true;
299 MyFlag::saFlag_ = true;
300 MyFlag::permissionFlag_ = true;
301 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
302 KioskStatus kioskStatus;
303 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
304 EXPECT_EQ(result, ERR_OK);
305 }
306
307 /*
308 * Feature: KioskManager
309 * Function: GetKioskStatus
310 * SubFunction: NA
311 * FunctionPoints: KioskManager GetKioskStatus
312 */
313 HWTEST_F(KioskManagerTest, GetKioskStatus_006, TestSize.Level1) {
314 MyStatus::GetInstance().paramGetBoolParameter_ = true;
315 MyFlag::flag_ = true;
316 MyFlag::saFlag_ = false;
317 MyFlag::permissionFlag_ = true;
318 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
319 KioskStatus kioskStatus;
320 auto result = KioskManager::GetInstance().GetKioskStatus(kioskStatus);
321 EXPECT_EQ(result, ERR_OK);
322 }
323
324 /*
325 * Feature: KioskManager
326 * Function: EnterKioskMode
327 * SubFunction: NA
328 * FunctionPoints: KioskManager EnterKioskMode
329 */
330 HWTEST_F(KioskManagerTest, EnterKioskMode_001, TestSize.Level1) {
331 MyStatus::GetInstance().paramGetBoolParameter_ = false;
332 auto callerToken = MockToken(AbilityType::PAGE);
333 auto result = KioskManager::GetInstance().EnterKioskMode(callerToken);
334 EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT);
335 }
336
337 /*
338 * Feature: KioskManager
339 * Function: EnterKioskMode
340 * SubFunction: NA
341 * FunctionPoints: KioskManager EnterKioskMode
342 */
343 HWTEST_F(KioskManagerTest, EnterKioskMode_002, TestSize.Level1) {
344 MyStatus::GetInstance().paramGetBoolParameter_ = true;
345 auto result = KioskManager::GetInstance().EnterKioskMode(nullptr);
346 EXPECT_EQ(result, INVALID_PARAMETERS_ERR);
347 }
348
349 /*
350 * Feature: KioskManager
351 * Function: UpdateKioskApplicationList
352 * SubFunction: NA
353 * FunctionPoints: KioskManager UpdateKioskApplicationList
354 */
355 HWTEST_F(KioskManagerTest, UpdateKioskApplicationList_001, TestSize.Level1) {
356 MyStatus::GetInstance().paramGetBoolParameter_ = false;
357 std::vector<std::string> appList;
358 auto result = KioskManager::GetInstance().UpdateKioskApplicationList(appList);
359 EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT);
360 }
361
362 /*
363 * Feature: KioskManager
364 * Function: KioskInterceptor
365 * SubFunction: NA
366 * FunctionPoints: KioskInterceptor DoProcess
367 */
368 HWTEST_F(KioskManagerTest, KioskInterceptor_001, TestSize.Level1)
369 {
370 auto kioskInterceptor = std::make_shared<KioskInterceptor>();
371 Want want;
__anon65b9d3600102() 372 auto shouldBlockFunc = []() { return false; };
373 AbilityInterceptorParam param = AbilityInterceptorParam(want, 0, 0, false, nullptr,
374 shouldBlockFunc);
375 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = false;
376 int32_t result = kioskInterceptor->DoProcess(param);
377 EXPECT_EQ(result, ERR_OK);
378 }
379
380 /*
381 * Feature: KioskManager
382 * Function: KioskInterceptor
383 * SubFunction: NA
384 * FunctionPoints: KioskInterceptor DoProcess
385 */
386 HWTEST_F(KioskManagerTest, KioskInterceptor_002, TestSize.Level1)
387 {
388 auto kioskInterceptor = std::make_shared<KioskInterceptor>();
389 Want want;
__anon65b9d3600202() 390 auto shouldBlockFunc = []() { return false; };
391 AbilityInterceptorParam param = AbilityInterceptorParam(want, 0, 0, false, nullptr,
392 shouldBlockFunc);
393 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
394 KioskManager::GetInstance().whitelist_.clear();
395 int32_t result = kioskInterceptor->DoProcess(param);
396 EXPECT_EQ(result, ERR_KIOSK_MODE_NOT_IN_WHITELIST);
397 }
398
399 /*
400 * Feature: KioskManager
401 * Function: KioskInterceptor
402 * SubFunction: NA
403 * FunctionPoints: KioskInterceptor DoProcess
404 */
405 HWTEST_F(KioskManagerTest, KioskInterceptor_003, TestSize.Level1)
406 {
407 auto kioskInterceptor = std::make_shared<KioskInterceptor>();
408 Want want;
409 std::string bundleName = "com.test.example";
410 want.SetElementName(bundleName, "");
__anon65b9d3600302() 411 auto shouldBlockFunc = []() { return false; };
412 AbilityInterceptorParam param = AbilityInterceptorParam(want, 0, 0, false, nullptr,
413 shouldBlockFunc);
414 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
415 KioskManager::GetInstance().whitelist_.emplace(bundleName);
416 int32_t result = kioskInterceptor->DoProcess(param);
417 EXPECT_EQ(result, ERR_OK);
418 }
419
420 /*
421 * Feature: KioskManager
422 * Function: AddKioskInterceptor
423 * SubFunction: NA
424 * FunctionPoints: KioskManager AddKioskInterceptor
425 */
426 HWTEST_F(KioskManagerTest, AddKioskInterceptor_001, TestSize.Level1) {
427 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
428 ASSERT_NE(abilityMgr, nullptr);
429 abilityMgr->InitInterceptor();
430 KioskManager::GetInstance().AddKioskInterceptor();
431 KioskManager::GetInstance().GetEnterKioskModeCallback()();
432 auto interceptorExecuter = abilityMgr->GetAbilityInterceptorExecuter();
433 ASSERT_NE(interceptorExecuter, nullptr);
434 EXPECT_NE(interceptorExecuter->interceptorMap_.count(KIOSK_WHITE_LIST), 0);
435 }
436
437 /*
438 * Feature: KioskManager
439 * Function: AddKioskInterceptor
440 * SubFunction: NA
441 * FunctionPoints: KioskManager AddKioskInterceptor
442 */
443 HWTEST_F(KioskManagerTest, AddKioskInterceptor_002, TestSize.Level1) {
444 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
445 ASSERT_NE(abilityMgr, nullptr);
446 abilityMgr->interceptorExecuter_ = nullptr;
447 KioskManager::GetInstance().AddKioskInterceptor();
448 auto interceptorExecuter = abilityMgr->GetAbilityInterceptorExecuter();
449 ASSERT_EQ(interceptorExecuter, nullptr);
450 }
451
452 /*
453 * Feature: KioskManager
454 * Function: RemoveKioskInterceptor
455 * SubFunction: NA
456 * FunctionPoints: KioskManager RemoveKioskInterceptor
457 */
458 HWTEST_F(KioskManagerTest, RemoveKioskInterceptor_001, TestSize.Level1) {
459 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
460 ASSERT_NE(abilityMgr, nullptr);
461 abilityMgr->InitInterceptor();
462 KioskManager::GetInstance().AddKioskInterceptor();
463 KioskManager::GetInstance().GetExitKioskModeCallback()();
464 KioskManager::GetInstance().RemoveKioskInterceptor();
465 auto interceptorExecuter = abilityMgr->GetAbilityInterceptorExecuter();
466 ASSERT_NE(interceptorExecuter, nullptr);
467 EXPECT_EQ(interceptorExecuter->interceptorMap_.count(KIOSK_WHITE_LIST), 0);
468 }
469
470 /*
471 * Feature: KioskManager
472 * Function: RemoveKioskInterceptor
473 * SubFunction: NA
474 * FunctionPoints: KioskManager RemoveKioskInterceptor
475 */
476 HWTEST_F(KioskManagerTest, RemoveKioskInterceptor_002, TestSize.Level1) {
477 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
478 ASSERT_NE(abilityMgr, nullptr);
479 abilityMgr->interceptorExecuter_ = nullptr;
480 KioskManager::GetInstance().RemoveKioskInterceptor();
481 auto interceptorExecuter = abilityMgr->GetAbilityInterceptorExecuter();
482 ASSERT_EQ(interceptorExecuter, nullptr);
483 }
484
485 /*
486 * Feature: KioskManager
487 * Function: UpdateKioskApplicationList
488 * SubFunction: NA
489 * FunctionPoints: KioskManager UpdateKioskApplicationList
490 */
491 HWTEST_F(KioskManagerTest, UpdateKioskApplicationList_002, TestSize.Level1) {
492 MyStatus::GetInstance().paramGetBoolParameter_ = true;
493 MyFlag::flag_ = true;
494 MyFlag::permissionFlag_ = true;
495 std::vector<std::string> appList;
496 KioskManager::GetInstance().whitelist_.clear();
497 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
498 bool result = KioskManager::GetInstance().IsInKioskModeInner();
499 EXPECT_EQ(result, true);
500 auto ret = KioskManager::GetInstance().UpdateKioskApplicationList(appList);
501 EXPECT_EQ(ret, ERR_KIOSK_MODE_NOT_IN_WHITELIST);
502 }
503
504 /*
505 * Feature: KioskManager
506 * Function: UpdateKioskApplicationList
507 * SubFunction: NA
508 * FunctionPoints: KioskManager UpdateKioskApplicationList
509 */
510 HWTEST_F(KioskManagerTest, UpdateKioskApplicationList_003, TestSize.Level1) {
511 MyStatus::GetInstance().paramGetBoolParameter_ = true;
512 MyFlag::flag_ = true;
513 MyFlag::permissionFlag_ = true;
514 std::vector<std::string> appList;
515 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
516 KioskManager::GetInstance().NotifyKioskModeChanged(true);
517 std::string bundleName = "com.test.demo";
518 appList.emplace_back(bundleName);
519 KioskManager::GetInstance().kioskStatus_.kioskBundleName_ = bundleName;
520 auto ret = KioskManager::GetInstance().UpdateKioskApplicationList(appList);
521 EXPECT_EQ(ret, INNER_ERR);
522 }
523
524 /*
525 * Feature: KioskManager
526 * Function: ExitKioskMode
527 * SubFunction: NA
528 * FunctionPoints: KioskManager ExitKioskModeInner
529 */
530 HWTEST_F(KioskManagerTest, ExitKioskModeInner_002, TestSize.Level1) {
531 KioskManager::GetInstance().kioskStatus_.isKioskMode_ = true;
532 auto callerToken = MockToken(AbilityType::PAGE);
533 std::string bundleName = "com.test.demo";
534 KioskManager::GetInstance().whitelist_.emplace(bundleName);
535 KioskManager::GetInstance().NotifyKioskModeChanged(false);
536 AppInfo appInfo;
537 appInfo.bundleName = bundleName;
538 appInfo.state = AppState::BEGIN;
539 KioskManager::GetInstance().OnAppStop(appInfo);
540 auto result = KioskManager::GetInstance().ExitKioskModeInner(bundleName, callerToken);
541 EXPECT_EQ(result, INNER_ERR);
542 }
543 } // namespace AAFwk
544 } // namespace OHOS
545