diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..cf8ead89c57121e3c733e937057b2ee2b24d776c --- /dev/null +++ b/services/privacymanager/test/BUILD.gn @@ -0,0 +1,85 @@ +# 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/mic_global_switch_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..3a455f71a4c666cbddde935ec39aa43d0174d448 --- /dev/null +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -0,0 +1,155 @@ +/* + * 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::Rosen; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::AudioStandard; + +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() +{ +} + +static bool micglo_isMute = true; +static void OnChangeMicGlobalSwitch(bool isMute) +{ + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch micglo_isMute: " << micglo_isMute; + micglo_isMute = isMute; +} + +static void ResetEnv() +{ + micglo_isMute = true; +} + +/** + * @tc.name: MicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: call RegisterMicGlobalSwitchChangeCallback once. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, MicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); + + bool isMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, micglo_isMute); + + ResetEnv(); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, micglo_isMute); + + ResetEnv(); + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(isMute); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} + + +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the RegisterMicGlobalSwitchChangeCallback abnormal branch callback is nullptr. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(nullptr)); +} + +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_003 + * @tc.desc: Verify the RegisterMicGlobalSwitchChangeCallback abnormal branch callback is already registered. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_003, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} + +/** + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); +} + +/** + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback abnormal branch callback is nullptr. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(nullptr)); +} + +/** + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_003 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_003, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); +}