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