diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index d79c567b82e43464b623d1a8631c8cd23ef2f959..8c55efb5df0c2e0b8aaf615cf4125b9cdeb41d71 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -115,6 +115,7 @@ ohos_unittest("KvStoreDataServiceTest") { "device_auth:deviceauth_sdk", "device_manager:devicemanagersdk", "file_api:securitylabel", + "googletest:gmock", "googletest:gtest_main", "hicollie:libhicollie", "hilog:libhilog", diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp index 18167c5daefe90f0dbaddd421be0b87de963782e..ac0b9e412a7e99cc025eafd57212856778be2d2b 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp @@ -13,12 +13,14 @@ * limitations under the License. */ +#include "account_delegate_mock.h" #include "auth_delegate.h" #include "bootstrap.h" #include "crypto/crypto_manager.h" #include "device_manager_adapter.h" #include "executor_pool.h" #include +#include "meta_data_manager_mock.h" #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include @@ -32,6 +34,7 @@ #include "upgrade_manager.h" #include "utils/base64_utils.h" +using namespace testing; using namespace testing::ext; using namespace OHOS; using namespace OHOS::DistributedKv; @@ -59,18 +62,40 @@ const std::string NORMAL_BACKUP_DATA = "\"dbType\":\"1\",\"user\":\"0\",\"sKey\":\"9aJQwx3XD3EN7To2j/" "I9E9MCzn2+6f/bBqFjOPcY+1pRgx/" "XI6jXedyuzEEVdwrc\",\"time\":[50,180,137,103,0,0,0,0]}]}"; +static constexpr const char *TEST_BUNDLE_NAME = "TestApplication"; +static constexpr const char *TEST_STORE_NAME = "TestStore"; + class KvStoreDataServiceTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); + static void ConfigSendParameters(bool isCancel); static constexpr int32_t TEST_USERID = 100; static constexpr int32_t TEST_APP_INDEX = 0; static constexpr int32_t TEST_TOKENID = 100; std::string TEST_INDENTATION = " "; + static DeviceId deviceId_; + static ExtraDataInfo extraInfo_; + static DataInfo dataInfo_; + static MessageInfo msgInfo_; + static std::string foregroundUserId_; + static AppDistributedKv::DeviceInfo localDeviceInfo_; + static AppDistributedKv::DeviceInfo remoteDeviceInfo_; + static std::shared_ptr dbStoreMock_; + static StoreMetaData metaData_; + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; +DeviceId KvStoreDataServiceTest::deviceId_; +ExtraDataInfo KvStoreDataServiceTest::extraInfo_; +DataInfo KvStoreDataServiceTest::dataInfo_; +std::string KvStoreDataServiceTest::foregroundUserId_; +AppDistributedKv::DeviceInfo KvStoreDataServiceTest::localDeviceInfo_; +AppDistributedKv::DeviceInfo KvStoreDataServiceTest::remoteDeviceInfo_; +std::shared_ptr KvStoreDataServiceTest::dbStoreMock_; + void KvStoreDataServiceTest::SetUpTestCase(void) { mode_t mode = S_IRWXU | S_IRWXG | S_IXOTH; // 0771 @@ -82,6 +107,11 @@ void KvStoreDataServiceTest::SetUpTestCase(void) KvStoreMetaManager::GetInstance().BindExecutor(executors); KvStoreMetaManager::GetInstance().InitMetaParameter(); KvStoreMetaManager::GetInstance().InitMetaListener(); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } } void KvStoreDataServiceTest::TearDownTestCase(void) @@ -94,7 +124,51 @@ void KvStoreDataServiceTest::SetUp(void) {} void KvStoreDataServiceTest::TearDown(void) -{} +{ + ConfigSendParameters(true); + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } +} + +void KvStoreDataServiceTest::ConfigSendParameters(bool isCancel) +{ + deviceId_.deviceId = isCancel ? "" : remoteDeviceInfo_.uuid; + + extraInfo_.userId = isCancel ? "" : foregroundUserId_; + extraInfo_.bundleName = isCancel ? "" : TEST_BUNDLE_NAME; + extraInfo_.storeId = isCancel ? "" : TEST_STORE_NAME; + + dataInfo_.extraInfo.userId = isCancel ? "" : foregroundUserId_; + dataInfo_.extraInfo.appId = isCancel ? "" : TEST_BUNDLE_NAME; + dataInfo_.extraInfo.storeId = isCancel ? "" : TEST_STORE_NAME; + + StoreMetaData localMetaData; + localMetaData.deviceId = localDeviceInfo_.uuid; + localMetaData.user = foregroundUserId_; + localMetaData.bundleName = TEST_BUNDLE_NAME; + localMetaData.storeId = TEST_STORE_NAME; + + StoreMetaData remoteMetaData; + remoteMetaData.deviceId = remoteDeviceInfo_.uuid; + remoteMetaData.user = foregroundUserId_; + remoteMetaData.bundleName = TEST_BUNDLE_NAME; + remoteMetaData.storeId = TEST_STORE_NAME; + + if (isCancel) { + MetaDataManager::GetInstance().DelMeta(TEST_BUNDLE_NAME, true); + MetaDataManager::GetInstance().DelMeta(localMetaData.GetKeyWithoutPath()); + MetaDataManager::GetInstance().DelMeta(remoteMetaData.GetKeyWithoutPath()); + } else { + AppIDMetaData appIdMeta; + appIdMeta.appId = TEST_BUNDLE_NAME; + appIdMeta.bundleName = TEST_BUNDLE_NAME; + MetaDataManager::GetInstance().SaveMeta(TEST_BUNDLE_NAME, appIdMeta, true); + MetaDataManager::GetInstance().SaveMeta(localMetaData.GetKeyWithoutPath(), localMetaData); + MetaDataManager::GetInstance().SaveMeta(remoteMetaData.GetKeyWithoutPath(), remoteMetaData); + } +} class UpgradeManagerTest : public testing::Test { public: @@ -488,6 +562,38 @@ HWTEST_F(KvStoreDataServiceTest, DumpStoreInfo001, TestSize.Level0) EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpStoreInfo(fd, params)); } +/** +* @tc.name: DumpStoreInfo002 +* @tc.desc: test DumpStoreInfo function +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KvStoreDataServiceTest, DumpStoreInfo002, TestSize.Level0) +{ + KvStoreDataService kvStoreDataServiceTest; + int fd = 0; + std::map> params = {}; + ConfigSendParameters(true); + EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpStoreInfo(fd, params)); +} + +/** +* @tc.name: DumpStoreInfo003 +* @tc.desc: test DumpStoreInfo function +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KvStoreDataServiceTest, DumpStoreInfo003, TestSize.Level0) +{ + KvStoreDataService kvStoreDataServiceTest; + int fd = 0; + std::map> params = {}; + EXPECT_CALL(*accountDelegateMock, QueryForegroundUserId(_)).WillRepeatedly(Return(false)); + EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpStoreInfo(fd, params)); +} + /** * @tc.name: FilterData001 * @tc.desc: test FilterData function @@ -688,6 +794,39 @@ HWTEST_F(KvStoreDataServiceTest, DumpBundleInfo001, TestSize.Level0) EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpBundleInfo(fd, params)); } +/** +* @tc.name: DumpBundleInfo002 +* @tc.desc: test DumpBundleInfo function +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KvStoreDataServiceTest, DumpBundleInfo002, TestSize.Level0) +{ + KvStoreDataService kvStoreDataServiceTest; + int fd = 0; + std::map> params = {}; + ConfigSendParameters(true); + EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpBundleInfo(fd, params)); +} + +/** +* @tc.name: DumpBundleInfo003 +* @tc.desc: test DumpBundleInfo function +* @tc.type: FUNC +* @tc.require: +* @tc.author: SQL +*/ +HWTEST_F(KvStoreDataServiceTest, DumpBundleInfo002, TestSize.Level0) +{ + KvStoreDataService kvStoreDataServiceTest; + int fd = 0; + std::map> params = {}; + EXPECT_CALL(*accountDelegateMock, QueryForegroundUserId(testing::)).WillRepeatedly(testing::Return(false)); + EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpBundleInfo(fd, params)); +} + + /** * @tc.name: OnExtensionRestore001 * @tc.desc: restore with invalid fd