• 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 <chrono>
16 #include <gtest/gtest.h>
17 #include <string>
18 
19 #include "auto_launch_callback.h"
20 #include "distributed_test_tools.h"
21 #include "distributeddb_data_generator.h"
22 #include "distributeddb_nb_test_tools.h"
23 #include "distributeddb_schema_test_tools.h"
24 #include "kv_store_delegate.h"
25 #include "kv_store_delegate_manager.h"
26 #include "kv_store_nb_delegate.h"
27 #include "process_communicator_test_stub.h"
28 
29 using namespace std;
30 using namespace chrono;
31 using namespace testing;
32 #if defined TESTCASES_USING_GTEST_EXT
33 using namespace testing::ext;
34 #endif
35 using namespace std::placeholders;
36 using namespace DistributedDB;
37 using namespace DistributedDBDataGenerator;
38 
39 namespace DistributeddbNbAutolaunch {
40 #ifdef NB_AUTOLAUNCH
41 const int PARAM_CRITICAL_LENGTH = 128;
42 #endif
43 const int STRING_ONE_BYTE = 1;
44 const int STRING_TEN_BYTE = 10;
45 const int STRING_ONE_TWO_EIGHT_BYTE = 128;
46 const int STRING_ONE_TWO_NINE_BYTE = 129;
47 
48 class DistributeddbNbAutolaunchTest  : public testing::Test {
49 public:
50     static void SetUpTestCase(void);
51     static void TearDownTestCase(void);
52     void SetUp();
53     void TearDown();
54 private:
55 };
56 
SetUpTestCase(void)57 void DistributeddbNbAutolaunchTest ::SetUpTestCase(void)
58 {
59     SetDir(DistributedDBConstant::NB_DIRECTOR);
60 }
61 
TearDownTestCase(void)62 void DistributeddbNbAutolaunchTest ::TearDownTestCase(void)
63 {
64 }
65 
SetUp(void)66 void DistributeddbNbAutolaunchTest ::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 DistributeddbNbAutolaunchTest ::TearDown(void)
77 {
78 }
79 
80 /*
81  * @tc.name: ParamCheck 001
82  * @tc.desc: test EnableKvStoreAutoLaunch and DisableKvStoreAutoLaunch interface checking 3 elements function.
83  * @tc.type: FUNC
84  * @tc.require: SR000DR9JR
85  * @tc.author: fengxiaoyun
86  */
87 #ifdef NB_AUTOLAUNCH
88 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck001, TestSize.Level0)
89 {
90     string schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_DEFINE_1, VALID_INDEX_1);
91     AutoLaunchOption option = {true, false, CipherType::DEFAULT, NULL_PASSWD, schema, true,
92         DistributedDBConstant::NB_DIRECTOR, nullptr};
93     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
94     ASSERT_NE(manager, nullptr);
95     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
96     /**
97      * @tc.steps: step1. call EnableKvStoreAutoLaunch and DisableKvStoreAutoLaunch interface using the storeId such as
98      *    "", or string with "\0", or length of the storeId is 128\129, or that has Uppercase letters, Lowercase
99      *    letters, digit "_", or some Special characters.
100      * @tc.expected: step1. only the storeId has "\0", or length is 128, or has Upper or lower letters or digit or "_"
101      *    can enable success, otherwise it will return INVALID_ARGS.
102      */
103     string storeId1(PARAM_CRITICAL_LENGTH, 'a');
104     vector<string> storeIdValid = {storeId1, "abc\0", "Abc576_"};
105     for (unsigned long index = 0; index < storeIdValid.size(); index++) {
106         EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, storeIdValid[index], option, nullptr), OK);
107         EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, storeIdValid[index]), OK);
108         EXPECT_EQ(manager->DeleteKvStore(storeIdValid[index]), OK);
109     }
110 
111     string storeId2(PARAM_CRITICAL_LENGTH + 1, 'a');
112     vector<string> storeIdInValid = {"", "6\\", "6//", "6&", "6^", "6%", "6#", "6-", "6中文", "6Ää", storeId2};
113     for (unsigned long index = 0; index < storeIdInValid.size(); index++) {
114         EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, storeIdInValid[index], option, nullptr),
115             INVALID_ARGS);
116         EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, storeIdInValid[index]), NOT_FOUND);
117     }
118 
119     /**
120      * @tc.steps: step2. call EnableKvStoreAutoLaunch and DisableKvStoreAutoLaunch interface using the appId such as
121      *    "", or length of the storeId is 128\129.
122      * @tc.expected: step2. only the storeId has the length as 128
123      *    can enable success, otherwise it will return INVALID_ARGS.
124      */
125     string appId1(PARAM_CRITICAL_LENGTH, 'b');
126     string appId2(PARAM_CRITICAL_LENGTH + 1, 'b');
127     vector<string> appId = {"", appId1, appId2};
128     DBStatus result[] = {INVALID_ARGS, OK, INVALID_ARGS};
129     DBStatus resultDisable[] = {NOT_FOUND, OK, NOT_FOUND};
130     for (unsigned long index = 0; index < appId.size(); index++) {
131         EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, appId[index], STORE_ID_1, option, nullptr),
132             result[index]);
133         EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, appId[index], STORE_ID_1), resultDisable[index]);
134     }
135     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
136 
137     /**
138      * @tc.steps: step3. call EnableKvStoreAutoLaunch and DisableKvStoreAutoLaunch interface using the userId such as
139      *    "", or length of the storeId is 128\129.
140      * @tc.expected: step3. only the storeId has the length as 128
141      *    can enable success, otherwise it will return INVALID_ARGS.
142      */
143     string userId1(PARAM_CRITICAL_LENGTH, 'c');
144     string userId2(PARAM_CRITICAL_LENGTH + 1, 'c');
145     vector<string> userId = {"", appId1, appId2};
146     for (unsigned long index = 0; index < userId.size(); index++) {
147         EXPECT_EQ(manager->EnableKvStoreAutoLaunch(userId[index], APP_ID_1, STORE_ID_1, option, nullptr),
148             result[index]);
149         EXPECT_EQ(manager->DisableKvStoreAutoLaunch(userId[index], APP_ID_1, STORE_ID_1), resultDisable[index]);
150     }
151     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
152 
153     delete manager;
154     manager = nullptr;
155 }
156 
157 /*
158  * @tc.name: ParamCheck 002
159  * @tc.desc: test EnableKvStoreAutoLaunch can only effect when option.createIfNecessary=true if the DB is not exist.
160  * @tc.type: FUNC
161  * @tc.require: SR000DR9JR
162  * @tc.author: fengxiaoyun
163  */
164 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck002, TestSize.Level0)
165 {
166     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
167     ASSERT_NE(manager, nullptr);
168     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
169     /**
170      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is false,
171      *  dataDir is NB_DIRECTOR and observer is TheAppoitObserver
172      * @tc.expected: step1. enable failed, and return DB_ERROR.
173      */
174     KvStoreObserverImpl *observer = nullptr;
175     AutoLaunchOption option;
176     option.createIfNecessary = false;
177     option.dataDir = DistributedDBConstant::NB_DIRECTOR;
178     option.observer = observer;
179     EXPECT_NE(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
180     /**
181      * @tc.steps: step2. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
182      *    and other settings don't change;
183      * @tc.expected: step2. enable succeed, and return OK.
184      */
185     option.createIfNecessary = true;
186     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
187 
188     /**
189      * @tc.steps: step3. create db use option.createIfNecessary = false, and put (k1, v1), (k2, v2);
190      * @tc.expected: step3. create and put succeed.
191      */
192     Option dbOption;
193     dbOption.createIfNecessary = false;
194     dbOption.isMemoryDb = false;
195     KvStoreNbDelegate *delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, dbOption);
196     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
197 
198     EXPECT_EQ(delegate->Put(KEY_1, VALUE_1), OK);
199     EXPECT_EQ(delegate->Put(KEY_2, VALUE_2), OK);
200 
201     /**
202      * @tc.steps: step4. update (k1, v1) to (k1, v2), and check the data with Get interface, and then delete (k2, v2);
203      * @tc.expected: step4. operate succeed.
204      */
205     EXPECT_EQ(delegate->Put(KEY_1, VALUE_2), OK);
206     Value realValue;
207     EXPECT_EQ(delegate->Get(KEY_1, realValue), OK);
208     EXPECT_EQ(realValue, VALUE_2);
209     EXPECT_EQ(delegate->Delete(KEY_2), OK);
210 
211     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
212     delegate = nullptr;
213     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
214     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
215     delete manager;
216     manager = nullptr;
217 }
218 
219 /*
220  * @tc.name: ParamCheck 003
221  * @tc.desc: verification of isEncrypted, cipher, passwd of AutoLaunchOption.
222  * @tc.type: FUNC
223  * @tc.require: SR000DR9JR
224  * @tc.author: fengxiaoyun
225  */
226 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck003, TestSize.Level0)
227 {
228     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
229     ASSERT_NE(manager, nullptr);
230     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
231     /**
232      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
233      *    isEncryptedDb = true, but the passwd is "";
234      * @tc.expected: step1. enable failed, and return INVALID_ARGS.
235      */
236     AutoLaunchOption option = {true, true, CipherType::DEFAULT, NULL_PASSWD, "", true,
237         DistributedDBConstant::NB_DIRECTOR, nullptr};
238     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), INVALID_ARGS);
239     /**
240      * @tc.steps: step2. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
241      *    isEncryptedDb = true, and the passwd is 100 length;
242      * @tc.expected: step2. enable succeed, and return OK.
243      */
244     CipherPassword passwd1, passwd2, passwd3;
245     vector<uint8_t> passwordVector1(ENCRYPT_COUNT, 'a');
246     passwd1.SetValue(passwordVector1.data(), ENCRYPT_COUNT);
247     option.passwd = passwd1;
248     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
249     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
250     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
251 
252     /**
253      * @tc.steps: step3. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
254      *    isEncryptedDb = true, the passwd is 128 length;
255      * @tc.expected: step3. enable succeed, and return OK.
256      */
257     vector<uint8_t> passwordVector2(PARAM_CRITICAL_LENGTH, 'a');
258     passwd2.SetValue(passwordVector2.data(), PARAM_CRITICAL_LENGTH);
259     option.passwd = passwd2;
260     option.cipher = CipherType::AES_256_GCM;
261     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
262     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
263     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
264     /**
265      * @tc.steps: step4. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
266      *    isEncryptedDb = true, the passwd is 129 length;
267      * @tc.expected: step4. enable failed, and return INVALID_ARGS.
268      */
269     vector<uint8_t> passwordVector3(PASSWD_BYTE, 'a');
270     passwd3.SetValue(passwordVector3.data(), PASSWD_BYTE);
271     option.passwd = passwd3;
272     option.cipher = CipherType::DEFAULT;
273     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), INVALID_ARGS);
274 
275     /**
276      * @tc.steps: step5. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
277      *    isEncryptedDb = false, the passwd is 129 length;
278      * @tc.expected: step5. enable success, and return OK.
279      */
280     option.isEncryptedDb = false;
281     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
282     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
283     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
284     delete manager;
285     manager = nullptr;
286 }
287 
288 /*
289  * @tc.name: ParamCheck 004
290  * @tc.desc: verify that isEncrypted, passwd of AutoLaunchOption must be the same to params of the db created already.
291  * @tc.type: FUNC
292  * @tc.require: SR000DR9JR
293  * @tc.author: fengxiaoyun
294  */
295 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck004, TestSize.Level0)
296 {
297     /**
298      * @tc.steps: step1. create db with the option isEncryptedDb = true, passwd = PASSWD_VECTOR_1;
299      * @tc.expected: step1. create successfully.
300      */
301     string schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_DEFINE_1, VALID_INDEX_1);
302     KvStoreDelegateManager *manager = nullptr;
303     Option dbOption;
304     dbOption.isEncryptedDb = true;
305     dbOption.passwd = PASSWD_VECTOR_1;
306     dbOption.schema = schema;
307     dbOption.isMemoryDb = false;
308     KvStoreNbDelegate *delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, dbOption);
309     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
310     /**
311      * @tc.steps: step2. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false,
312      *    isEncryptedDb = false, the passwd is passwd1 and a valid schema;
313      * @tc.expected: step2. enable failed, and return INVALID_PASSWD_OR_CORRUPTED_DB.
314      */
315     CipherPassword passwd1, passwd2;
316     passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
317     AutoLaunchOption option = {false, false, CipherType::DEFAULT, passwd1, schema, false,
318         DistributedDBConstant::NB_DIRECTOR, nullptr};
319     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr),
320         INVALID_PASSWD_OR_CORRUPTED_DB);
321 
322     /**
323      * @tc.steps: step3. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false,
324      *    isEncryptedDb = true, and the passwd is passwd2;
325      * @tc.expected: step3. enable failed, and return INVALID_PASSWD_OR_CORRUPTED_DB.
326      */
327     passwd2.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
328     option = {false, true, CipherType::DEFAULT, passwd2, schema, false, DistributedDBConstant::NB_DIRECTOR, nullptr};
329     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr),
330         INVALID_PASSWD_OR_CORRUPTED_DB);
331 
332     /**
333      * @tc.steps: step4. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false,
334      *    isEncryptedDb = true, and the passwd is passwd1;
335      * @tc.expected: step4. enable success, and return OK.
336      */
337     option = {false, true, CipherType::DEFAULT, passwd1, schema, false, DistributedDBConstant::NB_DIRECTOR, nullptr};
338     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
339     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
340 
341     EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_1, dbOption.isMemoryDb));
342 
343     /**
344      * @tc.steps: step5. create unEncrypted DB2, but passwd = p1;
345      * @tc.expected: step5. create success.
346      */
347     dbOption.isEncryptedDb = false;
348     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, dbOption);
349     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
350 
351     /**
352      * @tc.steps: step6. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false,
353      *    isEncryptedDb = true, and the passwd is passwd2;
354      * @tc.expected: step6. enable failed, and return INVALID_PASSWD_OR_CORRUPTED_DB.
355      */
356     passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
357     option = {false, true, CipherType::DEFAULT, passwd1, schema, false, DistributedDBConstant::NB_DIRECTOR, nullptr};
358     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_2, APP_ID_2, STORE_ID_2, option, nullptr),
359         INVALID_PASSWD_OR_CORRUPTED_DB);
360 
361     /**
362      * @tc.steps: step7. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false,
363      *    isEncryptedDb = false, and the passwd is passwd2;
364      * @tc.expected: step7. enable success, and return OK.
365      */
366     passwd2.SetValue(PASSWD_VECTOR_2.data(), PASSWD_VECTOR_2.size());
367     option = {false, false, CipherType::DEFAULT, passwd2, schema, false, DistributedDBConstant::NB_DIRECTOR, nullptr};
368     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_2, APP_ID_2, STORE_ID_2, option, nullptr), OK);
369     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_2, APP_ID_2, STORE_ID_2), OK);
370 
371     EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, dbOption.isMemoryDb));
372 }
373 
374 /*
375  * @tc.name: ParamCheck 005
376  * @tc.desc: verify that EnableKvStoreAutoLaunch interface would check the schema param's legitimacy.
377  * @tc.type: FUNC
378  * @tc.require: SR000DR9JR
379  * @tc.author: fengxiaoyun
380  */
381 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck005, TestSize.Level0)
382 {
383     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
384     ASSERT_NE(manager, nullptr);
385     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
386     /**
387      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
388      *    isEncryptedDb = false, and a valid schema;
389      * @tc.expected: step1. enable success, and return OK.
390      */
391     string schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_DEFINE_1, VALID_INDEX_1);
392     AutoLaunchOption option = {true, false, CipherType::DEFAULT, NULL_PASSWD, schema, false,
393         DistributedDBConstant::NB_DIRECTOR, nullptr};
394     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr), OK);
395     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
396     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
397     /**
398      * @tc.steps: step2. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
399      *    isEncryptedDb = false, and a invalid schema;
400      * @tc.expected: step2. enable failed, and return INVALID_SCHEMA.
401      */
402     string inValidSchema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, INVALID_DEFINE_2, VALID_INDEX_1);
403     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, inValidSchema, false,
404         DistributedDBConstant::NB_DIRECTOR, nullptr};
405     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr),
406         INVALID_SCHEMA);
407 
408     delete manager;
409     manager = nullptr;
410 }
411 
412 /*
413  * @tc.name: ParamCheck 006
414  * @tc.desc: verify that EnableKvStoreAutoLaunch interface would check the dataDir param's legitimacy.
415  * @tc.type: FUNC
416  * @tc.require: SR000DR9JR
417  * @tc.author: fengxiaoyun
418  */
419 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck006, TestSize.Level0)
420 {
421     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
422     ASSERT_NE(manager, nullptr);
423     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
424     /**
425      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true,
426      *    isEncryptedDb = false, and different dataDir that include many special characters;
427      * @tc.expected: step1. enable success, and return OK.
428      */
429     string middlePath = "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd/"
430         "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff/"
431         "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg/";
432     // the length of the valid string is 504-102 = 402
433     string tailPathValid(402 - (DistributedDBConstant::NB_DIRECTOR + middlePath).length(), 'e');
434     // the length of the invalid string is 504-101 = 403
435     string tailPathInvalid(403 - (DistributedDBConstant::NB_DIRECTOR + middlePath).length(), 'h');
436     string validLengthDir = DistributedDBConstant::NB_DIRECTOR + middlePath + tailPathValid;
437     string inValidLengthDir = DistributedDBConstant::NB_DIRECTOR + middlePath + tailPathInvalid;
438 
439     vector<string> dirs = {"", DistributedDBConstant::NB_DIRECTOR + "a\0", validLengthDir, inValidLengthDir,
440         DistributedDBConstant::NB_DIRECTOR + "a..b", DistributedDBConstant::NB_DIRECTOR + "a/…b",
441         DistributedDBConstant::NB_DIRECTOR + "a/中文", DistributedDBConstant::NB_DIRECTOR + "a\\//&^a%#",
442         DistributedDBConstant::NB_DIRECTOR + "diehgid/"};
443     vector<DBStatus> results = {INVALID_ARGS, OK, OK, INVALID_ARGS, OK, OK, OK, OK, OK, OK, INVALID_ARGS};
444     string schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_DEFINE_1, VALID_INDEX_1);
445 
446     AutoLaunchOption option;
447     DBStatus status;
448     RemoveDir(DistributedDBConstant::NB_DIRECTOR);
449     for (unsigned long index = 0; index < dirs.size(); index++) {
450         if (results[index] == OK) {
451             SetDir(dirs[index]);
452         }
453         option = {true, false, CipherType::DEFAULT, NULL_PASSWD, schema, false, dirs[index], nullptr};
454         status = manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, nullptr);
455         if (index == 3) { // the 3th element
456             EXPECT_NE(status, OK);
457         } else {
458             EXPECT_EQ(status, results[index]);
459         }
460         if (status == OK) {
461             EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
462         }
463         if (results[index] == OK) {
464             RemoveDir(dirs[index]);
465         }
466     }
467 
468     delete manager;
469     manager = nullptr;
470 }
471 
472 /*
473  * @tc.name: ParamCheck 007
474  * @tc.desc: verify that EnableKvStoreAutoLaunch many times, it can only be successfully when the option is same as the
475  *    first time unless call DisableKvStoreAutoLaunch interface to disable it.
476  * @tc.type: FUNC
477  * @tc.require: SR000DR9JR
478  * @tc.author: fengxiaoyun
479  */
480 HWTEST_F(DistributeddbNbAutolaunchTest, ParamCheck007, TestSize.Level0)
481 {
482     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
483     ASSERT_NE(manager, nullptr);
484     EXPECT_EQ(manager->SetKvStoreConfig({ .dataDir = DistributedDBConstant::NB_DIRECTOR }), OK);
485     AutoLaunchCallback callback;
486     auto notifier = bind(&AutoLaunchCallback::AutoLaunchNotifier, &callback, placeholders::_1, placeholders::_2,
487         placeholders::_3, placeholders::_4);
488     /**
489      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true and
490      *  isEncryptedDb is false
491      * @tc.expected: step1. enable success, and return OK.
492      */
493     KvStoreObserverImpl observer;
494     AutoLaunchOption option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", false,
495         DistributedDBConstant::NB_DIRECTOR, &observer};
496     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), OK);
497 
498     /**
499      * @tc.steps: step2. call EnableKvStoreAutoLaunch use the option with which createIfNecessary = false.
500      * @tc.expected: step2. enable failed, and return ALREADY_SET.
501      */
502     option = {false, false, CipherType::DEFAULT, NULL_PASSWD, "", false, DistributedDBConstant::NB_DIRECTOR, &observer};
503     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
504 
505     /**
506      * @tc.steps: step3. call EnableKvStoreAutoLaunch use the option with which isEncrypt = true, passwd = p1.
507      * @tc.expected: step3. enable failed, and return ALREADY_SET.
508      */
509     CipherPassword passwd1;
510     passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
511     option = {true, true, CipherType::DEFAULT, passwd1, "", false, DistributedDBConstant::NB_DIRECTOR, &observer};
512     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
513 
514     /**
515      * @tc.steps: step4. call EnableKvStoreAutoLaunch use the option with which schema is a valid schema.
516      * @tc.expected: step4. enable failed, and return ALREADY_SET.
517      */
518     string schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_DEFINE_1, VALID_INDEX_1);
519     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, schema, false,
520         DistributedDBConstant::NB_DIRECTOR, &observer};
521     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
522 
523     /**
524      * @tc.steps: step5. call EnableKvStoreAutoLaunch use the option with which createDirByStoreIdOnly = true.
525      * @tc.expected: step5. enable failed, and return ALREADY_SET.
526      */
527     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", true, DistributedDBConstant::NB_DIRECTOR, &observer};
528     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
529 
530     /**
531      * @tc.steps: step6. call EnableKvStoreAutoLaunch use the option with which dataDir = DIRECTOR.
532      * @tc.expected: step6. enable failed, and return ALREADY_SET.
533      */
534     SetDir(DIRECTOR);
535     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", false, DIRECTOR, &observer};
536     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
537 
538     /**
539      * @tc.steps: step7. call EnableKvStoreAutoLaunch use the option with which observer = observer2.
540      * @tc.expected: step7. enable failed, and return ALREADY_SET.
541      */
542     KvStoreObserverImpl observer2;
543     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", false, DistributedDBConstant::NB_DIRECTOR, &observer2};
544     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
545 
546     /**
547      * @tc.steps: step8. call EnableKvStoreAutoLaunch use the option with which notifier is not null.
548      * @tc.expected: step8. enable failed, and return ALREADY_SET.
549      */
550     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", false, DistributedDBConstant::NB_DIRECTOR, &observer};
551     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
552     /**
553      * @tc.steps: step9. call EnableKvStoreAutoLaunch use the option with the params is the same with the first time .
554      * @tc.expected: step9. enable failed, and return ALREADY_SET.
555      */
556     option = {true, false, CipherType::DEFAULT, NULL_PASSWD, "", false, DistributedDBConstant::NB_DIRECTOR, &observer};
557     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), ALREADY_SET);
558     /**
559      * @tc.steps: step10. call DisableKvStoreAutoLaunch interface to disable the function,
560      *     and use the option which is different from the first time EnableKvStoreAutoLaunch used.
561      * @tc.expected: step10. enable success, and return OK.
562      */
563     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
564     option = {true, true, CipherType::DEFAULT, passwd1, schema, true, DIRECTOR, &observer2};
565     EXPECT_EQ(manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1, option, notifier), OK);
566     EXPECT_EQ(manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1), OK);
567 
568     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
569     delete manager;
570     manager = nullptr;
571 }
572 #endif
VerifyDisableRetStatus(KvStoreDelegateManager * & manager)573 void VerifyDisableRetStatus(KvStoreDelegateManager *&manager)
574 {
575     DBStatus status = manager->DisableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1);
576     EXPECT_TRUE(status == OK || status == NOT_FOUND || status == BUSY);
577 }
578 /*
579  * @tc.name: ClosedSyncPressure 001
580  * @tc.desc: concurrency open/close operation of EnableKvStoreAutoLaunch or DisableKvStoreAutoLaunch.
581  * @tc.type: FUNC
582  * @tc.require: SR000DR9JR
583  * @tc.author: fengxiaoyun
584  */
585 HWTEST_F(DistributeddbNbAutolaunchTest, ClosedSyncPressure001, TestSize.Level0)
586 {
587     KvStoreNbDelegate *delegate = nullptr;
588     KvStoreDelegateManager *manager = nullptr;
589     Option option = g_option;
590     option.isMemoryDb = false;
591     AutoLaunchOption autoLaunchOption;
592     autoLaunchOption.isEncryptedDb = option.isEncryptedDb;
593     autoLaunchOption.passwd.SetValue(option.passwd.data(), option.passwd.size());
594     autoLaunchOption.dataDir = DistributedDBConstant::NB_DIRECTOR;
595     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, option);
596     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
597     EXPECT_TRUE(manager->CloseKvStore(delegate) == OK);
598     delegate = nullptr;
599     /**
600      * @tc.steps: step1. call EnableKvStoreAutoLaunch use the option with which createIfNecessary is true and
601      *  isEncryptedDb is false.
602      * @tc.expected: step1. enable success, and return OK.
603      */
604     vector<thread> threads;
605     const unsigned int threeThreads = 3;
606     for (unsigned int threadId = 0; threadId < threeThreads; ++threadId) {
__anonb29698940102() 607         threads.push_back(thread([&]() {
608             DBStatus enableStatus = manager->EnableKvStoreAutoLaunch(USER_ID_1, APP_ID_1, STORE_ID_1,
609                 autoLaunchOption, nullptr);
610             EXPECT_TRUE(enableStatus == OK || enableStatus == ALREADY_SET);
611         }));
612     }
613 
614     for (unsigned int threadId = 0; threadId < threeThreads; ++threadId) {
__anonb29698940202() 615         threads.push_back(thread([&]() {
616             VerifyDisableRetStatus(manager);
617         }));
618     }
619 
620     for (unsigned int threadId = 0; threadId <= threeThreads; ++threadId) {
__anonb29698940302() 621         threads.push_back(thread([&]() {
622             KvStoreNbDelegate *delegateThread = nullptr;
623             KvStoreDelegateManager *managerThread = nullptr;
624             option.createIfNecessary = false;
625             delegateThread = DistributedDBNbTestTools::GetNbDelegateSuccess(managerThread, g_dbParameter1, option);
626             ASSERT_TRUE(managerThread != nullptr && delegateThread != nullptr);
627             EXPECT_TRUE(managerThread->CloseKvStore(delegateThread) == OK);
628             delegateThread = nullptr;
629             delete managerThread;
630             managerThread = nullptr;
631         }));
632     }
633 
634     for (auto &th : threads) {
635         th.join();
636     }
637     VerifyDisableRetStatus(manager);
638 
639     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
640     delete manager;
641     manager = nullptr;
642 }
643 
644 #ifndef DB_DEBUG_ENV
645 /*
646  * @tc.name: SyncCommErr 001
647  * @tc.desc: Test Sync return Code, when Communicator get some error.
648  * @tc.type: FUNC
649  * @tc.require:
650  * @tc.author: xushaohua
651  */
652 HWTEST_F(DistributeddbNbAutolaunchTest, SyncCommErr001, TestSize.Level1)
653 {
654     KvStoreDelegateManager *manager = new (std::nothrow) KvStoreDelegateManager(APP_ID_1, USER_ID_1);
655     ASSERT_NE(manager, nullptr);
656     manager->SetProcessLabel("MST", "GetDevicesID");
657     std::shared_ptr<ProcessCommunicatorTestStub> communicator = std::make_shared<ProcessCommunicatorTestStub>();
658     manager->SetProcessCommunicator(communicator);
659 
660     /**
661      * @tc.steps: step1. Get a KvStoreNbDelegate delegate.
662      * @tc.expected: step1. delegate is not null.
663      */
664     Option dbOption;
665     KvStoreNbDelegate *delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter1, dbOption);
666     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
667 
668     /**
669      * @tc.steps: step2. Set the ProcessCommunicatorTestStub CommErr to make communicator error scene, and
670      *    call sync.
671      * @tc.expected: step2. Sync callback will return COMM_FAILURE.
672      */
673     communicator->SetCommErr(true);
674     const std::string testDeviceName = "test_device";
675     std::vector<std::string> deviceList;
676     deviceList.push_back(testDeviceName);
677 
__anonb29698940402(const std::map<std::string, DBStatus> &statusMap) 678     auto syncCallBack = [testDeviceName](const std::map<std::string, DBStatus> &statusMap) {
679         auto iter = statusMap.find(testDeviceName);
680         EXPECT_NE(iter, statusMap.end());
681         EXPECT_EQ(iter->second, COMM_FAILURE);
682     };
683     EXPECT_EQ(delegate->Sync(deviceList, SYNC_MODE_PUSH_ONLY, syncCallBack, true), OK);
684 
685     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
686     delegate = nullptr;
687     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_1), OK);
688     delete manager;
689     manager = nullptr;
690 }
691 #endif
692 
693 /*
694  * @tc.name: AutoLaunch 001
695  * @tc.desc: GetKvStoreIdentifier returns the responding hashIdentity if the parameters are valid,
696  *  or it returns empty string.
697  * @tc.type: FUNC
698  * @tc.require: SR000EPA24
699  * @tc.author: fengxiaoyun
700  */
701 HWTEST_F(DistributeddbNbAutolaunchTest, AutoLaunch001, TestSize.Level1)
702 {
703     KvStoreDelegateManager manager(APP_ID_1, USER_ID_1);
704     /**
705      * @tc.steps: step1. call GetKvStoreIdentifier with valid useId, appId and storeId
706      * @tc.expected: step1. returns valid hashIdentity.
707      */
708     string hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, STORE_ID_SYNC_1);
709     EXPECT_NE(hashIdentity, "");
710     /**
711      * @tc.steps: step2. call GetKvStoreIdentifier separately with empty string, 128 or 129 characters as userId
712      * @tc.expected: step2. returns empty string.
713      */
714     string userId = "";
715     hashIdentity = manager.GetKvStoreIdentifier(APP_ID_1, userId, STORE_ID_SYNC_1);
716     EXPECT_EQ(hashIdentity, "");
717 
718     GenerateFixedLenRandString(STRING_ONE_TWO_EIGHT_BYTE, RandType::ALPHA_NUM, userId);
719     hashIdentity = manager.GetKvStoreIdentifier(APP_ID_1, userId, STORE_ID_SYNC_1);
720     EXPECT_NE(hashIdentity, "");
721 
722     GenerateFixedLenRandString(STRING_ONE_TWO_NINE_BYTE, RandType::ALPHA_NUM, userId);
723     hashIdentity = manager.GetKvStoreIdentifier(APP_ID_1, userId, STORE_ID_SYNC_1);
724     EXPECT_EQ(hashIdentity, "");
725     /**
726      * @tc.steps: step3. call GetKvStoreIdentifier separately with empty string, 128 or 129 characters as appId
727      * @tc.expected: step3. returns empty string.
728      */
729     string appId = "";
730     hashIdentity = manager.GetKvStoreIdentifier(appId, USER_ID_1, STORE_ID_SYNC_1);
731     EXPECT_EQ(hashIdentity, "");
732 
733     GenerateFixedLenRandString(STRING_ONE_TWO_EIGHT_BYTE, RandType::ALPHA_NUM, appId);
734     hashIdentity = manager.GetKvStoreIdentifier(appId, USER_ID_1, STORE_ID_SYNC_1);
735     EXPECT_NE(hashIdentity, "");
736 
737     GenerateFixedLenRandString(STRING_ONE_TWO_NINE_BYTE, RandType::ALPHA_NUM, appId);
738     hashIdentity = manager.GetKvStoreIdentifier(appId, USER_ID_1, STORE_ID_SYNC_1);
739     EXPECT_EQ(hashIdentity, "");
740     /**
741      * @tc.steps: step4. call GetKvStoreIdentifier separately with empty string, '\0' included characters,
742      *  128 or 129 characters, upper case, lower case, number, _ randomed characters,
743      *  special characters(\\ // & ^ % # -, Chinese or Latin characters) as storeId
744      * @tc.expected: step4. returns empty string.
745      */
746     string storeId = "";
747     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
748     EXPECT_EQ(hashIdentity, "");
749 
750     string prefixStoreId, suffixStoreId;
751     GenerateFixedLenRandString(GetRandInt(STRING_ONE_BYTE, STRING_TEN_BYTE), RandType::ALPHA_NUM, prefixStoreId);
752     GenerateFixedLenRandString(GetRandInt(STRING_ONE_BYTE, STRING_TEN_BYTE), RandType::ALPHA_NUM, suffixStoreId);
753     storeId = prefixStoreId + "\0" + suffixStoreId;
754     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
755     EXPECT_NE(hashIdentity, "");
756 
757     GenerateFixedLenRandString(STRING_ONE_TWO_EIGHT_BYTE, RandType::ALPHA_NUM, storeId);
758     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
759     EXPECT_NE(hashIdentity, "");
760 
761     GenerateFixedLenRandString(STRING_ONE_TWO_NINE_BYTE, RandType::ALPHA_NUM, storeId);
762     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
763     EXPECT_EQ(hashIdentity, "");
764 
765     GenerateFixedLenRandString(ONE_K_LONG_STRING, RandType::ALPHA_NUM_UNDERLINE, storeId);
766     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
767     EXPECT_EQ(hashIdentity, "");
768 
769     GenerateFixedLenRandString(ONE_K_LONG_STRING, RandType::SPECIAL, storeId);
770     storeId += string("中文");
771     storeId += string("Ää");
772     hashIdentity = manager.GetKvStoreIdentifier(USER_ID_1, APP_ID_1, storeId);
773     EXPECT_EQ(hashIdentity, "");
774 }
775 }