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 #ifdef USE_RD_KERNEL
16 #include <gtest/gtest.h>
17 
18 #include "db_constant.h"
19 #include "db_common.h"
20 #include "distributeddb_storage_rd_single_ver_natural_store_testcase.h"
21 
22 using namespace testing::ext;
23 using namespace DistributedDB;
24 using namespace DistributedDBUnitTest;
25 using namespace std;
26 
27 namespace {
28     DistributedDB::KvStoreConfig g_config;
29 
30     std::string g_testDir;
31     std::string g_databaseName;
32     std::string g_identifier;
33 
34     DistributedDB::RdSingleVerNaturalStore *g_store = nullptr;
35     DistributedDB::RdSingleVerNaturalStoreConnection *g_connection = nullptr;
36 
37 class DistributedDBStorageRdSingleVerNaturalStoreTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void DistributedDBStorageRdSingleVerNaturalStoreTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void DistributedDBStorageRdSingleVerNaturalStoreTest::TearDownTestCase(void) {}
49 
SetUp(void)50 void DistributedDBStorageRdSingleVerNaturalStoreTest::SetUp(void)
51 {
52     DistributedDBToolsUnitTest::PrintTestCaseInfo();
53     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
54     LOGD("DistributedDBStorageRdSingleVerNaturalStoreTest dir is %s", g_testDir.c_str());
55     std::string oriIdentifier = APP_ID + "-" + USER_ID + "-" + "TestGeneralNB";
56     std::string identifier = DBCommon::TransferHashString(oriIdentifier);
57     std::string g_identifier = DBCommon::TransferStringToHex(identifier);
58 
59     g_databaseName = "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR + "/" + DBConstant::MAINDB_DIR + "/" +
60         DBConstant::SINGLE_VER_DATA_STORE + ".db";
61     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR);
62     KvDBProperties property;
63     property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
64     property.SetStringProp(KvDBProperties::STORE_ID, "TestGeneralNB");
65     property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, g_identifier);
66     property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE_RD_KERNAL);
67 
68     g_store = new (std::nothrow) RdSingleVerNaturalStore;
69     ASSERT_NE(g_store, nullptr);
70     ASSERT_EQ(g_store->Open(property), E_OK);
71 
72     int erroCode = E_OK;
73     g_connection = static_cast<RdSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(erroCode));
74     ASSERT_NE(g_connection, nullptr);
75     g_store->DecObjRef(g_store);
76     EXPECT_EQ(erroCode, E_OK);
77 }
78 
TearDown(void)79 void DistributedDBStorageRdSingleVerNaturalStoreTest::TearDown(void)
80 {
81     if (g_connection != nullptr) {
82         g_connection->Close();
83         g_connection = nullptr;
84     }
85 
86     g_store = nullptr;
87     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir + "/" + g_identifier + "/" +
88         DBConstant::SINGLE_SUB_DIR);
89 }
90 
91 /**
92   * @tc.name: SyncDatabaseOperate001
93   * @tc.desc: To test the function of inserting data of the local device in the synchronization database.
94   * @tc.type: FUNC
95   * @tc.require: AR000CCPOM
96   * @tc.author: wangbingquan
97   */
98 HWTEST_F(DistributedDBStorageRdSingleVerNaturalStoreTest, SyncDatabaseOperate001, TestSize.Level1)
99 {
100     /**
101      * @tc.steps: step1/2. Set Ioption to the local data and insert a record of key1 and value1.
102      * @tc.expected: step1/2. Return OK.
103      */
104     /**
105      * @tc.steps: step3. Set Ioption to the local data and obtain the value of key1.
106      *  Check whether the value is the same as the value of value1.
107      * @tc.expected: step3. The obtained value and value2 are the same.
108      */
109     /**
110      * @tc.steps: step4. Ioption Set this parameter to the local data. Insert key1.
111      *  The value cannot be empty. value2(!=value1)
112      * @tc.expected: step4. Return OK.
113      */
114     /**
115      * @tc.steps: step5. Set Ioption to the local data, GetMetaData to obtain the value of key1,
116      *  and check whether the value is the same as the value of value2.
117      * @tc.expected: step5. The obtained  and value2 are the same.
118      */
119     /**
120      * @tc.steps: step6. The Ioption parameter is set to the local data.
121      *  The data record whose key is empty and value is not empty is inserted.
122      * @tc.expected: step6. Return E_INVALID_DATA.
123      */
124     /**
125      * @tc.steps: step7. Set Ioption to the local data, insert data
126      *  whose key2(!=key1) is not empty, and value is empty.
127      * @tc.expected: step7. Return OK.
128      */
129     /**
130      * @tc.steps: step8. Set option to local data, obtain the value of key2,
131      *  and check whether the value is empty.
132      * @tc.expected: step8. Return OK, value is empty.
133      */
134     /**
135      * @tc.steps: step9. Ioption Set the local data.
136      *  Insert the data whose key size is 1024 and value size is 4Mb.
137      * @tc.expected: step9. Return OK.
138      */
139     /**
140      * @tc.steps: step10/11. Set Ioption to the local data and insert data items
141      *  whose value is greater than 4Mb or key is bigger than 1Kb
142      * @tc.expected: step10/11. Return E_INVALID_ARGS.
143      */
144     DistributedDBStorageRdSingleVerNaturalStoreTestCase::SyncDatabaseOperate001(g_store, g_connection);
145 }
146 
147 /**
148   * @tc.name: SyncDatabaseOperate003
149   * @tc.desc: test the delete operation in sync database.
150   * @tc.type: FUNC
151   * @tc.require: AR000CCPOM
152   * @tc.author: wangbingquan
153   */
154 HWTEST_F(DistributedDBStorageRdSingleVerNaturalStoreTest, SyncDatabaseOperate003, TestSize.Level1)
155 {
156     /**
157      * @tc.steps: step2. Set Ioption to the local data and delete the data whose key is key1 (empty).
158      * @tc.expected: step2. Return E_INVALID_ARGS.
159      */
160     /**
161      * @tc.steps: step3. Set Ioption to the local data, insert non-null key1, and non-null value1 data.
162      * @tc.expected: step3. Return E_OK.
163      */
164     /**
165      * @tc.steps: step4. Set Ioption to the local data, obtain the value of key1,
166      *  and check whether the value is the same as that of value1.
167      * @tc.expected: step4. Return E_OK. The obtained value is the same as the value of value1.
168      */
169     /**
170      * @tc.steps: step5. Set Ioption to the local data and delete the data whose key is key1.
171      * @tc.expected: step5. Return E_OK.
172      */
173     /**
174      * @tc.steps: step5. Set Ioption to the local data and obtain the value of Key1.
175      * @tc.expected: step5. Return E_NOT_FOUND.
176      */
177     DistributedDBStorageRdSingleVerNaturalStoreTestCase::SyncDatabaseOperate003(g_store, g_connection);
178 }
179 
180 /**
181   * @tc.name: SyncDatabaseOperate005
182   * @tc.desc: test the reading for sync database.
183   * @tc.type: FUNC
184   * @tc.require: AR000CCPOM
185   * @tc.author: wangbingquan
186   */
187 HWTEST_F(DistributedDBStorageRdSingleVerNaturalStoreTest, SyncDatabaseOperate005, TestSize.Level1)
188 {
189     /**
190      * @tc.steps: step2. Set Ioption to the local data and delete the data whose key is key1 (empty).
191      * @tc.expected: step2. Return E_INVALID_ARGS.
192      */
193     /**
194      * @tc.steps: step3. Set Ioption to the local data, insert non-null key1, and non-null value1 data.
195      * @tc.expected: step3. Return E_OK.
196      */
197     /**
198      * @tc.steps: step4. Set Ioption to the local data, obtain the value of key1,
199      *  and check whether the value is the same as that of value1.
200      * @tc.expected: step4. Return E_OK. The obtained value is the same as the value of value1.
201      */
202     /**
203      * @tc.steps: step5. Set Ioption to the local data and obtain the value data of Key1.
204      *  Check whether the value is the same as the value of value2.
205      * @tc.expected: step4. Return E_OK, and the value is the same as the value of value2.
206      */
207     /**
208      * @tc.steps: step5. The Ioption is set to the local.
209      *  The data of the key1 and value2(!=value1) is inserted.
210      * @tc.expected: step4. Return E_OK.
211      */
212     DistributedDBStorageRdSingleVerNaturalStoreTestCase::SyncDatabaseOperate005(g_store, g_connection);
213 }
214 
215 /**
216   * @tc.name: SyncDatabaseOperate006
217   * @tc.desc: test the get entries for sync database
218   * @tc.type: FUNC
219   * @tc.require: AR000CCPOM
220   * @tc.author: wangbingquan
221   */
222 HWTEST_F(DistributedDBStorageRdSingleVerNaturalStoreTest, SyncDatabaseOperate006, TestSize.Level1)
223 {
224     /**
225      * @tc.steps: step2/3/4. Set Ioption to synchronous data.
226      * Insert the data of key=keyPrefix + 'a', value1.
227      * Insert the data of key=keyPrefix + 'c', value2.
228      * Insert the data of key length=keyPrefix length - 1, value3.
229      * @tc.expected: step2/3/4. Return E_NOT_FOUND.
230      */
231     /**
232      * @tc.steps: step5. Obtain all data whose prefixKey is keyPrefix.
233      * @tc.expected: step5. Return OK. The number of obtained data records is 2.
234      */
235     /**
236      * @tc.steps: step6. Obtain all data whose prefixKey is empty.
237      * @tc.expected: step6. Return OK. The number of obtained data records is 3.
238      */
239     /**
240      * @tc.steps: step7. Obtain all data whose prefixKey is keyPrefix.
241      * @tc.expected: step7. Return E_NOT_SUPPORT.
242      */
243     DistributedDBStorageRdSingleVerNaturalStoreTestCase::SyncDatabaseOperate006(g_store, g_connection);
244 }
245 
246 /**
247  * @tc.name: ExportBusy001
248  * @tc.desc: Test export with busy
249  * @tc.type: FUNC
250  * @tc.require:
251  * @tc.author: zhangqiquan
252  */
253 HWTEST_F(DistributedDBStorageRdSingleVerNaturalStoreTest, ExportBusy001, TestSize.Level1)
254 {
255     ASSERT_NE(g_store, nullptr);
256     ASSERT_EQ(g_store->TryToDisableConnection(OperatePerm::NORMAL_WRITE), E_OK);
257     CipherPassword password;
258     EXPECT_EQ(g_store->Export(g_testDir, password), -E_READ_ONLY);
259     g_store->ReEnableConnection(OperatePerm::NORMAL_WRITE);
260 }
261 }
262 #endif // USE_RD_KERNEL