diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp index 0d80c15d91173fc425bcebab62db211d277c0c52..0c70f3e9461b51600fa1b3bacb8d927e177e4af1 100644 --- a/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -48,6 +48,31 @@ bool CloudInfo::Unmarshal(const Serializable::json &node) return true; } +bool CloudInfo::IsAppsEqual(const std::map &appInfos) const +{ + if (apps.size() != appInfos.size()) { + return false; + } + for (auto it = apps.begin(), rIt = appInfos.begin(); it != apps.end(); ++it, ++rIt) { + if (it->second != rIt->second) { + return false; + } + } + return true; +} + +bool CloudInfo::operator==(const CloudInfo &info) const +{ + return std::tie(user, id, totalSpace, remainSpace, enableCloud, maxNumber, maxSize) == + std::tie(info.user, info.id, info.totalSpace, info.remainSpace, info.enableCloud, info.maxNumber, + info.maxSize) && IsAppsEqual(info.apps); +} + +bool CloudInfo::operator!=(const CloudInfo &info) const +{ + return !operator==(info); +} + bool CloudInfo::AppInfo::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(bundleName)], bundleName); @@ -68,6 +93,17 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) return true; } +bool CloudInfo::AppInfo::operator==(const AppInfo &info) const +{ + return std::tie(bundleName, appId, version, instanceId, cloudSwitch) == std::tie(info.bundleName, info.appId, + info.version, info.instanceId, info.cloudSwitch); +} + +bool CloudInfo::AppInfo::operator!=(const AppInfo &info) const +{ + return !operator==(info); +} + std::string CloudInfo::GetKey() const { return GetKey(INFO_PREFIX, { std::to_string(user) }); diff --git a/services/distributeddataservice/framework/cloud/cloud_mark.cpp b/services/distributeddataservice/framework/cloud/cloud_mark.cpp index 37e69ad2e8a8eabf7fdcf1c223a5bf745ab3ad9c..f6003a2096010f5f0d88fefd57272179d23076df 100644 --- a/services/distributeddataservice/framework/cloud/cloud_mark.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_mark.cpp @@ -31,6 +31,18 @@ bool CloudMark::Unmarshal(const Serializable::json &node) return true; } +bool CloudMark::operator==(const CloudMark &cloudMark) const +{ + return std::tie(bundleName, deviceId, index, isClearWaterMark, storeId, userId) == + std::tie(cloudMark.bundleName, cloudMark.deviceId, cloudMark.index, cloudMark.isClearWaterMark, + cloudMark.storeId, cloudMark.userId); +} + +bool CloudMark::operator!=(const CloudMark &cloudMark) const +{ + return !operator==(cloudMark); +} + std::string CloudMark::GetKey() { return GetKey({ deviceId, std::to_string(userId), "default", bundleName, storeId, std::to_string(index) }); diff --git a/services/distributeddataservice/framework/cloud/schema_meta.cpp b/services/distributeddataservice/framework/cloud/schema_meta.cpp index 273fc6b48eed65f1b6859108100c7abeb218dec7..a34b86873bfcdcfa05f93710024b7fba970fc0a1 100644 --- a/services/distributeddataservice/framework/cloud/schema_meta.cpp +++ b/services/distributeddataservice/framework/cloud/schema_meta.cpp @@ -40,6 +40,17 @@ bool SchemaMeta::Unmarshal(const Serializable::json &node) return true; } +bool SchemaMeta::operator==(const SchemaMeta &meta) const +{ + return std::tie(metaVersion, version, bundleName, databases, e2eeEnable) == + std::tie(meta.metaVersion, meta.version, meta.bundleName, meta.databases, meta.e2eeEnable); +} + +bool SchemaMeta::operator!=(const SchemaMeta &meta) const +{ + return !operator==(meta); +} + std::vector Database::GetTableNames() const { std::vector tableNames; @@ -99,6 +110,18 @@ bool Database::Unmarshal(const Serializable::json &node) return true; } +bool Database::operator==(const Database &database) const +{ + return std::tie(name, alias, tables, version, bundleName, user, deviceId, autoSyncType) == + std::tie(database.name, database.alias, database.tables, database.version, database.bundleName, database.user, + database.deviceId, database.autoSyncType); +} + +bool Database::operator!=(const Database &database) const +{ + return !operator==(database); +} + bool Table::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); @@ -122,6 +145,18 @@ bool Table::Unmarshal(const Serializable::json &node) return true; } +bool Table::operator==(const Table &table) const +{ + return std::tie(name, alias, fields, deviceSyncFields, cloudSyncFields, sharedTableName) == + std::tie(table.name, table.alias, table.fields, table.deviceSyncFields, table.cloudSyncFields, + table.sharedTableName); +} + +bool Table::operator!=(const Table &table) const +{ + return !operator==(table); +} + bool Field::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(colName)], colName); @@ -144,6 +179,16 @@ bool Field::Unmarshal(const Serializable::json &node) GetValue(node, GET_NAME(notNull), nullable); return true; } +bool Field::operator==(const Field &field) const +{ + return std::tie(colName, alias, type, primary, nullable) == + std::tie(field.colName, field.alias, field.type, field.primary, field.nullable); +} + +bool Field::operator!=(const Field &field) const +{ + return !operator==(field); +} Database SchemaMeta::GetDataBase(const std::string &storeId) { diff --git a/services/distributeddataservice/framework/include/cloud/cloud_info.h b/services/distributeddataservice/framework/include/cloud/cloud_info.h index c565fcd2f4fd090899044837887c5333f7416945..f9f3c2af5cdc9be88fe779ca3d18e328acb539cc 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -32,6 +32,8 @@ public: bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const AppInfo &info) const; + bool operator!=(const AppInfo &info) const; }; int32_t user = 0; std::string id = ""; @@ -57,12 +59,15 @@ public: bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const CloudInfo &info) const; + bool operator!=(const CloudInfo &info) const; private: static constexpr const char *INFO_PREFIX = "CLOUD_INFO"; static constexpr const char *SCHEMA_PREFIX = "CLOUD_SCHEMA"; static std::string GetKey(const std::string &prefix, const std::initializer_list &fields); + bool IsAppsEqual(const std::map &appInfos) const; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H diff --git a/services/distributeddataservice/framework/include/cloud/cloud_mark.h b/services/distributeddataservice/framework/include/cloud/cloud_mark.h index 47c5783d976c613594226763b49c9cdfece92a2a..e4e101fab6c55f6094c89cd9950118e2e6769b4a 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_mark.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_mark.h @@ -34,6 +34,8 @@ public: } bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const CloudMark &cloudMark) const; + bool operator!=(const CloudMark &cloudMark) const; std::string GetKey(); std::string GetKey(const std::initializer_list &fields); }; diff --git a/services/distributeddataservice/framework/include/cloud/schema_meta.h b/services/distributeddataservice/framework/include/cloud/schema_meta.h index 889392199550eb6d11c11bbfba87f1beb4afdb3d..4d8c6ad83e5105fff8316161c9614f2a0c70784a 100644 --- a/services/distributeddataservice/framework/include/cloud/schema_meta.h +++ b/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -25,6 +25,8 @@ struct API_EXPORT Field final : public Serializable { bool nullable = true; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const Field &field) const; + bool operator!=(const Field &field) const; }; struct API_EXPORT Table final : public Serializable { @@ -36,6 +38,8 @@ struct API_EXPORT Table final : public Serializable { std::vector cloudSyncFields = {}; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const Table &table) const; + bool operator!=(const Table &table) const; }; struct API_EXPORT Database final : public Serializable { @@ -53,6 +57,8 @@ struct API_EXPORT Database final : public Serializable { API_EXPORT static std::string GetPrefix(const std::initializer_list &fields); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const Database &database) const; + bool operator!=(const Database &database) const; }; class API_EXPORT SchemaMeta final : public Serializable { @@ -95,6 +101,8 @@ public: bool IsValid() const; Database GetDataBase(const std::string &storeId); std::vector GetStores(); + bool operator==(const SchemaMeta &meta) const; + bool operator!=(const SchemaMeta &meta) const; }; // Table mode of device data sync time diff --git a/services/distributeddataservice/framework/test/cloud_test.cpp b/services/distributeddataservice/framework/test/cloud_test.cpp index 57e4ab7ec63550b492ef1a1d4add7825e2dce4bc..e39d12fe64f21b6a7f4f9d8dbd08b5f117453cd3 100644 --- a/services/distributeddataservice/framework/test/cloud_test.cpp +++ b/services/distributeddataservice/framework/test/cloud_test.cpp @@ -20,6 +20,7 @@ #include "cloud/cloud_db.h" #include "cloud/cloud_event.h" #include "cloud/cloud_info.h" +#include "cloud/cloud_mark.h" #include "cloud/cloud_server.h" #include "cloud/schema_meta.h" #include "utils/crypto.h" @@ -256,13 +257,98 @@ HWTEST_F(CloudInfoTest, CloudInfoTest001, TestSize.Level0) } /** -* @tc.name: AppInfoTest +* @tc.name: CloudInfoTest002 +* @tc.desc: CloudInfo Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, CloudInfoTest002, TestSize.Level0) +{ + CloudInfo cloudInfo1; + cloudInfo1.user = 111; + cloudInfo1.id = "test1_id"; + cloudInfo1.totalSpace = 0; + cloudInfo1.remainSpace = 0; + cloudInfo1.enableCloud = false; + cloudInfo1.maxNumber = CloudInfo::DEFAULT_BATCH_NUMBER; + cloudInfo1.maxSize = CloudInfo::DEFAULT_BATCH_SIZE; + + CloudInfo::AppInfo cloudInfoAppInfo1; + cloudInfoAppInfo1.bundleName = "ohos.test.demo"; + cloudInfoAppInfo1.appId = "test1_id"; + cloudInfoAppInfo1.version = 0; + cloudInfoAppInfo1.instanceId = 0; + cloudInfoAppInfo1.cloudSwitch = false; + cloudInfo1.apps["ohos.test.demo"] = cloudInfoAppInfo1; + + CloudInfo cloudInfo2 = cloudInfo1; + EXPECT_EQ(cloudInfo2, cloudInfo1); + + cloudInfo2.maxSize = 1; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.maxNumber = 1; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.enableCloud = true; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.remainSpace = 1; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.totalSpace = 1; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.id = "test2_id"; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.user = 222; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.apps["ohos.test.demo"].cloudSwitch = true; + EXPECT_NE(cloudInfo2, cloudInfo1); +} + +/** +* @tc.name: CloudInfoTest003 +* @tc.desc: CloudInfo Overload function apps test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, CloudInfoTest003, TestSize.Level0) +{ + CloudInfo cloudInfo1; + cloudInfo1.user = 111; + cloudInfo1.id = "test1_id"; + cloudInfo1.totalSpace = 0; + cloudInfo1.remainSpace = 0; + cloudInfo1.enableCloud = false; + cloudInfo1.maxNumber = CloudInfo::DEFAULT_BATCH_NUMBER; + cloudInfo1.maxSize = CloudInfo::DEFAULT_BATCH_SIZE; + + CloudInfo::AppInfo cloudInfoAppInfo1; + cloudInfoAppInfo1.bundleName = "ohos.test.demo"; + cloudInfoAppInfo1.appId = "test1_id"; + cloudInfoAppInfo1.version = 0; + cloudInfoAppInfo1.instanceId = 0; + cloudInfoAppInfo1.cloudSwitch = false; + cloudInfo1.apps["ohos.test.demo"] = cloudInfoAppInfo1; + + CloudInfo cloudInfo2 = cloudInfo1; + cloudInfo2.apps["ohos.test.demo"].cloudSwitch = true; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.apps["ohos.test.demo"].instanceId = 1; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.apps["ohos.test.demo"].version = 1;EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.apps["ohos.test.demo"].appId = "test2_id"; + EXPECT_NE(cloudInfo2, cloudInfo1); + cloudInfo2.apps["ohos.test.demo"].bundleName = "ohos.test.demo2"; + EXPECT_NE(cloudInfo2, cloudInfo1); +} + +/** +* @tc.name: AppInfoTest001 * @tc.desc: Marshal and Unmarshal of AppInfo. * @tc.type: FUNC * @tc.require: * @tc.author: Anvette */ -HWTEST_F(CloudInfoTest, AppInfoTest, TestSize.Level0) +HWTEST_F(CloudInfoTest, AppInfoTest001, TestSize.Level0) { CloudInfo::AppInfo cloudInfoAppInfo1; cloudInfoAppInfo1.bundleName = "ohos.test.demo"; @@ -281,13 +367,44 @@ HWTEST_F(CloudInfoTest, AppInfoTest, TestSize.Level0) } /** -* @tc.name: TableTest +* @tc.name: AppInfoTest002 +* @tc.desc: AppInfoTest Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, AppInfoTest002, TestSize.Level0) +{ + CloudInfo::AppInfo cloudInfoAppInfo1; + cloudInfoAppInfo1.bundleName = "ohos.test.demo"; + cloudInfoAppInfo1.appId = "test1_id"; + cloudInfoAppInfo1.version = 0; + cloudInfoAppInfo1.instanceId = 0; + cloudInfoAppInfo1.cloudSwitch = false; + + CloudInfo::AppInfo cloudInfoAppInfo2 = cloudInfoAppInfo1; + EXPECT_EQ(cloudInfoAppInfo2, cloudInfoAppInfo1); + + cloudInfoAppInfo2.cloudSwitch = true; + EXPECT_NE(cloudInfoAppInfo2, cloudInfoAppInfo1); + cloudInfoAppInfo2.instanceId = 1; + EXPECT_NE(cloudInfoAppInfo2, cloudInfoAppInfo1); + cloudInfoAppInfo2.version = 1; + EXPECT_NE(cloudInfoAppInfo2, cloudInfoAppInfo1); + cloudInfoAppInfo2.appId = "test2_id"; + EXPECT_NE(cloudInfoAppInfo2, cloudInfoAppInfo1); + cloudInfoAppInfo2.bundleName = "ohos.test.demo2"; + EXPECT_NE(cloudInfoAppInfo2, cloudInfoAppInfo1); +} + +/** +* @tc.name: TableTest001 * @tc.desc: Marshal and Unmarshal of Table. * @tc.type: FUNC * @tc.require: * @tc.author: Anvette */ -HWTEST_F(CloudInfoTest, TableTest, TestSize.Level0) +HWTEST_F(CloudInfoTest, TableTest001, TestSize.Level0) { Field field1; field1.colName = "test1_colName"; @@ -310,6 +427,112 @@ HWTEST_F(CloudInfoTest, TableTest, TestSize.Level0) EXPECT_EQ(Serializable::Marshall(table1), Serializable::Marshall(table2)); } +/** +* @tc.name: TableTest002 +* @tc.desc: Table Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, TableTest002, TestSize.Level0) +{ + Field field1; + field1.colName = "test1_colName"; + field1.alias = "test1_alias"; + field1.type = 1; + field1.primary = true; + field1.nullable = false; + + Table table1; + table1.name = "test1_name"; + table1.sharedTableName = "test1_sharedTableName"; + table1.alias = "test1_alias"; + table1.fields.push_back(field1); + + Table table2 = table1; + EXPECT_EQ(table2, table1); + + table2.sharedTableName = "share"; + EXPECT_NE(table2, table1); + table2.cloudSyncFields.push_back("cloud"); + EXPECT_NE(table2, table1); + table2.deviceSyncFields.push_back("device"); + EXPECT_NE(table2, table1); + table2.fields[0].primary = true; + EXPECT_NE(table2, table1); + table2.alias = "test2_alias"; + EXPECT_NE(table2, table1); + table2.name = "test2_name"; + EXPECT_NE(table2, table1); +} + +/** +* @tc.name: TableTest003 +* @tc.desc: Table Overload function Field test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, TableTest003, TestSize.Level0) +{ + Field field1; + field1.colName = "test1_colName"; + field1.alias = "test1_alias"; + field1.type = 1; + field1.primary = true; + field1.nullable = false; + + Table table1; + table1.name = "test1_name"; + table1.sharedTableName = "test1_sharedTableName"; + table1.alias = "test1_alias"; + table1.fields.push_back(field1); + + Table table2 = table1; + table2.fields[0].nullable = true; + EXPECT_NE(table2, table1); + table2.fields[0].primary = false; + EXPECT_NE(table2, table1); + table2.fields[0].type = 2; + EXPECT_NE(table2, table1); + table2.fields[0].alias = "test2_alias"; + EXPECT_NE(table2, table1); + table2.fields[0].colName = "test1_colName"; + EXPECT_NE(table2, table1); +} + +/** +* @tc.name: FieldTest +* @tc.desc: Field Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, FieldTest, TestSize.Level0) +{ + Field field1; + field1.colName = "test1_colName"; + field1.alias = "test1_alias"; + field1.type = 1; + field1.primary = true; + field1.nullable = false; + + + Field field2 = field1; + EXPECT_EQ(field2, field1); + + field2.nullable = true; + EXPECT_NE(field2, field1); + field2.primary = false; + EXPECT_NE(field2, field1); + field2.type = 2; + EXPECT_NE(field2, field1); + field2.alias = "test2_alias"; + EXPECT_NE(field2, field1); + field2.colName = "test2_colName"; + EXPECT_NE(field2, field1); +} + /** * @tc.name: IsAllSwitchOffTest * @tc.desc: Determine if all apps have their cloud synchronization disabled. @@ -514,13 +737,13 @@ HWTEST_F(ServicesCloudDBTest, CloudDB, TestSize.Level0) } /** -* @tc.name: SchemaMeta +* @tc.name: SchemaMeta001 * @tc.desc: test SchemaMeta GetLowVersion and GetHighVersion function. * @tc.type: FUNC * @tc.require: * @tc.author: SQL */ -HWTEST_F(CloudInfoTest, SchemaMeta, TestSize.Level0) +HWTEST_F(CloudInfoTest, SchemaMeta001, TestSize.Level0) { SchemaMeta schemaMeta; auto metaVersion = SchemaMeta::CURRENT_VERSION & 0xFFFF; @@ -531,6 +754,184 @@ HWTEST_F(CloudInfoTest, SchemaMeta, TestSize.Level0) EXPECT_EQ(result2, metaVersion); } +/** +* @tc.name: SchemaMeta002 +* @tc.desc: SchemaMeta Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, SchemaMeta002, TestSize.Level0) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + + SchemaMeta::Database database; + database.name = "test_cloud_store"; + database.alias = "test_cloud_database_alias_1"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta1; + schemaMeta1.version = 1; + schemaMeta1.bundleName = "test_cloud_bundleName"; + schemaMeta1.databases.emplace_back(database); + schemaMeta1.e2eeEnable = false; + + SchemaMeta schemaMeta2 = schemaMeta1; + EXPECT_EQ(schemaMeta2, schemaMeta1); + + schemaMeta2.e2eeEnable = true; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].alias = "test_cloud_database_alias_2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.bundleName = "test_cloud_bundleName2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.version = 2; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.metaVersion = 2; + EXPECT_NE(schemaMeta2, schemaMeta1); +} + +/** +* @tc.name: SchemaMeta003 +* @tc.desc: SchemaMeta Overload function database test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, SchemaMeta003, TestSize.Level0) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + + SchemaMeta::Database database; + database.name = "test_cloud_store"; + database.alias = "test_cloud_database_alias_1"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta1; + schemaMeta1.version = 1; + schemaMeta1.bundleName = "test_cloud_bundleName"; + schemaMeta1.databases.emplace_back(database); + schemaMeta1.e2eeEnable = false; + + SchemaMeta schemaMeta2 = schemaMeta1; + schemaMeta2.databases[0].autoSyncType = 1; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].deviceId = "111"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].user = "111"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].bundleName = "test_cloud_bundleName2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].version = 2; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].name = "test_cloud_table_name2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].alias = "test_cloud_database_alias_2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].name = "test_cloud_store2"; + EXPECT_NE(schemaMeta2, schemaMeta1); +} + +/** +* @tc.name: SchemaMeta004 +* @tc.desc: SchemaMeta Overload function Table test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, SchemaMeta004, TestSize.Level0) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + + SchemaMeta::Database database; + database.name = "test_cloud_store"; + database.alias = "test_cloud_database_alias_1"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta1; + schemaMeta1.version = 1; + schemaMeta1.bundleName = "test_cloud_bundleName"; + schemaMeta1.databases.emplace_back(database); + schemaMeta1.e2eeEnable = false; + + SchemaMeta schemaMeta2 = schemaMeta1; + schemaMeta2.databases[0].tables[0].sharedTableName = "share"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].cloudSyncFields.push_back("cloud"); + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].deviceSyncFields.push_back("device"); + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].fields[0].alias = "test_cloud_field_alias2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].alias = "test_cloud_table_alias2"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].name = "test_cloud_table_name2"; + EXPECT_NE(schemaMeta2, schemaMeta1); +} + +/** +* @tc.name: SchemaMeta005 +* @tc.desc: SchemaMeta Overload function Field test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, SchemaMeta005, TestSize.Level0) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + + SchemaMeta::Database database; + database.name = "test_cloud_store"; + database.alias = "test_cloud_database_alias_1"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta1; + schemaMeta1.version = 1; + schemaMeta1.bundleName = "test_cloud_bundleName"; + schemaMeta1.databases.emplace_back(database); + schemaMeta1.e2eeEnable = false; + + SchemaMeta schemaMeta2 = schemaMeta1; + schemaMeta2.databases[0].tables[0].fields[0].nullable = false; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].fields[0].primary = true; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].fields[0].type = 1; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].fields[0].alias = "test_cloud_field_alias1"; + EXPECT_NE(schemaMeta2, schemaMeta1); + schemaMeta2.databases[0].tables[0].fields[0].colName = "test_cloud_field_name2"; + EXPECT_NE(schemaMeta2, schemaMeta1); +} + /** * @tc.name: GetEventId * @tc.desc: test GetEventId function @@ -546,4 +947,35 @@ HWTEST_F(CloudEventTest, GetEventId, TestSize.Level0) auto ret = event.GetEventId(); EXPECT_EQ(ret, evtId); } + +/** +* @tc.name: CloudMarkTest +* @tc.desc: CloudMark Overload function test. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(CloudInfoTest, CloudMarkTest, TestSize.Level0) +{ + CloudMark cloudmark1; + cloudmark1.bundleName = "test_cloud_bundleName"; + cloudmark1.deviceId = "1111"; + cloudmark1.storeId = "test_db"; + + CloudMark cloudmark2 = cloudmark1; + EXPECT_EQ(cloudmark2, cloudmark1); + + cloudmark2.userId = 1; + EXPECT_NE(cloudmark2, cloudmark1); + cloudmark2.storeId = "test_db2"; + EXPECT_NE(cloudmark2, cloudmark1); + cloudmark2.isClearWaterMark = true; + EXPECT_NE(cloudmark2, cloudmark1); + cloudmark2.index = 1; + EXPECT_NE(cloudmark2, cloudmark1); + cloudmark2.deviceId = "222"; + EXPECT_NE(cloudmark2, cloudmark1); + cloudmark2.bundleName = "test_cloud_bundleName2"; + EXPECT_NE(cloudmark2, cloudmark1); +} } // namespace OHOS::Test diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index a1f141257df3005726d47cb56ef3601b201cdbe0..664e72ff0432c2dff21861b5746be153b63faea3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -142,8 +142,12 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapGetUserByToken(tokenId); - CloudSyncScene scene; + CloudSyncScene scene = CloudSyncScene::SWITCH_OFF; if (appSwitch == SWITCH_ON) { scene = CloudSyncScene::SWITCH_ON; - } else { - scene = CloudSyncScene::SWITCH_OFF; } std::lock_guard lock(rwMetaMutex_); auto [status, cloudInfo] = GetCloudInfo(user); @@ -223,11 +229,14 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri "ChangeAppSwitch ret=" + std::to_string(status)); return INVALID_ARGUMENT; } - ZLOGI("add app switch, bundleName:%{public}s", bundleName.c_str()); } cloudInfo.apps[bundleName].cloudSwitch = (appSwitch == SWITCH_ON); - if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { - return ERROR; + CloudInfo oldCloudInfo; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), oldCloudInfo, true) || + oldCloudInfo != cloudInfo) { + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } } Execute(GenTask(0, cloudInfo.user, scene, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); if (cloudInfo.enableCloud && appSwitch == SWITCH_ON) { @@ -865,6 +874,10 @@ int32_t CloudServiceImpl::UpdateCloudInfoFromServer(int32_t user) ZLOGE("userId:%{public}d, status:%{public}d", user, status); return E_ERROR; } + CloudInfo oldCloudInfo; + if (MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), oldCloudInfo, true) && oldCloudInfo == cloudInfo) { + return E_OK; + } return MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true) ? E_OK : E_ERROR; } @@ -883,7 +896,9 @@ bool CloudServiceImpl::UpdateCloudInfo(int32_t user, CloudSyncScene scene) MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); return true; } - MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + if (cloudInfo != oldInfo) { + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + } if (oldInfo.id != cloudInfo.id) { ReleaseUserInfo(user, scene); ZLOGE("different id, [server] id:%{public}s, [meta] id:%{public}s", Anonymous::Change(cloudInfo.id).c_str(), @@ -935,7 +950,9 @@ bool CloudServiceImpl::UpdateSchema(int32_t user, CloudSyncScene scene) UpgradeSchemaMeta(user, oldMeta); UpdateClearWaterMark(hapInfo, schemaMeta, oldMeta); } - MetaDataManager::GetInstance().SaveMeta(key, schemaMeta, true); + if (oldMeta != schemaMeta) { + MetaDataManager::GetInstance().SaveMeta(key, schemaMeta, true); + } } return true; } @@ -1124,14 +1141,16 @@ std::pair CloudServiceImpl::GetCloudInfo(int32_t userId) int32_t CloudServiceImpl::CloudStatic::OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) { Subscription sub; - if (MetaDataManager::GetInstance().LoadMeta(Subscription::GetKey(user), sub, true)) { + if (MetaDataManager::GetInstance().LoadMeta(Subscription::GetKey(user), sub, true) && + sub.expiresTime.find(bundleName) != sub.expiresTime.end()) { sub.expiresTime.erase(bundleName); MetaDataManager::GetInstance().SaveMeta(Subscription::GetKey(user), sub, true); } CloudInfo cloudInfo; cloudInfo.user = user; - if (MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + if (MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true) && + cloudInfo.apps.find(bundleName) != cloudInfo.apps.end()) { cloudInfo.apps.erase(bundleName); MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); } @@ -1206,7 +1225,9 @@ int32_t CloudServiceImpl::UpdateSchemaFromHap(const HapInfo &hapInfo) if (MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { UpdateClearWaterMark(hapInfo, newSchemaMeta, schemaMeta); } - MetaDataManager::GetInstance().SaveMeta(schemaKey, newSchemaMeta, true); + if (newSchemaMeta != schemaMeta) { + MetaDataManager::GetInstance().SaveMeta(schemaKey, newSchemaMeta, true); + } return SUCCESS; } @@ -1232,7 +1253,10 @@ void CloudServiceImpl::UpdateClearWaterMark( if (dbMap.find(database.name) != dbMap.end() && database.version != dbMap[database.name]) { metaData.storeId = database.name; metaData.isClearWaterMark = true; - MetaDataManager::GetInstance().SaveMeta(metaData.GetKey(), metaData, true); + CloudMark oldMark; + if (!MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), oldMark, true) || oldMark != metaData) { + MetaDataManager::GetInstance().SaveMeta(metaData.GetKey(), metaData, true); + } ZLOGI("clear watermark, storeId:%{public}s, newVersion:%{public}d, oldVersion:%{public}d", Anonymous::Change(metaData.storeId).c_str(), database.version, dbMap[database.name]); } @@ -1897,7 +1921,9 @@ int32_t CloudServiceImpl::SaveNetworkStrategy(const std::vector> SyncManager::GetCloudSyncInfo(const ZLOGE("cloud is empty, user: %{public}d", cloud.user); return cloudSyncInfos; } - if (!MetaDataManager::GetInstance().SaveMeta(cloud.GetKey(), cloud, true)) { - ZLOGW("save cloud info fail, user: %{public}d", cloud.user); + CloudInfo oldInfo; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), oldInfo, true) || oldInfo != cloud) { + MetaDataManager::GetInstance().SaveMeta(cloud.GetKey(), cloud, true); } } auto schemaKey = CloudInfo::GetSchemaKey(cloud.user, info.bundleName_); diff --git a/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.cpp b/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.cpp index ae1cb2a475fb8484e4b37775a5b8f0f4fa298403..8f939d028757a831dabdeaa2b53c16791a5ac8db 100644 --- a/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.cpp +++ b/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.cpp @@ -89,6 +89,11 @@ bool NetworkSyncStrategy::StrategyInfo::Unmarshal(const Serializable::json &node return false; } +bool NetworkSyncStrategy::StrategyInfo::operator==(const StrategyInfo &info) const +{ + return user == info.user && bundleName == info.bundleName && strategy == info.strategy; +} + std::string NetworkSyncStrategy::StrategyInfo::GetKey() { return Constant::Join(StrategyInfo::PREFIX, Constant::KEY_SEPARATOR, { std::to_string(user), bundleName }); diff --git a/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.h b/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.h index 2c32abc0b3feb22722d7f0fe2011c35e6aa75de6..53a713345a1f46e53ccad2e8224ff2db7ea79b92 100644 --- a/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.h +++ b/services/distributeddataservice/service/cloud/sync_strategies/network_sync_strategy.h @@ -34,6 +34,7 @@ public: uint32_t strategy = DEFAULT_STRATEGY; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool operator==(const StrategyInfo &info) const; std::string GetKey(); static constexpr int32_t INVALID_USER = -1; static constexpr const char *PREFIX = "NETWORK_SYNC_STRATEGY"; diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 7e29955084907fb46dc304659c89580132cb0eb9..81325cea478c762fc16690da51a2838ed83e217e 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -55,6 +55,7 @@ #include "store/general_value.h" #include "store/store_info.h" #include "sync_manager.h" +#include "sync_strategies/network_sync_strategy.h" #include "token_setproc.h" using namespace testing::ext; @@ -1396,12 +1397,12 @@ HWTEST_F(CloudDataTest, ChangeAppSwitch, TestSize.Level0) } /** -* @tc.name: EnableCloud -* @tc.desc: +* @tc.name: EnableCloud01 +* @tc.desc: Test the EnableCloud function to ensure that the id in cloudinfo matches the passed id. * @tc.type: FUNC * @tc.require: */ -HWTEST_F(CloudDataTest, EnableCloud, TestSize.Level0) +HWTEST_F(CloudDataTest, EnableCloud01, TestSize.Level0) { std::string bundleName = "testName"; std::map switches; @@ -1411,6 +1412,20 @@ HWTEST_F(CloudDataTest, EnableCloud, TestSize.Level0) EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); } +/** +* @tc.name: EnableCloud02 +* @tc.desc: Test the EnableCloud function to SWITCH_OFF. +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, EnableCloud02, TestSize.Level0) +{ + std::map switches; + switches.insert_or_assign(TEST_CLOUD_BUNDLE, CloudData::CloudService::SWITCH_OFF); + auto ret = cloudServiceImpl_->EnableCloud(TEST_CLOUD_ID, switches); + EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); +} + /** * @tc.name: OnEnableCloud * @tc.desc: @@ -3167,5 +3182,37 @@ HWTEST_F(CloudDataTest, GetValidGeneralCode, TestSize.Level0) ret = CloudData::SyncManager::ConvertValidGeneralCode(E_SYNC_TASK_MERGED); EXPECT_TRUE(ret == E_ERROR); } + +/** +* @tc.name: StrategyInfo +* @tc.desc: StrategyInfo Overload function test. +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataTest, StrategyInfo, TestSize.Level0) +{ + CloudData::NetworkSyncStrategy::StrategyInfo info1; + info1.bundleName = "test_cloud_bundleName"; + + CloudData::NetworkSyncStrategy::StrategyInfo info2 = info1; + auto ret = info2 == info1; + EXPECT_TRUE(ret); + + info2.strategy = CloudData::NetworkSyncStrategy::Strategy::WIFI; + ret = info2 == info1; + EXPECT_FALSE(ret); + info2.strategy = CloudData::NetworkSyncStrategy::Strategy::CELLULAR; + ret = info2 == info1; + EXPECT_FALSE(ret); + info2.strategy = CloudData::NetworkSyncStrategy::Strategy::BUTT; + ret = info2 == info1; + EXPECT_TRUE(ret); + info2.bundleName = "test_cloud_bundleName2"; + ret = info2 == info1; + EXPECT_FALSE(ret); + info2.user = 1; + ret = info2 == info1; + EXPECT_FALSE(ret); +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file