diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 4f5321129ba37fa0653758e427019d5145762c17..c9b5125a2e7cee3cad54c92e9948dbc5ea4e2a5d 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -67,6 +67,9 @@ if (is_standard_system && ability_base_enable == true) { "src/record/permission_record_repository.cpp", "src/record/permission_used_record_cache.cpp", "src/sensitive/application_status_change_callback.cpp", + "src/sensitive/camera_float_window_change_callback.cpp", + "src/sensitive/camera_global_switch_change_callback.cpp", + "src/sensitive/mic_global_switch_change_callback.cpp", "src/sensitive/sensitive_resource_manager.cpp", "src/service/privacy_manager_service.cpp", "src/service/privacy_manager_stub.cpp", diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..46ff47f55e5d748510ae17dd87129ae39954ae5a --- /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/camera_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..efc7909252f8cfe2e3d30501d660ab5236f531e8 --- /dev/null +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -0,0 +1,136 @@ +/* + * 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; + +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 camera_isShowing = false; +static void OnChangeCameraGlobalSwitch(bool isShowing) +{ + GTEST_LOG_(INFO) << " OnChangeCameraGlobalSwitch isShowing: " << camera_isShowing; + camera_isShowing = isShowing; + GTEST_LOG_(INFO) << " camera_isShowing: " << camera_isShowing; +} + +static void ResetEnv() +{ + camera_isShowing = false; +} + +/** + * @tc.name: RegisterCameraGlobalSwitchChangeCallbackTest_001 + * @tc.desc: call RegisterCameraGlobalSwitchChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWXA + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterCameraGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().RegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch); + + ASSERT_EQ(false, camera_isShowing); + + ResetEnv(); + + CameraStandard::CameraManager::GetInstance()->SetCameraMute(true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, camera_isShowing); + + ResetEnv(); + + CameraStandard::CameraManager::GetInstance()->SetCameraMute(false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, camera_isShowing); + + ResetEnv(); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch); +} + + +/** + * @tc.name: RegisterCameraGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the RegisterCameraGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterCameraGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterCameraGlobalSwitchChangeCallback(nullptr)); + + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch)); + + SensitiveResourceManager::GetInstance().UnRegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch); +} + +/** + * @tc.name: UnRegisterCameraGlobalSwitchChangeCallbackTest_001 + * @tc.desc: Verify the UnRegisterCameraGlobalSwitchChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWXA + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch)); +} + +/** + * @tc.name: UnRegisterCameraGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the UnRegisterCameraGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraGlobalSwitchChangeCallback(nullptr)); + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraGlobalSwitchChangeCallback(OnChangeCameraGlobalSwitch)); +} \ No newline at end of file