• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
16 #include <string>
17 
18 #include "relational_store_delegate.h"
19 #include "relational_store_manager.h"
20 #include "distributed_rdb_tools.h"
21 
22 using namespace std;
23 using namespace testing;
24 #if defined TESTCASES_USING_GTEST_EXT
25 using namespace testing::ext;
26 #endif
27 using namespace DistributedDB;
28 using namespace DistributedDBDataGenerator;
29 
30 // set sqlite3 rdb_A and create 33 dataTables
31 
32 namespace DistributedRelatinalDBExceptionOperation {
33 sqlite3 *gdb = nullptr;
34 
35 RelatetionalStoreManager *g_manager = nullptr;
36 
37 RelatetionalStoreDelegate *g_delegate = nullptr;
38 
39 RelatetionalStoreDelegate *g_delegate1 = nullptr;
40 
41 class DistributedRDBExceptionTest : public testing::Test {
42 public:
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45     void SetUp();
46     void TearDown();
47 private:
48 };
49 
SetUpTestCase(void)50 void DistributedRDBExceptionTest::SetUpTestCase(void)
51 {
52     bool init1 = DistributedRdbTools::InitSqlite3Store(gdb, g_rdbParameter1);
53     ASSERT_EQ(init1, true);
54     bool init2 = DistributedRdbTools::InitTableDataAndTrigger(gdb);
55     ASSERT_EQ(init2, true);
56     g_manager = new (std::nothrow) RelatetionalStoreManager(g_rdbParameter1.appId, g_rdbParameter1.userId);
57     DBStatus status1 = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate, g_rdbParameter1);
58     ASSERT_EQ(status1, DBStatus::OK);
59 }
60 
TearDownTestCase(void)61 void DistributedRDBExceptionTest::TearDownTestCase(void)
62 {
63     CloseSqlite3Store(gdb);
64 }
65 
SetUp(void)66 void DistributedRDBExceptionTest::SetUp(void)
67 {
68     UnitTest *test = UnitTest::GetInstance();
69     ASSERT_NE(test, nullptr);
70     const TestInfo *testinfo = test->current_test_info();
71     ASSERT_NE(testinfo, nullptr);
72     string testCaseName = string(testinfo->name());
73     MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
74 }
75 
TearDown(void)76 void DistributedRDBExceptionTest::TearDown(void)
77 {
78     MST_LOG("TearDownTestCase after case.");
79 }
80 
81 /**
82  * @tc.name:  dbPathException001
83  * @tc.desc: db path does not exist, openStore failed.
84  * @tc.type: FUNC
85  * @tc.require: SR000DORPP
86  * @tc.author: xuhongkang
87  */
88 HWTEST_F(DistributedRDBExceptionTest, dbPathException001, TestSize.Level4)
89 {
90     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter6);
91     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
92 }
93 
94 /**
95  * @tc.name:  dbPathException002
96  * @tc.desc: db path unreadable, openStore failed.
97  * @tc.type: FUNC
98  * @tc.require: SR000DORPP
99  * @tc.author: xuhongkang
100  */
101 HWTEST_F(DistributedRDBExceptionTest, dbPathException002, TestSize.Level4)
102 {
103     sqlite3 *db = nullptr;
104     bool init1 = DistributedRdbTools::InitSqlite3Store(db, g_rdbParameter7);
105     ASSERT_EQ(init1, true);
106     bool bpermission = DBStributedDB::OS::SetFilePermissions(g_rdbParameter7.path, S_IWUSR | S_IXUSR);
107     ASSERT_EQ(bpermission, true);
108     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter7);
109     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
110     bpermission = DBStributedDB::OS::SetFilePermissions(g_rdbParameter7.path, S_IWUSR | S_IREAD | S_IXUSR);
111     ASSERT_EQ(bpermission, true);
112     CloseSqlite3Store(db);
113 }
114 
115 /**
116  * @tc.name:  dbPathException003
117  * @tc.desc: db path unwritdable, openStore failed.
118  * @tc.type: FUNC
119  * @tc.require: SR000DORPP
120  * @tc.author: xuhongkang
121  */
122 HWTEST_F(DistributedRDBExceptionTest, dbPathException003, TestSize.Level4)
123 {
124     sqlite3 *db = nullptr;
125     bool init1 = DistributedRdbTools::InitSqlite3Store(db, g_rdbParameter8);
126     ASSERT_EQ(init1, true);
127     bool bpermission = DBStributedDB::OS::SetFilePermissions(g_rdbParameter8.path, S_IREAD  | S_IXUSR);
128     ASSERT_EQ(bpermission, true);
129     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter8);
130     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
131     bpermission = DBStributedDB::OS::SetFilePermissions(g_rdbParameter8.path, S_IWUSR | S_IREAD | S_IXUSR);
132     ASSERT_EQ(bpermission, true);
133     CloseSqlite3Store(db);
134 }
135 
136 /**
137  * @tc.name:  storeIdException001
138  * @tc.desc: storeId is empty, openStore failed.
139  * @tc.type: FUNC
140  * @tc.require: SR000DORPP
141  * @tc.author: xuhongkang
142  */
143 HWTEST_F(DistributedRDBExceptionTest, storeIdException001, TestSize.Level4)
144 {
145     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter2);
146     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
147 }
148 
149 /**
150  * @tc.name:  storeIdException002
151  * @tc.desc: storeId value larger 128, openStore failed.
152  * @tc.type: FUNC
153  * @tc.require: SR000DORPP
154  * @tc.author: xuhongkang
155  */
156 HWTEST_F(DistributedRDBExceptionTest, storeIdException002, TestSize.Level4)
157 {
158     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter5);
159     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
160 }
161 
162 /**
163  * @tc.name:  storeIdException003
164  * @tc.desc: storeId is illegal, openStore failed.
165  * @tc.type: FUNC
166  * @tc.require: SR000DORPP
167  * @tc.author: xuhongkang
168  */
169 HWTEST_F(DistributedRDBExceptionTest, storeIdException003, TestSize.Level4)
170 {
171     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter3);
172     ASSERT_EQ(status, DBStatus::INVALID_ARGS);
173 }
174 
175 /**
176  * @tc.name:  dbModeException001
177  * @tc.desc: db mode isn't wal or SYNCHRONOUS full, openStore failed.
178  * @tc.type: FUNC
179  * @tc.require: SR000DORPP
180  * @tc.author: xuhongkang
181  */
182 HWTEST_F(DistributedRDBExceptionTest, dbModeException001, TestSize.Level4)
183 {
184     sqlite3 *db = nullptr;
185     bool init1 = DistributedRdbTools::InitSqlite3Store(db, g_rdbParameter4);
186     ASSERT_EQ(init1, true);
187     bool res1 = DistributedRdbTools::Sqlite3ExecOpration(db, SQL_JOURNAL_MODE);
188     ASSERT_EQ(res1, true);
189     DBStatus status1 = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter4);
190     ASSERT_EQ(status1, DBStatus::NOT_SUPPORT);
191     CloseSqlite3Store(db);
192 
193     bool init2 = DistributedRdbTools::InitSqlite3Store(db, g_rdbParameter9);
194     ASSERT_EQ(init2, true);
195     bool res2 = DistributedRdbTools::Sqlite3ExecOpration(db, SQL_SYNCHRONOUS_MODE);
196     ASSERT_EQ(res2, true);
197     DBStatus status2 = DistributedRdbTools::GetOpenStoreStatus(g_manager, g_delegate1, g_rdbParameter4);
198     ASSERT_EQ(status2, DBStatus::NOT_SUPPORT);
199     CloseSqlite3Store(db);
200 }
201 
202 /**
203  * @tc.name:  tableException001
204  * @tc.desc: db hasn't non_table, createDistributedTable failed.
205  * @tc.type: FUNC
206  * @tc.require: SR000DORPP
207  * @tc.author: xuhongkang
208  */
209 HWTEST_F(DistributedRDBExceptionTest, tableException001, TestSize.Level4)
210 {
211     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, NON_EXISTENT_TABLE);
212     ASSERT_EQ(status2, DBStatus::INVALID_ARGS);
213 }
214 
215 /**
216  * @tc.name:  tableException002
217  * @tc.desc: db create natrualbase_rdb_A relatinalTable, createDistributedTable failed.
218  * @tc.type: FUNC
219  * @tc.require: SR000DORPP
220  * @tc.author: xuhongkang
221  */
222 HWTEST_F(DistributedRDBExceptionTest, tableException002, TestSize.Level4)
223 {
224     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, KEYWORD_START_TABLE);
225     ASSERT_EQ(status2, DBStatus::INVALID_ARGS);
226 }
227 
228 /**
229  * @tc.name:  tableException003
230  * @tc.desc: db NORMAL_RDB table has data, createDistributedTable failed.
231  * @tc.type: FUNC
232  * @tc.require: SR000DORPP
233  * @tc.author: xuhongkang
234  */
235 HWTEST_F(DistributedRDBExceptionTest, tableException003, TestSize.Level4)
236 {
237     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, NORMAL_TABLE);
238     ASSERT_EQ(status2, DBStatus::NOT_SUPPORT);
239 }
240 
241 /**
242  * @tc.name:  tableMulCreate001
243  * @tc.desc: db create RDB_1 RelatinalTable, multiple createDistributedTable successful.
244  * @tc.type: FUNC
245  * @tc.require: SR000DORPP
246  * @tc.author: xuhongkang
247  */
248 HWTEST_F(DistributedRDBExceptionTest, tableMulCreate001, TestSize.Level4)
249 {
250     for (int i = 1; i <= 5; i++) {
251         DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, gtableNameList[0]);
252         ASSERT_EQ(status2, DBStatus::OK);
253     }
254 }
255 
256 /**
257  * @tc.name:  mulTableCreate001
258  * @tc.desc: dbA createDistributedTable 32 tables, successful
259  * then dbA createDistributedTable the 33'th table failed.
260  * @tc.type: FUNC
261  * @tc.require: SR000DORPP
262  * @tc.author: xuhongkang
263  */
264 HWTEST_F(DistributedRDBExceptionTest, mulTableCreate001, TestSize.Level4)
265 {
266     for (auto tableName: gtableNameList) {
267         DBStatus status = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, tableName);
268         if (tableName == gtableNameList[32]) {
269             ASSERT_EQ(status, DBStatus::NOT_SUPPORT);
270         } else {
271             ASSERT_EQ(status, DBStatus::OK);
272         }
273     }
274 }
275 
276 /**
277  * @tc.name:  mulTableCreate002
278  * @tc.desc: dbA createDistributedTable RDB_1...20 tables, And
279  * dbB createDistributedTable RDB_21...32 tables,successful
280  * then dbA createDistributedTable the 33'th table failed.
281  * @tc.type: FUNC
282  * @tc.require: SR000DORPP
283  * @tc.author: xuhongkang
284  */
285 HWTEST_F(DistributedRDBExceptionTest, mulTableCreate002, TestSize.Level4)
286 {
287     RelatetionalStoreDelegate *delegateA = nullptr;
288     RelatetionalStoreDelegate *delegateB = nullptr;
289     DBStatus status = DistributedRdbTools::GetOpenStoreStatus(g_manager, delegateA, g_rdbParameter1);
290     ASSERT_EQ(status, DBStatus::OK);
291     status = DistributedRdbTools::GetOpenStoreStatus(g_manager, delegateB, g_rdbParameter1);
292     ASSERT_EQ(status, DBStatus::OK);
293     for (int index = 0; index < gtableNameList.size(); index++) {
294         if (index < 20) {
295             status = DistributedRdbTools::GetCreateDistributedTableStatus(delegateA, gtableNameList[index]);
296             ASSERT_EQ(status, DBStatus::OK);
297         } else if (index >= 20 && index < 32) {
298             status = DistributedRdbTools::GetCreateDistributedTableStatus(delegateB, gtableNameList[index]);
299             ASSERT_EQ(status, DBStatus::OK);
300         } else {
301             ASSERT_EQ(status, DBStatus::NOT_SUPPORT);
302         }
303     }
304 }
305 
306 /**
307  * @tc.name:  relatinalTable001
308  * @tc.desc: db has RDB_1 relatinalTable, then alter RDB_1 and createDistributedTable failed.
309  * @tc.type: FUNC
310  * @tc.require: SR000DORPP
311  * @tc.author: xuhongkang
312  */
313 HWTEST_F(DistributedRDBExceptionTest, relatinalTable001, TestSize.Level4)
314 {
315     bool result = DistributedRdbTools::AlterTableAttributes(gdb);
316     ASSERT_EQ(result, true);
317     for (int i = 0; i < 4; i++) {
318         DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, gtableNameList[i]);
319         ASSERT_EQ(status2, DBStatus::OK);
320     }
321 }
322 
323 /**
324  * @tc.name:  constraintTable001
325  * @tc.desc: db has constraint table, then createDistributedTable successful.
326  * @tc.type: FUNC
327  * @tc.require: SR000DORPP
328  * @tc.author: xuhongkang
329  */
330 HWTEST_F(DistributedRDBExceptionTest, constraintTable001, TestSize.Level4)
331 {
332     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, CONSTRAINT_TABLE);
333     ASSERT_EQ(status2, DBStatus::OK);
334 }
335 
336 /**
337  * @tc.name:  constraintTable001
338  * @tc.desc: db has relatinalTable RDB_5, then add data in local RDB_5 and createDistributedTable successful.
339  * @tc.type: FUNC
340  * @tc.require: SR000DORPP
341  * @tc.author: xuhongkang
342  */
343 HWTEST_F(DistributedRDBExceptionTest, constraintTable001, TestSize.Level4)
344 {
345     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, gtableNameList[4]);
346     ASSERT_EQ(status2, DBStatus::OK);
347     bool res1 = DistributedRdbTools::Sqlite3ExecOpration(db, SQL_INSERT_RDB5_TABLE);
348     ASSERT_EQ(res1, true);
349     DBStatus status2 = DistributedRdbTools::GetCreateDistributedTableStatus(g_delegate, gtableNameList[4]);
350     ASSERT_EQ(status2, DBStatus::OK);
351 }
352 }
353