1 /* 2 * Copyright (c) 2022 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 DISTRIBUTEDDB_TOOLS_TEST_H 17 #define DISTRIBUTEDDB_TOOLS_TEST_H 18 19 #include <mutex> 20 #include <string> 21 #include <sys/stat.h> 22 #include <sys/types.h> 23 #include <unistd.h> 24 #include <vector> 25 26 #include "db_types.h" 27 #include "store_types.h" 28 #include "kv_store_changed_data.h" 29 #include "kv_store_delegate_impl.h" 30 #include "kv_store_delegate_manager.h" 31 #include "kv_store_nb_delegate.h" 32 #include "kv_store_observer.h" 33 #include "log_print.h" 34 #include "message.h" 35 #include "query.h" 36 37 #include "sqlite_utils.h" 38 39 namespace DistributedDBTest { 40 using namespace DistributedDB; 41 42 class DistributedDBToolsTest final { 43 public: DistributedDBToolsTest()44 DistributedDBToolsTest() {} ~DistributedDBToolsTest()45 ~DistributedDBToolsTest() {} 46 47 static void TestDirInit(std::string &dir); 48 // remove the test db files in the test directory of dir. 49 static int RemoveTestDbFiles(const std::string &dir); 50 static int GetCurrentDir(std::string& dir); 51 static void GetRandomKeyValue(std::vector<uint8_t> &value, uint32_t defaultSize = 0); 52 static DBStatus SyncTestWithQuery(KvStoreNbDelegate* delegate, const std::vector<std::string>& devices, 53 SyncMode mode, std::map<std::string, DBStatus>& statuses, const Query &query); 54 static DBStatus SyncTest(KvStoreNbDelegate* delegate, const std::vector<std::string>& devices, SyncMode mode, 55 std::map<std::string, DBStatus>& statuses); 56 }; 57 58 class KvStoreObserverTest : public DistributedDB::KvStoreObserver { 59 public: 60 KvStoreObserverTest(); ~KvStoreObserverTest()61 ~KvStoreObserverTest() {} 62 63 // callback function will be called when the db data is changed. 64 void OnChange(const DistributedDB::KvStoreChangedData&); 65 66 private: 67 unsigned long callCount_; 68 bool isCleared_; 69 std::list<DistributedDB::Entry> inserted_; 70 std::list<DistributedDB::Entry> updated_; 71 std::list<DistributedDB::Entry> deleted_; 72 }; 73 74 class RdbTestUtils { 75 public: 76 static sqlite3 *CreateDataBase(const std::string &dbUri); 77 static int ExecSql(sqlite3 *db, const std::string &sql); 78 static int CreateDeviceTable(sqlite3 *db, const std::string &table, const std::string &device); 79 }; 80 } // namespace DistributedDBTest 81 #endif // DISTRIBUTEDDB_TOOLS_TEST_H