diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index d79c567b82e43464b623d1a8631c8cd23ef2f959..ef3b17c9260a23463524cff66e9d69ec90514789 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -584,6 +584,76 @@ ohos_unittest("BundleCheckerTest") { ] } +ohos_unittest("DDMSDumpTest") { + module_out_path = module_output_path + sources = [ + "${data_service_path}/app/src/checker/bundle_checker.cpp", + "${data_service_path}/app/src/checker/system_checker.cpp", + "${data_service_path}/app/src/clone/clone_backup_info.cpp", + "${data_service_path}/app/src/clone/secret_key_backup_data.cpp", + "${data_service_path}/app/src/db_info_handle_impl.cpp", + "${data_service_path}/app/src/feature_stub_impl.cpp", + "${data_service_path}/app/src/installer/installer.cpp", + "${data_service_path}/app/src/installer/installer_impl.cpp", + "${data_service_path}/app/src/kvstore_account_observer.cpp", + "${data_service_path}/app/src/kvstore_data_service.cpp", + "${data_service_path}/app/src/kvstore_data_service_stub.cpp", + "${data_service_path}/app/src/kvstore_device_listener.cpp", + "${data_service_path}/app/src/kvstore_meta_manager.cpp", + "${data_service_path}/app/src/kvstore_screen_observer.cpp", + "${data_service_path}/app/src/security/security.cpp", + "${data_service_path}/app/src/security/sensitive.cpp", + "${data_service_path}/app/src/session_manager/route_head_handler_impl.cpp", + "${data_service_path}/app/src/session_manager/session_manager.cpp", + "${data_service_path}/app/src/session_manager/upgrade_manager.cpp", + "${data_service_path}/app/src/task_manager.cpp", + "${data_service_path}/app/test/unittest/ddms_dump_test.cpp", + "${data_service_path}/service/test/mock/account_delegate_mock.cpp", + "${data_service_path}/service/test/mock/db_store_mock.cpp", + "${data_service_path}/service/test/mock/db_change_data_mock.cpp", + "${data_service_path}/service/test/mock/device_manager_adapter_mock.cpp", +] + sanitize = { + ubsan = true + boundary_sanitize = true + integer_overflow = true + debug = false + blocklist = "${datamgr_service_path}/cfi_blocklist.txt" + } + + configs = [ ":module_private_config" ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "dataclassification:data_transit_mgr", + "device_auth:deviceauth_sdk", + "device_manager:devicemanagersdk", + "file_api:securitylabel", + "googletest:gmock", + "googletest:gtest_main", + "hicollie:libhicollie", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "ipc:ipc_core", + "kv_store:distributeddata_mgr", + "kv_store:distributeddb", + "memmgr:memmgrclient", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + deps = [ + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] + part_name = "datamgr_service" +} ############################################################################### group("unittest") { @@ -595,6 +665,7 @@ group("unittest") { ":FeatureStubImplTest", ":KvStoreDataServiceClearTest", ":KvStoreDataServiceTest", + ":DDMSDumpTest", ":KvStoreMetaManagerUpdateKeyTest", ":SessionManagerTest", ":UpgradeManagerTest", diff --git a/services/distributeddataservice/app/test/unittest/ddms_dump_test.cpp b/services/distributeddataservice/app/test/unittest/ddms_dump_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5292577d870fffc8357cb4467b3023141038fc68 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/ddms_dump_test.cpp @@ -0,0 +1,205 @@ +/* + * 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 "kvstore_data_service.h" +#include "gtest/gtest.h" +#include "account_delegate_mock.h" +#include "db_store_mock.h" +#include "device_manager_adapter.h" +#include "device_manager_adapter_mock.h" +#include "metadata/store_meta_data.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AppDistributedKv; +using namespace OHOS::DistributedData; + +namespace OHOS::Test { +static constexpr const char *TEST_BUNDLE_NAME = "TestApplication"; +static constexpr const char *TEST_STORE_NAME = "TestStore"; +static constexpr const char *TEST_UUID = "ABCD"; + +class DDMSDumpTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static void ConfigSendParameters(bool isCancel); + static void MockUserId(); + static std::string foregroundUserId_; + static std::shared_ptr dbStoreMock_; + static StoreMetaData metaData_; + static DistributedKv::KvStoreDataService dumpTest_; + static inline AccountDelegateMock *accountDelegateMock = nullptr; + static inline std::shared_ptr deviceManagerAdapterMock = nullptr; +}; + +std::string DDMSDumpTest::foregroundUserId_ = "0"; +std::shared_ptr DDMSDumpTest::dbStoreMock_; +DistributedKv::KvStoreDataService DDMSDumpTest::dumpTest_; + +void DDMSDumpTest::SetUpTestCase(void) +{ + dbStoreMock_ = std::make_shared(); + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } + deviceManagerAdapterMock = std::make_shared(); + BDeviceManagerAdapter::deviceManagerAdapter = deviceManagerAdapterMock; + DeviceInfo deviceInfo; + deviceInfo.uuid = TEST_UUID; + EXPECT_CALL(*deviceManagerAdapterMock, GetLocalDevice()).WillRepeatedly(Return(deviceInfo)); +} + +void DDMSDumpTest::TearDownTestCase(void) +{ + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } + deviceManagerAdapterMock = nullptr; + BDeviceManagerAdapter::deviceManagerAdapter = nullptr; +} + +void DDMSDumpTest::SetUp(void) +{} + +void DDMSDumpTest::TearDown(void) +{ + ConfigSendParameters(true); +} + +void DDMSDumpTest::ConfigSendParameters(bool isCancel) +{ + StoreMetaData localMetaData; + localMetaData.deviceId = TEST_UUID; + localMetaData.user = foregroundUserId_; + localMetaData.bundleName = TEST_BUNDLE_NAME; + localMetaData.storeId = TEST_STORE_NAME; + if (isCancel) { + MetaDataManager::GetInstance().DelMeta(localMetaData.GetKeyWithoutPath()); + } else { + MetaDataManager::GetInstance().SaveMeta(localMetaData.GetKeyWithoutPath(), localMetaData); + } +} + +void DDMSDumpTest::MockUserId() +{ + int32_t userId = 0; + if (accountDelegateMock == nullptr) { + return; + } + EXPECT_CALL(*accountDelegateMock, QueryForegroundUserId(userId)).WillOnce(Return(true)); +} + +/** +@tc.name: DumpStoreInfo001 +@tc.desc: test DumpStoreInfo function +@tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpStoreInfo001, TestSize.Level0) +{ + int fd = 0; + std::map> params = {}; + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpStoreInfo(fd, params)); +} + +/** +* @tc.name: DumpBundleInfo001 +* @tc.desc: test DumpBundleInfo function +* @tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpBundleInfo001, TestSize.Level0) +{ + int fd = 0; + std::map> params = {}; + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpBundleInfo(fd, params)); +} + +/** +* @tc.name: DumpStoreInfo002 +* @tc.desc: test DumpStoreInfo function +* @tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpStoreInfo002, TestSize.Level0) +{ + int fd = 1; + std::map> params = {}; + MockUserId(); + ConfigSendParameters(true); + std::vector metas; + auto res = MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ TEST_UUID, foregroundUserId_ }), + metas, true); + EXPECT_EQ(res, false); + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpStoreInfo(fd, params)); +} + +/** +* @tc.name: DumpBundleInfo002 +* @tc.desc: test DumpBundleInfo function +* @tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpBundleInfo002, TestSize.Level0) +{ + int fd = 1; + std::map> params = {}; + MockUserId(); + ConfigSendParameters(true); + std::vector metas; + auto res = MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ TEST_UUID, foregroundUserId_ }), + metas, true); + EXPECT_EQ(res, false); + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpBundleInfo(fd, params)); +} + +/** +* @tc.name: DumpStoreInfo003 +* @tc.desc: test DumpStoreInfo function +* @tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpStoreInfo003, TestSize.Level0) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + int fd = 1; + std::map> params = {}; + MockUserId(); + ConfigSendParameters(false); + std::vector metas; + EXPECT_EQ(MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ TEST_UUID, foregroundUserId_ }), metas, + true), true); + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpStoreInfo(fd, params)); +} + +/** +* @tc.name: DumpBundleInfo003 +* @tc.desc: test DumpBundleInfo function +* @tc.type: FUNC +*/ +HWTEST_F(DDMSDumpTest, DumpBundleInfo003, TestSize.Level0) +{ + int fd = 1; + std::map> params = {}; + MockUserId(); + ConfigSendParameters(false); + std::vector metas; + EXPECT_EQ(MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ TEST_UUID, foregroundUserId_ }), metas, + true), true); + EXPECT_NO_FATAL_FAILURE(dumpTest_.DumpBundleInfo(fd, params)); +} +} \ No newline at end of file