1 /* 2 * Copyright (c) 2025 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 16 #ifndef RDB_GENERAL_UT_H 17 #define RDB_GENERAL_UT_H 18 19 #include "basic_unit_test.h" 20 #include "rdb_data_generator.h" 21 #include "relational_store_manager.h" 22 #include "virtual_asset_loader.h" 23 24 namespace DistributedDB { 25 const std::string g_defaultTable1 = "defaultTable1"; 26 const std::string g_defaultTable2 = "defaultTable2"; 27 const std::vector<uint8_t> PASSWD_VECTOR = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '@', '1'}; 28 29 class RDBGeneralUt : public BasicUnitTest { 30 public: 31 void SetUp() override; 32 void TearDown() override; 33 protected: 34 int InitDelegate(const StoreInfo &info) override; 35 std::pair<int, RelationalStoreDelegate *> OpenRDBStore(const StoreInfo &info); 36 int CloseDelegate(const StoreInfo &info) override; 37 void CloseAllDelegate() override; 38 39 // If SetOption is not invoked before InitDelegate is invoked, the default data of Option is used to open store. 40 void SetOption(const RelationalStoreDelegate::Option& option); 41 // If SetSchemaInfo is not invoked before InitDelegate is invoked, g_defaultSchemaInfo is used to create table. 42 void SetSchemaInfo(const StoreInfo &info, const DistributedDBUnitTest::UtDateBaseSchemaInfo& schemaInfo); 43 44 DistributedDBUnitTest::UtDateBaseSchemaInfo GetTableSchemaInfo(const StoreInfo &info) const; 45 DataBaseSchema GetSchema(const StoreInfo &info) const; 46 TableSchema GetTableSchema(const StoreInfo &info, const std::string &tableName = g_defaultTable1) const; 47 std::vector<TrackerSchema> GetAllTrackerSchema(const StoreInfo &info, const std::vector<std::string> &tables) const; 48 sqlite3 *GetSqliteHandle(const StoreInfo &info) const; 49 RelationalStoreDelegate *GetDelegate(const StoreInfo &info) const; 50 RelationalStoreDelegate::Option GetOption() const; 51 52 int InitDatabase(const StoreInfo &info); 53 54 int InsertLocalDBData(int64_t begin, int64_t count, const StoreInfo &info); 55 56 int ExecuteSQL(const std::string &sql, const StoreInfo &info); 57 58 int CreateDistributedTable(const StoreInfo &info, const std::string &table, 59 TableSyncType type = TableSyncType::DEVICE_COOPERATION); 60 61 int SetDistributedTables(const StoreInfo &info, const std::vector<std::string> &tables, 62 TableSyncType type = TableSyncType::DEVICE_COOPERATION); 63 64 // use for device to device sync 65 void BlockPush(const StoreInfo &from, const StoreInfo &to, const std::string &table, 66 DBStatus expectRet = DBStatus::OK); 67 68 void BlockPull(const StoreInfo &from, const StoreInfo &to, const std::string &table, 69 DBStatus expectRet = DBStatus::OK); 70 71 int CountTableData(const StoreInfo &info, const std::string &table, const std::string &condition = ""); 72 73 int CountTableDataByDev(const StoreInfo &info, const std::string &table, const std::string &dev, 74 const std::string &condition = ""); 75 76 int SetTrackerTables(const StoreInfo &info, const std::vector<std::string> &tables, bool isForce = false); 77 78 // use for cloud sync 79 std::shared_ptr<VirtualCloudDb> GetVirtualCloudDb() const; 80 81 std::shared_ptr<VirtualAssetLoader> GetVirtualAssetLoader() const; 82 83 void CloudBlockSync(const StoreInfo &from, const Query &query, DBStatus exceptStatus = OK, 84 DBStatus callbackExpect = OK); 85 86 void SetCloudDbConfig(const StoreInfo &info) const; 87 88 int GetCloudDataCount(const std::string &tableName) const; 89 90 void SetIsDbEncrypted(bool isdbEncrypted); 91 bool GetIsDbEncrypted() const; 92 int EncryptedDb(sqlite3 *db); 93 94 void BlockSync(const StoreInfo &from, const StoreInfo &to, const std::string &table, SyncMode mode, 95 DBStatus expectRet); 96 97 void RemoteQuery(const StoreInfo &from, const StoreInfo &to, const std::string &sql, DBStatus expectRet); 98 99 int PutMetaData(const StoreInfo &store, const Key &key, const Value &value); 100 101 mutable std::mutex storeMutex_; 102 std::map<StoreInfo, RelationalStoreDelegate *> stores_; 103 std::map<StoreInfo, sqlite3 *> sqliteDb_; 104 std::map<StoreInfo, DistributedDBUnitTest::UtDateBaseSchemaInfo> schemaInfoMap_; 105 std::shared_ptr<VirtualCloudDb> virtualCloudDb_ = nullptr; 106 std::shared_ptr<VirtualAssetLoader> virtualAssetLoader_ = nullptr; 107 RelationalStoreDelegate::Option option_; 108 bool isDbEncrypted_ = false; 109 }; 110 } 111 #endif // RDB_GENERAL_UT_H 112