• 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 
16 #include "connection.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <climits>
21 #include <string>
22 
23 #include "common.h"
24 #include "grd_type_export.h"
25 #include "rdb_errno.h"
26 #include "rdb_helper.h"
27 #include "rdb_open_callback.h"
28 #include "rdb_store.h"
29 #include "rdb_store_config.h"
30 #include "sqlite_connection.h"
31 #include "sqlite_global_config.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::NativeRdb;
35 
36 namespace Test {
37 class ConnectionTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
SetUp(void)41     void SetUp(void){};
TearDown(void)42     void TearDown(void){};
43     static const std::string rdbStorePath;
44 };
45 
46 const std::string ConnectionTest::rdbStorePath = RDB_TEST_PATH + "connection_ut_test.db";
47 
48 class ConnectionTestOpenCallback : public RdbOpenCallback {
49 public:
50     int OnCreate(RdbStore &store) override;
51     int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
52 };
53 
OnCreate(RdbStore & store)54 int ConnectionTestOpenCallback::OnCreate(RdbStore &store)
55 {
56     return E_OK;
57 }
58 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)59 int ConnectionTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
60 {
61     return E_OK;
62 }
63 
SetUpTestCase(void)64 void ConnectionTest::SetUpTestCase(void)
65 {
66 }
67 
TearDownTestCase(void)68 void ConnectionTest::TearDownTestCase(void)
69 {
70 }
71 
72 /**
73  * @tc.name: Connection_Test_001
74  * @tc.desc: Normal testCase of sqlite_utils for IsSpecial, if sqlType is special
75  * @tc.type: FUNC
76  */
77 HWTEST_F(ConnectionTest, Connection_Test_001, TestSize.Level1)
78 {
79     SqliteGlobalConfig::InitSqliteGlobalConfig();
80     RdbStoreConfig config(rdbStorePath);
81     config.SetDBType(OHOS::NativeRdb::DBType::DB_BUTT);
82     auto [errCode, connection] = Connection::Create(config, true);
83     EXPECT_EQ(errCode, E_INVALID_ARGS);
84     EXPECT_EQ(connection, nullptr);
85 
86     config.SetDBType(OHOS::NativeRdb::DBType::DB_SQLITE);
87     auto [errCode1, connection1] = Connection::Create(config, true);
88     EXPECT_EQ(errCode1, E_OK);
89     EXPECT_NE(connection1, nullptr);
90 }
91 
92 /**
93  * @tc.name: Connection_Test_002
94  * @tc.desc: Normal testCase of sqlite_utils for IsSpecial, if sqlType is special
95  * @tc.type: FUNC
96  */
97 HWTEST_F(ConnectionTest, Connection_Test_002, TestSize.Level1)
98 {
99     RdbStoreConfig config(rdbStorePath);
100     config.SetDBType(OHOS::NativeRdb::DBType::DB_BUTT);
101     int ret = Connection::Repair(config);
102     EXPECT_EQ(ret, E_INVALID_ARGS);
103 
104     config.SetDBType(OHOS::NativeRdb::DBType::DB_SQLITE);
105     ret = Connection::Repair(config);
106     EXPECT_EQ(ret, E_NOT_SUPPORT);
107 }
108 
109 /**
110  * @tc.name: SetEncryptAgo_Test_001
111  * @tc.desc: The test case is to test whether the param is available.
112  * @tc.type: FUNC
113  */
114 HWTEST_F(ConnectionTest, SetEncryptAgo_Test_001, TestSize.Level2)
115 {
116     int errCode;
117     RdbStoreConfig config(rdbStorePath);
118     auto conn = std::make_shared<SqliteConnection>(config, true);
119     config.SetIter(-1);
120     errCode = conn->SetEncryptAgo(config);
121     EXPECT_EQ(errCode, E_INVALID_ARGS);
122 }
123 
124 /**
125  * @tc.name: ResetKey_Test_001
126  * @tc.desc: The test case is to test whether ResetKey is a write connection.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(ConnectionTest, ReSetKey_Test_001, TestSize.Level2)
130 {
131     int errCode;
132     RdbStoreConfig config(rdbStorePath);
133     auto conn = std::make_shared<SqliteConnection>(config, false);
134     errCode = conn->ResetKey(config);
135     EXPECT_EQ(errCode, E_OK);
136 }
137 
138 /**
139  * @tc.name: SetEncrypt_Test_001
140  * @tc.desc: The test case is to test whether SetEncrypt is a memory RDB.
141  * @tc.type: FUNC
142  */
143 HWTEST_F(ConnectionTest, SetEncrypt_Test_001, TestSize.Level2)
144 {
145     int errCode;
146     RdbStoreConfig config(rdbStorePath);
147     auto conn = std::make_shared<SqliteConnection>(config, true);
148     config.SetEncryptStatus(true);
149     config.SetStorageMode(StorageMode::MODE_MEMORY);
150     errCode = conn->SetEncrypt(config);
151     EXPECT_EQ(errCode, E_NOT_SUPPORT);
152 }
153 
154 /**
155  * @tc.name: RegDefaultFunctions_Test_001
156  * @tc.desc: The testCase of RegDefaultFunctions.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(ConnectionTest, RegDefaultFunctions_Test_001, TestSize.Level2)
160 {
161     int errCode;
162     RdbStoreConfig config(rdbStorePath);
163     auto conn = std::make_shared<SqliteConnection>(config, true);
164     errCode = conn->RegDefaultFunctions(nullptr);
165     EXPECT_EQ(errCode, SQLITE_OK);
166 }
167 
168 /**
169  * @tc.name: SetTokenizer_Test_001
170  * @tc.desc: The testCase of SetTokenizer.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(ConnectionTest, SetTokenizer_Test_001, TestSize.Level2)
174 {
175     int errCode;
176     RdbStoreConfig config(rdbStorePath);
177     auto conn = std::make_shared<SqliteConnection>(config, true);
178     config.SetTokenizer(TOKENIZER_END);
179     errCode = conn->SetTokenizer(config);
180     EXPECT_EQ(errCode, E_INVALID_ARGS);
181 }
182 
183 /**
184  * @tc.name: Backup_Test_001
185  * @tc.desc: The testCase of Backup.
186  * @tc.type: FUNC
187  */
188 HWTEST_F(ConnectionTest, Backup_Test_001, TestSize.Level2)
189 {
190     int errCode;
191     RdbStoreConfig config(rdbStorePath);
192     auto conn = std::make_shared<SqliteConnection>(config, true);
193     std::shared_ptr<SlaveStatus> slaveStatus = std::make_shared<SlaveStatus>(SlaveStatus::BACKING_UP);
194     errCode = conn->Backup("test", { 1, 2, 3 }, true, slaveStatus);
195     EXPECT_EQ(errCode, E_OK);
196     EXPECT_EQ(*slaveStatus, BACKING_UP);
197 }
198 
199 /**
200  * @tc.name: ExchangeVerify_Test_001
201  * @tc.desc: The testCase of ExchangeVerify.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(ConnectionTest, ExchangeVerify_Test_001, TestSize.Level2)
205 {
206     int errCode;
207     RdbStoreConfig config(rdbStorePath);
208     auto conn = std::make_shared<SqliteConnection>(config, true);
209     errCode = conn->ExchangeVerify(false);
210     EXPECT_EQ(errCode, E_ALREADY_CLOSED);
211 }
212 
213 /**
214  * @tc.name: VerifySlaveIntegrity_Test_001
215  * @tc.desc: The testCase of VerifySlaveIntegrity.
216  * @tc.type: FUNC
217  */
218 HWTEST_F(ConnectionTest, VerifySlaveIntegrity_Test_001, TestSize.Level2)
219 {
220     int errCode;
221     RdbStoreConfig config(rdbStorePath);
222     auto conn = std::make_shared<SqliteConnection>(config, true);
223     errCode = conn->VerifySlaveIntegrity();
224     EXPECT_EQ(errCode, E_ALREADY_CLOSED);
225 }
226 
227 /**
228  * @tc.name: IsDbVersionBelowSlave_Test_001
229  * @tc.desc: The testCase of IsDbVersionBelowSlave.
230  * @tc.type: FUNC
231  */
232 HWTEST_F(ConnectionTest, IsDbVersionBelowSlave_Test_001, TestSize.Level2)
233 {
234     RdbStoreConfig config(rdbStorePath);
235     auto conn = std::make_shared<SqliteConnection>(config, true);
236     bool res = conn->IsDbVersionBelowSlave();
237     EXPECT_EQ(res, false);
238 }
239 
240 /**
241  * @tc.name: CheckReplicaIntegrity_Test_001
242  * @tc.desc: The testCase of CheckReplicaIntegrity.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(ConnectionTest, CheckReplicaIntegrity_Test_001, TestSize.Level2)
246 {
247     RdbStoreConfig config(rdbStorePath);
248     config.SetDBType(OHOS::NativeRdb::DBType::DB_BUTT);
249     EXPECT_EQ(Connection::CheckReplicaIntegrity(config), E_INVALID_ARGS);
250 
251     config.SetDBType(OHOS::NativeRdb::DBType::DB_VECTOR);
252     EXPECT_EQ(Connection::CheckReplicaIntegrity(config), E_NOT_SUPPORT);
253 }
254 
255 /**
256  * @tc.name: CheckReplicaIntegrity_Test_002
257  * @tc.desc: The testCase of CheckReplicaIntegrity.
258  * @tc.type: FUNC
259  */
260 HWTEST_F(ConnectionTest, CheckReplicaIntegrity_Test_002, TestSize.Level2)
261 {
262     RdbHelper::DeleteRdbStore(rdbStorePath);
263     RdbStoreConfig config(rdbStorePath);
264     config.SetDBType(OHOS::NativeRdb::DBType::DB_SQLITE);
265     config.SetHaMode(HAMode::SINGLE);
266     EXPECT_EQ(Connection::CheckReplicaIntegrity(config), E_NOT_SUPPORT);
267     RdbHelper::DeleteRdbStore(rdbStorePath);
268 }
269 
MockReplicaChecker(const RdbStoreConfig & config)270 static int32_t MockReplicaChecker(const RdbStoreConfig &config)
271 {
272     return E_OK;
273 }
274 
275 /**
276  * @tc.name: RegisterReplicaChecker_Test_001
277  * @tc.desc: The testCase of RegisterReplicaChecker.
278  * @tc.type: FUNC
279  */
280 HWTEST_F(ConnectionTest, RegisterReplicaChecker_Test_001, TestSize.Level2)
281 {
282     EXPECT_EQ(Connection::RegisterReplicaChecker(OHOS::NativeRdb::DBType::DB_BUTT, nullptr), E_INVALID_ARGS);
283     EXPECT_EQ(Connection::RegisterReplicaChecker(OHOS::NativeRdb::DBType::DB_VECTOR, MockReplicaChecker), E_OK);
284     EXPECT_EQ(Connection::RegisterReplicaChecker(OHOS::NativeRdb::DBType::DB_VECTOR, MockReplicaChecker), E_OK);
285     EXPECT_EQ(Connection::RegisterReplicaChecker(OHOS::NativeRdb::DBType::DB_SQLITE, MockReplicaChecker), E_OK);
286     EXPECT_EQ(Connection::RegisterReplicaChecker(OHOS::NativeRdb::DBType::DB_SQLITE, MockReplicaChecker), E_OK);
287 }
288 } // namespace Test