• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "RdbGeneralStoreTest"
16 
17 #include "rdb_general_store.h"
18 
19 #include <random>
20 #include <thread>
21 
22 #include "bootstrap.h"
23 #include "cloud/schema_meta.h"
24 #include "gtest/gtest.h"
25 #include "log_print.h"
26 #include "metadata/meta_data_manager.h"
27 #include "metadata/secret_key_meta_data.h"
28 #include "metadata/store_meta_data.h"
29 #include "metadata/store_meta_data_local.h"
30 #include "mock/general_watcher_mock.h"
31 #include "rdb_query.h"
32 #include "store/general_store.h"
33 #include "types.h"
34 
35 using namespace testing::ext;
36 using namespace DistributedDB;
37 using namespace OHOS::DistributedData;
38 using namespace OHOS::DistributedRdb;
39 using DBStatus = DistributedDB::DBStatus;
40 using StoreMetaData = OHOS::DistributedData::StoreMetaData;
41 RdbGeneralStore::Values g_RdbValues = { { "0000000" }, { true }, { int64_t(100) }, { double(100) }, { int64_t(1) },
42     { Bytes({ 1, 2, 3, 4 }) } };
43 RdbGeneralStore::VBucket g_RdbVBucket = { { "#gid", { "0000000" } }, { "#flag", { true } },
44     { "#value", { int64_t(100) } }, { "#float", { double(100) } } };
45 bool g_testResult = false;
46 namespace OHOS::Test {
47 namespace DistributedRDBTest {
48 static constexpr uint32_t PRINT_ERROR_CNT = 150;
49 static constexpr const char *BUNDLE_NAME = "test_rdb_general_store";
50 static constexpr const char *STORE_NAME = "test_service_rdb";
51 class RdbGeneralStoreTest : public testing::Test {
52 public:
SetUpTestCase(void)53     static void SetUpTestCase(void){};
TearDownTestCase(void)54     static void TearDownTestCase(void){};
SetUp()55     void SetUp()
56     {
57         Bootstrap::GetInstance().LoadDirectory();
58         InitMetaData();
59     };
TearDown()60     void TearDown()
61     {
62         g_testResult = false;
63     };
64 
65 protected:
66     void InitMetaData();
67     StoreMetaData metaData_;
68 };
69 
InitMetaData()70 void RdbGeneralStoreTest::InitMetaData()
71 {
72     metaData_.bundleName = BUNDLE_NAME;
73     metaData_.appId = BUNDLE_NAME;
74     metaData_.user = "0";
75     metaData_.area = DistributedKv::Area::EL1;
76     metaData_.instanceId = 0;
77     metaData_.isAutoSync = true;
78     metaData_.storeType = 1;
79     metaData_.storeId = STORE_NAME;
80     metaData_.dataDir = "/data/service/el1/public/database/" + std::string(BUNDLE_NAME) + "/rdb";
81     metaData_.securityLevel = DistributedKv::SecurityLevel::S2;
82 }
83 
84 class MockRelationalStoreDelegate : public DistributedDB::RelationalStoreDelegate {
85 public:
86     ~MockRelationalStoreDelegate() = default;
87 
Sync(const std::vector<std::string> & devices,DistributedDB::SyncMode mode,const Query & query,const SyncStatusCallback & onComplete,bool wait)88     DBStatus Sync(const std::vector<std::string> &devices, DistributedDB::SyncMode mode, const Query &query,
89         const SyncStatusCallback &onComplete, bool wait) override
90     {
91         return DBStatus::OK;
92     }
93 
GetCloudSyncTaskCount()94     int32_t GetCloudSyncTaskCount() override
95     {
96         static int32_t count = 0;
97         count = (count + 1) % 2; // The result of count + 1 is the remainder of 2.
98         return count;
99     }
100 
RemoveDeviceData(const std::string & device,const std::string & tableName)101     DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) override
102     {
103         return DBStatus::OK;
104     }
105 
RemoteQuery(const std::string & device,const RemoteCondition & condition,uint64_t timeout,std::shared_ptr<ResultSet> & result)106     DBStatus RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
107         std::shared_ptr<ResultSet> &result) override
108     {
109         if (device == "test") {
110             return DBStatus::DB_ERROR;
111         }
112         return DBStatus::OK;
113     }
114 
RemoveDeviceData()115     DBStatus RemoveDeviceData() override
116     {
117         return DBStatus::OK;
118     }
119 
Sync(const std::vector<std::string> & devices,DistributedDB::SyncMode mode,const Query & query,const SyncProcessCallback & onProcess,int64_t waitTime)120     DBStatus Sync(const std::vector<std::string> &devices, DistributedDB::SyncMode mode, const Query &query,
121         const SyncProcessCallback &onProcess, int64_t waitTime) override
122     {
123         return DBStatus::OK;
124     }
125 
SetCloudDB(const std::shared_ptr<ICloudDb> & cloudDb)126     DBStatus SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) override
127     {
128         return DBStatus::OK;
129     }
130 
SetCloudDbSchema(const DataBaseSchema & schema)131     DBStatus SetCloudDbSchema(const DataBaseSchema &schema) override
132     {
133         return DBStatus::OK;
134     }
135 
RegisterObserver(StoreObserver * observer)136     DBStatus RegisterObserver(StoreObserver *observer) override
137     {
138         return DBStatus::OK;
139     }
140 
UnRegisterObserver()141     DBStatus UnRegisterObserver() override
142     {
143         return DBStatus::OK;
144     }
145 
UnRegisterObserver(StoreObserver * observer)146     DBStatus UnRegisterObserver(StoreObserver *observer) override
147     {
148         return DBStatus::OK;
149     }
150 
SetIAssetLoader(const std::shared_ptr<IAssetLoader> & loader)151     DBStatus SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) override
152     {
153         return DBStatus::OK;
154     }
155 
Sync(const CloudSyncOption & option,const SyncProcessCallback & onProcess)156     DBStatus Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) override
157     {
158         return DBStatus::OK;
159     }
160 
SetTrackerTable(const TrackerSchema & schema)161     DBStatus SetTrackerTable(const TrackerSchema &schema) override
162     {
163         if (schema.tableName == "WITH_INVENTORY_DATA") {
164             return DBStatus::WITH_INVENTORY_DATA;
165         }
166         if (schema.tableName == "test") {
167             return DBStatus::DB_ERROR;
168         }
169         return DBStatus::OK;
170     }
171 
ExecuteSql(const SqlCondition & condition,std::vector<DistributedDB::VBucket> & records)172     DBStatus ExecuteSql(const SqlCondition &condition, std::vector<DistributedDB::VBucket> &records) override
173     {
174         if (condition.sql == "") {
175             return DBStatus::DB_ERROR;
176         }
177 
178         std::string sqls = "INSERT INTO test ( #flag, #float, #gid, #value) VALUES  ( ?, ?, ?, ?)";
179         std::string sqlIn = " UPDATE test SET setSql WHERE whereSql";
180         std::string sql = "REPLACE INTO test ( #flag, #float, #gid, #value) VALUES  ( ?, ?, ?, ?)";
181         if (condition.sql == sqls || condition.sql == sqlIn || condition.sql == sql) {
182             return DBStatus::DB_ERROR;
183         }
184         return DBStatus::OK;
185     }
186 
SetReference(const std::vector<TableReferenceProperty> & tableReferenceProperty)187     DBStatus SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty) override
188     {
189         if (g_testResult) {
190             return DBStatus::DB_ERROR;
191         }
192         return DBStatus::OK;
193     }
194 
CleanTrackerData(const std::string & tableName,int64_t cursor)195     DBStatus CleanTrackerData(const std::string &tableName, int64_t cursor) override
196     {
197         return DBStatus::OK;
198     }
199 
Pragma(PragmaCmd cmd,PragmaData & pragmaData)200     DBStatus Pragma(PragmaCmd cmd, PragmaData &pragmaData) override
201     {
202         return DBStatus::OK;
203     }
204 
UpsertData(const std::string & tableName,const std::vector<DistributedDB::VBucket> & records,RecordStatus status=RecordStatus::WAIT_COMPENSATED_SYNC)205     DBStatus UpsertData(const std::string &tableName, const std::vector<DistributedDB::VBucket> &records,
206         RecordStatus status = RecordStatus::WAIT_COMPENSATED_SYNC) override
207     {
208         return DBStatus::OK;
209     }
210 
SetCloudSyncConfig(const CloudSyncConfig & config)211     DBStatus SetCloudSyncConfig(const CloudSyncConfig &config) override
212     {
213         return DBStatus::OK;
214     }
215 
216 protected:
RemoveDeviceDataInner(const std::string & device,ClearMode mode)217     DBStatus RemoveDeviceDataInner(const std::string &device, ClearMode mode) override
218     {
219         if (g_testResult) {
220             return DBStatus::DB_ERROR;
221         }
222         return DBStatus::OK;
223     }
224 
CreateDistributedTableInner(const std::string & tableName,TableSyncType type)225     DBStatus CreateDistributedTableInner(const std::string &tableName, TableSyncType type) override
226     {
227         if (tableName == "test") {
228             return DBStatus::DB_ERROR;
229         }
230         return DBStatus::OK;
231     }
232 };
233 
234 class MockStoreChangedData : public DistributedDB::StoreChangedData {
235 public:
GetDataChangeDevice() const236     std::string GetDataChangeDevice() const override
237     {
238         return "DataChangeDevice";
239     }
240 
GetStoreProperty(DistributedDB::StoreProperty & storeProperty) const241     void GetStoreProperty(DistributedDB::StoreProperty &storeProperty) const override
242     {
243         return;
244     }
245 };
246 
247 /**
248 * @tc.name: BindSnapshots001
249 * @tc.desc: RdbGeneralStore BindSnapshots test
250 * @tc.type: FUNC
251 * @tc.require:
252 * @tc.author: SQL
253 */
254 HWTEST_F(RdbGeneralStoreTest, BindSnapshots001, TestSize.Level1)
255 {
256     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
257     ASSERT_NE(store, nullptr);
258     BindAssets bindAssets;
259     auto result = store->BindSnapshots(bindAssets.bindAssets);
260     EXPECT_EQ(result, GeneralError::E_OK);
261 }
262 
263 /**
264 * @tc.name: BindSnapshots002
265 * @tc.desc: RdbGeneralStore BindSnapshots nullptr test
266 * @tc.type: FUNC
267 * @tc.require:
268 * @tc.author: SQL
269 */
270 HWTEST_F(RdbGeneralStoreTest, BindSnapshots002, TestSize.Level1)
271 {
272     DistributedData::StoreMetaData meta;
273     meta = metaData_;
274     meta.isEncrypt = true;
275     auto store = new (std::nothrow) RdbGeneralStore(meta);
276     ASSERT_NE(store, nullptr);
277     store->snapshots_.bindAssets = nullptr;
278     BindAssets bindAssets;
279     auto result = store->BindSnapshots(bindAssets.bindAssets);
280     EXPECT_EQ(result, GeneralError::E_OK);
281 }
282 
283 /**
284 * @tc.name: Bind001
285 * @tc.desc: RdbGeneralStore Bind bindInfo test
286 * @tc.type: FUNC
287 * @tc.require:
288 * @tc.author: SQL
289 */
290 HWTEST_F(RdbGeneralStoreTest, Bind001, TestSize.Level1)
291 {
292     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
293     ASSERT_NE(store, nullptr);
294     DistributedData::Database database;
295     GeneralStore::CloudConfig config;
296     std::map<uint32_t, DistributedData::GeneralStore::BindInfo> bindInfos;
297     auto result = store->Bind(database, bindInfos, config);
298     EXPECT_TRUE(bindInfos.empty());
299     EXPECT_EQ(result, GeneralError::E_OK);
300 
301     std::shared_ptr<CloudDB> db;
302     std::shared_ptr<AssetLoader> loader;
303     GeneralStore::BindInfo bindInfo(db, loader);
304     EXPECT_EQ(bindInfo.db_, nullptr);
305     EXPECT_EQ(bindInfo.loader_, nullptr);
306     uint32_t key = 1;
307     bindInfos[key] = bindInfo;
308     result = store->Bind(database, bindInfos, config);
309     EXPECT_TRUE(!bindInfos.empty());
310     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
311 
312     std::shared_ptr<CloudDB> dbs = std::make_shared<CloudDB>();
313     std::shared_ptr<AssetLoader> loaders = std::make_shared<AssetLoader>();
314     GeneralStore::BindInfo bindInfo1(dbs, loader);
315     EXPECT_NE(bindInfo1.db_, nullptr);
316     bindInfos[key] = bindInfo1;
317     result = store->Bind(database, bindInfos, config);
318     EXPECT_TRUE(!bindInfos.empty());
319     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
320 
321     GeneralStore::BindInfo bindInfo2(db, loaders);
322     EXPECT_NE(bindInfo2.loader_, nullptr);
323     bindInfos[key] = bindInfo2;
324     result = store->Bind(database, bindInfos, config);
325     EXPECT_TRUE(!bindInfos.empty());
326     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
327 }
328 
329 /**
330 * @tc.name: Bind002
331 * @tc.desc: RdbGeneralStore Bind delegate_ is nullptr test
332 * @tc.type: FUNC
333 * @tc.require:
334 * @tc.author: SQL
335 */
336 HWTEST_F(RdbGeneralStoreTest, Bind002, TestSize.Level1)
337 {
338     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
339     ASSERT_NE(store, nullptr);
340     DistributedData::Database database;
341     std::map<uint32_t, DistributedData::GeneralStore::BindInfo> bindInfos;
342 
343     std::shared_ptr<CloudDB> db = std::make_shared<CloudDB>();
344     std::shared_ptr<AssetLoader> loader = std::make_shared<AssetLoader>();
345     GeneralStore::BindInfo bindInfo(db, loader);
346     uint32_t key = 1;
347     bindInfos[key] = bindInfo;
348     GeneralStore::CloudConfig config;
349     auto result = store->Bind(database, bindInfos, config);
350     EXPECT_EQ(store->delegate_, nullptr);
351     EXPECT_EQ(result, GeneralError::E_ALREADY_CLOSED);
352 
353     store->isBound_ = true;
354     result = store->Bind(database, bindInfos, config);
355     EXPECT_EQ(result, GeneralError::E_OK);
356 }
357 
358 /**
359 * @tc.name: Bind003
360 * @tc.desc: RdbGeneralStore Bind delegate_ test
361 * @tc.type: FUNC
362 * @tc.require:
363 * @tc.author: SQL
364 */
365 HWTEST_F(RdbGeneralStoreTest, Bind003, TestSize.Level1)
366 {
367     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
368     ASSERT_NE(store, nullptr);
369     DistributedData::Database database;
370     std::map<uint32_t, DistributedData::GeneralStore::BindInfo> bindInfos;
371 
372     std::shared_ptr<CloudDB> db = std::make_shared<CloudDB>();
373     std::shared_ptr<AssetLoader> loader = std::make_shared<AssetLoader>();
374     GeneralStore::BindInfo bindInfo(db, loader);
375     uint32_t key = 1;
376     bindInfos[key] = bindInfo;
377     MockRelationalStoreDelegate mockDelegate;
378     store->delegate_ = &mockDelegate;
379     GeneralStore::CloudConfig config;
380     auto result = store->Bind(database, bindInfos, config);
381     EXPECT_NE(store->delegate_, nullptr);
382     EXPECT_EQ(result, GeneralError::E_OK);
383 }
384 
385 /**
386 * @tc.name: Close
387 * @tc.desc: RdbGeneralStore Close and IsBound function test
388 * @tc.type: FUNC
389 * @tc.require:
390 * @tc.author: SQL
391 */
392 HWTEST_F(RdbGeneralStoreTest, Close, TestSize.Level1)
393 {
394     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
395     ASSERT_NE(store, nullptr);
396     auto result = store->IsBound();
397     EXPECT_EQ(result, false);
398     EXPECT_EQ(store->delegate_, nullptr);
399     auto ret = store->Close();
400     EXPECT_EQ(ret, GeneralError::E_OK);
401 
402     MockRelationalStoreDelegate mockDelegate;
403     store->delegate_ = &mockDelegate;
404     ret = store->Close();
405     EXPECT_EQ(ret, GeneralError::E_BUSY);
406 }
407 
408 /**
409 * @tc.name: Close
410 * @tc.desc: RdbGeneralStore Close test
411 * @tc.type: FUNC
412 * @tc.require:
413 * @tc.author: shaoyuanzhao
414 */
415 HWTEST_F(RdbGeneralStoreTest, BusyClose, TestSize.Level1)
416 {
417     auto store = std::make_shared<RdbGeneralStore>(metaData_);
418     ASSERT_NE(store, nullptr);
__anoneb61fe710102() 419     std::thread thread([store]() {
420         std::unique_lock<decltype(store->rwMutex_)> lock(store->rwMutex_);
421         std::this_thread::sleep_for(std::chrono::seconds(1));
422     });
423     std::this_thread::sleep_for(std::chrono::milliseconds(500));
424     auto ret = store->Close();
425     EXPECT_EQ(ret, GeneralError::E_BUSY);
426     thread.join();
427     ret = store->Close();
428     EXPECT_EQ(ret, GeneralError::E_OK);
429 }
430 
431 /**
432 * @tc.name: Execute
433 * @tc.desc: RdbGeneralStore Execute function test
434 * @tc.type: FUNC
435 * @tc.require:
436 * @tc.author: SQL
437 */
438 HWTEST_F(RdbGeneralStoreTest, Execute, TestSize.Level1)
439 {
440     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
441     ASSERT_NE(store, nullptr);
442     std::string table = "table";
443     std::string sql = "sql";
444     EXPECT_EQ(store->delegate_, nullptr);
445     auto result = store->Execute(table, sql);
446     EXPECT_EQ(result, GeneralError::E_ERROR);
447 
448     MockRelationalStoreDelegate mockDelegate;
449     store->delegate_ = &mockDelegate;
450     result = store->Execute(table, sql);
451     EXPECT_EQ(result, GeneralError::E_OK);
452 
453     std::string null = "";
454     result = store->Execute(table, null);
455     EXPECT_EQ(result, GeneralError::E_ERROR);
456 }
457 
458 /**
459 * @tc.name: SqlConcatenate
460 * @tc.desc: RdbGeneralStore SqlConcatenate function test
461 * @tc.type: FUNC
462 * @tc.require:
463 * @tc.author: SQL
464 */
465 HWTEST_F(RdbGeneralStoreTest, SqlConcatenate, TestSize.Level1)
466 {
467     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
468     ASSERT_NE(store, nullptr);
469     DistributedData::VBucket value;
470     std::string strColumnSql = "strColumnSql";
471     std::string strRowValueSql = "strRowValueSql";
472     auto result = store->SqlConcatenate(value, strColumnSql, strRowValueSql);
473     size_t columnSize = value.size();
474     EXPECT_EQ(columnSize, 0);
475     EXPECT_EQ(result, columnSize);
476 
477     DistributedData::VBucket values = g_RdbVBucket;
478     result = store->SqlConcatenate(values, strColumnSql, strRowValueSql);
479     columnSize = values.size();
480     EXPECT_NE(columnSize, 0);
481     EXPECT_EQ(result, columnSize);
482 }
483 
484 /**
485 * @tc.name: Insert001
486 * @tc.desc: RdbGeneralStore Insert error test
487 * @tc.type: FUNC
488 * @tc.require:
489 * @tc.author: SQL
490 */
491 HWTEST_F(RdbGeneralStoreTest, Insert001, TestSize.Level1)
492 {
493     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
494     ASSERT_NE(store, nullptr);
495     DistributedData::VBuckets values;
496     EXPECT_EQ(values.size(), 0);
497     std::string table = "table";
498     auto result = store->Insert("", std::move(values));
499     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
500     result = store->Insert(table, std::move(values));
501     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
502 
503     DistributedData::VBuckets extends = { { { "#gid", { "0000000" } }, { "#flag", { true } },
504                                               { "#value", { int64_t(100) } }, { "#float", { double(100) } } },
505         { { "#gid", { "0000001" } } } };
506     result = store->Insert("", std::move(extends));
507     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
508 
509     DistributedData::VBucket value;
510     DistributedData::VBuckets vbuckets = { value };
511     result = store->Insert(table, std::move(vbuckets));
512     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
513 
514     result = store->Insert(table, std::move(extends));
515     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
516 }
517 
518 /**
519 * @tc.name: Insert002
520 * @tc.desc: RdbGeneralStore Insert function test
521 * @tc.type: FUNC
522 * @tc.require:
523 * @tc.author: SQL
524 */
525 HWTEST_F(RdbGeneralStoreTest, Insert002, TestSize.Level1)
526 {
527     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
528     ASSERT_NE(store, nullptr);
529     std::string table = "table";
530     DistributedData::VBuckets extends = { { g_RdbVBucket } };
531     auto result = store->Insert(table, std::move(extends));
532     EXPECT_EQ(result, GeneralError::E_ERROR);
533 
534     MockRelationalStoreDelegate mockDelegate;
535     store->delegate_ = &mockDelegate;
536     result = store->Insert(table, std::move(extends));
537     EXPECT_EQ(result, GeneralError::E_OK);
538 
539     std::string test = "test";
540     result = store->Insert(test, std::move(extends));
541     EXPECT_EQ(result, GeneralError::E_ERROR);
542 
543     result = store->Insert(test, std::move(extends));
544     EXPECT_EQ(result, GeneralError::E_ERROR);
545 
546     for (size_t i = 0; i < PRINT_ERROR_CNT + 1; i++) {
547         result = store->Insert(test, std::move(extends));
548         EXPECT_EQ(result, GeneralError::E_ERROR);
549     }
550 }
551 
552 /**
553 * @tc.name: Update
554 * @tc.desc: RdbGeneralStore Update function test
555 * @tc.type: FUNC
556 * @tc.require:
557 * @tc.author: SQL
558 */
559 HWTEST_F(RdbGeneralStoreTest, Update, TestSize.Level1)
560 {
561     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
562     ASSERT_NE(store, nullptr);
563     std::string table = "table";
564     std::string setSql = "setSql";
565     RdbGeneralStore::Values values;
566     std::string whereSql = "whereSql";
567     RdbGeneralStore::Values conditions;
568     auto result = store->Update("", setSql, std::move(values), whereSql, std::move(conditions));
569     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
570 
571     result = store->Update(table, "", std::move(values), whereSql, std::move(conditions));
572     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
573 
574     result = store->Update(table, setSql, std::move(values), whereSql, std::move(conditions));
575     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
576 
577     result = store->Update(table, setSql, std::move(g_RdbValues), "", std::move(conditions));
578     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
579 
580     result = store->Update(table, setSql, std::move(g_RdbValues), whereSql, std::move(conditions));
581     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
582 
583     result = store->Update(table, setSql, std::move(g_RdbValues), whereSql, std::move(g_RdbValues));
584     EXPECT_EQ(result, GeneralError::E_ERROR);
585 
586     MockRelationalStoreDelegate mockDelegate;
587     store->delegate_ = &mockDelegate;
588     result = store->Update(table, setSql, std::move(g_RdbValues), whereSql, std::move(g_RdbValues));
589     EXPECT_EQ(result, GeneralError::E_OK);
590 
591     result = store->Update("test", setSql, std::move(g_RdbValues), whereSql, std::move(g_RdbValues));
592     EXPECT_EQ(result, GeneralError::E_ERROR);
593 }
594 
595 /**
596 * @tc.name: Replace
597 * @tc.desc: RdbGeneralStore Replace function test
598 * @tc.type: FUNC
599 * @tc.require:
600 * @tc.author: SQL
601 */
602 HWTEST_F(RdbGeneralStoreTest, Replace, TestSize.Level1)
603 {
604     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
605     ASSERT_NE(store, nullptr);
606     std::string table = "table";
607     RdbGeneralStore::VBucket values;
608     auto result = store->Replace("", std::move(g_RdbVBucket));
609     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
610 
611     result = store->Replace(table, std::move(values));
612     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
613 
614     result = store->Replace(table, std::move(g_RdbVBucket));
615     EXPECT_EQ(result, GeneralError::E_ERROR);
616 
617     MockRelationalStoreDelegate mockDelegate;
618     store->delegate_ = &mockDelegate;
619     result = store->Replace(table, std::move(g_RdbVBucket));
620     EXPECT_EQ(result, GeneralError::E_OK);
621 
622     result = store->Replace("test", std::move(g_RdbVBucket));
623     EXPECT_EQ(result, GeneralError::E_ERROR);
624 }
625 
626 /**
627 * @tc.name: Delete
628 * @tc.desc: RdbGeneralStore Delete function test
629 * @tc.type: FUNC
630 * @tc.require:
631 * @tc.author: SQL
632 */
633 HWTEST_F(RdbGeneralStoreTest, Delete, TestSize.Level1)
634 {
635     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
636     ASSERT_NE(store, nullptr);
637     std::string table = "table";
638     std::string sql = "sql";
639     auto result = store->Delete(table, sql, std::move(g_RdbValues));
640     EXPECT_EQ(result, GeneralError::E_OK);
641 }
642 
643 /**
644 * @tc.name: Query001
645 * @tc.desc: RdbGeneralStore Query function test
646 * @tc.type: FUNC
647 * @tc.require:
648 * @tc.author: SQL
649 */
650 HWTEST_F(RdbGeneralStoreTest, Query001, TestSize.Level1)
651 {
652     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
653     ASSERT_NE(store, nullptr);
654     std::string table = "table";
655     std::string sql = "sql";
656     auto [err1, result1] = store->Query(table, sql, std::move(g_RdbValues));
657     EXPECT_EQ(err1, GeneralError::E_ALREADY_CLOSED);
658     EXPECT_EQ(result1, nullptr);
659 
660     MockRelationalStoreDelegate mockDelegate;
661     store->delegate_ = &mockDelegate;
662     auto [err2, result2] = store->Query(table, sql, std::move(g_RdbValues));
663     EXPECT_EQ(err2, GeneralError::E_OK);
664     EXPECT_NE(result2, nullptr);
665 }
666 
667 /**
668 * @tc.name: Query002
669 * @tc.desc: RdbGeneralStore Query function test
670 * @tc.type: FUNC
671 * @tc.require:
672 * @tc.author: SQL
673 */
674 HWTEST_F(RdbGeneralStoreTest, Query002, TestSize.Level1)
675 {
676     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
677     ASSERT_NE(store, nullptr);
678     std::string table = "table";
679     std::string sql = "sql";
680     MockQuery query;
681     auto [err1, result1] = store->Query(table, query);
682     EXPECT_EQ(err1, GeneralError::E_INVALID_ARGS);
683     EXPECT_EQ(result1, nullptr);
684 
685     query.lastResult = true;
686     auto [err2, result2] = store->Query(table, query);
687     EXPECT_EQ(err2, GeneralError::E_ALREADY_CLOSED);
688     EXPECT_EQ(result2, nullptr);
689 }
690 
691 /**
692 * @tc.name: MergeMigratedData
693 * @tc.desc: RdbGeneralStore MergeMigratedData function test
694 * @tc.type: FUNC
695 * @tc.require:
696 * @tc.author: SQL
697 */
698 HWTEST_F(RdbGeneralStoreTest, MergeMigratedData, TestSize.Level1)
699 {
700     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
701     ASSERT_NE(store, nullptr);
702     std::string tableName = "tableName";
703     DistributedData::VBuckets extends = { { g_RdbVBucket } };
704     auto result = store->MergeMigratedData(tableName, std::move(extends));
705     EXPECT_EQ(result, GeneralError::E_ERROR);
706 
707     MockRelationalStoreDelegate mockDelegate;
708     store->delegate_ = &mockDelegate;
709     result = store->MergeMigratedData(tableName, std::move(extends));
710     EXPECT_EQ(result, GeneralError::E_OK);
711 }
712 
713 /**
714 * @tc.name: Sync
715 * @tc.desc: RdbGeneralStore Sync function test
716 * @tc.type: FUNC
717 * @tc.require:
718 * @tc.author: SQL
719 */
720 HWTEST_F(RdbGeneralStoreTest, Sync, TestSize.Level1)
721 {
722     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
723     ASSERT_NE(store, nullptr);
724     GeneralStore::Devices devices;
725     MockQuery query;
726     GeneralStore::DetailAsync async;
727     SyncParam syncParam;
728     auto result = store->Sync(devices, query, async, syncParam);
729     EXPECT_EQ(result.first, GeneralError::E_ALREADY_CLOSED);
730 
731     MockRelationalStoreDelegate mockDelegate;
732     store->delegate_ = &mockDelegate;
733     result = store->Sync(devices, query, async, syncParam);
734     EXPECT_EQ(result.first, GeneralError::E_OK);
735 }
736 
737 /**
738 * @tc.name: PreSharing
739 * @tc.desc: RdbGeneralStore PreSharing function test
740 * @tc.type: FUNC
741 * @tc.require:
742 * @tc.author: SQL
743 */
744 HWTEST_F(RdbGeneralStoreTest, PreSharing, TestSize.Level1)
745 {
746     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
747     ASSERT_NE(store, nullptr);
748     MockQuery query;
749     auto [errCode, result] = store->PreSharing(query);
750     EXPECT_NE(errCode, GeneralError::E_OK);
751     EXPECT_EQ(result, nullptr);
752 }
753 
754 /**
755 * @tc.name: ExtractExtend
756 * @tc.desc: RdbGeneralStore ExtractExtend function test
757 * @tc.type: FUNC
758 * @tc.require:
759 * @tc.author: SQL
760 */
761 HWTEST_F(RdbGeneralStoreTest, ExtractExtend, TestSize.Level1)
762 {
763     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
764     ASSERT_NE(store, nullptr);
765     RdbGeneralStore::VBucket extend = { { "#gid", { "0000000" } }, { "#flag", { true } },
766         { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#cloud_gid", { "cloud_gid" } } };
767     DistributedData::VBuckets extends = { { extend } };
768     auto result = store->ExtractExtend(extends);
769     EXPECT_EQ(result.size(), extends.size());
770     DistributedData::VBuckets values;
771     result = store->ExtractExtend(values);
772     EXPECT_EQ(result.size(), values.size());
773 }
774 
775 /**
776 * @tc.name: Clean
777 * @tc.desc: RdbGeneralStore Clean function test
778 * @tc.type: FUNC
779 * @tc.require:
780 * @tc.author: SQL
781 */
782 HWTEST_F(RdbGeneralStoreTest, Clean, TestSize.Level1)
783 {
784     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
785     ASSERT_NE(store, nullptr);
786     std::string tableName = "tableName";
787     std::vector<std::string> devices = { "device1", "device2" };
788     auto result = store->Clean(devices, -1, tableName);
789     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
790     result = store->Clean(devices, GeneralStore::CLEAN_MODE_BUTT + 1, tableName);
791     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
792     result = store->Clean(devices, GeneralStore::CLOUD_INFO, tableName);
793     EXPECT_EQ(result, GeneralError::E_ALREADY_CLOSED);
794 
795     MockRelationalStoreDelegate mockDelegate;
796     store->delegate_ = &mockDelegate;
797     result = store->Clean(devices, GeneralStore::CLOUD_INFO, tableName);
798     EXPECT_EQ(result, GeneralError::E_OK);
799     result = store->Clean(devices, GeneralStore::CLOUD_DATA, tableName);
800     EXPECT_EQ(result, GeneralError::E_OK);
801     std::vector<std::string> devices1;
802     result = store->Clean(devices1, GeneralStore::NEARBY_DATA, tableName);
803     EXPECT_EQ(result, GeneralError::E_OK);
804 
805     g_testResult = true;
806     result = store->Clean(devices, GeneralStore::CLOUD_INFO, tableName);
807     EXPECT_EQ(result, GeneralError::E_ERROR);
808     result = store->Clean(devices, GeneralStore::CLOUD_DATA, tableName);
809     EXPECT_EQ(result, GeneralError::E_ERROR);
810     result = store->Clean(devices, GeneralStore::CLEAN_MODE_BUTT, tableName);
811     EXPECT_EQ(result, GeneralError::E_ERROR);
812     result = store->Clean(devices, GeneralStore::NEARBY_DATA, tableName);
813     EXPECT_EQ(result, GeneralError::E_OK);
814 }
815 
816 /**
817 * @tc.name: Watch
818 * @tc.desc: RdbGeneralStore Watch and Unwatch function test
819 * @tc.type: FUNC
820 * @tc.require:
821 * @tc.author: SQL
822 */
823 HWTEST_F(RdbGeneralStoreTest, Watch, TestSize.Level1)
824 {
825     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
826     ASSERT_NE(store, nullptr);
827     MockGeneralWatcher watcher;
828     auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher);
829     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
830     result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_CLOUD, watcher);
831     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
832 
833     result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
834     EXPECT_EQ(result, GeneralError::E_OK);
835     result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
836     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
837 
838     result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
839     EXPECT_EQ(result, GeneralError::E_OK);
840     result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
841     EXPECT_EQ(result, GeneralError::E_INVALID_ARGS);
842 }
843 
844 /**
845 * @tc.name: OnChange
846 * @tc.desc: RdbGeneralStore OnChange function test
847 * @tc.type: FUNC
848 * @tc.require:
849 * @tc.author: SQL
850 */
851 HWTEST_F(RdbGeneralStoreTest, OnChange, TestSize.Level1)
852 {
853     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
854     ASSERT_NE(store, nullptr);
855     MockGeneralWatcher watcher;
856     MockStoreChangedData data;
857     DistributedDB::ChangedData changedData;
858     store->observer_.OnChange(data);
859     store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData));
860     auto result = store->Watch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
861     EXPECT_EQ(result, GeneralError::E_OK);
862     store->observer_.OnChange(data);
863     store->observer_.OnChange(DistributedDB::Origin::ORIGIN_CLOUD, "originalId", std::move(changedData));
864     result = store->Unwatch(GeneralWatcher::Origin::ORIGIN_ALL, watcher);
865     EXPECT_EQ(result, GeneralError::E_OK);
866 }
867 
868 /**
869 * @tc.name: Release
870 * @tc.desc: RdbGeneralStore Release and AddRef function test
871 * @tc.type: FUNC
872 * @tc.require:
873 * @tc.author: SQL
874 */
875 HWTEST_F(RdbGeneralStoreTest, Release, TestSize.Level1)
876 {
877     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
878     ASSERT_NE(store, nullptr);
879     auto result = store->Release();
880     EXPECT_EQ(result, 0);
881     store = new (std::nothrow) RdbGeneralStore(metaData_);
882     store->ref_ = 0;
883     result = store->Release();
884     EXPECT_EQ(result, 0);
885     store->ref_ = 2;
886     result = store->Release();
887     EXPECT_EQ(result, 1);
888 
889     result = store->AddRef();
890     EXPECT_EQ(result, 2);
891     store->ref_ = 0;
892     result = store->AddRef();
893     EXPECT_EQ(result, 0);
894 }
895 
896 /**
897 * @tc.name: SetDistributedTables
898 * @tc.desc: RdbGeneralStore SetDistributedTables function test
899 * @tc.type: FUNC
900 * @tc.require:
901 * @tc.author: SQL
902 */
903 HWTEST_F(RdbGeneralStoreTest, SetDistributedTables, TestSize.Level1)
904 {
905     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
906     ASSERT_NE(store, nullptr);
907     std::vector<std::string> tables = { "table1", "table2" };
908     int32_t type = 0;
909     std::vector<DistributedData::Reference> references;
910     auto result = store->SetDistributedTables(tables, type, references);
911     EXPECT_EQ(result, GeneralError::E_ALREADY_CLOSED);
912 
913     MockRelationalStoreDelegate mockDelegate;
914     store->delegate_ = &mockDelegate;
915     result = store->SetDistributedTables(tables, type, references);
916     EXPECT_EQ(result, GeneralError::E_OK);
917 
918     std::vector<std::string> test = { "test" };
919     result = store->SetDistributedTables(test, type, references);
920     EXPECT_EQ(result, GeneralError::E_ERROR);
921     g_testResult = true;
922     result = store->SetDistributedTables(tables, type, references);
923     EXPECT_EQ(result, GeneralError::E_ERROR);
924 }
925 
926 /**
927 * @tc.name: SetTrackerTable
928 * @tc.desc: RdbGeneralStore SetTrackerTable function test
929 * @tc.type: FUNC
930 * @tc.require:
931 * @tc.author: SQL
932 */
933 HWTEST_F(RdbGeneralStoreTest, SetTrackerTable, TestSize.Level1)
934 {
935     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
936     ASSERT_NE(store, nullptr);
937     std::string tableName = "tableName";
938     std::set<std::string> trackerColNames = { "col1", "col2" };
939     std::string extendColName = "extendColName";
940     auto result = store->SetTrackerTable(tableName, trackerColNames, extendColName);
941     EXPECT_EQ(result, GeneralError::E_ALREADY_CLOSED);
942 
943     MockRelationalStoreDelegate mockDelegate;
944     store->delegate_ = &mockDelegate;
945     result = store->SetTrackerTable(tableName, trackerColNames, extendColName);
946     EXPECT_EQ(result, GeneralError::E_OK);
947     result = store->SetTrackerTable("WITH_INVENTORY_DATA", trackerColNames, extendColName);
948     EXPECT_EQ(result, GeneralError::E_WITH_INVENTORY_DATA);
949     result = store->SetTrackerTable("test", trackerColNames, extendColName);
950     EXPECT_EQ(result, GeneralError::E_ERROR);
951 }
952 
953 /**
954 * @tc.name: RemoteQuery
955 * @tc.desc: RdbGeneralStore RemoteQuery function test
956 * @tc.type: FUNC
957 * @tc.require:
958 * @tc.author: SQL
959 */
960 HWTEST_F(RdbGeneralStoreTest, RemoteQuery, TestSize.Level1)
961 {
962     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
963     ASSERT_NE(store, nullptr);
964     std::string device = "device";
965     DistributedDB::RemoteCondition remoteCondition;
966     MockRelationalStoreDelegate mockDelegate;
967     store->delegate_ = &mockDelegate;
968     auto result = store->RemoteQuery("test", remoteCondition);
969     EXPECT_EQ(result, nullptr);
970     result = store->RemoteQuery(device, remoteCondition);
971     EXPECT_NE(result, nullptr);
972 }
973 
974 /**
975 * @tc.name: ConvertStatus
976 * @tc.desc: RdbGeneralStore ConvertStatus function test
977 * @tc.type: FUNC
978 * @tc.require:
979 * @tc.author: SQL
980 */
981 HWTEST_F(RdbGeneralStoreTest, ConvertStatus, TestSize.Level1)
982 {
983     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
984     ASSERT_NE(store, nullptr);
985     auto result = store->ConvertStatus(DBStatus::OK);
986     EXPECT_EQ(result, GeneralError::E_OK);
987     result = store->ConvertStatus(DBStatus::CLOUD_NETWORK_ERROR);
988     EXPECT_EQ(result, GeneralError::E_NETWORK_ERROR);
989     result = store->ConvertStatus(DBStatus::CLOUD_LOCK_ERROR);
990     EXPECT_EQ(result, GeneralError::E_LOCKED_BY_OTHERS);
991     result = store->ConvertStatus(DBStatus::CLOUD_FULL_RECORDS);
992     EXPECT_EQ(result, GeneralError::E_RECODE_LIMIT_EXCEEDED);
993     result = store->ConvertStatus(DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT);
994     EXPECT_EQ(result, GeneralError::E_NO_SPACE_FOR_ASSET);
995     result = store->ConvertStatus(DBStatus::BUSY);
996     EXPECT_EQ(result, GeneralError::E_BUSY);
997     result = store->ConvertStatus(DBStatus::DB_ERROR);
998     EXPECT_EQ(result, GeneralError::E_ERROR);
999 }
1000 
1001 /**
1002 * @tc.name: QuerySql
1003 * @tc.desc: RdbGeneralStore QuerySql function test
1004 * @tc.type: FUNC
1005 * @tc.require:
1006 * @tc.author: SQL
1007 */
1008 HWTEST_F(RdbGeneralStoreTest, QuerySql, TestSize.Level1)
1009 {
1010     auto store = new (std::nothrow) RdbGeneralStore(metaData_);
1011     ASSERT_NE(store, nullptr);
1012     MockRelationalStoreDelegate mockDelegate;
1013     store->delegate_ = &mockDelegate;
1014     auto [err1, result1] = store->QuerySql("", std::move(g_RdbValues));
1015     EXPECT_EQ(err1, GeneralError::E_ERROR);
1016     EXPECT_TRUE(result1.empty());
1017 
1018     auto [err2, result2] = store->QuerySql("sql", std::move(g_RdbValues));
1019     EXPECT_EQ(err1, GeneralError::E_ERROR);
1020     EXPECT_TRUE(result2.empty());
1021 }
1022 } // namespace DistributedRDBTest
1023 } // namespace OHOS::Test