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