• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 <cstdint>
17 #include <cstring>
18 #include <securec.h>
19 #include <string>
20 
21 #include "lnn_kv_adapter_wrapper.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_error_code.h"
24 #include "gtest/gtest.h"
25 #include "dsoftbus_enhance_interface.h"
26 #include "g_enhance_lnn_func.h"
27 #include "lnn_kv_adapter_wrapper_mock.h"
28 
29 using namespace std;
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace {
35 constexpr int32_t MAX_STRING_LEN = 4096;
36 constexpr int32_t MIN_STRING_LEN = 1;
37 constexpr int32_t APP_ID_LEN = 8;
38 constexpr int32_t STORE_ID_LEN = 19;
39 constexpr int32_t MIN_DBID_COUNT = 1;
40 const std::string APP_ID = "dsoftbus";
41 const std::string STORE_ID = "dsoftbus_kv_db_test";
42 } // namespace
43 static int32_t g_dbId = 1;
44 class KVAdapterWrapperTest : public testing::Test {
45 protected:
46     static void SetUpTestCase(void);
47     static void TearDownTestCase(void);
48     void SetUp();
49     void TearDown();
50 };
SetUpTestCase(void)51 void KVAdapterWrapperTest::SetUpTestCase(void)
52 {
53     int32_t dbID;
54     LnnCreateKvAdapter(&dbID, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
55     g_dbId = dbID;
56 
57     LnnCreateKvAdapter(&dbID, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
58 }
59 
TearDownTestCase(void)60 void KVAdapterWrapperTest::TearDownTestCase(void)
61 {
62     LnnDestroyKvAdapter(g_dbId + 1);
63 
64     LnnDestroyKvAdapter(g_dbId); // g_dbId = 1
65 }
66 
SetUp()67 void KVAdapterWrapperTest::SetUp() { }
68 
TearDown()69 void KVAdapterWrapperTest::TearDown() { }
70 
71 /**
72  * @tc.name: LnnPutDBData
73  * @tc.desc: LnnPutDBData
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData001, TestSize.Level1)
78 {
79     int32_t dbId = g_dbId;
80     string keyStr = "aaa";
81     string valueStr = "aaa";
82     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), 3, valueStr.c_str(), 3), SOFTBUS_OK);
83     dbId++;
84     EXPECT_EQ(
85         LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, valueStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
86     dbId = 0;
87     EXPECT_EQ(
88         LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, valueStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
89     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, valueStr.c_str(), MAX_STRING_LEN + 1),
90         SOFTBUS_INVALID_PARAM);
91     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, valueStr.c_str(), MIN_STRING_LEN - 1),
92         SOFTBUS_INVALID_PARAM);
93     char *valuePtr = nullptr;
94     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, valuePtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
95     EXPECT_EQ(
96         LnnPutDBData(dbId, keyStr.c_str(), MAX_STRING_LEN + 1, valuePtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
97     EXPECT_EQ(
98         LnnPutDBData(dbId, keyStr.c_str(), MIN_STRING_LEN - 1, valuePtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
99     char *keyPtr = nullptr;
100     EXPECT_EQ(LnnPutDBData(dbId, keyPtr, MIN_STRING_LEN - 1, valuePtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
101 }
102 
103 /**
104  * @tc.name: LnnDeleteDBData
105  * @tc.desc: LnnDeleteDBData
106  * @tc.type: FUNC
107  * @tc.require:
108  */
109 HWTEST_F(KVAdapterWrapperTest, LnnDelete001, TestSize.Level1)
110 {
111     int32_t dbId = g_dbId;
112     string keyStr = "aaa";
113     string valueStr = "ccc";
114     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), 3, valueStr.c_str(), 3), SOFTBUS_OK);
115     EXPECT_EQ(LnnDeleteDBData(dbId, keyStr.c_str(), 3), SOFTBUS_OK);
116     dbId++;
117     EXPECT_EQ(LnnDeleteDBData(dbId, keyStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
118     dbId = 0;
119     EXPECT_EQ(LnnDeleteDBData(dbId, keyStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
120     EXPECT_EQ(LnnDeleteDBData(dbId, keyStr.c_str(), MAX_STRING_LEN + 1), SOFTBUS_INVALID_PARAM);
121     EXPECT_EQ(LnnDeleteDBData(dbId, keyStr.c_str(), MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
122     char *keyPtr = nullptr;
123     EXPECT_EQ(LnnDeleteDBData(dbId, keyPtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
124 }
125 
126 /**
127  * @tc.name: LnnDeleteDBDataByPrefix
128  * @tc.desc: LnnDeleteDBDataByPrefix
129  * @tc.type: FUNC
130  * @tc.require:
131  */
132 HWTEST_F(KVAdapterWrapperTest, LnnDeleteByPrefix001, TestSize.Level1)
133 {
134     int32_t dbId = g_dbId;
135     LnnRegisterDataChangeListener(dbId, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
136     LnnRegisterDataChangeListener(dbId + 1, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
137     dbId = g_dbId;
138     LnnRegisterDataChangeListener(dbId, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
139     string keyStr = "aa11";
140     string valueStr = "111";
141     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), 4, valueStr.c_str(), 3), SOFTBUS_OK);
142     string keyStr2 = "aa22";
143     string valueStr2 = "222";
144     EXPECT_EQ(LnnPutDBData(dbId, keyStr2.c_str(), 4, valueStr2.c_str(), 3), SOFTBUS_OK);
145     string keyPrefix = "aa";
146     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyPrefix.c_str(), 2), SOFTBUS_OK);
147     dbId++;
148     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
149     dbId = 0;
150     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyStr.c_str(), MAX_STRING_LEN), SOFTBUS_INVALID_PARAM);
151     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyStr.c_str(), MAX_STRING_LEN + 1), SOFTBUS_INVALID_PARAM);
152     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyStr.c_str(), MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
153     char *keyPtr = nullptr;
154     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyPtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
155 }
156 
157 /**
158  * @tc.name: LnnGetDBData
159  * @tc.desc: LnnGetDBData
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(KVAdapterWrapperTest, LnnGet001, TestSize.Level1)
164 {
165     int32_t dbId = g_dbId;
166     LnnUnRegisterDataChangeListener(dbId);
167     LnnUnRegisterDataChangeListener(dbId + 1);
168     dbId = g_dbId;
169     LnnUnRegisterDataChangeListener(dbId);
170     string keyStr = "aaa";
171     string valueStr = "aaa";
172     char *value = nullptr;
173     EXPECT_EQ(LnnPutDBData(dbId, keyStr.c_str(), 3, valueStr.c_str(), 3), SOFTBUS_OK);
174     EXPECT_EQ(LnnGetDBData(dbId, keyStr.c_str(), 3, &value), SOFTBUS_OK);
175     SoftBusFree(value);
176     value = nullptr;
177     dbId++;
178     EXPECT_EQ(LnnGetDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, &value), SOFTBUS_INVALID_PARAM);
179     dbId = 0;
180     EXPECT_EQ(LnnGetDBData(dbId, keyStr.c_str(), MAX_STRING_LEN, &value), SOFTBUS_INVALID_PARAM);
181     EXPECT_EQ(LnnGetDBData(dbId, keyStr.c_str(), MAX_STRING_LEN + 1, &value), SOFTBUS_INVALID_PARAM);
182     EXPECT_EQ(LnnGetDBData(dbId, keyStr.c_str(), MIN_STRING_LEN - 1, &value), SOFTBUS_INVALID_PARAM);
183     char *keyPtr = nullptr;
184     EXPECT_EQ(LnnGetDBData(dbId, keyPtr, MIN_STRING_LEN - 1, &value), SOFTBUS_INVALID_PARAM);
185 }
186 
187 /**
188  * @tc.name: LnnSubcribeKvStoreService
189  * @tc.desc: LnnSubcribeKvStoreService
190  * @tc.type: FUNC
191  * @tc.require:
192  */
193 HWTEST_F(KVAdapterWrapperTest, LnnSubcribeKvStoreService001, TestSize.Level1)
194 {
195     int32_t lnnSubcribeKvStoreRet = LnnSubcribeKvStoreService();
196     EXPECT_EQ(lnnSubcribeKvStoreRet, SOFTBUS_OK);
197 }
198 
199 /**
200  * @tc.name: LnnCreateKvAdapter_InvalidDbId
201  * @tc.desc: Test LnnCreateKvAdapter with dbId being nullptr.
202  * @tc.type: Functional Test
203  * @tc.require:
204  */
205 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidDbId, TestSize.Level1)
206 {
207     int32_t *dbId = nullptr;
208     const char *appId = "validAppId";
209     int32_t appIdLen = strlen(appId);
210     const char *storeId = "validStoreId";
211     int32_t storeIdLen = strlen(storeId);
212     int32_t ret = LnnCreateKvAdapter(dbId, appId, appIdLen, storeId, storeIdLen);
213     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
214 }
215 
216 /**
217  * @tc.name: LnnCreateKvAdapter_InvalidAppId
218  * @tc.desc: Test LnnCreateKvAdapter with appId being nullptr.
219  * @tc.type: Functional Test
220  * @tc.require:
221  */
222 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidAppId, TestSize.Level1)
223 {
224     int32_t dbId;
225     const char *appId = nullptr;
226     int32_t appIdLen = 10; // Valid length
227     const char *storeId = "validStoreId";
228     int32_t storeIdLen = strlen(storeId);
229     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
230     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
231 }
232 
233 /**
234  * @tc.name: LnnCreateKvAdapter_InvalidAppIdLen_LessThanMin
235  * @tc.desc: Test LnnCreateKvAdapter with appIdLen being less than MIN_STRING_LEN.
236  * @tc.type: Functional Test
237  * @tc.require:
238  */
239 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidAppIdLen_LessThanMin, TestSize.Level1)
240 {
241     int32_t dbId;
242     const char *appId = "validAppId";
243     int32_t appIdLen = MIN_STRING_LEN - 1; // Less than MIN_STRING_LEN
244     const char *storeId = "validStoreId";
245     int32_t storeIdLen = strlen(storeId);
246     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
247     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
248 }
249 
250 /**
251  * @tc.name: LnnCreateKvAdapter_InvalidAppIdLen_GreaterThanMax
252  * @tc.desc: Test LnnCreateKvAdapter with appIdLen being greater than MAX_STRING_LEN.
253  * @tc.type: Functional Test
254  * @tc.require:
255  */
256 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidAppIdLen_GreaterThanMax, TestSize.Level1)
257 {
258     int32_t dbId;
259     const char *appId = "validAppId";
260     int32_t appIdLen = MAX_STRING_LEN + 1; // Greater than MAX_STRING_LEN
261     const char *storeId = "validStoreId";
262     int32_t storeIdLen = strlen(storeId);
263     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
264     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
265 }
266 
267 /**
268  * @tc.name: LnnCreateKvAdapter_InvalidStoreId
269  * @tc.desc: Test LnnCreateKvAdapter with storeId being nullptr.
270  * @tc.type: Functional Test
271  * @tc.require:
272  */
273 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidStoreId, TestSize.Level1)
274 {
275     int32_t dbId;
276     const char *appId = "validAppId";
277     int32_t appIdLen = strlen(appId);
278     const char *storeId = nullptr;
279     int32_t storeIdLen = 10; // Valid length
280     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
281     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
282 }
283 
284 /**
285  * @tc.name: LnnCreateKvAdapter_InvalidStoreIdLen_LessThanMin
286  * @tc.desc: Test LnnCreateKvAdapter with storeIdLen being less than MIN_STRING_LEN.
287  * @tc.type: Functional Test
288  * @tc.require:
289  */
290 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidStoreIdLen_LessThanMin, TestSize.Level1)
291 {
292     int32_t dbId;
293     const char *appId = "validAppId";
294     int32_t appIdLen = strlen(appId);
295     const char *storeId = "validStoreId";
296     int32_t storeIdLen = MIN_STRING_LEN - 1; // Less than MIN_STRING_LEN
297     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
298     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
299 }
300 
301 /**
302  * @tc.name: LnnCreateKvAdapter_InvalidStoreIdLen_GreaterThanMax
303  * @tc.desc: Test LnnCreateKvAdapter with storeIdLen being greater than MAX_STRING_LEN.
304  * @tc.type: Functional Test
305  * @tc.require:
306  */
307 HWTEST_F(KVAdapterWrapperTest, LnnCreateKvAdapter_InvalidStoreIdLen_GreaterThanMax, TestSize.Level1)
308 {
309     int32_t dbId;
310     const char *appId = "validAppId";
311     int32_t appIdLen = strlen(appId);
312     const char *storeId = "validStoreId";
313     int32_t storeIdLen = MAX_STRING_LEN + 1; // Greater than MAX_STRING_LEN
314     int32_t ret = LnnCreateKvAdapter(&dbId, appId, appIdLen, storeId, storeIdLen);
315     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
316 }
317 
318 /**
319  * @tc.name: LnnDestroyKvAdapter_Dbid_LessThanMin
320  * @tc.desc: Test LnnDestroyKvAdapter with dbId being less than MIN_DBID_COUNT.
321  * @tc.type: Functional Test
322  * @tc.require:
323  */
324 HWTEST_F(KVAdapterWrapperTest, LnnDestroyKvAdapter_Dbid_LessThanMin, TestSize.Level1)
325 {
326     int32_t dbId = MIN_DBID_COUNT - 1;
327     int32_t ret = LnnDestroyKvAdapter(dbId);
328     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
329 }
330 
331 /**
332  * @tc.name: LnnPutDBData_InvalidKey
333  * @tc.desc: Test LnnPutDBData with key being nullptr.
334  * @tc.type: Functional Test
335  * @tc.require:
336  */
337 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_InvalidKey, TestSize.Level1)
338 {
339     int32_t dbId = g_dbId;
340     const char *key = nullptr;
341     int32_t keyLen = 10;
342     const char *value = "validValue";
343     int32_t valueLen = strlen(value);
344     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
345     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
346 }
347 
348 /**
349  * @tc.name: LnnPutDBData_KeyLen_LessThanMin
350  * @tc.desc: Test LnnPutDBData with keyLen being less than MIN_STRING_LEN.
351  * @tc.type: Functional Test
352  * @tc.require:
353  */
354 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_KeyLen_LessThanMin, TestSize.Level1)
355 {
356     int32_t dbId = g_dbId;
357     const char *key = "validKey";
358     int32_t keyLen = MIN_STRING_LEN - 1;
359     const char *value = "validValue";
360     int32_t valueLen = strlen(value);
361     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
362     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
363 }
364 
365 /**
366  * @tc.name: LnnPutDBData_KeyLen_GreaterThanMax
367  * @tc.desc: Test LnnPutDBData with keyLen being greater than MAX_STRING_LEN.
368  * @tc.type: Functional Test
369  * @tc.require:
370  */
371 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_KeyLen_GreaterThanMax, TestSize.Level1)
372 {
373     int32_t dbId = g_dbId;
374     const char *key = "validKey";
375     int32_t keyLen = MAX_STRING_LEN + 1;
376     const char *value = "validValue";
377     int32_t valueLen = strlen(value);
378     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
379     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
380 }
381 
382 /**
383  * @tc.name: LnnPutDBData_InvalidValue
384  * @tc.desc: Test LnnPutDBData with value being nullptr.
385  * @tc.type: Functional Test
386  * @tc.require:
387  */
388 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_InvalidValue, TestSize.Level1)
389 {
390     int32_t dbId = g_dbId;
391     const char *key = "validKey";
392     int32_t keyLen = strlen(key);
393     const char *value = nullptr;
394     int32_t valueLen = 10;
395     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
396     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
397 }
398 
399 /**
400  * @tc.name: LnnPutDBData_ValueLen_LessThanMin
401  * @tc.desc: Test LnnPutDBData with valueLen being less than MIN_STRING_LEN.
402  * @tc.type: Functional Test
403  * @tc.require:
404  */
405 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_ValueLen_LessThanMin, TestSize.Level1)
406 {
407     int32_t dbId = g_dbId;
408     const char *key = "validKey";
409     int32_t keyLen = strlen(key);
410     const char *value = "validValue";
411     int32_t valueLen = MIN_STRING_LEN - 1;
412     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
413     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
414 }
415 
416 /**
417  * @tc.name: LnnPutDBData_ValueLen_GreaterThanMax
418  * @tc.desc: Test LnnPutDBData with valueLen being greater than MAX_STRING_LEN.
419  * @tc.type: Functional Test
420  * @tc.require:
421  */
422 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_ValueLen_GreaterThanMax, TestSize.Level1)
423 {
424     int32_t dbId = g_dbId;
425     const char *key = "validKey";
426     int32_t keyLen = strlen(key);
427     const char *value = "validValue";
428     int32_t valueLen = MAX_STRING_LEN + 1;
429     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
430     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
431 }
432 
433 /**
434  * @tc.name: LnnPutDBData_Dbid_LessThanMin
435  * @tc.desc: Test LnnPutDBData with dbid being less than MIN_STRING_LEN.
436  * @tc.type: Functional Test
437  * @tc.require:
438  */
439 HWTEST_F(KVAdapterWrapperTest, LnnPutDBData_Dbid_LessThanMin, TestSize.Level1)
440 {
441     int32_t dbId = MIN_DBID_COUNT - 1;
442     const char *key = "validKey";
443     int32_t keyLen = strlen(key);
444     const char *value = "validValue";
445     int32_t valueLen = strlen(value);
446     int32_t ret = LnnPutDBData(dbId, key, keyLen, value, valueLen);
447     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
448 }
449 
450 /**
451  * @tc.name: LnnDeleteDBData_InvalidKey
452  * @tc.desc: Test LnnDeleteDBData with key being nullptr.
453  * @tc.type: Functional Test
454  * @tc.require:
455  */
456 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBData_InvalidKey, TestSize.Level1)
457 {
458     int32_t dbId = g_dbId;
459     const char *key = nullptr;
460     int32_t keyLen = 10;
461     int32_t ret = LnnDeleteDBData(dbId, key, keyLen);
462     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
463 }
464 
465 /**
466  * @tc.name: LnnDeleteDBData_KeyLen_LessThanMin
467  * @tc.desc: Test LnnDeleteDBData with keyLen being less than MIN_STRING_LEN.
468  * @tc.type: Functional Test
469  * @tc.require:
470  */
471 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBData_KeyLen_LessThanMin, TestSize.Level1)
472 {
473     int32_t dbId = g_dbId;
474     const char *key = "validKey";
475     int32_t keyLen = MIN_STRING_LEN - 1;
476     int32_t ret = LnnDeleteDBData(dbId, key, keyLen);
477     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
478 }
479 
480 /**
481  * @tc.name: LnnDeleteDBData_KeyLen_GreaterThanMax
482  * @tc.desc: Test LnnDeleteDBData with keyLen being greater than MAX_STRING_LEN.
483  * @tc.type: Functional Test
484  * @tc.require:
485  */
486 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBData_KeyLen_GreaterThanMax, TestSize.Level1)
487 {
488     int32_t dbId = g_dbId;
489     const char *key = "validKey";
490     int32_t keyLen = MAX_STRING_LEN + 1;
491     int32_t ret = LnnDeleteDBData(dbId, key, keyLen);
492     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
493 }
494 
495 /**
496  * @tc.name: LnnDeleteDBData_Dbid_LessThanMin
497  * @tc.desc: Test LnnDeleteDBData with dbid being less than MIN_STRING_LEN.
498  * @tc.type: Functional Test
499  * @tc.require:
500  */
501 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBData_Dbid_LessThanMin, TestSize.Level1)
502 {
503     int32_t dbId = MIN_DBID_COUNT - 1;
504     const char *key = "validKey";
505     int32_t keyLen = strlen(key);
506     int32_t ret = LnnDeleteDBData(dbId, key, keyLen);
507     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
508 }
509 
510 /**
511  * @tc.name: LnnGetDBData_InvalidValue
512  * @tc.desc: Test LnnGetDBData with value being nullptr.
513  * @tc.type: Functional Test
514  * @tc.require:
515  */
516 HWTEST_F(KVAdapterWrapperTest, LnnGetDBData_InvalidValue, TestSize.Level1)
517 {
518     int32_t dbId = g_dbId;
519     const char *key = "validKey";
520     int32_t keyLen = strlen(key);
521     char **value = nullptr;
522     int32_t ret = LnnGetDBData(dbId, key, keyLen, value);
523     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
524 }
525 
526 /**
527  * @tc.name: LnnGetDBData_InvalidKey
528  * @tc.desc: Test LnnGetDBData with key being nullptr.
529  * @tc.type: Functional Test
530  * @tc.require:
531  */
532 HWTEST_F(KVAdapterWrapperTest, LnnGetDBData_InvalidKey, TestSize.Level1)
533 {
534     int32_t dbId = g_dbId;
535     const char *key = nullptr;
536     int32_t keyLen = 10;
537     const int32_t num = 3;
538     char **value = new (std::nothrow) char *[num];
539     if (value == nullptr) {
540         return;
541     }
542     std::string strValue0 = "value";
543     value[0] = new (std::nothrow) char[strValue0.size() + 1];
544     if (value[0] == nullptr) {
545         delete[] value;
546         return;
547     }
548     std::copy_n(strValue0.c_str(), strValue0.size(), value[0]);
549     value[0][strValue0.size()] = '\0';
550     std::string strValue1 = "test";
551     value[1] = new (std::nothrow) char[strValue1.size() + 1];
552     if (value[1] == nullptr) {
553         delete[] value[0];
554         delete[] value;
555         return;
556     }
557     std::copy_n(strValue1.c_str(), strValue1.size(), value[1]);
558     value[1][strValue1.size()] = '\0';
559     std::string strValue2 = "char";
560     value[2] = new (std::nothrow) char[strValue2.size() + 1];
561     if (value[2] == nullptr) {
562         delete[] value[1];
563         delete[] value[0];
564         delete[] value;
565         return;
566     }
567     std::copy_n(strValue2.c_str(), strValue2.size(), value[2]);
568     value[2][strValue2.size()] = '\0';
569     int32_t ret = LnnGetDBData(dbId, key, keyLen, value);
570     for (int32_t i = 0; i < num; ++i) {
571         delete[] value[i];
572     }
573     delete[] value;
574     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
575 }
576 
577 /**
578  * @tc.name: LnnGetDBData_KeyLen_LessThanMin
579  * @tc.desc: Test LnnGetDBData with keyLen being less than MIN_STRING_LEN.
580  * @tc.type: Functional Test
581  * @tc.require:
582  */
583 HWTEST_F(KVAdapterWrapperTest, LnnGetDBData_KeyLen_LessThanMin, TestSize.Level1)
584 {
585     int32_t dbId = g_dbId;
586     const char *key = "validKey";
587     int32_t keyLen = MIN_STRING_LEN - 1;
588     const int32_t num = 3;
589     char **value = new (std::nothrow) char *[num];
590     if (value == nullptr) {
591         return;
592     }
593     std::string strValue0 = "value";
594     value[0] = new (std::nothrow) char[strValue0.size() + 1];
595     if (value[0] == nullptr) {
596         delete[] value;
597         return;
598     }
599     std::copy_n(strValue0.c_str(), strValue0.size(), value[0]);
600     value[0][strValue0.size()] = '\0';
601     std::string strValue1 = "test";
602     value[1] = new (std::nothrow) char[strValue1.size() + 1];
603     if (value[1] == nullptr) {
604         delete[] value[0];
605         delete[] value;
606         return;
607     }
608     std::copy_n(strValue1.c_str(), strValue1.size(), value[1]);
609     value[1][strValue1.size()] = '\0';
610     std::string strValue2 = "char";
611     value[2] = new (std::nothrow) char[strValue2.size() + 1];
612     if (value[2] == nullptr) {
613         delete[] value[1];
614         delete[] value[0];
615         delete[] value;
616         return;
617     }
618     std::copy_n(strValue2.c_str(), strValue2.size(), value[2]);
619     value[2][strValue2.size()] = '\0';
620     int32_t ret = LnnGetDBData(dbId, key, keyLen, value);
621     for (int32_t i = 0; i < num; ++i) {
622         delete[] value[i];
623     }
624     delete[] value;
625     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
626 }
627 
628 /**
629  * @tc.name: LnnGetDBData_KeyLen_GreaterThanMax
630  * @tc.desc: Test LnnGetDBData with keyLen being greater than MAX_STRING_LEN.
631  * @tc.type: Functional Test
632  * @tc.require:
633  */
634 HWTEST_F(KVAdapterWrapperTest, LnnGetDBData_KeyLen_GreaterThanMax, TestSize.Level1)
635 {
636     int32_t dbId = g_dbId;
637     const char *key = "validKey";
638     int32_t keyLen = MAX_STRING_LEN + 1;
639     const int32_t num = 3;
640     char **value = new (std::nothrow) char *[num];
641     if (value == nullptr) {
642         return;
643     }
644     std::string strValue0 = "value";
645     value[0] = new (std::nothrow) char[strValue0.size() + 1];
646     if (value[0] == nullptr) {
647         delete[] value;
648         return;
649     }
650     std::copy_n(strValue0.c_str(), strValue0.size(), value[0]);
651     value[0][strValue0.size()] = '\0';
652     std::string strValue1 = "test";
653     value[1] = new (std::nothrow) char[strValue1.size() + 1];
654     if (value[1] == nullptr) {
655         delete[] value[0];
656         delete[] value;
657         return;
658     }
659     std::copy_n(strValue1.c_str(), strValue1.size(), value[1]);
660     value[1][strValue1.size()] = '\0';
661     std::string strValue2 = "char";
662     value[2] = new (std::nothrow) char[strValue2.size() + 1];
663     if (value[2] == nullptr) {
664         delete[] value[1];
665         delete[] value[0];
666         delete[] value;
667         return;
668     }
669     std::copy_n(strValue2.c_str(), strValue2.size(), value[2]);
670     value[2][strValue2.size()] = '\0';
671     int32_t ret = LnnGetDBData(dbId, key, keyLen, value);
672     for (int32_t i = 0; i < num; ++i) {
673         delete[] value[i];
674     }
675     delete[] value;
676     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
677 }
678 
679 /**
680  * @tc.name: LnnGetDBData_Dbid_LessThanMin
681  * @tc.desc: Test LnnGetDBData with dbid being less than MIN_STRING_LEN.
682  * @tc.type: Functional Test
683  * @tc.require:
684  */
685 HWTEST_F(KVAdapterWrapperTest, LnnGetDBData_Dbid_LessThanMin, TestSize.Level1)
686 {
687     int32_t dbId = MIN_DBID_COUNT - 1;
688     const char *key = "validKey";
689     int32_t keyLen = strlen(key);
690     const int32_t num = 3;
691     char **value = new (std::nothrow) char *[num];
692     if (value == nullptr) {
693         return;
694     }
695     std::string strValue0 = "value";
696     value[0] = new (std::nothrow) char[strValue0.size() + 1];
697     if (value[0] == nullptr) {
698         delete[] value;
699         return;
700     }
701     std::copy_n(strValue0.c_str(), strValue0.size(), value[0]);
702     value[0][strValue0.size()] = '\0';
703     std::string strValue1 = "test";
704     value[1] = new (std::nothrow) char[strValue1.size() + 1];
705     if (value[1] == nullptr) {
706         delete[] value[0];
707         delete[] value;
708         return;
709     }
710     std::copy_n(strValue1.c_str(), strValue1.size(), value[1]);
711     value[1][strValue1.size()] = '\0';
712     std::string strValue2 = "char";
713     value[2] = new (std::nothrow) char[strValue2.size() + 1];
714     if (value[2] == nullptr) {
715         delete[] value[1];
716         delete[] value[0];
717         delete[] value;
718         return;
719     }
720     std::copy_n(strValue2.c_str(), strValue2.size(), value[2]);
721     value[2][strValue2.size()] = '\0';
722     int32_t ret = LnnGetDBData(dbId, key, keyLen, value);
723     for (int32_t i = 0; i < num; ++i) {
724         delete[] value[i];
725     }
726     delete[] value;
727     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
728 }
729 
730 /**
731  * @tc.name: LnnDeleteDBDataByPrefix_InvalidKeyPrefix
732  * @tc.desc: Test LnnDeleteDBDataByPrefix with keyPrefix being nullptr.
733  * @tc.type: Functional Test
734  * @tc.require:
735  */
736 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByPrefix_InvalidKeyPrefix, TestSize.Level1)
737 {
738     int32_t dbId = g_dbId;
739     const char *keyPrefix = nullptr;
740     int32_t keyPrefixLen = 10;
741     int32_t ret = LnnDeleteDBDataByPrefix(dbId, keyPrefix, keyPrefixLen);
742     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
743 }
744 
745 /**
746  * @tc.name: LnnDeleteDBDataByPrefix_KeyPrefixLen_LessThanMin
747  * @tc.desc: Test LnnDeleteDBDataByPrefix with keyPrefixLen being less than MIN_STRING_LEN.
748  * @tc.type: Functional Test
749  * @tc.require:
750  */
751 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByPrefix_KeyPrefixLen_LessThanMin, TestSize.Level1)
752 {
753     int32_t dbId = g_dbId;
754     const char *keyPrefix = "validKeyPrefix";
755     int32_t keyPrefixLen = MIN_STRING_LEN - 1;
756     int32_t ret = LnnDeleteDBDataByPrefix(dbId, keyPrefix, keyPrefixLen);
757     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
758 }
759 
760 /**
761  * @tc.name: LnnDeleteDBDataByPrefix_KeyPrefixLen_GreaterThanMax
762  * @tc.desc: Test LnnDeleteDBDataByPrefix with keyPrefixLen being greater than MAX_STRING_LEN.
763  * @tc.type: Functional Test
764  * @tc.require:
765  */
766 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByPrefix_KeyPrefixLen_GreaterThanMax, TestSize.Level1)
767 {
768     int32_t dbId = g_dbId;
769     const char *keyPrefix = "validKeyPrefix";
770     int32_t keyPrefixLen = MAX_STRING_LEN + 1;
771     int32_t ret = LnnDeleteDBDataByPrefix(dbId, keyPrefix, keyPrefixLen);
772     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
773 }
774 
775 /**
776  * @tc.name: LnnDeleteDBDataByPrefix_Dbid_LessThanMin
777  * @tc.desc: Test LnnDeleteDBDataByPrefix with dbid being less than MIN_STRING_LEN.
778  * @tc.type: Functional Test
779  * @tc.require:
780  */
781 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByPrefix_Dbid_LessThanMin, TestSize.Level1)
782 {
783     int32_t dbId = MIN_DBID_COUNT - 1;
784     const char *keyPrefix = "validKeyPrefix";
785     int32_t keyPrefixLen = strlen(keyPrefix);
786     int32_t ret = LnnDeleteDBDataByPrefix(dbId, keyPrefix, keyPrefixLen);
787     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
788 }
789 
790 /**
791  * @tc.name: LnnCloudSync_Dbid_LessThanMin
792  * @tc.desc: Test LnnCloudSync with dbId being less than MIN_DBID_COUNT.
793  * @tc.type: Functional Test
794  * @tc.require:
795  */
796 HWTEST_F(KVAdapterWrapperTest, LnnCloudSync_Dbid_LessThanMin, TestSize.Level1)
797 {
798     int32_t dbId = MIN_DBID_COUNT - 1;
799     int32_t ret = LnnCloudSync(dbId);
800     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
801 }
802 
803 /**
804  * @tc.name: LnnSetCloudAbilityInner_Dbid_LessThanMin
805  * @tc.desc: Test LnnSetCloudAbilityInner with dbId being less than MIN_DBID_COUNT.
806  * @tc.type: Functional Test
807  * @tc.require:
808  */
809 HWTEST_F(KVAdapterWrapperTest, LnnSetCloudAbilityInner_Dbid_LessThanMin, TestSize.Level1)
810 {
811     int32_t dbId = MIN_DBID_COUNT - 1;
812     const bool isEnableCloud = true;
813     int32_t ret = LnnSetCloudAbilityInner(dbId, isEnableCloud);
814     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
815 }
816 
817 /**
818  * @tc.name: LnnCloudSync002
819  * @tc.desc: test LnnCloudSync
820  * @tc.type: FUNC
821  * @tc.require:
822  */
823 HWTEST_F(KVAdapterWrapperTest, LnnCloudSync002, TestSize.Level1)
824 {
825     LnnEnhanceFuncList *pfnLnnEnhanceFuncList = LnnEnhanceFuncListGet();
826     ASSERT_TRUE(pfnLnnEnhanceFuncList != nullptr);
827     pfnLnnEnhanceFuncList->isCloudSyncEnabled = IsCloudSyncEnabled;
828     int32_t dbId = g_dbId;
829     constexpr int32_t idOffset = 1;
830     int32_t lnnCloudRet = LnnCloudSync(dbId + idOffset);
831     EXPECT_EQ(lnnCloudRet, SOFTBUS_INVALID_PARAM);
832 }
833 
834 /**
835  * @tc.name: LnnDeleteDBDataByNull
836  * @tc.desc: LnnDeleteDBData Invalid Param
837  * @tc.type: FUNC
838  * @tc.require:
839  */
840 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByNull, TestSize.Level1)
841 {
842     int32_t dbId = g_dbId;
843     const char * keyStr = nullptr;
844     string valueStr = "ccc";
845     EXPECT_EQ(LnnPutDBData(dbId, keyStr, 3, valueStr.c_str(), 3), SOFTBUS_INVALID_PARAM);
846 }
847 
848 /**
849  * @tc.name: LnnGetDBDataByKey
850  * @tc.desc: LnnGetDBData  Invalid Param
851  * @tc.type: FUNC
852  * @tc.require:
853  */
854 HWTEST_F(KVAdapterWrapperTest, LnnGetDBDataByKey, TestSize.Level1)
855 {
856     int32_t dbId = g_dbId;
857     const char * keyStr = nullptr;
858     char *value = nullptr;
859     EXPECT_EQ(LnnGetDBData(dbId, keyStr, 3, &value), SOFTBUS_INVALID_PARAM);
860 }
861 
862 /**
863  * @tc.name: LnnDeleteDBDataByInvalid
864  * @tc.desc: LnnDeleteDBDataByPrefix  Invalid Param
865  * @tc.type: FUNC
866  * @tc.require:
867  */
868 HWTEST_F(KVAdapterWrapperTest, LnnDeleteDBDataByInvalid, TestSize.Level1)
869 {
870     int32_t dbId = g_dbId;
871     char *keyPtr = nullptr;
872     EXPECT_EQ(LnnDeleteDBDataByPrefix(dbId, keyPtr, MIN_STRING_LEN - 1), SOFTBUS_INVALID_PARAM);
873 }
874 } // namespace OHOS