From f5f740b13757c10c86f5dedb94aa01c15a0db634 Mon Sep 17 00:00:00 2001 From: wan-xiaoqing321 Date: Wed, 30 Jul 2025 19:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wan-xiaoqing321 --- .../service/test/BUILD.gn | 41 ++++ .../test/mock/bundlemgr_adapter_mock.cpp | 39 ++++ .../test/mock/bundlemgr_adapter_mock.h | 40 ++++ .../udmf_service_impl_appId_mock_test.cpp | 213 ++++++++++++++++++ 4 files changed, 333 insertions(+) create mode 100644 services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.cpp create mode 100644 services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.h create mode 100644 services/distributeddataservice/service/test/udmf_service_impl_appId_mock_test.cpp diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index ce2389d90..014a05865 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1919,6 +1919,46 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { ] } +ohos_unittest("UdmfServiceImplAppIdMockTest") { + module_out_path = module_output_path + sources = [ + "${data_service_path}/service/test/mock/bundlemgr_adapter_mock.cpp", + "udmf_service_impl_appId_mock_test.cpp", + ] + + include_dirs = [ + "${data_service_path}/service/matrix/include", + "${data_service_path}/service/udmf", + "${data_service_path}/service/test/mock", + "${data_service_path}/service/udmf/store", + "${data_service_path}/framework/include", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + deps = [ + "${data_service_path}/service:distributeddatasvc", + "mock:distributeddata_mock_static", + ] + + external_deps = [ + "ability_runtime:uri_permission_mgr", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "udmf:udmf_client", + "kv_store:distributeddata_inner", + "kv_store:distributeddb", + "kv_store:distributeddata_mgr", + ] +} + ohos_unittest("UdmfCheckerManagerTest") { module_out_path = module_output_path sources = [ @@ -2501,6 +2541,7 @@ group("unittest") { ":UdmfServiceImplDbCorruptionMockTest", ":UdmfServiceStubMockTest", ":UdmfServiceStubTest", + ":UdmfServiceImplAppIdMockTest", ] } diff --git a/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.cpp b/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.cpp new file mode 100644 index 000000000..a5a496b91 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.cpp @@ -0,0 +1,39 @@ +/* +* 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. +*/ + +#ifndef OHOS_BUNDLEMGR_ADAPTER_MOCK_H +#define OHOS_BUNDLEMGR_ADAPTER_MOCK_H +#include "bundlemgr_adapter_mock.h" +#include + +namespace OHOS { +namespace DistributedData { +std::string BundleMgrAdapter::GetAppidFromCache(const std::string &bundleName, int32_t userId) +{ + return MBundleMgrAdapter::GetAppidFromCache(bundleName, userId); +} + +std::string BundleMgrAdapter::GetBundleAppId(const std::string &bundleName) +{ + return MBundleMgrAdapter::GetBundleAppId(bundleName); +} + +std::string BundleMgrAdapter::GetKey(const std::string &bundleName, int32_t userId) +{ + return MBundleMgrAdapter::GetKey(bundleName, userId); +} +} // namespace DistributedData +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.h b/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.h new file mode 100644 index 000000000..f934e4d47 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/bundlemgr_adapter_mock.h @@ -0,0 +1,40 @@ +/* +* 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. +*/ + +#ifndef OHOS_BUNDLEMGR_ADAPTER_MOCK_H +#define OHOS_BUNDLEMGR_ADAPTER_MOCK_H +#include "bundlemgr_adapter.h" +namespace OHOS { +namespace DistributedData { +class MBundleMgrAdapter { +public: + virtual std::string GetAppidFromCache(const std::string &bundleName, int32_t userId) = 0; + virtual std::string GetKey(const std::string &bundleName, int32_t userId) = 0; + virtual std::string GetBundleAppId(const std::string &bundleName) = 0; + MBundleMgrAdapter() = default; + virtual ~MBundleMgrAdapter() = default; +private: + static inline std::shared_ptr mBundleMgrAdapter = nullptr; +}; + +class BundleMgrAdapterMock : public MBundleMgrAdapter { +public: + MOCK_METHOD(std::string, GetAppidFromCache, (const std::string&, int32_t)); + MOCK_METHOD(std::string, GetKey, (const std::string&, int32_t)); + MOCK_METHOD(std::string, GetBundleAppId, (const std::string&)); +}; +} // namespace DistributedData +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_service_impl_appId_mock_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_appId_mock_test.cpp new file mode 100644 index 000000000..6132dbfa4 --- /dev/null +++ b/services/distributeddataservice/service/test/udmf_service_impl_appId_mock_test.cpp @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2023 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. + */ +#define LOG_TAG "UdmfServiceImplAppIdMockTest" +#include "udmf_service_impl.h" +#include "accesstoken_kit.h" +#include "bootstrap.h" +#include "device_manager_adapter.h" +#include "executor_pool.h" +#include "gtest/gtest.h" +#include "ipc_skeleton.h" +#include "kvstore_meta_manager.h" +#include "metadata/meta_data_manager.h" +#include "nativetoken_kit.h" +#include "preprocess_utils.h" +#include "runtime_store.h" +#include "text.h" +#include "plain_text.h" +#include "token_setproc.h" + +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::UDMF; +using namespace OHOS; +namespace OHOS::Test { +constexpr int SLEEP_TIME = 50; // 50 ms +constexpr int BATCH_SIZE_2K = 2000; +constexpr int BATCH_SIZE_5K = 5000; +constexpr double BASE_CONVERSION = 1000.0; +constexpr const char *FILE_SCHEME_PREFIX = "file://"; +class UdmfServiceImplAppIdMockTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + + void SetNativeToken(const std::string &processName); + static void AllocHapToken1(); + void SetHapToken1(); + + void AddPrivilege(QueryOption &option); + static constexpr int USER_ID = 100; + static constexpr int INST_INDEX = 0; +}; + +void UdmfServiceImplAppIdMockTest::SetUpTestCase() +{ + AllocHapToken1(); +} + +void UdmfServiceImplAppIdMockTest::TearDownTestCase() +{ + auto tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo1", INST_INDEX); + AccessTokenKit::DeleteToken(tokenId); +} + +void UdmfServiceImplAppIdMockTest::SetUp() +{ + SetHapToken1(); +} + +void UdmfServiceImplAppIdMockTest::TearDown() +{ + QueryOption query = { .intention = Intention::UD_INTENTION_DATA_HUB }; + std::vector unifiedDataSet; + UdmfClient::GetInstance().DeleteData(query, unifiedDataSet); + query = { .intention = Intention::UD_INTENTION_DRAG }; + UdmfClient::GetInstance().DeleteData(query, unifiedDataSet); +} + +void UdmfServiceImplAppIdMockTest::SetNativeToken(const std::string &processName) +{ + auto tokenId = AccessTokenKit::GetNativeTokenId(processName); + SetSelfTokenID(tokenId); +} + +void UdmfServiceImplAppIdMockTest::AllocHapToken1() +{ + HapInfoParams info = { + .userID = USER_ID, + .bundleName = "ohos.test.demo1", + .instIndex = INST_INDEX, + .appIDDesc = "ohos.test.demo1" + }; + + HapPolicyParams policy = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = { + { + .permissionName = "ohos.permission.test", + .bundleName = "ohos.test.demo1", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "test1", + .descriptionId = 1 + } + }, + .permStateList = { + { + .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } + } + } + }; + auto tokenID = AccessTokenKit::AllocHapToken(info, policy); + SetSelfTokenID(tokenID.tokenIDEx); +} + +void UdmfServiceImplAppIdMockTest::SetHapToken1() +{ + auto tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo1", INST_INDEX); + SetSelfTokenID(tokenId); +} + +void UdmfServiceImplAppIdMockTest::AddPrivilege(QueryOption &option) +{ + Privilege privilege; + privilege.tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo2", INST_INDEX); + privilege.readPermission = "readPermission"; + privilege.writePermission = "writePermission"; + SetNativeToken("msdp_sa"); + auto status = UdmfClient::GetInstance().AddPrivilege(option, privilege); + ASSERT_EQ(status, E_OK); +} + +/** +* @tc.name: UpdateData002 +* @tc.desc: Update data with valid params +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplAppIdMockTest, UpdateData002, TestSize.Level1) +{ + UnifiedData data; + auto plainText = std::make_shared(); + plainText->SetContent("content"); + data.AddRecord(plainText); + + CustomOption customOption = { .intention = Intention::UD_INTENTION_DATA_HUB }; + UnifiedData data1; + auto plainText1 = std::make_shared<PlainText>(); + plainText1->SetContent("content1"); + data1.AddRecord(plainText1); + std::string key; + auto status = UdmfClient::GetInstance().SetData(customOption, data1, key); + + ASSERT_EQ(status, E_OK); + QueryOption queryOption = { .key = key }; + SetHapToken2(); + status = UdmfClient::GetInstance().UpdateData(queryOption, data); + ASSERT_EQ(status, E_INVALID_PARAMETERS); + + SetHapToken1(); + status = UdmfClient::GetInstance().UpdateData(queryOption, data); + ASSERT_EQ(status, E_OK); + + std::vector<UnifiedData> dataSet; + status = UdmfClient::GetInstance().GetBatchData(queryOption, dataSet); + std::shared_ptr<UnifiedRecord> record2 = dataSet[0].GetRecordAt(0); + ASSERT_NE(record2, nullptr); + auto type = record2->GetType(); + ASSERT_EQ(type, UDType::PLAIN_TEXT); + auto plainText2 = static_cast<PlainText *>(record2.get()); + ASSERT_EQ(plainText2->GetContent(), "content"); + +} + +/** +* @tc.name: DeleteData002 +* @tc.desc: Delete data with valid params +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplAppIdMockTest, DeleteData002, TestSize.Level1) +{ + + CustomOption customOption = { .intention = UD_INTENTION_DATA_HUB }; + UnifiedData data; + auto plainText = std::make_shared<PlainText>(); + plainText->SetContent("content1"); + data.AddRecord(plainText); + std::string key; + auto status = UdmfClient::GetInstance().SetData(customOption, data, key); + ASSERT_EQ(status, E_OK); + status = UdmfClient::GetInstance().SetData(customOption, data, key); + ASSERT_EQ(status, E_OK); + status = UdmfClient::GetInstance().SetData(customOption, data, key); + ASSERT_EQ(status, E_OK); + + QueryOption queryOption = { .key = key }; + + SetHapToken1(); + status = UdmfClient::GetInstance().DeleteData(queryOption, unifiedDataSet); + ASSERT_EQ(status, E_OK); + ASSERT_EQ(1, unifiedDataSet.size()); +} +} // OHOS::Test \ No newline at end of file -- Gitee