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
16 #include <gtest/gtest.h>
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <iostream>
22 #include <string>
23
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_open_callback.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::NativeRdb;
30
31 static std::shared_ptr<RdbStore> rdbStore;
32
33 class RdbStoreDistributedTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
TearDown()38 void TearDown() {}
39
40 void InsertValue(std::shared_ptr<RdbStore> &store);
41 void CheckResultSet(std::shared_ptr<RdbStore> &store);
42
43 static constexpr int ddmsGroupId_ = 1000;
44 static const std::string DRDB_NAME;
45 static const std::string DRDB_PATH;
46 };
47
48 const std::string RdbStoreDistributedTest::DRDB_NAME = "distributed_rdb.db";
49 const std::string RdbStoreDistributedTest::DRDB_PATH = "/data/test/";
50
51 class TestOpenCallback : public RdbOpenCallback {
52 public:
OnCreate(RdbStore & store)53 int OnCreate(RdbStore& store) override
54 {
55 std::string sql = "CREATE TABLE IF NOT EXISTS employee ("
56 "id INTEGER PRIMARY KEY AUTOINCREMENT,"
57 "name TEXT NOT NULL,"
58 "age INTEGER,"
59 "salary REAL,"
60 "data BLOB)";
61 store.ExecuteSql(sql);
62 return 0;
63 }
64
OnOpen(RdbStore & store)65 int OnOpen(RdbStore& store) override
66 {
67 return 0;
68 }
69
OnUpgrade(RdbStore & store,int currentVersion,int targetVersion)70 int OnUpgrade(RdbStore& store, int currentVersion, int targetVersion) override
71 {
72 return 0;
73 }
74 };
75
SetUpTestCase()76 void RdbStoreDistributedTest::SetUpTestCase()
77 {
78 int errCode = 0;
79 std::string path = RdbStoreDistributedTest::DRDB_PATH + RdbStoreDistributedTest::DRDB_NAME;
80 RdbHelper::DeleteRdbStore(path);
81 int fd = open(path.c_str(), O_CREAT, S_IRWXU | S_IRWXG);
82 if (fd < 0) {
83 return;
84 }
85 if (fd > 0) {
86 close(fd);
87 }
88 chown(path.c_str(), 0, ddmsGroupId_);
89
90 RdbStoreConfig config(path);
91 config.SetBundleName("com.example.distributed.rdb");
92 config.SetName(RdbStoreDistributedTest::DRDB_NAME);
93 TestOpenCallback callback;
94 rdbStore = RdbHelper::GetRdbStore(config, 1, callback, errCode);
95 EXPECT_NE(nullptr, rdbStore);
96 }
97
TearDownTestCase()98 void RdbStoreDistributedTest::TearDownTestCase()
99 {
100 RdbHelper::DeleteRdbStore(RdbStoreDistributedTest::DRDB_PATH + RdbStoreDistributedTest::DRDB_NAME);
101 }
102
SetUp()103 void RdbStoreDistributedTest::SetUp()
104 {
105 EXPECT_NE(nullptr, rdbStore);
106 rdbStore->ExecuteSql("DELETE FROM test");
107 }
108
InsertValue(std::shared_ptr<RdbStore> & store)109 void RdbStoreDistributedTest::InsertValue(std::shared_ptr<RdbStore> &store)
110 {
111 int64_t id;
112 ValuesBucket values;
113
114 values.PutInt("id", 1);
115 values.PutString("name", std::string("zhangsan"));
116 values.PutInt("age", 18); // 18 age
117 values.PutDouble("salary", 100.5); // 100.5
118 values.PutBlob("data", std::vector<uint8_t>{ 1, 2, 3 });
119 EXPECT_EQ(E_OK, store->Insert(id, "employee", values));
120 EXPECT_EQ(1, id);
121 }
122
CheckResultSet(std::shared_ptr<RdbStore> & store)123 void RdbStoreDistributedTest::CheckResultSet(std::shared_ptr<RdbStore> &store)
124 {
125 std::shared_ptr<ResultSet> resultSet =
126 store->QuerySql("SELECT * FROM employee WHERE name = ?", std::vector<std::string>{ "zhangsan" });
127 EXPECT_NE(nullptr, resultSet);
128
129 int columnIndex;
130 int intVal;
131 std::string strVal;
132 ColumnType columnType;
133 int position;
134 int ret = resultSet->GetRowIndex(position);
135 EXPECT_EQ(E_OK, ret);
136 EXPECT_EQ(-1, position);
137
138 ret = resultSet->GetColumnType(0, columnType);
139 EXPECT_EQ(E_INVALID_STATEMENT, ret);
140
141 ret = resultSet->GoToFirstRow();
142 EXPECT_EQ(E_OK, ret);
143
144 ret = resultSet->GetColumnIndex("id", columnIndex);
145 EXPECT_EQ(E_OK, ret);
146 EXPECT_EQ(0, columnIndex);
147 ret = resultSet->GetColumnType(columnIndex, columnType);
148 EXPECT_EQ(E_OK, ret);
149 EXPECT_EQ(columnType, ColumnType::TYPE_INTEGER);
150 ret = resultSet->GetInt(columnIndex, intVal);
151 EXPECT_EQ(E_OK, ret);
152 EXPECT_EQ(1, intVal);
153
154 ret = resultSet->GetColumnIndex("name", columnIndex);
155 EXPECT_EQ(E_OK, ret);
156 ret = resultSet->GetColumnType(columnIndex, columnType);
157 EXPECT_EQ(E_OK, ret);
158 EXPECT_EQ(ColumnType::TYPE_STRING, columnType);
159 ret = resultSet->GetString(columnIndex, strVal);
160 EXPECT_EQ(E_OK, ret);
161 EXPECT_EQ("zhangsan", strVal);
162 }
163
164 /**
165 * @tc.name: RdbStore_Distributed_Test_001
166 * @tc.desc: test RdbStore set distributed tables
167 * @tc.type: FUNC
168 */
169 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_001, TestSize.Level1)
170 {
171 EXPECT_NE(nullptr, rdbStore);
172 InsertValue(rdbStore);
173 CheckResultSet(rdbStore);
174 }
175
176 /**
177 * @tc.name: RdbStore_Distributed_Test_002
178 * @tc.desc: Abnormal testCase of ObtainDistributedTableName, if networkId is ""
179 * @tc.type: FUNC
180 */
181 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_002, TestSize.Level2)
182 {
183 EXPECT_NE(nullptr, rdbStore);
184 int errCode;
185 EXPECT_EQ("", rdbStore->ObtainDistributedTableName("", "employee", errCode));
186 EXPECT_EQ(-1, errCode);
187 }
188
189 /**
190 * @tc.name: RdbStore_Distributed_Test_003
191 * @tc.desc: Abnormal testCase of ObtainDistributedTableName, if networkId is invalid
192 * @tc.type: FUNC
193 */
194 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_003, TestSize.Level2)
195 {
196 EXPECT_NE(nullptr, rdbStore);
197 int errCode;
198 EXPECT_EQ("", rdbStore->ObtainDistributedTableName("123456", "employee", errCode));
199 EXPECT_EQ(-1, errCode);
200 }
201
202 /**
203 * @tc.name: RdbStore_Distributed_Test_004
204 * @tc.desc: Abnormal testCase of SetDistributedTables
205 * @tc.type: FUNC
206 */
207 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_Test_004, TestSize.Level2)
208 {
209 int errCode;
210 std::vector<std::string> tables;
211 OHOS::DistributedRdb::DistributedConfig distributedConfig;
212
213 // if tabels empty, return ok
214 errCode = rdbStore->SetDistributedTables(tables, 1, distributedConfig);
215 EXPECT_EQ(E_OK, errCode);
216
217 // if tabels not empty, IPC_SEND failed
218 tables.push_back("employee");
219 errCode = rdbStore->SetDistributedTables(tables, 1, distributedConfig);
220 EXPECT_NE(E_OK, errCode);
221
222 std::string path = RdbStoreDistributedTest::DRDB_PATH + "test.db";
223 RdbStoreConfig config(path);
224 TestOpenCallback callback;
225 std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, callback, errCode);
226 EXPECT_NE(nullptr, store);
227
228 // if tabels not empty, bundleName empty
229 errCode = store->SetDistributedTables(tables, 1, distributedConfig);
230 EXPECT_EQ(E_INVALID_ARGS, errCode);
231
232 RdbHelper::DeleteRdbStore(path);
233 }
234
235 /**
236 * @tc.name: RdbStore_Distributed_Test_005
237 * @tc.desc: Normal testCase of Sync
238 * @tc.type: FUNC
239 */
240 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_Test_005, TestSize.Level2)
241 {
242 int errCode;
243 OHOS::DistributedRdb::SyncOption option = { OHOS::DistributedRdb::TIME_FIRST, false };
244 AbsRdbPredicates predicate("employee");
245 std::vector<std::string> tables;
246
247 // get rdb service succeeded, if configuration file has already been configured
248 errCode = rdbStore->Sync(option, predicate, OHOS::DistributedRdb::AsyncBrief());
249 EXPECT_EQ(E_OK, errCode);
250
251 errCode = rdbStore->Sync(option, tables, OHOS::DistributedRdb::AsyncDetail());
252 EXPECT_EQ(E_OK, errCode);
253
254 errCode = rdbStore->Sync(option, predicate, OHOS::DistributedRdb::AsyncDetail());
255 EXPECT_EQ(E_OK, errCode);
256
257 std::string path = RdbStoreDistributedTest::DRDB_PATH + "test.db";
258 RdbStoreConfig config(path);
259 TestOpenCallback callback;
260 std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, callback, errCode);
261 EXPECT_NE(nullptr, store);
262
263 // get rdb service failed, if not configured
264 errCode = store->Sync(option, predicate, OHOS::DistributedRdb::AsyncBrief());
265 EXPECT_EQ(E_INVALID_ARGS, errCode);
266 errCode = store->Sync(option, tables, nullptr);
267 EXPECT_EQ(E_INVALID_ARGS, errCode);
268
269 RdbHelper::DeleteRdbStore(path);
270 }