diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 98186e63161fa33680b20e7cd5a45b3b994ecc27..4f5321129ba37fa0653758e427019d5145762c17 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -99,4 +99,4 @@ if (is_standard_system && ability_base_enable == true) { "window_manager:libwm", ] } -} \ No newline at end of file +} diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ebcf7e51e0d97f32a5c2355214f229c5a9fd6133 --- /dev/null +++ b/services/privacymanager/test/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/security/access_token/access_token.gni") +import("//build/test.gni") + +ohos_unittest("sensitive_resource_manager_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/frameworks/privacy/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", + "//base/security/access_token/services/common/database/include", + "//base/global/resource_management/interfaces/inner_api/include", + "//base/security/access_token/services/privacymanager/include/sensitive", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/token_setproc/include", + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime", + "//foundation/arkui/napi", + "//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiomanager/include", + "//foundation/window/window_manager/wm/include", + "//third_party/googletest/include", + ] + + sources = [ + "//base/security/access_token/services/privacymanager/src/sensitive/application_status_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp", + "unittest/sensitive_resource_manager_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + if (dlp_permission_enable == true) { + cflags_cc += [ "-DSUPPORT_SANDBOX_APP" ] + } + + configs = [ "//base/security/access_token/config:coverage_flags" ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + "//base/security/access_token/services/privacymanager:privacymanager", + "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + "//foundation/ability/ability_runtime/services/abilitymgr:abilityms_target", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "multimedia_audio_framework:audio_client", + "samgr:samgr_proxy", + "window_manager:libwm", + ] +} + +group("unittest") { + testonly = true + deps = [ ":sensitive_resource_manager_test" ] +} diff --git a/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ecaf906f2eb0801713b7df73415e3fb6ac16089 --- /dev/null +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "ability_context_impl.h" +#include "accesstoken_kit.h" +#include "audio_system_manager.h" +#include "sensitive_resource_manager.h" +#include "token_setproc.h" +#include "window.h" +#include "window_scene.h" +#include "wm_common.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::AccessToken; + +class SensitiveResourceManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void SensitiveResourceManagerTest::SetUpTestCase() +{ +} + +void SensitiveResourceManagerTest::TearDownTestCase() +{ +} + +void SensitiveResourceManagerTest::SetUp() +{ + SensitiveResourceManager::GetInstance().Init(); +} + +void SensitiveResourceManagerTest::TearDown() +{ +} + +/** + * @tc.name: GetGlobalSwitchTest_001 + * @tc.desc: Verify the GetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); +} + +/** + * @tc.name: GetGlobalSwitchTest_002 + * @tc.desc: Verify the GetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_002, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::INVALID)); +} + +/** + * @tc.name: SetGlobalSwitchTest_001 + * @tc.desc: Verify the SetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); +} + +/** + * @tc.name: SetGlobalSwitchTest_002 + * @tc.desc: Verify the SetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_002, TestSize.Level1) +{ + bool isMicrophoneMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); +} \ No newline at end of file