1 /*
2  * Copyright (c) 2021 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 #ifndef DISTRIBUTEDDB_STORAGE_SINGLE_VER_NATURAL_STORE_TESTCASE_H
16 #define DISTRIBUTEDDB_STORAGE_SINGLE_VER_NATURAL_STORE_TESTCASE_H
17 
18 #include <gtest/gtest.h>
19 #include "db_errno.h"
20 #include "distributeddb_data_generate_unit_test.h"
21 #include "distributeddb_tools_unit_test.h"
22 #include "multi_ver_natural_store.h"
23 #include "sqlite_local_kvdb.h"
24 #include "sqlite_single_ver_natural_store.h"
25 #include "sqlite_single_ver_natural_store_connection.h"
26 #include "sqlite_utils.h"
27 
28 struct SyncData {
29     std::vector<uint8_t> hashKey;
30     std::vector<uint8_t> key;
31     std::vector<uint8_t> value;
32     uint64_t timestamp;
33     uint64_t flag;
34     std::string deviceInfo;
35 };
36 
37 class DistributedDBStorageSingleVerNaturalStoreTestCase final {
38 public:
DistributedDBStorageSingleVerNaturalStoreTestCase()39     DistributedDBStorageSingleVerNaturalStoreTestCase() {};
~DistributedDBStorageSingleVerNaturalStoreTestCase()40     ~DistributedDBStorageSingleVerNaturalStoreTestCase() {};
41 
42     static void GetSyncData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
43         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
44 
45     static void GetSyncData002(DistributedDB::SQLiteSingleVerNaturalStore *&store,
46         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
47 
48     static void GetSyncData003(DistributedDB::SQLiteSingleVerNaturalStore *&store,
49         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
50 
51     static void GetSyncData004(DistributedDB::SQLiteSingleVerNaturalStore *&store,
52         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
53 
54     static void GetSyncData005(DistributedDB::SQLiteSingleVerNaturalStore *&store,
55         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
56 
57     static void GetSyncData006(DistributedDB::SQLiteSingleVerNaturalStore *&store,
58         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
59 
60     static void PutSyncData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
61         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
62 
63     static void PutSyncData002(DistributedDB::SQLiteSingleVerNaturalStore *&store,
64         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
65 
66     static void PutSyncData003(DistributedDB::SQLiteSingleVerNaturalStore *&store,
67         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
68 
69     static void PutMetaData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
70         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
71 
72     static void GetMetaData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
73         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
74 
75     static void DeleteMetaData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
76         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
77 
78     static void GetCurrentMaxTimestamp001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
79         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
80 
81     static void GetCurrentMaxTimestamp002(DistributedDB::SQLiteSingleVerNaturalStore *&store);
82 
83     static void LocalDatabaseOperate001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
84         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
85 
86     static void LocalDatabaseOperate002(DistributedDB::SQLiteSingleVerNaturalStore *&store,
87         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
88 
89     static void LocalDatabaseOperate003(DistributedDB::SQLiteSingleVerNaturalStore *&store,
90         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
91 
92     static void SyncDatabaseOperate001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
93         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
94 
95     static void SyncDatabaseOperate002(DistributedDB::SQLiteSingleVerNaturalStore *&store,
96         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
97 
98     static void SyncDatabaseOperate003(DistributedDB::SQLiteSingleVerNaturalStore *&store,
99         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
100 
101     static void SyncDatabaseOperate004(DistributedDB::SQLiteSingleVerNaturalStore *&store,
102         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
103 
104     static void SyncDatabaseOperate005(DistributedDB::SQLiteSingleVerNaturalStore *&store,
105         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
106 
107     static void SyncDatabaseOperate006(DistributedDB::SQLiteSingleVerNaturalStore *&store,
108         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
109 
110     static void ClearRemoteData001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
111         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
112 
113     static void DeleteUserKeyValue001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
114         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
115 
116     static void MemoryDbDeleteUserKeyValue001(DistributedDB::SQLiteSingleVerNaturalStore *&store,
117         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
118 
119     static void DeleteUserKeyValue002(DistributedDB::SQLiteSingleVerNaturalStore *&store,
120         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
121 
122     static void DeleteUserKeyValue003(DistributedDB::SQLiteSingleVerNaturalStore *&store,
123         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
124 
125     static void DeleteUserKeyValue004(DistributedDB::SQLiteSingleVerNaturalStore *&store,
126         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
127 
128     static void MemoryDbDeleteUserKeyValue004(DistributedDB::SQLiteSingleVerNaturalStore *&store,
129         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
130 
131     static void DeleteUserKeyValue005(DistributedDB::SQLiteSingleVerNaturalStore *&store,
132         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
133 
134     static void MemoryDbDeleteUserKeyValue005(DistributedDB::SQLiteSingleVerNaturalStore *&store,
135         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
136 
137     static void DeleteUserKeyValue006(DistributedDB::SQLiteSingleVerNaturalStore *&store,
138         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, const std::string &url);
139     static int GetRawSyncData(const std::string &dbName, const std::string &strSql, std::vector<SyncData> &vecSyncData);
140 
141 private:
142     static bool IsSqlinteExistKey(const std::vector<SyncData> &vecSyncData, const std::vector<uint8_t> &key);
143 
144     static void TestMetaDataPutAndGet(DistributedDB::SQLiteSingleVerNaturalStore *&store,
145     DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
146 
147     static void TestMetaDataDeleteByPrefixKey(DistributedDB::SQLiteSingleVerNaturalStore *&store,
148         DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection);
149 
150     static void DataBaseCommonPutOperate(DistributedDB::SQLiteSingleVerNaturalStore *&store,
151      DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, DistributedDB::IOption option);
152 
153     static void DataBaseCommonDeleteOperate(DistributedDB::SQLiteSingleVerNaturalStore *&store,
154     DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, DistributedDB::IOption option);
155 
156     static void DataBaseCommonGetOperate(DistributedDB::SQLiteSingleVerNaturalStore *&store,
157     DistributedDB::SQLiteSingleVerNaturalStoreConnection *&connection, DistributedDB::IOption option);
158 };
159 #endif