From d305e90093b654c9bd30a9ab68c7ab9d81b940d9 Mon Sep 17 00:00:00 2001 From: guochao Date: Sat, 28 Jun 2025 18:02:08 +0800 Subject: [PATCH 01/13] add test coverage Signed-off-by: guochao --- .../framework/test/BUILD.gn | 1 + .../service/test/BUILD.gn | 3 + .../service/test/mock/cloud_db_mock.h | 51 ++++ .../service/test/mock/cursor_mock.h | 18 ++ .../service/test/rdb_cloud_test.cpp | 250 +++++++++++++++--- 5 files changed, 283 insertions(+), 40 deletions(-) create mode 100644 services/distributeddataservice/service/test/mock/cloud_db_mock.h diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 4e1fb58fc..89d48976e 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -155,6 +155,7 @@ ohos_unittest("StoreTest") { "access_token:libnativetoken", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "googletest:gmock", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e7d978476..61b104dc7 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -24,6 +24,7 @@ config("module_private_config") { "${data_service_path}/adapter/include", "${data_service_path}/app/src", "${data_service_path}/framework/include", + "${data_service_path}/framework/include/cloud", "${data_service_path}/service/bootstrap/include/", "${data_service_path}/service/backup/include/", "${data_service_path}/service/cloud/", @@ -483,6 +484,7 @@ ohos_unittest("RdbResultSetImplTest") { "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "c_utils:utils", + "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", @@ -537,6 +539,7 @@ ohos_unittest("RdbServiceTest") { "access_token:libnativetoken", "c_utils:utils", "device_manager:devicemanagersdk", + "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", diff --git a/services/distributeddataservice/service/test/mock/cloud_db_mock.h b/services/distributeddataservice/service/test/mock/cloud_db_mock.h new file mode 100644 index 000000000..d86aad27f --- /dev/null +++ b/services/distributeddataservice/service/test/mock/cloud_db_mock.h @@ -0,0 +1,51 @@ +/* + * 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_CLOUD_DB_MOCK_H +#define OHOS_CLOUD_DB_MOCK_H + +#include + +#include "cloud_db.h" + +namespace OHOS::DistributedData { + +class MockCloudDB : public CloudDB { +public: + using Watcher = GeneralWatcher; + using Async = std::function>)>; + using Devices = std::vector; + + MOCK_METHOD(int32_t, Execute, (const std::string &, const std::string &, const VBucket &), (override)); + MOCK_METHOD(int32_t, BatchInsert, (const std::string &, VBuckets &&, VBuckets &), (override)); + MOCK_METHOD(int32_t, BatchUpdate, (const std::string &, VBuckets &&, VBuckets &), (override)); + MOCK_METHOD(int32_t, BatchUpdate, (const std::string &, VBuckets &&, const VBuckets &), (override)); + MOCK_METHOD(int32_t, BatchDelete, (const std::string &, VBuckets &), (override)); + MOCK_METHOD( + (std::pair>), Query, (const std::string &, const VBucket &), (override)); + MOCK_METHOD((std::pair>), Query, (GenQuery &, const VBucket &), (override)); + MOCK_METHOD(int32_t, PreSharing, (const std::string &, VBuckets &), (override)); + MOCK_METHOD(int32_t, Sync, (const Devices &, int32_t, const GenQuery &, Async, int32_t), (override)); + MOCK_METHOD(int32_t, Watch, (int32_t, Watcher &), (override)); + MOCK_METHOD(int32_t, Unwatch, (int32_t, Watcher &), (override)); + MOCK_METHOD(int32_t, Lock, (), (override)); + MOCK_METHOD(int32_t, Heartbeat, (), (override)); + MOCK_METHOD(int32_t, Unlock, (), (override)); + MOCK_METHOD(int64_t, AliveTime, (), (override)); + MOCK_METHOD(int32_t, Close, (), (override)); + MOCK_METHOD((std::pair), GetEmptyCursor, (const std::string &), (override)); + MOCK_METHOD(void, SetPrepareTraceId, (const std::string &), (override)); +}; +} // namespace OHOS::DistributedData +#endif // OHOS_CLOUD_DB_MOCK_H diff --git a/services/distributeddataservice/service/test/mock/cursor_mock.h b/services/distributeddataservice/service/test/mock/cursor_mock.h index c63bdacc0..f06e9ae3c 100644 --- a/services/distributeddataservice/service/test/mock/cursor_mock.h +++ b/services/distributeddataservice/service/test/mock/cursor_mock.h @@ -14,6 +14,8 @@ */ #ifndef OHOS_DISTRIBUTEDDATA_SERVICE_TEST_CURSOR_MOCK_H #define OHOS_DISTRIBUTEDDATA_SERVICE_TEST_CURSOR_MOCK_H +#include + #include #include #include @@ -45,6 +47,22 @@ private: std::shared_ptr resultSet_; int32_t index_ = 0; }; + +class MockCursor : public DistributedData::Cursor { + MOCK_METHOD(int32_t, GetColumnNames, (std::vector & names), (const, override)); + MOCK_METHOD(int32_t, GetColumnName, (int32_t col, std::string &name), (const, override)); + MOCK_METHOD(int32_t, GetColumnType, (int32_t col), (const, override)); + MOCK_METHOD(int32_t, GetCount, (), (const, override)); + MOCK_METHOD(int32_t, MoveToFirst, (), (override)); + MOCK_METHOD(int32_t, MoveToNext, (), (override)); + MOCK_METHOD(int32_t, MoveToPrev, (), (override)); + MOCK_METHOD(int32_t, GetEntry, (VBucket & entry), (override)); + MOCK_METHOD(int32_t, GetRow, (VBucket & data), (override)); + MOCK_METHOD(int32_t, Get, (int32_t col, Value &value), (override)); + MOCK_METHOD(int32_t, Get, (const std::string &col, Value &value), (override)); + MOCK_METHOD(int32_t, Close, (), (override)); + MOCK_METHOD(bool, IsEnd, (), (override)); +}; } // namespace DistributedData } // namespace OHOS #endif // OHOS_DISTRIBUTEDDATA_SERVICE_TEST_CURSOR_MOCK_H diff --git a/services/distributeddataservice/service/test/rdb_cloud_test.cpp b/services/distributeddataservice/service/test/rdb_cloud_test.cpp index f64e67f44..10a3e10d1 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -16,27 +16,76 @@ #include "rdb_cloud.h" +#include "cloud_db_mock.h" +#include "cursor_mock.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" #include "log_print.h" using namespace testing::ext; +using namespace testing; using namespace OHOS::DistributedData; using namespace OHOS::DistributedRdb; using DBVBucket = DistributedDB::VBucket; using DBStatus = DistributedDB::DBStatus; -std::vector g_DBVBucket = { { { "#gid", { "0000000" } }, { "#flag", { true } }, +using DBAsset = DistributedDB::Asset; +using DBAssets = DistributedDB::Assets; +using AssetOpType = DistributedDB::AssetOpType; +using AssetStatus = DistributedDB::AssetStatus; +DBAsset assetValue1 = { .version = 1, + .name = "texture_diffuse", + .assetId = "123", + .subpath = "textures/environment", + .uri = "http://asset.com/textures/123.jpg", + .modifyTime = "2025-04-05T12:30:00Z", + .createTime = "2025-04-05T10:15:00Z", + .size = "1024", + .hash = "sha256-abc123", + .flag = static_cast(AssetOpType::INSERT), + .status = static_cast(AssetStatus::NORMAL), + .timestamp = std::time(nullptr) }; + +DBAsset assetValue2 = { .version = 2, + .name = "texture_diffuse", + .assetId = "456", + .subpath = "textures/environment", + .uri = "http://asset.com/textures/456.jpg", + .modifyTime = "2025-06-19T12:30:00Z", + .createTime = "2025-04-05T10:15:00Z", + .size = "1024", + .hash = "sha256-abc456", + .flag = static_cast(AssetOpType::UPDATE), + .status = static_cast(AssetStatus::NORMAL), + .timestamp = std::time(nullptr) }; + +DBAssets assetsValue = { assetValue1, assetValue2 }; +std::vector g_DBVBuckets = { { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, - { "#_query", { Bytes({ 1, 2, 3, 4 }) } } } }; + { "#_error", { "E_ERROR" } }, { "#_error", { "INVALID" } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } }, + { "#asset", assetValue1 }, { "#assets", assetsValue } } }; +DBVBucket g_DBVBucket = { { "#value", { int64_t(100) } } }; + namespace OHOS::Test { namespace DistributedRDBTest { class RdbCloudTest : public testing::Test { public: - static void SetUpTestCase(void){}; - static void TearDownTestCase(void){}; + static void SetUpTestCase(); + static void TearDownTestCase(); void SetUp(){}; void TearDown(){}; + static inline std::shared_ptr mockCloudDB = nullptr; + static constexpr int32_t COUNT = 2; }; +void RdbCloudTest::SetUpTestCase() +{ + mockCloudDB = std::make_shared(); +} + +void RdbCloudTest::TearDownTestCase() +{ + mockCloudDB = nullptr; +} /** * @tc.name: RdbCloudTest001 * @tc.desc: RdbCloud BatchInsert BatchUpdate BatchDelete test. @@ -51,11 +100,11 @@ HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1) std::shared_ptr cloudDB = std::make_shared(); RdbCloud rdbCloud(cloudDB, &bindAssets); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBucket), g_DBVBucket); + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - result = rdbCloud.BatchUpdate(tableName, std::move(g_DBVBucket), g_DBVBucket); + result = rdbCloud.BatchUpdate(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - result = rdbCloud.BatchDelete(tableName, g_DBVBucket); + result = rdbCloud.BatchDelete(tableName, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -75,18 +124,14 @@ HWTEST_F(RdbCloudTest, RdbCloudTest002, TestSize.Level1) rdbCloud.Lock(); std::string traceId = "id"; rdbCloud.SetPrepareTraceId(traceId); - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBucket), g_DBVBucket); + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - DBVBucket extends = { - {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}, - {"#_type", {int64_t(1)}}, {"#_query", {Bytes({ 1, 2, 3, 4 })}} - }; - result = rdbCloud.Query(tableName, extends, g_DBVBucket); + DBVBucket extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; + result = rdbCloud.Query(tableName, extends, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - std::vector vBuckets = { - {{"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}, - {"#_type", {int64_t(1)}}, {"#_query", {Bytes({ 1, 2, 3, 4 })}}} - }; + std::vector vBuckets = { { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } } }; rdbCloud.PreSharing(tableName, vBuckets); result = rdbCloud.HeartBeat(); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -110,27 +155,20 @@ HWTEST_F(RdbCloudTest, RdbCloudTest003, TestSize.Level1) std::shared_ptr cloudDB = std::make_shared(); RdbCloud rdbCloud(cloudDB, &bindAssets); std::string tableName = "testTable"; - DBVBucket extends = { - {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}, - {"#_type", {int64_t(1)}} - }; - std::vector data = { - {{"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}} - }; + DBVBucket extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(1) } } }; + std::vector data = { { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } } } }; auto result = rdbCloud.Query(tableName, extends, data); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - extends = { - {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}, - {"#_query", {Bytes({ 1, 2, 3, 4 })}} - }; + extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; result = rdbCloud.Query(tableName, extends, data); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - extends = { - {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}, - {"#_type", {int64_t(0)}} - }; + extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + { "#float", { double(100) } }, { "#_type", { int64_t(0) } } }; result = rdbCloud.Query(tableName, extends, data); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -209,27 +247,159 @@ HWTEST_F(RdbCloudTest, ConvertStatus, TestSize.Level1) HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1) { RdbCloud::DBQueryNodes nodes; - DistributedDB::QueryNode node = { DistributedDB::QueryNodeType::IN, "", {int64_t(1)} }; + DistributedDB::QueryNode node = { DistributedDB::QueryNodeType::IN, "", { int64_t(1) } }; nodes.push_back(node); - node = { DistributedDB::QueryNodeType::OR, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::OR, "", { int64_t(1) } }; nodes.push_back(node); - node = { DistributedDB::QueryNodeType::AND, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::AND, "", { int64_t(1) } }; nodes.push_back(node); - node = { DistributedDB::QueryNodeType::EQUAL_TO, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::EQUAL_TO, "", { int64_t(1) } }; nodes.push_back(node); - node = { DistributedDB::QueryNodeType::BEGIN_GROUP, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::BEGIN_GROUP, "", { int64_t(1) } }; nodes.push_back(node); - node = { DistributedDB::QueryNodeType::END_GROUP, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::END_GROUP, "", { int64_t(1) } }; nodes.push_back(node); - + auto result = RdbCloud::ConvertQuery(std::move(nodes)); EXPECT_EQ(result.size(), 6); nodes.clear(); - node = { DistributedDB::QueryNodeType::ILLEGAL, "", {int64_t(1)} }; + node = { DistributedDB::QueryNodeType::ILLEGAL, "", { int64_t(1) } }; nodes.push_back(node); result = RdbCloud::ConvertQuery(std::move(nodes)); EXPECT_EQ(result.size(), 0); } + +/** +* @tc.name: PostEvent001 +* @tc.desc: RdbCloud PostEvent test bindAssets is nullptr. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent001, TestSize.Level1) +{ + BindAssets bindAssets; + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &bindAssets); + rdbCloud.snapshots_->bindAssets = nullptr; + std::string traceId = "testId"; + rdbCloud.SetPrepareTraceId(traceId); + std::string tableName = "testTable"; + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent002 +* @tc.desc: RdbCloud PostEvent test snapshots contains asset.uri. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) +{ + BindAssets snapshots; + std::shared_ptr snapshot; + snapshots.bindAssets->insert_or_assign(assetValue1.uri, snapshot); + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + std::string tableName = "testTable"; + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent003 +* @tc.desc: RdbCloud PostEvent test snapshots does not contains asset.uri. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) +{ + BindAssets snapshots; + std::shared_ptr snapshot; + std::string uri = "testuri"; + snapshots.bindAssets->insert_or_assign(uri, snapshot); + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + std::string tableName = "testTable"; + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: PostEvent004 +* @tc.desc: RdbCloud PostEvent test the snapshot contained in the snapshots is nullpt. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent004, TestSize.Level1) +{ + BindAssets snapshots; + std::shared_ptr snapshot = nullptr; + std::string uri = "testuri"; + snapshots.bindAssets->insert_or_assign(uri, snapshot); + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + std::string tableName = "testTable"; + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + +/** +* @tc.name: Query001 +* @tc.desc: RdbCloud Query test the cursor is nullptr and code is not E_OK. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query001, TestSize.Level1) +{ + std::shared_ptr cursor = nullptr; + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, cursor))); + BindAssets snapshots; + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::SKIP_ASSET); +} + +/** +* @tc.name: Query002 +* @tc.desc: RdbCloud Query test code is QUERY_END. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query002, TestSize.Level1) +{ + std::shared_ptr mockCursor; + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, mockCursor))); + EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); + EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); + EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); + EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(true)); + + BindAssets snapshots; + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::QUERY_END); +} + +/** +* @tc.name: Query003 +* @tc.desc: RdbCloud Query test code is CLOUD_ERROR. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, Query003, TestSize.Level1) +{ + std::shared_ptr mockCursor; + std::string tableName = "testTable"; + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, mockCursor))); + EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); + EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); + EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); + EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(false)); + + BindAssets snapshots; + std::shared_ptr cloudDB = std::make_shared(); + RdbCloud rdbCloud(cloudDB, &snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} } // namespace DistributedRDBTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 15d08893e5bad197a23c599be98d96662fe5bfc0 Mon Sep 17 00:00:00 2001 From: guochao Date: Sat, 28 Jun 2025 19:11:30 +0800 Subject: [PATCH 02/13] format Signed-off-by: guochao --- .../service/test/mock/cursor_mock.h | 6 +-- .../service/test/rdb_cloud_test.cpp | 37 ++++--------------- 2 files changed, 10 insertions(+), 33 deletions(-) diff --git a/services/distributeddataservice/service/test/mock/cursor_mock.h b/services/distributeddataservice/service/test/mock/cursor_mock.h index f06e9ae3c..57be55a6f 100644 --- a/services/distributeddataservice/service/test/mock/cursor_mock.h +++ b/services/distributeddataservice/service/test/mock/cursor_mock.h @@ -49,15 +49,15 @@ private: }; class MockCursor : public DistributedData::Cursor { - MOCK_METHOD(int32_t, GetColumnNames, (std::vector & names), (const, override)); + MOCK_METHOD(int32_t, GetColumnNames, (std::vector &names), (const, override)); MOCK_METHOD(int32_t, GetColumnName, (int32_t col, std::string &name), (const, override)); MOCK_METHOD(int32_t, GetColumnType, (int32_t col), (const, override)); MOCK_METHOD(int32_t, GetCount, (), (const, override)); MOCK_METHOD(int32_t, MoveToFirst, (), (override)); MOCK_METHOD(int32_t, MoveToNext, (), (override)); MOCK_METHOD(int32_t, MoveToPrev, (), (override)); - MOCK_METHOD(int32_t, GetEntry, (VBucket & entry), (override)); - MOCK_METHOD(int32_t, GetRow, (VBucket & data), (override)); + MOCK_METHOD(int32_t, GetEntry, (VBucket &entry), (override)); + MOCK_METHOD(int32_t, GetRow, (VBucket &data), (override)); MOCK_METHOD(int32_t, Get, (int32_t col, Value &value), (override)); MOCK_METHOD(int32_t, Get, (const std::string &col, Value &value), (override)); MOCK_METHOD(int32_t, Close, (), (override)); diff --git a/services/distributeddataservice/service/test/rdb_cloud_test.cpp b/services/distributeddataservice/service/test/rdb_cloud_test.cpp index 10a3e10d1..d0c09e288 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -86,6 +86,7 @@ void RdbCloudTest::TearDownTestCase() { mockCloudDB = nullptr; } + /** * @tc.name: RdbCloudTest001 * @tc.desc: RdbCloud BatchInsert BatchUpdate BatchDelete test. @@ -270,24 +271,6 @@ HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1) EXPECT_EQ(result.size(), 0); } -/** -* @tc.name: PostEvent001 -* @tc.desc: RdbCloud PostEvent test bindAssets is nullptr. -* @tc.type: FUNC -*/ -HWTEST_F(RdbCloudTest, PostEvent001, TestSize.Level1) -{ - BindAssets bindAssets; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); - rdbCloud.snapshots_->bindAssets = nullptr; - std::string traceId = "testId"; - rdbCloud.SetPrepareTraceId(traceId); - std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); - EXPECT_EQ(result, DBStatus::CLOUD_ERROR); -} - /** * @tc.name: PostEvent002 * @tc.desc: RdbCloud PostEvent test snapshots contains asset.uri. @@ -298,8 +281,7 @@ HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) BindAssets snapshots; std::shared_ptr snapshot; snapshots.bindAssets->insert_or_assign(assetValue1.uri, snapshot); - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -316,8 +298,7 @@ HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) std::shared_ptr snapshot; std::string uri = "testuri"; snapshots.bindAssets->insert_or_assign(uri, snapshot); - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -334,8 +315,7 @@ HWTEST_F(RdbCloudTest, PostEvent004, TestSize.Level1) std::shared_ptr snapshot = nullptr; std::string uri = "testuri"; snapshots.bindAssets->insert_or_assign(uri, snapshot); - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -352,8 +332,7 @@ HWTEST_F(RdbCloudTest, Query001, TestSize.Level1) std::string tableName = "testTable"; EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, cursor))); BindAssets snapshots; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); EXPECT_EQ(result, DBStatus::SKIP_ASSET); } @@ -374,8 +353,7 @@ HWTEST_F(RdbCloudTest, Query002, TestSize.Level1) EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(true)); BindAssets snapshots; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); EXPECT_EQ(result, DBStatus::QUERY_END); } @@ -396,8 +374,7 @@ HWTEST_F(RdbCloudTest, Query003, TestSize.Level1) EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(false)); BindAssets snapshots; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &snapshots); + RdbCloud rdbCloud(std::make_shared(), &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } -- Gitee From 0e811369111388ceb1fbd1920ee0ba2ffea575d7 Mon Sep 17 00:00:00 2001 From: guochao Date: Tue, 1 Jul 2025 19:45:48 +0800 Subject: [PATCH 03/13] =?UTF-8?q?fix=EF=BC=9Acrash=E9=97=AE=E9=A2=98=20Sig?= =?UTF-8?q?ned-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/test/rdb_cloud_test.cpp | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/test/rdb_cloud_test.cpp b/services/distributeddataservice/service/test/rdb_cloud_test.cpp index d0c09e288..f5e7b7a72 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -127,11 +127,11 @@ HWTEST_F(RdbCloudTest, RdbCloudTest002, TestSize.Level1) rdbCloud.SetPrepareTraceId(traceId); auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - DBVBucket extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + DBVBucket extends = { { "#gid", { "00000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; result = rdbCloud.Query(tableName, extends, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - std::vector vBuckets = { { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, + std::vector vBuckets = { { { "#gid", { "00000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } } }; rdbCloud.PreSharing(tableName, vBuckets); result = rdbCloud.HeartBeat(); @@ -271,6 +271,38 @@ HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1) EXPECT_EQ(result.size(), 0); } +/** +* @tc.name: SetPrepareTraceId001 +* @tc.desc: RdbCloud PostEvent test cloudDB_ is nullptr. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, SetPrepareTraceId001, TestSize.Level1) +{ + std::string traceId = "testId"; + EXPECT_CALL(*mockCloudDB, SetPrepareTraceId(_)).Times(0); + BindAssets bindAssets; + std::shared_ptr mockCloudDB = nullptr; + RdbCloud rdbCloud(mockCloudDB, &bindAssets); + rdbCloud.SetPrepareTraceId(traceId); +} + +/** +* @tc.name: PostEvent001 +* @tc.desc: RdbCloud PostEvent test bindAssets is nullptr. +* @tc.type: FUNC +*/ +HWTEST_F(RdbCloudTest, PostEvent001, TestSize.Level1) +{ + BindAssets bindAssets; + RdbCloud rdbCloud(std::make_shared(), &bindAssets); + rdbCloud.snapshots_->bindAssets = nullptr; + std::string traceId = "testId"; + rdbCloud.SetPrepareTraceId(traceId); + std::string tableName = "testTable"; + auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_ERROR); +} + /** * @tc.name: PostEvent002 * @tc.desc: RdbCloud PostEvent test snapshots contains asset.uri. @@ -279,6 +311,7 @@ HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1) HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) { BindAssets snapshots; + snapshots.bindAssets = std::make_shared>>(); std::shared_ptr snapshot; snapshots.bindAssets->insert_or_assign(assetValue1.uri, snapshot); RdbCloud rdbCloud(std::make_shared(), &snapshots); @@ -295,6 +328,7 @@ HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) { BindAssets snapshots; + snapshots.bindAssets = std::make_shared>>(); std::shared_ptr snapshot; std::string uri = "testuri"; snapshots.bindAssets->insert_or_assign(uri, snapshot); @@ -312,6 +346,7 @@ HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) HWTEST_F(RdbCloudTest, PostEvent004, TestSize.Level1) { BindAssets snapshots; + snapshots.bindAssets = std::make_shared>>(); std::shared_ptr snapshot = nullptr; std::string uri = "testuri"; snapshots.bindAssets->insert_or_assign(uri, snapshot); @@ -330,11 +365,11 @@ HWTEST_F(RdbCloudTest, Query001, TestSize.Level1) { std::shared_ptr cursor = nullptr; std::string tableName = "testTable"; - EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, cursor))); + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_NETWORK_ERROR, cursor))); BindAssets snapshots; - RdbCloud rdbCloud(std::make_shared(), &snapshots); + RdbCloud rdbCloud(mockCloudDB, &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); - EXPECT_EQ(result, DBStatus::SKIP_ASSET); + EXPECT_EQ(result, DBStatus::CLOUD_NETWORK_ERROR); } /** @@ -344,16 +379,16 @@ HWTEST_F(RdbCloudTest, Query001, TestSize.Level1) */ HWTEST_F(RdbCloudTest, Query002, TestSize.Level1) { - std::shared_ptr mockCursor; + std::shared_ptr mockCursor = std::make_shared(); std::string tableName = "testTable"; - EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, mockCursor))); + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_OK, mockCursor))); EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(true)); BindAssets snapshots; - RdbCloud rdbCloud(std::make_shared(), &snapshots); + RdbCloud rdbCloud(mockCloudDB, &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); EXPECT_EQ(result, DBStatus::QUERY_END); } @@ -365,16 +400,16 @@ HWTEST_F(RdbCloudTest, Query002, TestSize.Level1) */ HWTEST_F(RdbCloudTest, Query003, TestSize.Level1) { - std::shared_ptr mockCursor; + std::shared_ptr mockCursor = std::make_shared(); std::string tableName = "testTable"; - EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_SKIP_ASSET, mockCursor))); + EXPECT_CALL(*mockCloudDB, Query(tableName, _)).WillOnce(Return(std::make_pair(E_OK, mockCursor))); EXPECT_CALL(*mockCursor, GetCount()).WillOnce(Return(COUNT)); EXPECT_CALL(*mockCursor, GetEntry(_)).WillOnce(Return(E_OK)).WillOnce(Return(E_ERROR)); EXPECT_CALL(*mockCursor, MoveToNext()).WillRepeatedly(Return(E_OK)); EXPECT_CALL(*mockCursor, IsEnd()).WillOnce(Return(false)); BindAssets snapshots; - RdbCloud rdbCloud(std::make_shared(), &snapshots); + RdbCloud rdbCloud(mockCloudDB, &snapshots); auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } -- Gitee From 61a6f51317901a3f02a9a0d7581ed33449d16274 Mon Sep 17 00:00:00 2001 From: guochao Date: Tue, 1 Jul 2025 21:58:46 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E4=BD=BF=E7=94=A8move=E5=90=8E=E7=9A=84?= =?UTF-8?q?=E5=8F=98=E9=87=8F=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/test/BUILD.gn | 1 - .../service/test/mock/cloud_db_mock.h | 8 ++-- .../service/test/rdb_cloud_test.cpp | 45 ++++++++++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 61b104dc7..e35b05f17 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -24,7 +24,6 @@ config("module_private_config") { "${data_service_path}/adapter/include", "${data_service_path}/app/src", "${data_service_path}/framework/include", - "${data_service_path}/framework/include/cloud", "${data_service_path}/service/bootstrap/include/", "${data_service_path}/service/backup/include/", "${data_service_path}/service/cloud/", diff --git a/services/distributeddataservice/service/test/mock/cloud_db_mock.h b/services/distributeddataservice/service/test/mock/cloud_db_mock.h index d86aad27f..0fa703209 100644 --- a/services/distributeddataservice/service/test/mock/cloud_db_mock.h +++ b/services/distributeddataservice/service/test/mock/cloud_db_mock.h @@ -12,12 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef OHOS_CLOUD_DB_MOCK_H -#define OHOS_CLOUD_DB_MOCK_H +#ifndef OHOS_DISTRIBUTEDDATA_SERVICE_CLOUD_DB_MOCK_H +#define OHOS_DISTRIBUTEDDATA_SERVICE_CLOUD_DB_MOCK_H #include -#include "cloud_db.h" +#include "cloud/cloud_db.h" namespace OHOS::DistributedData { @@ -48,4 +48,4 @@ public: MOCK_METHOD(void, SetPrepareTraceId, (const std::string &), (override)); }; } // namespace OHOS::DistributedData -#endif // OHOS_CLOUD_DB_MOCK_H +#endif // OHOS_DISTRIBUTEDDATA_SERVICE_CLOUD_DB_MOCK_H diff --git a/services/distributeddataservice/service/test/rdb_cloud_test.cpp b/services/distributeddataservice/service/test/rdb_cloud_test.cpp index f5e7b7a72..c7688325b 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -98,12 +98,17 @@ HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1) { BindAssets bindAssets; Bytes bytes; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); + RdbCloud rdbCloud(std::make_shared(), &bindAssets); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - result = rdbCloud.BatchUpdate(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + + std::vector dataUpdate = g_DBVBuckets; + std::vector extendUpdate = g_DBVBuckets; + result = rdbCloud.BatchUpdate(tableName, std::move(dataUpdate), extendUpdate); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); result = rdbCloud.BatchDelete(tableName, g_DBVBuckets); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -119,13 +124,14 @@ HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1) HWTEST_F(RdbCloudTest, RdbCloudTest002, TestSize.Level1) { BindAssets bindAssets; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); + RdbCloud rdbCloud(std::make_shared(), &bindAssets); std::string tableName = "testTable"; rdbCloud.Lock(); std::string traceId = "id"; rdbCloud.SetPrepareTraceId(traceId); - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); DBVBucket extends = { { "#gid", { "00000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } }, { "#_query", { Bytes({ 1, 2, 3, 4 }) } } }; @@ -153,8 +159,7 @@ HWTEST_F(RdbCloudTest, RdbCloudTest003, TestSize.Level1) { BindAssets bindAssets; bindAssets.bindAssets = nullptr; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); + RdbCloud rdbCloud(std::make_shared(), &bindAssets); std::string tableName = "testTable"; DBVBucket extends = { { "#gid", { "0000000" } }, { "#flag", { true } }, { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } } }; @@ -184,8 +189,7 @@ HWTEST_F(RdbCloudTest, RdbCloudTest003, TestSize.Level1) HWTEST_F(RdbCloudTest, RdbCloudTest004, TestSize.Level1) { BindAssets bindAssets; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); + RdbCloud rdbCloud(std::make_shared(), &bindAssets); auto err = rdbCloud.UnLockCloudDB(OHOS::DistributedRdb::RdbCloud::FLAG::SYSTEM_ABILITY); EXPECT_EQ(err, GeneralError::E_NOT_SUPPORT); @@ -208,8 +212,7 @@ HWTEST_F(RdbCloudTest, RdbCloudTest004, TestSize.Level1) HWTEST_F(RdbCloudTest, ConvertStatus, TestSize.Level1) { BindAssets bindAssets; - std::shared_ptr cloudDB = std::make_shared(); - RdbCloud rdbCloud(cloudDB, &bindAssets); + RdbCloud rdbCloud(std::make_shared(), &bindAssets); auto result = rdbCloud.ConvertStatus(GeneralError::E_OK); EXPECT_EQ(result, DBStatus::OK); result = rdbCloud.ConvertStatus(GeneralError::E_NETWORK_ERROR); @@ -299,7 +302,9 @@ HWTEST_F(RdbCloudTest, PostEvent001, TestSize.Level1) std::string traceId = "testId"; rdbCloud.SetPrepareTraceId(traceId); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -316,7 +321,9 @@ HWTEST_F(RdbCloudTest, PostEvent002, TestSize.Level1) snapshots.bindAssets->insert_or_assign(assetValue1.uri, snapshot); RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -334,7 +341,9 @@ HWTEST_F(RdbCloudTest, PostEvent003, TestSize.Level1) snapshots.bindAssets->insert_or_assign(uri, snapshot); RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -352,7 +361,9 @@ HWTEST_F(RdbCloudTest, PostEvent004, TestSize.Level1) snapshots.bindAssets->insert_or_assign(uri, snapshot); RdbCloud rdbCloud(std::make_shared(), &snapshots); std::string tableName = "testTable"; - auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBuckets), g_DBVBuckets); + std::vector dataInsert = g_DBVBuckets; + std::vector extendInsert = g_DBVBuckets; + auto result = rdbCloud.BatchInsert(tableName, std::move(dataInsert), extendInsert); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } -- Gitee From fdc36ac33473be3b29d3186667bc797def00e60e Mon Sep 17 00:00:00 2001 From: guochao Date: Wed, 2 Jul 2025 14:13:32 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E4=BD=BF=E7=94=A8move=E5=90=8E=E7=9A=84?= =?UTF-8?q?=E5=8F=98=E9=87=8F=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../distributeddataservice/service/test/rdb_cloud_test.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/rdb_cloud_test.cpp b/services/distributeddataservice/service/test/rdb_cloud_test.cpp index c7688325b..868a32ef9 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -110,7 +110,9 @@ HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1) std::vector extendUpdate = g_DBVBuckets; result = rdbCloud.BatchUpdate(tableName, std::move(dataUpdate), extendUpdate); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); - result = rdbCloud.BatchDelete(tableName, g_DBVBuckets); + + std::vector extendDelete = g_DBVBuckets; + result = rdbCloud.BatchDelete(tableName, extendDelete); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } -- Gitee From d274bed46bd31b877b5b2c0b148eb5648f25a09a Mon Sep 17 00:00:00 2001 From: guochao Date: Wed, 2 Jul 2025 17:44:08 +0800 Subject: [PATCH 06/13] add test Signed-off-by: guochao --- .../distributeddataservice/app/test/BUILD.gn | 1 + .../app/test/unittest/security_test.cpp | 277 ++++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100755 services/distributeddataservice/app/test/unittest/security_test.cpp diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index d5b6eaa36..df95a2b2b 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -87,6 +87,7 @@ ohos_unittest("KvStoreDataServiceTest") { "${data_service_path}/app/src/session_manager/upgrade_manager.cpp", "${data_service_path}/app/src/task_manager.cpp", "${data_service_path}/app/test/unittest/kvstore_data_service_test.cpp", + "${data_service_path}/app/test/unittest/security_test.cpp", "${data_service_path}/service/common/xcollie.cpp", ] diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp new file mode 100755 index 000000000..bf7e86a56 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -0,0 +1,277 @@ +/* + * 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 "security.h" + +#include + +#include "device_manager_adapter.h" + + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +using namespace DistributedDB; +using SecurityOption = DistributedDB::SecurityOption; +using DBStatus = DistributedDB::DBStatus; +namespace OHOS::Test { +class SecurityTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + static constexpr const char *FILE_PATH = "/data/test/KvStoreDataServiceTest"; + static constexpr const char *INVALID_FILE_PATH = "/data/test/invalidTest"; + static constexpr const char *DIR_PATH = "/data/test"; +}; + +void SecurityTest::SetUpTestCase(void) +{ +} + +void SecurityTest::TearDownTestCase(void) +{ +} + +void SecurityTest::SetUp(void) +{ +} + +void SecurityTest::TearDown(void) +{ +} + +/** + * @tc.name: IsXattrValueValid + * @tc.desc: Test for IsXattrValueValid value is empty. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, IsXattrValueValid001, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string value = ""; + auto res = security->IsXattrValueValid(value); + EXPECT_EQ(res, false); +} + +/** + * @tc.name: SetSecurityOption001 + * @tc.desc: Test for SetSecurityOption filePath is empty. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption001, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath; + SecurityOption option; + auto res = security->SetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::INVALID_ARGS); +} + +/** + * @tc.name: SetSecurityOption002 + * @tc.desc: Test for SetSecurityOption filePath is invalid. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption002, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = INVALID_FILE_PATH; + SecurityOption option; + auto res = security->SetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::INVALID_ARGS); +} + +/** + * @tc.name: SetSecurityOption003 + * @tc.desc: Test for SetSecurityOption securityLabel is NOT_SET. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption003, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = FILE_PATH; + SecurityOption option; + option.securityLabel = NOT_SET; + auto res = security->SetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::OK); +} + +/** + * @tc.name: SetSecurityOption004 + * @tc.desc: Test for SetSecurityOption securityLabel is invalid. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = FILE_PATH; + SecurityOption option; + option.securityLabel = INVALID_SEC_LABEL; + auto res = security->SetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::INVALID_ARGS); +} + +/** + * @tc.name: SetSecurityOption005 + * @tc.desc: Test for SetSecurityOption securityLabel is invalid. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption005, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = FILE_PATH; + SecurityOption option; + option.securityLabel = S0; + auto res = security->SetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::OK); +} + +/** + * @tc.name: GetSecurityOption001 + * @tc.desc: Test for GetSecurityOption filePath is empty. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, GetSecurityOption001, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath; + SecurityOption option; + auto res = security->GetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::INVALID_ARGS); +} + +/** + * @tc.name: GetSecurityOption002 + * @tc.desc: Test for GetSecurityOption dirPath is NOT_SUPPORT. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, GetSecurityOption002, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = DIR_PATH; + SecurityOption option; + auto res = security->GetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::NOT_SUPPORT); +} + +/** + * @tc.name: GetSecurityOption003 + * @tc.desc: Test for GetSecurityOption filePath is invalid. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, GetSecurityOption003, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = INVALID_FILE_PATH; + SecurityOption option; + auto res = security->GetSecurityOption(filePath, option); + EXPECT_EQ(res, DBStatus::OK); + EXPECT_EQ(option.securityLabel, NOT_SET); + EXPECT_EQ(option.securityFlag, ECE); +} + +/** + * @tc.name: GetSecurityOption005 + * @tc.desc: Test for GetSecurityOption file of securityLabel is S3. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, GetSecurityOption005, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = FILE_PATH; + SecurityOption setOption; + setOption.securityLabel = S3; + auto res = security->SetSecurityOption(filePath, setOption); + EXPECT_EQ(res, DBStatus::OK); + SecurityOption getOption; + res = security->GetSecurityOption(filePath, getOption); + EXPECT_EQ(res, DBStatus::OK); + EXPECT_EQ(getOption.securityLabel, S3); + EXPECT_EQ(getOption.securityFlag, SECE); +} + +/** + * @tc.name: OnDeviceChanged001 + * @tc.desc: Test for OnDeviceChanged networkId is empty. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, OnDeviceChanged001, TestSize.Level1) +{ + std::shared_ptr executors = std::make_shared(1, 0); + auto security = std::make_shared(executors); + AppDistributedKv::DeviceInfo info; + info.networkId = ""; + AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; + security->OnDeviceChanged(info, type); + EXPECT_EQ(security->devicesUdid_.Empty(), true); +} + +/** + * @tc.name: OnDeviceChanged002 + * @tc.desc: Test for OnDeviceChanged uuid is CLOUD_DEVICE_UUID. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, OnDeviceChanged002, TestSize.Level1) +{ + std::shared_ptr executors = std::make_shared(1, 0); + auto security = std::make_shared(executors); + AppDistributedKv::DeviceInfo info; + info.networkId = "netTest"; + info.uuid = DistributedData::DeviceManagerAdapter::CLOUD_DEVICE_UUID; + AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; + security->OnDeviceChanged(info, type); + EXPECT_EQ(security->devicesUdid_.Empty(), true); +} + +/** + * @tc.name: OnDeviceChanged003 + * @tc.desc: Test for OnDeviceChanged uuid is normal, type is DEVICE_ONREADY. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, OnDeviceChanged003, TestSize.Level1) +{ + std::shared_ptr executors = std::make_shared(1, 0); + auto security = std::make_shared(executors); + AppDistributedKv::DeviceInfo info; + info.networkId = "netTest"; + info.uuid = "123456"; + AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONREADY; + security->OnDeviceChanged(info, type); + EXPECT_EQ(security->devicesUdid_.Empty(), true); +} + +/** + * @tc.name: OnDeviceChanged004 + * @tc.desc: Test for OnDeviceChanged uuid is normal, test device online and offline. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, OnDeviceChanged004, TestSize.Level1) +{ + std::shared_ptr executors = std::make_shared(1, 0); + auto security = std::make_shared(executors); + AppDistributedKv::DeviceInfo info; + info.networkId = "netTest"; + info.uuid = "12345"; + info.udid = "67890"; + AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONLINE; + security->OnDeviceChanged(info, type); + type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; + security->OnDeviceChanged(info, type); + EXPECT_EQ(security->devicesUdid_.Empty(), true); +} +} // namespace OHOS::Test \ No newline at end of file -- Gitee From 8cecbcb56c49ebbfb6860e450dbb29a778862131 Mon Sep 17 00:00:00 2001 From: guochao Date: Wed, 2 Jul 2025 17:54:20 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BF=AE=E6=94=B9=20Si?= =?UTF-8?q?gned-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../distributeddataservice/app/test/unittest/security_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp index bf7e86a56..f010c203e 100755 --- a/services/distributeddataservice/app/test/unittest/security_test.cpp +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -128,7 +128,7 @@ HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) /** * @tc.name: SetSecurityOption005 - * @tc.desc: Test for SetSecurityOption securityLabel is invalid. + * @tc.desc: Test for SetSecurityOption securityLabel is normal. * @tc.type: FUNC */ HWTEST_F(SecurityTest, SetSecurityOption005, TestSize.Level1) -- Gitee From bf1870336e39be19ac537ee11fd162f5af7f197a Mon Sep 17 00:00:00 2001 From: guochao Date: Wed, 2 Jul 2025 18:07:27 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E8=A7=84=E8=8C=83=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/test/unittest/security_test.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp index f010c203e..d4e6acd16 100755 --- a/services/distributeddataservice/app/test/unittest/security_test.cpp +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -34,9 +34,9 @@ public: void TearDown(); protected: - static constexpr const char *FILE_PATH = "/data/test/KvStoreDataServiceTest"; - static constexpr const char *INVALID_FILE_PATH = "/data/test/invalidTest"; - static constexpr const char *DIR_PATH = "/data/test"; + static constexpr const char *fileTestPath = "/data/test/KvStoreDataServiceTest"; + static constexpr const char *invalidFilePath = "/data/test/invalidTest"; + static constexpr const char *dirPath = "/data/test"; }; void SecurityTest::SetUpTestCase(void) @@ -90,7 +90,7 @@ HWTEST_F(SecurityTest, SetSecurityOption001, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption002, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = INVALID_FILE_PATH; + std::string filePath = invalidFilePath; SecurityOption option; auto res = security->SetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::INVALID_ARGS); @@ -104,7 +104,7 @@ HWTEST_F(SecurityTest, SetSecurityOption002, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption003, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = FILE_PATH; + std::string filePath = fileTestPath; SecurityOption option; option.securityLabel = NOT_SET; auto res = security->SetSecurityOption(filePath, option); @@ -119,7 +119,7 @@ HWTEST_F(SecurityTest, SetSecurityOption003, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = FILE_PATH; + std::string filePath = fileTestPath; SecurityOption option; option.securityLabel = INVALID_SEC_LABEL; auto res = security->SetSecurityOption(filePath, option); @@ -134,7 +134,7 @@ HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption005, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = FILE_PATH; + std::string filePath = fileTestPath; SecurityOption option; option.securityLabel = S0; auto res = security->SetSecurityOption(filePath, option); @@ -163,7 +163,7 @@ HWTEST_F(SecurityTest, GetSecurityOption001, TestSize.Level1) HWTEST_F(SecurityTest, GetSecurityOption002, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = DIR_PATH; + std::string filePath = dirPath; SecurityOption option; auto res = security->GetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::NOT_SUPPORT); @@ -177,7 +177,7 @@ HWTEST_F(SecurityTest, GetSecurityOption002, TestSize.Level1) HWTEST_F(SecurityTest, GetSecurityOption003, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = INVALID_FILE_PATH; + std::string filePath = invalidFilePath; SecurityOption option; auto res = security->GetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::OK); @@ -193,7 +193,7 @@ HWTEST_F(SecurityTest, GetSecurityOption003, TestSize.Level1) HWTEST_F(SecurityTest, GetSecurityOption005, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = FILE_PATH; + std::string filePath = fileTestPath; SecurityOption setOption; setOption.securityLabel = S3; auto res = security->SetSecurityOption(filePath, setOption); -- Gitee From cfa409fd7fd0373b6b79a4aefd52ff9abe9cfa55 Mon Sep 17 00:00:00 2001 From: guochao Date: Thu, 3 Jul 2025 11:49:17 +0800 Subject: [PATCH 09/13] =?UTF-8?q?gtest=E5=8A=A0=E5=85=A5cfi=E7=99=BD?= =?UTF-8?q?=E5=90=8D=E5=8D=95=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/distributeddataservice/app/test/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index df95a2b2b..dd82b3960 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -95,6 +95,7 @@ ohos_unittest("KvStoreDataServiceTest") { cfi = true cfi_cross_dso = true debug = false + blocklist = "${datamgr_service_path}/cfi_blocklist.txt" } configs = [ ":module_private_config" ] -- Gitee From b5664430121cb7ed14397b2d5958852b22c41a80 Mon Sep 17 00:00:00 2001 From: guochao Date: Thu, 3 Jul 2025 15:04:01 +0800 Subject: [PATCH 10/13] add sensitive test Signed-off-by: guochao --- .../distributeddataservice/app/test/BUILD.gn | 1 + .../app/test/unittest/security_test.cpp | 1 - .../app/test/unittest/sensitive_test.cpp | 138 ++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 services/distributeddataservice/app/test/unittest/sensitive_test.cpp diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index dd82b3960..6526ba184 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -88,6 +88,7 @@ ohos_unittest("KvStoreDataServiceTest") { "${data_service_path}/app/src/task_manager.cpp", "${data_service_path}/app/test/unittest/kvstore_data_service_test.cpp", "${data_service_path}/app/test/unittest/security_test.cpp", + "${data_service_path}/app/test/unittest/sensitive_test.cpp", "${data_service_path}/service/common/xcollie.cpp", ] diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp index d4e6acd16..13706739a 100755 --- a/services/distributeddataservice/app/test/unittest/security_test.cpp +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -19,7 +19,6 @@ #include "device_manager_adapter.h" - using namespace testing::ext; using namespace OHOS::DistributedKv; using namespace DistributedDB; diff --git a/services/distributeddataservice/app/test/unittest/sensitive_test.cpp b/services/distributeddataservice/app/test/unittest/sensitive_test.cpp new file mode 100644 index 000000000..353baa07a --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/sensitive_test.cpp @@ -0,0 +1,138 @@ +/* + * 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 "sensitive.h" + +#include + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +namespace OHOS::Test { +class SensitiveTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void SensitiveTest::SetUpTestCase(void) +{ +} + +void SensitiveTest::TearDownTestCase(void) +{ +} + +void SensitiveTest::SetUp(void) +{ +} + +void SensitiveTest::TearDown(void) +{ +} + +/** + * @tc.name: GetDeviceSecurityLevel001 + * @tc.desc: Test for GetDeviceSecurityLevel deviceId is empty. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, GetDeviceSecurityLevel001, TestSize.Level1) +{ + auto sensitive = std::make_shared(); + auto res = sensitive->GetDeviceSecurityLevel(); + EXPECT_EQ(res, DATA_SEC_LEVEL1); +} + +/** + * @tc.name: GetDeviceSecurityLevel002 + * @tc.desc: Test for GetDeviceSecurityLevel secruityLevel > DATA_SEC_LEVEL1. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, GetDeviceSecurityLevel002, TestSize.Level1) +{ + auto sensitive = std::make_shared(); + sensitive->securityLevel = DATA_SEC_LEVEL2; + auto res = sensitive->GetDeviceSecurityLevel(); + EXPECT_EQ(res, DATA_SEC_LEVEL2); +} + +/** + * @tc.name: Operator001 + * @tc.desc: Test for Operator securityLabel is NOT_SET. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, Operator001, TestSize.Level1) +{ + Sensitive sensitive; + DistributedDB::SecurityOption option; + option.securityLabel = DistributedDB::NOT_SET; + EXPECT_TRUE(sensitive >= option); +} + +/** + * @tc.name: Operator002 + * @tc.desc: Test for Operator securityLabel is S3. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, Operator002, TestSize.Level1) +{ + Sensitive sensitive; + DistributedDB::SecurityOption option; + option.securityLabel = DistributedDB::S3; + EXPECT_FALSE(sensitive >= option); +} + +/** + * @tc.name: Operator002 + * @tc.desc: Test for Operator securityLabel is S1. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, Operator003, TestSize.Level1) +{ + Sensitive sensitive; + DistributedDB::SecurityOption option; + option.securityLabel = DistributedDB::S1; + EXPECT_TRUE(sensitive >= option); +} + +/** + * @tc.name: Operator003 + * @tc.desc: Test for Operator securityLabel is S4. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, Operator004, TestSize.Level1) +{ + Sensitive sensitive; + sensitive.securityLevel = DATA_SEC_LEVEL2; + DistributedDB::SecurityOption option; + option.securityLabel = DistributedDB::S4; + EXPECT_FALSE(sensitive >= option); +} + +/** + * @tc.name: Operator004 + * @tc.desc: Test for Operator securityLabel is S4. + * @tc.type: FUNC + */ +HWTEST_F(SensitiveTest, Operator005, TestSize.Level1) +{ + Sensitive sensitive; + sensitive.securityLevel = DATA_SEC_LEVEL2; + DistributedDB::SecurityOption option; + option.securityLabel = DistributedDB::S1; + EXPECT_TRUE(sensitive >= option); +} +} // namespace OHOS::Test \ No newline at end of file -- Gitee From 62837a257d9c3157f7f1240fb16ecd7af5d150c8 Mon Sep 17 00:00:00 2001 From: guochao Date: Thu, 3 Jul 2025 17:43:08 +0800 Subject: [PATCH 11/13] =?UTF-8?q?cfi=E4=BD=BF=E8=83=BD=E4=BC=A0=E6=9F=93?= =?UTF-8?q?=E6=80=A7=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/distributeddataservice/app/test/BUILD.gn | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 6526ba184..9fc966e3c 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -93,6 +93,10 @@ ohos_unittest("KvStoreDataServiceTest") { ] sanitize = { + ubsan = true + boundary_sanitize = true + integer_overflow = true + cfi_no_nvcall = true cfi = true cfi_cross_dso = true debug = false -- Gitee From 78d707f2db7ed6ab3a15fa0edcbf47bdbf1a8e40 Mon Sep 17 00:00:00 2001 From: guochao Date: Fri, 4 Jul 2025 16:00:28 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=BD=AE=E7=A9=BA=20Signed-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/test/unittest/security_test.cpp | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp index 13706739a..e1a29fc32 100755 --- a/services/distributeddataservice/app/test/unittest/security_test.cpp +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -33,9 +33,9 @@ public: void TearDown(); protected: - static constexpr const char *fileTestPath = "/data/test/KvStoreDataServiceTest"; - static constexpr const char *invalidFilePath = "/data/test/invalidTest"; - static constexpr const char *dirPath = "/data/test"; + static constexpr const char *FILE_TEST_PATH = "/data/test/KvStoreDataServiceTest"; + static constexpr const char *INVALID_FILE_PATH = "/data/test/invalidTest"; + static constexpr const char *DIR_PATH = "/data/test"; }; void SecurityTest::SetUpTestCase(void) @@ -89,7 +89,7 @@ HWTEST_F(SecurityTest, SetSecurityOption001, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption002, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = invalidFilePath; + std::string filePath = INVALID_FILE_PATH; SecurityOption option; auto res = security->SetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::INVALID_ARGS); @@ -103,7 +103,7 @@ HWTEST_F(SecurityTest, SetSecurityOption002, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption003, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = fileTestPath; + std::string filePath = FILE_TEST_PATH; SecurityOption option; option.securityLabel = NOT_SET; auto res = security->SetSecurityOption(filePath, option); @@ -118,7 +118,7 @@ HWTEST_F(SecurityTest, SetSecurityOption003, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = fileTestPath; + std::string filePath = FILE_TEST_PATH; SecurityOption option; option.securityLabel = INVALID_SEC_LABEL; auto res = security->SetSecurityOption(filePath, option); @@ -133,7 +133,7 @@ HWTEST_F(SecurityTest, SetSecurityOption004, TestSize.Level1) HWTEST_F(SecurityTest, SetSecurityOption005, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = fileTestPath; + std::string filePath = FILE_TEST_PATH; SecurityOption option; option.securityLabel = S0; auto res = security->SetSecurityOption(filePath, option); @@ -156,13 +156,13 @@ HWTEST_F(SecurityTest, GetSecurityOption001, TestSize.Level1) /** * @tc.name: GetSecurityOption002 - * @tc.desc: Test for GetSecurityOption dirPath is NOT_SUPPORT. + * @tc.desc: Test for GetSecurityOption DIR_PATH is NOT_SUPPORT. * @tc.type: FUNC */ HWTEST_F(SecurityTest, GetSecurityOption002, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = dirPath; + std::string filePath = DIR_PATH; SecurityOption option; auto res = security->GetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::NOT_SUPPORT); @@ -176,7 +176,7 @@ HWTEST_F(SecurityTest, GetSecurityOption002, TestSize.Level1) HWTEST_F(SecurityTest, GetSecurityOption003, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = invalidFilePath; + std::string filePath = INVALID_FILE_PATH; SecurityOption option; auto res = security->GetSecurityOption(filePath, option); EXPECT_EQ(res, DBStatus::OK); @@ -192,7 +192,7 @@ HWTEST_F(SecurityTest, GetSecurityOption003, TestSize.Level1) HWTEST_F(SecurityTest, GetSecurityOption005, TestSize.Level1) { auto security = std::make_shared(); - std::string filePath = fileTestPath; + std::string filePath = FILE_TEST_PATH; SecurityOption setOption; setOption.securityLabel = S3; auto res = security->SetSecurityOption(filePath, setOption); @@ -217,6 +217,8 @@ HWTEST_F(SecurityTest, OnDeviceChanged001, TestSize.Level1) info.networkId = ""; AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; security->OnDeviceChanged(info, type); + security->executors_ = nullptr; + executors = nullptr; EXPECT_EQ(security->devicesUdid_.Empty(), true); } @@ -234,6 +236,8 @@ HWTEST_F(SecurityTest, OnDeviceChanged002, TestSize.Level1) info.uuid = DistributedData::DeviceManagerAdapter::CLOUD_DEVICE_UUID; AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; security->OnDeviceChanged(info, type); + security->executors_ = nullptr; + executors = nullptr; EXPECT_EQ(security->devicesUdid_.Empty(), true); } @@ -251,6 +255,8 @@ HWTEST_F(SecurityTest, OnDeviceChanged003, TestSize.Level1) info.uuid = "123456"; AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONREADY; security->OnDeviceChanged(info, type); + security->executors_ = nullptr; + executors = nullptr; EXPECT_EQ(security->devicesUdid_.Empty(), true); } @@ -269,8 +275,14 @@ HWTEST_F(SecurityTest, OnDeviceChanged004, TestSize.Level1) info.udid = "67890"; AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONLINE; security->OnDeviceChanged(info, type); + security->executors_ = nullptr; + executors = nullptr; + std::shared_ptr executors1 = std::make_shared(1, 0); + auto security1 = std::make_shared(executors1); type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; - security->OnDeviceChanged(info, type); - EXPECT_EQ(security->devicesUdid_.Empty(), true); + security1->OnDeviceChanged(info, type); + security1->executors_ = nullptr; + executors1 = nullptr; + EXPECT_EQ(security1->devicesUdid_.Empty(), true); } } // namespace OHOS::Test \ No newline at end of file -- Gitee From 3879893bfe4e2c263162b2fdbac6afca0d5bd4df Mon Sep 17 00:00:00 2001 From: guochao Date: Fri, 4 Jul 2025 16:41:02 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E4=B8=8B=E6=8E=89=E7=94=A8=E4=BE=8B=20Si?= =?UTF-8?q?gned-off-by:=20guochao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/test/unittest/security_test.cpp | 82 ------------------- .../app/test/unittest/sensitive_test.cpp | 2 +- 2 files changed, 1 insertion(+), 83 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/security_test.cpp b/services/distributeddataservice/app/test/unittest/security_test.cpp index e1a29fc32..de3fe5753 100755 --- a/services/distributeddataservice/app/test/unittest/security_test.cpp +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -203,86 +203,4 @@ HWTEST_F(SecurityTest, GetSecurityOption005, TestSize.Level1) EXPECT_EQ(getOption.securityLabel, S3); EXPECT_EQ(getOption.securityFlag, SECE); } - -/** - * @tc.name: OnDeviceChanged001 - * @tc.desc: Test for OnDeviceChanged networkId is empty. - * @tc.type: FUNC - */ -HWTEST_F(SecurityTest, OnDeviceChanged001, TestSize.Level1) -{ - std::shared_ptr executors = std::make_shared(1, 0); - auto security = std::make_shared(executors); - AppDistributedKv::DeviceInfo info; - info.networkId = ""; - AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; - security->OnDeviceChanged(info, type); - security->executors_ = nullptr; - executors = nullptr; - EXPECT_EQ(security->devicesUdid_.Empty(), true); -} - -/** - * @tc.name: OnDeviceChanged002 - * @tc.desc: Test for OnDeviceChanged uuid is CLOUD_DEVICE_UUID. - * @tc.type: FUNC - */ -HWTEST_F(SecurityTest, OnDeviceChanged002, TestSize.Level1) -{ - std::shared_ptr executors = std::make_shared(1, 0); - auto security = std::make_shared(executors); - AppDistributedKv::DeviceInfo info; - info.networkId = "netTest"; - info.uuid = DistributedData::DeviceManagerAdapter::CLOUD_DEVICE_UUID; - AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; - security->OnDeviceChanged(info, type); - security->executors_ = nullptr; - executors = nullptr; - EXPECT_EQ(security->devicesUdid_.Empty(), true); -} - -/** - * @tc.name: OnDeviceChanged003 - * @tc.desc: Test for OnDeviceChanged uuid is normal, type is DEVICE_ONREADY. - * @tc.type: FUNC - */ -HWTEST_F(SecurityTest, OnDeviceChanged003, TestSize.Level1) -{ - std::shared_ptr executors = std::make_shared(1, 0); - auto security = std::make_shared(executors); - AppDistributedKv::DeviceInfo info; - info.networkId = "netTest"; - info.uuid = "123456"; - AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONREADY; - security->OnDeviceChanged(info, type); - security->executors_ = nullptr; - executors = nullptr; - EXPECT_EQ(security->devicesUdid_.Empty(), true); -} - -/** - * @tc.name: OnDeviceChanged004 - * @tc.desc: Test for OnDeviceChanged uuid is normal, test device online and offline. - * @tc.type: FUNC - */ -HWTEST_F(SecurityTest, OnDeviceChanged004, TestSize.Level1) -{ - std::shared_ptr executors = std::make_shared(1, 0); - auto security = std::make_shared(executors); - AppDistributedKv::DeviceInfo info; - info.networkId = "netTest"; - info.uuid = "12345"; - info.udid = "67890"; - AppDistributedKv::DeviceChangeType type = AppDistributedKv::DeviceChangeType::DEVICE_ONLINE; - security->OnDeviceChanged(info, type); - security->executors_ = nullptr; - executors = nullptr; - std::shared_ptr executors1 = std::make_shared(1, 0); - auto security1 = std::make_shared(executors1); - type = AppDistributedKv::DeviceChangeType::DEVICE_OFFLINE; - security1->OnDeviceChanged(info, type); - security1->executors_ = nullptr; - executors1 = nullptr; - EXPECT_EQ(security1->devicesUdid_.Empty(), true); -} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/app/test/unittest/sensitive_test.cpp b/services/distributeddataservice/app/test/unittest/sensitive_test.cpp index 353baa07a..411f20d08 100644 --- a/services/distributeddataservice/app/test/unittest/sensitive_test.cpp +++ b/services/distributeddataservice/app/test/unittest/sensitive_test.cpp @@ -124,7 +124,7 @@ HWTEST_F(SensitiveTest, Operator004, TestSize.Level1) /** * @tc.name: Operator004 - * @tc.desc: Test for Operator securityLabel is S4. + * @tc.desc: Test for Operator securityLabel is S1. * @tc.type: FUNC */ HWTEST_F(SensitiveTest, Operator005, TestSize.Level1) -- Gitee