diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index d5b6eaa360c4478dbf862d277c86b2850175789d..9fc966e3c3ed9a9f679f2ca425d4986937320ce9 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -87,13 +87,20 @@ 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}/app/test/unittest/sensitive_test.cpp", "${data_service_path}/service/common/xcollie.cpp", ] sanitize = { + ubsan = true + boundary_sanitize = true + integer_overflow = true + cfi_no_nvcall = true cfi = true cfi_cross_dso = true debug = false + blocklist = "${datamgr_service_path}/cfi_blocklist.txt" } configs = [ ":module_private_config" ] 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 0000000000000000000000000000000000000000..de3fe575378a696c386b2734322ad8c88bd333a5 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/security_test.cpp @@ -0,0 +1,206 @@ +/* + * 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_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) +{ +} + +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_TEST_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_TEST_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 normal. + * @tc.type: FUNC + */ +HWTEST_F(SecurityTest, SetSecurityOption005, TestSize.Level1) +{ + auto security = std::make_shared(); + std::string filePath = FILE_TEST_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 DIR_PATH 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_TEST_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); +} +} // 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 new file mode 100644 index 0000000000000000000000000000000000000000..411f20d083e7105564631906282cf313f697786f --- /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 S1. + * @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 diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 4e1fb58fc8d394bdd63ecb1b2ee82ec7a1f69f5d..89d48976e5165d143f068e8b38a3ef5e9889e7c3 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 e7d9784768c74503239b9072a1d9d0d8f023a150..e35b05f178dd86a05813ec281091e99676aead20 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -483,6 +483,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 +538,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 0000000000000000000000000000000000000000..0fa7032096366c95c9bf78ecb629acbe53cf8818 --- /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_DISTRIBUTEDDATA_SERVICE_CLOUD_DB_MOCK_H +#define OHOS_DISTRIBUTEDDATA_SERVICE_CLOUD_DB_MOCK_H + +#include + +#include "cloud/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_DISTRIBUTEDDATA_SERVICE_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 c63bdacc07e7f0ecb790e23e8a881339105c97d3..57be55a6fbef71f9e58a4a8be84bffedff7ea4e5 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 f64e67f44e155d5a872dc82d5bb4bdeb6bb86604..868a32ef9bfdbcf2aa4a9076d010b8142800eb18 100644 --- a/services/distributeddataservice/service/test/rdb_cloud_test.cpp +++ b/services/distributeddataservice/service/test/rdb_cloud_test.cpp @@ -16,27 +16,77 @@ #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. @@ -48,14 +98,21 @@ 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_DBVBucket), g_DBVBucket); + + 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_DBVBucket), g_DBVBucket); + + 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_DBVBucket); + + std::vector extendDelete = g_DBVBuckets; + result = rdbCloud.BatchDelete(tableName, extendDelete); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); } @@ -69,24 +126,21 @@ 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_DBVBucket), g_DBVBucket); + 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", {"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", { "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)}}, {"#float", {double(100)}}, - {"#_type", {int64_t(1)}}, {"#_query", {Bytes({ 1, 2, 3, 4 })}}} - }; + 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(); EXPECT_EQ(result, DBStatus::CLOUD_ERROR); @@ -107,30 +161,22 @@ 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)}} - }; - 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); } @@ -145,8 +191,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); @@ -169,8 +214,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); @@ -209,27 +253,178 @@ 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: 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"; + 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); +} + +/** +* @tc.name: PostEvent002 +* @tc.desc: RdbCloud PostEvent test snapshots contains asset.uri. +* @tc.type: FUNC +*/ +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); + std::string tableName = "testTable"; + 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); +} + +/** +* @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; + snapshots.bindAssets = std::make_shared>>(); + std::shared_ptr snapshot; + std::string uri = "testuri"; + snapshots.bindAssets->insert_or_assign(uri, snapshot); + RdbCloud rdbCloud(std::make_shared(), &snapshots); + std::string tableName = "testTable"; + 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); +} + +/** +* @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; + snapshots.bindAssets = std::make_shared>>(); + std::shared_ptr snapshot = nullptr; + std::string uri = "testuri"; + snapshots.bindAssets->insert_or_assign(uri, snapshot); + RdbCloud rdbCloud(std::make_shared(), &snapshots); + std::string tableName = "testTable"; + 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); +} + +/** +* @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_NETWORK_ERROR, cursor))); + BindAssets snapshots; + RdbCloud rdbCloud(mockCloudDB, &snapshots); + auto result = rdbCloud.Query(tableName, g_DBVBucket, g_DBVBuckets); + EXPECT_EQ(result, DBStatus::CLOUD_NETWORK_ERROR); +} + +/** +* @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::make_shared(); + std::string tableName = "testTable"; + 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(mockCloudDB, &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::make_shared(); + std::string tableName = "testTable"; + 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(mockCloudDB, &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