From f926223d9420e1149d9a8e46ff2d8144240b3369 Mon Sep 17 00:00:00 2001 From: wangchen Date: Tue, 17 Jun 2025 21:39:33 +0800 Subject: [PATCH] mock media test close #ICFW67 Signed-off-by: wangchen --- services/sandbox_manager/test/BUILD.gn | 13 +- .../include/media_library_extend_manager.h | 64 +++++ .../test/mock/src/media_path_mock.cpp | 102 +++++++ .../test/unittest/media_path_mock_test.cpp | 252 ++++++++++++++++++ 4 files changed, 429 insertions(+), 2 deletions(-) create mode 100644 services/sandbox_manager/test/mock/include/media_library_extend_manager.h create mode 100644 services/sandbox_manager/test/mock/src/media_path_mock.cpp create mode 100644 services/sandbox_manager/test/unittest/media_path_mock_test.cpp diff --git a/services/sandbox_manager/test/BUILD.gn b/services/sandbox_manager/test/BUILD.gn index b2b9d8f..43ba231 100644 --- a/services/sandbox_manager/test/BUILD.gn +++ b/services/sandbox_manager/test/BUILD.gn @@ -28,6 +28,14 @@ sandbox_manager_service_src = [ "../main/cpp/src/media/media_path_support.cpp", ] +sandbox_manager_service_mock_src = [ + "mock/src/media_path_mock.cpp" +] + +sandbox_manager_service_mock_include = [ + "mock/include" +] + ohos_unittest("libsandbox_manager_service_standard_test") { subsystem_name = "accesscontrol" module_out_path = "sandbox_manager/sandbox_manager" @@ -51,13 +59,16 @@ ohos_unittest("libsandbox_manager_service_standard_test") { "${sandbox_manager_path}/services/sandbox_manager/main/cpp/include/mac", "${sandbox_manager_path}/services/sandbox_manager/main/cpp/include/media", ] + include_dirs += sandbox_manager_service_mock_include sources = [ "unittest/policy_info_manager_test.cpp", "unittest/sandbox_manager_rdb_test.cpp", "unittest/sandbox_manager_service_test.cpp", + "unittest/media_path_mock_test.cpp", ] sources += sandbox_manager_service_src + sources += sandbox_manager_service_mock_src configs = [ "${sandbox_manager_path}/config:coverage_flags" ] @@ -84,12 +95,10 @@ ohos_unittest("libsandbox_manager_service_standard_test") { "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", - "data_share:datashare_consumer", "eventhandler:libeventhandler", "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "media_library:media_library_extend_manager", "os_account:os_account_innerkits", "relational_store:native_rdb", "safwk:system_ability_fwk", diff --git a/services/sandbox_manager/test/mock/include/media_library_extend_manager.h b/services/sandbox_manager/test/mock/include/media_library_extend_manager.h new file mode 100644 index 0000000..f489dce --- /dev/null +++ b/services/sandbox_manager/test/mock/include/media_library_extend_manager.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025-2025 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. + */ +#ifndef MEDIA_LIBRARY_EXTEND_MANAGER_H +#define MEDIA_LIBRARY_EXTEND_MANAGER_H + +#include + +namespace OHOS { +namespace Media { +enum class PhotoPermissionType : int32_t { + TEMPORARY_READ_IMAGEVIDEO = 0, + PERSIST_READ_IMAGEVIDEO, + TEMPORARY_WRITE_IMAGEVIDEO, + TEMPORARY_READWRITE_IMAGEVIDEO, + PERSIST_READWRITE_IMAGEVIDEO, // Internal reserved value, not open to the public + PERSIST_WRITE_IMAGEVIDEO, + GRANT_PERSIST_READWRITE_IMAGEVIDEO, +}; + +enum class HideSensitiveType : int32_t { + ALL_DESENSITIZE = 0, + GEOGRAPHIC_LOCATION_DESENSITIZE, + SHOOTING_PARAM_DESENSITIZE, + NO_DESENSITIZE +}; + +enum class OperationMode : uint32_t { + READ_MODE = 0b01, + WRITE_MODE = 0b10, + READ_WRITE_MODE = 0b11, +}; + +class MediaLibraryExtendManager { +public: + MediaLibraryExtendManager() = default; + virtual ~MediaLibraryExtendManager() = default; + static MediaLibraryExtendManager *GetMediaLibraryExtendManager(); + void InitMediaLibraryExtendManager(); + int32_t CheckPhotoUriPermission(uint32_t tokenId, + const std::vector &urisSource, std::vector &result, const std::vector &flags); + int32_t GrantPhotoUriPermission(uint32_t srcTokenId, uint32_t targetTokenId, const std::vector &uris, + const std::vector &photoPermissionTypes, HideSensitiveType hideSensitiveTpye); + int32_t CancelPhotoUriPermission(uint32_t srcTokenId, uint32_t targetTokenId, + const std::vector &uris, const bool persistFlag = false, + const std::vector &mode = {OperationMode::READ_WRITE_MODE}); + int32_t GetPhotoUrisPermission(uint32_t targetTokenld, const std::vector &uris, + const std::vector &photoPermissionTypes, std::vector &result); + int32_t GetUrisFromFusePaths(const std::vector paths, std::vector &uris); +}; +} // namespace Media +} // namespace OHOS +#endif // MEDIA_LIBRARY_EXTEND_MANAGER_H \ No newline at end of file diff --git a/services/sandbox_manager/test/mock/src/media_path_mock.cpp b/services/sandbox_manager/test/mock/src/media_path_mock.cpp new file mode 100644 index 0000000..f0fdf6f --- /dev/null +++ b/services/sandbox_manager/test/mock/src/media_path_mock.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2025 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 "media_library_extend_manager.h" +namespace OHOS { +namespace Media { +MediaLibraryExtendManager *MediaLibraryExtendManager::GetMediaLibraryExtendManager() +{ + static MediaLibraryExtendManager mediaLibMgr; + return &mediaLibMgr; +} + +void MediaLibraryExtendManager::InitMediaLibraryExtendManager() +{ + return; +} + +constexpr const char* MEDIA_PATH_1 = "/data/storage/el2/media/Photo/1/1/1.jpg"; +constexpr const char* MEDIA_PATH_2 = "/data/storage/el2/media/Photo/2/2/2.jpg"; +// For coverage, when input path 3 , will return no policy +constexpr const char* MEDIA_PATH_3 = "/data/storage/el2/media/Photo/3/3/3.jpg"; +// For coverage, when input path 4, will return failed, and no need to define path 4 here + +/* for mock + * path 1 or 2, will return 0 and result = true; + * path 3, will return 0, but result = false; + * path 4, will return -1; + */ +int32_t MediaLibraryExtendManager::CheckPhotoUriPermission(uint32_t tokenId, + const std::vector &urisSource, std::vector &result, const std::vector &flags) +{ + for (size_t i = 0; i < urisSource.size(); ++i) { + const std::string &uri = urisSource[i]; + if ((uri == MEDIA_PATH_1) || ((uri == MEDIA_PATH_2))) { + result[i] = true; + } else if (uri == MEDIA_PATH_3) { + result[i] = false; + } else { + return -1; + } + } + return 0; +} + +/* for mock + * path 1 or 2, will return 0 and result = true; + * path 3, will return 0, but result = false; + * path 4, will return -1; + */ +int32_t MediaLibraryExtendManager::GetPhotoUrisPermission(uint32_t targetTokenld, const std::vector &uris, + const std::vector &photoPermissionTypes, std::vector &result) +{ + for (size_t i = 0; i < uris.size(); ++i) { + const std::string &uri = uris[i]; + if ((uri == MEDIA_PATH_1) || ((uri == MEDIA_PATH_2))) { + result[i] = true; + } else if (uri == MEDIA_PATH_3) { + result[i] = false; + } else { + return -1; + } + } + return 0; +} + +/* for mock + * only mock, the URI format is not reused + */ +int32_t MediaLibraryExtendManager::GetUrisFromFusePaths(const std::vector paths, + std::vector &uris) +{ + for (const auto& path : paths) { + uris.push_back(path); + } + return 0; +} + +int32_t MediaLibraryExtendManager::GrantPhotoUriPermission(uint32_t srcTokenId, uint32_t targetTokenId, + const std::vector &uris, const std::vector &photoPermissionTypes, + HideSensitiveType hideSensitiveTpye) +{ + return 0; +} + +int32_t MediaLibraryExtendManager::CancelPhotoUriPermission(uint32_t srcTokenId, uint32_t targetTokenId, + const std::vector &uris, const bool persistFlag, const std::vector &operationModes) +{ + return 0; +} +} +} \ No newline at end of file diff --git a/services/sandbox_manager/test/unittest/media_path_mock_test.cpp b/services/sandbox_manager/test/unittest/media_path_mock_test.cpp new file mode 100644 index 0000000..83fb17b --- /dev/null +++ b/services/sandbox_manager/test/unittest/media_path_mock_test.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2023-2025 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 +#include +#include +#include +#include +#include "access_token.h" +#include "accesstoken_kit.h" +#include "generic_values.h" +#include "hap_token_info.h" +#include "mac_adapter.h" +#include "policy_field_const.h" +#include "policy_info.h" +#define private public +#include "policy_info_manager.h" +#undef private +#include "sandbox_manager_const.h" +#include "sandbox_manager_log.h" +#include "sandbox_manager_err_code.h" + +using namespace testing::ext; + +namespace OHOS { +namespace AccessControl { +namespace SandboxManager { +class MediaPathMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + uint32_t selfTokenId_; +}; + +constexpr const char* MEDIA_PATH_1 = "/data/storage/el2/media/Photo/1/1/1.jpg"; +constexpr const char* MEDIA_PATH_2 = "/data/storage/el2/media/Photo/2/2/2.jpg"; +// For coverage, when input this path , will return no policy +constexpr const char* MEDIA_PATH_3 = "/data/storage/el2/media/Photo/3/3/3.jpg"; +// For coverage, when input this path , will return failed +constexpr const char* MEDIA_PATH_4 = "/data/storage/el2/media/Photo/4/4/4.jpg"; + +void MediaPathMockTest::SetUpTestCase(void) +{} + +void MediaPathMockTest::TearDownTestCase(void) +{} + +void MediaPathMockTest::SetUp(void) +{ + selfTokenId_ = 0; + PolicyInfoManager::GetInstance().Init(); +} + +void MediaPathMockTest::TearDown(void) +{} + +#ifdef DEC_ENABLED +/** + * @tc.name: MediaPathMockTest001 + * @tc.desc: Test AddPolicy - normal cases + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(MediaPathMockTest, MediaPathMockTest001, TestSize.Level0) +{ + PolicyInfo info; + uint64_t sizeLimit = 1; + std::vector policy; + policy.emplace_back(info); + + info.path = MEDIA_PATH_1; + info.mode = OperateMode::READ_MODE; + policy[0] = info; + std::vector result11; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result11)); + EXPECT_EQ(sizeLimit, result11.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result11[0]); + std::vector result21; + EXPECT_EQ(SANDBOX_MANAGER_OK, + PolicyInfoManager::GetInstance().StartAccessingPolicy(selfTokenId_, policy, result21)); + ASSERT_EQ(sizeLimit, result21.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result21[0]); + + info.path = MEDIA_PATH_1; + info.mode = OperateMode::WRITE_MODE; + policy[0] = info; + std::vector result12; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result12)); + EXPECT_EQ(sizeLimit, result12.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result12[0]); + + std::vector result22; + EXPECT_EQ(SANDBOX_MANAGER_OK, + PolicyInfoManager::GetInstance().StopAccessingPolicy(selfTokenId_, policy, result22)); + ASSERT_EQ(sizeLimit, result22.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result22[0]); + + info.path = MEDIA_PATH_2; + info.mode = OperateMode::READ_MODE + OperateMode::WRITE_MODE; + policy[0] = info; + std::vector result13; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result13)); + EXPECT_EQ(sizeLimit, result13.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result13[0]); + + std::vector result33; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().RemovePolicy(selfTokenId_, policy, result33)); + ASSERT_EQ(sizeLimit, result33.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result33[0]); +} +#endif + +#ifdef DEC_ENABLED +/** + * @tc.name: MediaPathMockTest002 + * @tc.desc: Test AddPolicy - normal cases 2 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(MediaPathMockTest, MediaPathMockTest002, TestSize.Level0) +{ + PolicyInfo info; + uint64_t sizeLimit = 1; + std::vector policy; + policy.emplace_back(info); + + info.path = MEDIA_PATH_1; + info.mode = OperateMode::READ_MODE; + policy[0] = info; + std::vector result11; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result11)); + EXPECT_EQ(sizeLimit, result11.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result11[0]); + + std::vector result21; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().MatchPolicy(selfTokenId_, policy, result21)); + ASSERT_EQ(sizeLimit, result21.size()); + EXPECT_EQ(SandboxRetType::OPERATE_SUCCESSFULLY, result21[0]); +} +#endif + +#ifdef DEC_ENABLED +/** + * @tc.name: MediaPathMockTest002 + * @tc.desc: Test AddPolicy - normal cases 2 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(MediaPathMockTest, MediaPathMockTest003, TestSize.Level0) +{ + PolicyInfo info; + uint64_t sizeLimit = 1; + std::vector policy; + policy.emplace_back(info); + + info.path = MEDIA_PATH_3; + info.mode = OperateMode::READ_MODE; + policy[0] = info; + std::vector result11; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result11)); + EXPECT_EQ(sizeLimit, result11.size()); + EXPECT_EQ(SandboxRetType::FORBIDDEN_TO_BE_PERSISTED, result11[0]); + + std::vector result21; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().MatchPolicy(selfTokenId_, policy, result21)); + ASSERT_EQ(sizeLimit, result21.size()); + EXPECT_EQ(SandboxRetType::POLICY_HAS_NOT_BEEN_PERSISTED, result21[0]); + + std::vector result33; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().RemovePolicy(selfTokenId_, policy, result33)); + ASSERT_EQ(sizeLimit, result33.size()); + EXPECT_EQ(SandboxRetType::POLICY_HAS_NOT_BEEN_PERSISTED, result33[0]); +} +#endif + +#ifdef DEC_ENABLED +/** + * @tc.name: MediaPathMockTest004 + * @tc.desc: Test AddPolicy - Abnormal branch + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(MediaPathMockTest, MediaPathMockTest004, TestSize.Level0) +{ + PolicyInfo info; + std::vector policy; + policy.emplace_back(info); + + info.path = MEDIA_PATH_4; + info.mode = OperateMode::READ_MODE; + policy[0] = info; + std::vector result11; + EXPECT_EQ(SANDBOX_MANAGER_MEDIA_CALL_ERR, + PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result11)); + std::vector result21; + EXPECT_EQ(SANDBOX_MANAGER_MEDIA_CALL_ERR, + PolicyInfoManager::GetInstance().MatchPolicy(selfTokenId_, policy, result21)); + std::vector result31; + EXPECT_EQ(SANDBOX_MANAGER_MEDIA_CALL_ERR, + PolicyInfoManager::GetInstance().RemovePolicy(selfTokenId_, policy, result31)); +} +#endif + +#ifdef DEC_ENABLED +/** + * @tc.name: MediaPathMockTest005 + * @tc.desc: Test AddPolicy - Abnormal branch + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(MediaPathMockTest, MediaPathMockTest005, TestSize.Level0) +{ + PolicyInfo info; + uint64_t sizeLimit = 1; + std::vector policy; + policy.emplace_back(info); + + info.path = MEDIA_PATH_1; + info.mode = OperateMode::READ_MODE + OperateMode::WRITE_MODE + OperateMode::READ_MODE + OperateMode::WRITE_MODE; + policy[0] = info; + std::vector result11; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().AddPolicy(selfTokenId_, policy, result11)); + EXPECT_EQ(sizeLimit, result11.size()); + EXPECT_EQ(SandboxRetType::INVALID_MODE, result11[0]); + + std::vector result21; + EXPECT_EQ(INVALID_PARAMTER, PolicyInfoManager::GetInstance().MatchPolicy(selfTokenId_, policy, result21)); + + std::vector result31; + EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().RemovePolicy(selfTokenId_, policy, result31)); + EXPECT_EQ(sizeLimit, result11.size()); + EXPECT_EQ(SandboxRetType::INVALID_MODE, result31[0]); +} +#endif +} // SandboxManager +} // AccessControl +} // OHOS \ No newline at end of file -- Gitee