1 /*
2 * Copyright (c) 2020-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
16 #include "ohos_types.h"
17 #include <securec.h>
18 #include "hctest.h"
19 #include "ohos_errno.h"
20 #include "kv_store.h"
21 #include "iot_watchdog.h"
22 #include "utils_config.h"
23
24 #define MAX_KEY_LEN_TEST 32
25 #define MAX_VALUE_LEN_TEST 128
26 #define MAX_KEY_NUM_TEST 50
27 #define MAX_CACHE_NUM_TEST 10
28 #define INVALID_KEY_NUM 128
29
30 #ifndef DATA_PATH
31 #define DATA_PATH "/data"
32 #endif
33
34 /**
35 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
36 * @param : subsystem name is utils
37 * @param : module name is kvStore
38 * @param : test suit name is KvStoreFuncTestSuite
39 */
40 LITE_TEST_SUIT(utils, kvStore, KvStoreFuncTestSuite);
41
42 /**
43 * @tc.setup : setup for all testcases
44 * @return : setup result, TRUE is success, FALSE is fail
45 */
KvStoreFuncTestSuiteSetUp(void)46 static BOOL KvStoreFuncTestSuiteSetUp(void)
47 {
48 UtilsSetEnv(DATA_PATH);
49 return TRUE;
50 }
51
52 /**
53 * @tc.teardown : teardown for all testcases
54 * @return : teardown result, TRUE is success, FALSE is fail
55 */
KvStoreFuncTestSuiteTearDown(void)56 static BOOL KvStoreFuncTestSuiteTearDown(void)
57 {
58 printf("+-------------------------------------------+\n");
59 return TRUE;
60 }
61
62 /* Create files in batches. */
SetKVFiles(int num,const char * key,const char * value)63 BOOL SetKVFiles (int num, const char* key, const char* value)
64 {
65 int size;
66 int ret;
67 char keytemp[MAX_KEY_LEN_TEST] = {0};
68 char valuetemp[MAX_VALUE_LEN_TEST] = {0};
69 char temp[MAX_VALUE_LEN_TEST] = {0};
70 if (num <= 0) {
71 return FALSE;
72 }
73 for (int i = 1; i <= num; i++) {
74 size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, i);
75 if (size < 0) {
76 return FALSE;
77 }
78 size = sprintf_s(valuetemp, MAX_VALUE_LEN_TEST, "%s_%d", value, i);
79 if (size < 0) {
80 return FALSE;
81 }
82 ret = UtilsSetValue(keytemp, valuetemp);
83 if (i <= MAX_KEY_NUM_TEST) {
84 TEST_ASSERT_EQUAL_INT(0, ret);
85 } else {
86 TEST_ASSERT_EQUAL_INT(-1, ret);
87 }
88 ret = UtilsGetValue(keytemp, temp, MAX_VALUE_LEN_TEST);
89 if (i <= MAX_KEY_NUM_TEST) {
90 #ifdef FEATURE_KV_CACHE
91 TEST_ASSERT_EQUAL_INT(0, ret);
92 #else
93 TEST_ASSERT_GREATER_THAN_INT(0, ret);
94 #endif
95 TEST_ASSERT_EQUAL_STRING(valuetemp, temp);
96 } else {
97 TEST_ASSERT_EQUAL_INT(-1, ret);
98 }
99 memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
100 memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
101 memset_s(valuetemp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
102 IoTWatchDogKick();
103 }
104 IoTWatchDogKick();
105 return TRUE;
106 }
107
108 /* Read files in batches. */
ReadKVFiles(int num,const char * key,const char * value)109 BOOL ReadKVFiles (int num, const char* key, const char* value)
110 {
111 int size;
112 int ret;
113 int i = 1;
114 char keytemp[MAX_KEY_LEN_TEST] = {0};
115 char valuetemp[MAX_VALUE_LEN_TEST] = {0};
116 char temp[MAX_VALUE_LEN_TEST] = {0};
117 if (num <= 0) {
118 return FALSE;
119 }
120 for (int loop = num; loop > 0; loop--) {
121 size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, loop);
122 if (size < 0) {
123 return FALSE;
124 }
125 size = sprintf_s(valuetemp, MAX_VALUE_LEN_TEST, "%s_%d", value, loop);
126 if (size < 0) {
127 return FALSE;
128 }
129 ret = UtilsGetValue(keytemp, temp, MAX_VALUE_LEN_TEST);
130 if (loop <= MAX_KEY_NUM_TEST) {
131 if (i <= MAX_CACHE_NUM_TEST) {
132 #ifdef FEATURE_KV_CACHE
133 TEST_ASSERT_EQUAL_INT(0, ret);
134 #else
135 TEST_ASSERT_GREATER_THAN_INT(0, ret);
136 #endif
137 } else {
138 TEST_ASSERT_GREATER_THAN_INT(0, ret);
139 }
140 TEST_ASSERT_EQUAL_STRING(valuetemp, temp);
141 i++;
142 } else {
143 TEST_ASSERT_EQUAL_INT(-1, ret);
144 }
145 memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
146 memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
147 memset_s(valuetemp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
148 IoTWatchDogKick();
149 }
150 IoTWatchDogKick();
151 return TRUE;
152 }
153
154 /* Delete files in batches. */
DeleteKVFiles(int num,const char * key)155 BOOL DeleteKVFiles (int num, const char* key)
156 {
157 int size;
158 int ret;
159 char keytemp[MAX_KEY_LEN_TEST] = {0};
160
161 if (num <= 0) {
162 return FALSE;
163 }
164 for (int i = 1; i <= num; i++) {
165 size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, i);
166 if (size < 0) {
167 return FALSE;
168 }
169 ret = UtilsDeleteValue(keytemp);
170 if (i <= MAX_KEY_NUM_TEST) {
171 TEST_ASSERT_EQUAL_INT(0, ret);
172 } else {
173 TEST_ASSERT_EQUAL_INT(-1, ret);
174 }
175 memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
176 IoTWatchDogKick();
177 }
178 IoTWatchDogKick();
179 return TRUE;
180 }
181
182 /**
183 * @tc.number : SUB_UTILS_KV_STORE_0100
184 * @tc.name : UtilsSetValue parameter legal test
185 * @tc.desc : [C- SOFTWARE -0200]
186 */
187 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue001, Function | MediumTest | Level1)
188 {
189 char key[] = "rw.sys.version";
190 char value[] = "Hello world !";
191 int ret = UtilsSetValue(key, value);
192 TEST_ASSERT_EQUAL_INT(0, ret);
193
194 ret = UtilsDeleteValue(key);
195 TEST_ASSERT_EQUAL_INT(0, ret);
196 };
197
198 /**
199 * @tc.number : SUB_UTILS_KV_STORE_0200
200 * @tc.name : UtilsSetValue parameter legal test using key with underline
201 * @tc.desc : [C- SOFTWARE -0200]
202 */
203 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue002, Function | MediumTest | Level1)
204 {
205 char key[] = "rw.sys.version_100";
206 char value[] = "Hello world !";
207 int ret = UtilsSetValue(key, value);
208 TEST_ASSERT_EQUAL_INT(0, ret);
209
210 ret = UtilsDeleteValue(key);
211 TEST_ASSERT_EQUAL_INT(0, ret);
212 };
213
214 /**
215 * @tc.number : SUB_UTILS_KV_STORE_0300
216 * @tc.name : UtilsSetValue parameter legal test when key contains only number
217 * @tc.desc : [C- SOFTWARE -0200]
218 */
219 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue003, Function | MediumTest | Level1)
220 {
221 char key[] = "100";
222 char value[] = "Hello world !";
223 int ret = UtilsSetValue(key, value);
224 TEST_ASSERT_EQUAL_INT(0, ret);
225
226 ret = UtilsDeleteValue(key);
227 TEST_ASSERT_EQUAL_INT(0, ret);
228 };
229
230 /**
231 * @tc.number : SUB_UTILS_KV_STORE_0400
232 * @tc.name : UtilsSetValue parameter legal test when key is equal to 31 Byte
233 * @tc.desc : [C- SOFTWARE -0200]
234 */
235 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue004, Function | MediumTest | Level1)
236 {
237 char key[] = "rw.sys.version.utilskvparameter";
238 char value[] = "Hello world !";
239 int ret = UtilsSetValue(key, value);
240 TEST_ASSERT_EQUAL_INT(0, ret);
241
242 ret = UtilsDeleteValue(key);
243 TEST_ASSERT_EQUAL_INT(0, ret);
244 };
245
246 /**
247 * @tc.number : SUB_UTILS_KV_STORE_0500
248 * @tc.name : UtilsSetValue parameter Illegal test when key is equal to 32 Byte
249 * @tc.desc : [C- SOFTWARE -0200]
250 */
251 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue005, Function | MediumTest | Level1)
252 {
253 char key[] = "rw.sys.version.utilskvparameter1";
254 char value[] = "Hello world !";
255 int ret = UtilsSetValue(key, value);
256 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
257 };
258
259 /**
260 * @tc.number : SUB_UTILS_KV_STORE_0600
261 * @tc.name : UtilsSetValue parameter Illegal test when key is gteater than 32 Byte
262 * @tc.desc : [C- SOFTWARE -0200]
263 */
264 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue006, Function | MediumTest | Level1)
265 {
266 char key[] = "rw.sys.version.utilskvparameterforillegal";
267 char value[] = "Hello world !";
268 int ret = UtilsSetValue(key, value);
269 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
270 };
271
272 /**
273 * @tc.number : SUB_UTILS_KV_STORE_0700
274 * @tc.name : UtilsSetValue parameter Illegal test when key contains uppercase
275 * @tc.desc : [C- SOFTWARE -0200]
276 */
277 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue007, Function | MediumTest | Level1)
278 {
279 char key[] = "Rw.sys.version";
280 char value[] = "Hello world !";
281 int ret = UtilsSetValue(key, value);
282 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
283 };
284
285 /**
286 * @tc.number : SUB_UTILS_KV_STORE_0800
287 * @tc.name : UtilsSetValue parameter Illegal test when key contains horizontal line
288 * @tc.desc : [C- SOFTWARE -0200]
289 */
290 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue008, Function | MediumTest | Level1)
291 {
292 char key[] = "rw.sys.version-r3";
293 char value[] = "Hello world !";
294 int ret = UtilsSetValue(key, value);
295 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
296 };
297
298 /**
299 * @tc.number : SUB_UTILS_KV_STORE_0900
300 * @tc.name : UtilsSetValue parameter Illegal test when key contains plus sign
301 * @tc.desc : [C- SOFTWARE -0200]
302 */
303 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue009, Function | MediumTest | Level1)
304 {
305 char key[] = "re+r3";
306 char value[] = "Hello world !";
307 int ret = UtilsSetValue(key, value);
308 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
309 };
310
311 /**
312 * @tc.number : SUB_UTILS_KV_STORE_1000
313 * @tc.name : UtilsSetValue parameter Illegal test when key contains asterisk
314 * @tc.desc : [C- SOFTWARE -0200]
315 */
316 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue010, Function | MediumTest | Level1)
317 {
318 char key[] = "rw.sys.version*r3";
319 char value[] = "Hello world !";
320 int ret = UtilsSetValue(key, value);
321 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
322 };
323
324 /**
325 * @tc.number : SUB_UTILS_KV_STORE_1100
326 * @tc.name : UtilsSetValue parameter legal test when Value is equal to 127 characters
327 * @tc.desc : [C- SOFTWARE -0200]
328 */
329 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue011, Function | MediumTest | Level1)
330 {
331 char key[] = "rw.sys.version";
332 char value[] = "Two tigers Two tigers two tiger running so fast \
333 running so fast one has no ears one has no tail How strange How strangesleeping";
334 int ret = UtilsSetValue(key, value);
335 TEST_ASSERT_EQUAL_INT(0, ret);
336
337 ret = UtilsDeleteValue(key);
338 TEST_ASSERT_EQUAL_INT(0, ret);
339 };
340
341 /**
342 * @tc.number : SUB_UTILS_KV_STORE_1200
343 * @tc.name : UtilsSetValue parameter Illegal test when Value is equal to 128 characters
344 * @tc.desc : [C- SOFTWARE -0200]
345 */
346 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue012, Function | MediumTest | Level1)
347 {
348 char key[] = "rw.sys.version";
349 char value[] = "Two tigers Two tigers two tiger running so fast \
350 running so fast one has no ears one has no tail How strange How strange sleeping";
351 int ret = UtilsSetValue(key, value);
352 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
353 };
354
355 /**
356 * @tc.number : SUB_UTILS_KV_STORE_1300
357 * @tc.name : UtilsSetValue parameter Illegal test when Value greater than 128 characters
358 * @tc.desc : [C- SOFTWARE -0200]
359 */
360 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue013, Function | MediumTest | Level1)
361 {
362 char key[] = "rw.sys.version";
363 char value[] = "Two tigers Two tigers two tiger running so fast \
364 running so fast one has no ears one has no tail How strange How strange Are you sleeping";
365 int ret = UtilsSetValue(key, value);
366 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
367 };
368
369 /**
370 * @tc.number : SUB_UTILS_KV_STORE_1400
371 * @tc.name : Value greater than 128 characters and key is an invalid character
372 * @tc.desc : [C- SOFTWARE -0200]
373 */
374 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue014, Function | MediumTest | Level1)
375 {
376 char key[] = "Rw.sys.version";
377 char value[] = "Two tigers Two tigers two tiger running so fast \
378 running so fast one has no ears one has no tail How strange How strange Are you sleeping";
379 int ret = UtilsSetValue(key, value);
380 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
381 };
382
383 /**
384 * @tc.number : SUB_UTILS_KV_STORE_1500
385 * @tc.name : UtilsSetValue parameter legal test with Special characters
386 * @tc.desc : [C- SOFTWARE -0200]
387 */
388 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue015, Function | MediumTest | Level1)
389 {
390 char key[] = "_._..__...___";
391 char value[] = "!@#¥%……&*()——+~《》?,。、“‘;:、12345767890";
392 int ret = UtilsSetValue(key, value);
393 TEST_ASSERT_EQUAL_INT(0, ret);
394
395 ret = UtilsDeleteValue(key);
396 TEST_ASSERT_EQUAL_INT(0, ret);
397 };
398
399 /**
400 * @tc.number : SUB_UTILS_KV_STORE_1600
401 * @tc.name : UtilsSetValue parameter Illegal test when key contains blank
402 * @tc.desc : [C- SOFTWARE -0200]
403 */
404 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue016, Function | MediumTest | Level1)
405 {
406 char key[] = "rw.sys.version space test";
407 char value[] = "Hello world !";
408 int ret = UtilsSetValue(key, value);
409 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
410 };
411
412 /**
413 * @tc.number : SUB_UTILS_KV_STORE_1700
414 * @tc.name : UtilsSetValue parameter legal test when value contains only blank
415 * @tc.desc : [C- SOFTWARE -0200]
416 */
417 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreSetValue017, Function | MediumTest | Level1)
418 {
419 char key[] = "rw.sys.version";
420 char value[] = " ";
421 int ret = UtilsSetValue(key, value);
422 TEST_ASSERT_EQUAL_INT(0, ret);
423
424 ret = UtilsDeleteValue(key);
425 TEST_ASSERT_EQUAL_INT(0, ret);
426 };
427
428 /**
429 * @tc.number : SUB_UTILS_KV_STORE_1800
430 * @tc.name : Use the interface to get the kv value or cache
431 * @tc.desc : [C- SOFTWARE -0200]
432 */
433 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreGetValue001, Function | MediumTest | Level1)
434 {
435 char key[] = "rw.sys.version";
436 char value[] = "It is never too old to learn";
437 char temp[MAX_VALUE_LEN_TEST] = {0};
438 int ret = UtilsSetValue(key, value);
439 TEST_ASSERT_EQUAL_INT(0, ret);
440
441 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
442 #ifdef FEATURE_KV_CACHE
443 TEST_ASSERT_EQUAL_INT(0, ret);
444 #else
445 TEST_ASSERT_GREATER_THAN_INT(0, ret);
446 #endif
447 TEST_ASSERT_EQUAL_STRING(value, temp);
448 ret = UtilsDeleteValue(key);
449 TEST_ASSERT_EQUAL_INT(0, ret);
450 };
451
452 /**
453 * @tc.number : SUB_UTILS_KV_STORE_1900
454 * @tc.name : Use the interface to get the kv value or cache when key contains only number
455 * @tc.desc : [C- SOFTWARE -0200]
456 */
457 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreGetValue002, Function | MediumTest | Level1)
458 {
459 char key[] = "100";
460 char value[] = "!@#¥%……&*()——+~《》?,。、“‘;:、12345767890";
461 char temp[MAX_VALUE_LEN_TEST] = {0};
462 int ret = UtilsSetValue(key, value);
463 TEST_ASSERT_EQUAL_INT(0, ret);
464
465 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
466 #ifdef FEATURE_KV_CACHE
467 TEST_ASSERT_EQUAL_INT(0, ret);
468 #else
469 TEST_ASSERT_GREATER_THAN_INT(0, ret);
470 #endif
471 TEST_ASSERT_EQUAL_STRING(value, temp);
472 ret = UtilsDeleteValue(key);
473 TEST_ASSERT_EQUAL_INT(0, ret);
474 };
475
476 /**
477 * @tc.number : SUB_UTILS_KV_STORE_2000
478 * @tc.name : Use the interface to get the kv value or cache after value is updated
479 * @tc.desc : [C- SOFTWARE -0200]
480 */
481 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreGetValue003, Function | MediumTest | Level1)
482 {
483 char key[] = "rw.sys.version.utilskvparameter";
484 char value[] = "It is never too old to learn";
485 char temp[MAX_VALUE_LEN_TEST] = {0};
486 int ret = UtilsSetValue(key, value);
487 TEST_ASSERT_EQUAL_INT(0, ret);
488
489 // Update the value of key
490 char value1[] = "Two tigers Two tigers two tiger running so fast \
491 running so fast one has no ears one has no tail How strange How strangesleeping";
492 ret = UtilsSetValue(key, value1);
493 TEST_ASSERT_EQUAL_INT(0, ret);
494
495 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
496 #ifdef FEATURE_KV_CACHE
497 TEST_ASSERT_EQUAL_INT(0, ret);
498 #else
499 TEST_ASSERT_GREATER_THAN_INT(0, ret);
500 #endif
501 TEST_ASSERT_EQUAL_STRING(value1, temp);
502 ret = UtilsDeleteValue(key);
503 TEST_ASSERT_EQUAL_INT(0, ret);
504 };
505
506 /**
507 * @tc.number : SUB_UTILS_KV_STORE_2100
508 * @tc.name : UtilsGetValue parameter Illegal test
509 * @tc.desc : [C- SOFTWARE -0200]
510 */
511 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreGetValue004, Function | MediumTest | Level1)
512 {
513 char key[] = "Rw.sys.version";
514 char temp[MAX_VALUE_LEN_TEST] = {0};
515 int ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
516 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
517 };
518
519 /**
520 * @tc.number : SUB_UTILS_KV_STORE_2200
521 * @tc.name : Get the kv value or cache when key contains only special characters and value contains only blank
522 * @tc.desc : [C- SOFTWARE -0200]
523 */
524 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreGetValue005, Function | MediumTest | Level1)
525 {
526 char key[] = "_._..__...___";
527 char value[] = " ";
528 char temp[MAX_VALUE_LEN_TEST] = {0};
529 int ret = UtilsSetValue(key, value);
530 TEST_ASSERT_EQUAL_INT(0, ret);
531
532 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
533 #ifdef FEATURE_KV_CACHE
534 TEST_ASSERT_EQUAL_INT(0, ret);
535 #else
536 TEST_ASSERT_GREATER_THAN_INT(0, ret);
537 #endif
538 TEST_ASSERT_EQUAL_STRING(value, temp);
539 ret = UtilsDeleteValue(key);
540 TEST_ASSERT_EQUAL_INT(0, ret);
541 };
542
543 /**
544 * @tc.number : SUB_UTILS_KV_STORE_2300
545 * @tc.name : UtilsDeleteValue parameter Illegal test
546 * @tc.desc : [C- SOFTWARE -0200]
547 */
548 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreDeleteValue, Function | MediumTest | Level1)
549 {
550 char key[] = "Rw.sys.version";
551 int ret = UtilsDeleteValue(key);
552 TEST_ASSERT_EQUAL_INT(EC_INVALID, ret);
553 };
554
555 #ifdef FEATURE_KV_CACHE
556 /**
557 * @tc.number : SUB_UTILS_KV_STORE_2400
558 * @tc.name : Use the interface to clear cache
559 * @tc.desc : [C- SOFTWARE -0200]
560 */
561 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreClearCache001, Function | MediumTest | Level1)
562 {
563 char key[] = "rw.sys.version";
564 char value[] = "It is never too old to learn";
565 char temp[MAX_VALUE_LEN_TEST] = {0};
566 int ret = UtilsSetValue(key, value);
567 TEST_ASSERT_EQUAL_INT(0, ret);
568 // Get the value of key
569 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
570 TEST_ASSERT_EQUAL_INT(0, ret);
571 TEST_ASSERT_EQUAL_STRING(value, temp);
572 // Clear cache
573 ret = ClearKVCache();
574 TEST_ASSERT_EQUAL_INT(0, ret);
575 // Get the value of key
576 memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
577 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
578 TEST_ASSERT_GREATER_THAN_INT(0, ret);
579 TEST_ASSERT_EQUAL_STRING(value, temp);
580 // Clear key
581 ret = UtilsDeleteValue(key);
582 TEST_ASSERT_EQUAL_INT(0, ret);
583 };
584
585 /**
586 * @tc.number : SUB_UTILS_KV_STORE_2500
587 * @tc.name : Use the interface to clear cache after updating operation
588 * @tc.desc : [C- SOFTWARE -0200]
589 */
590 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreClearCache002, Function | MediumTest | Level1)
591 {
592 char key[] = "rw.sys.version";
593 char value[] = "It is never too old to learn";
594 char temp[MAX_VALUE_LEN_TEST] = {0};
595 int ret = UtilsSetValue(key, value);
596 TEST_ASSERT_EQUAL_INT(0, ret);
597
598 // Update the value of key
599 char value1[] = "Two tigers,Two tigers,two tiger,running so fast";
600 ret = UtilsSetValue(key, value1);
601 TEST_ASSERT_EQUAL_INT(0, ret);
602 // Get the value of key
603 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
604 TEST_ASSERT_EQUAL_INT(0, ret);
605 TEST_ASSERT_EQUAL_STRING(value1, temp);
606 // Clear cache
607 ret = ClearKVCache();
608 TEST_ASSERT_EQUAL_INT(0, ret);
609 // Get the value of key
610 memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
611 ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
612 TEST_ASSERT_GREATER_THAN_INT(0, ret);
613 TEST_ASSERT_EQUAL_STRING(value1, temp);
614 // Clear key
615 ret = UtilsDeleteValue(key);
616 TEST_ASSERT_EQUAL_INT(0, ret);
617 };
618 #endif
619
620 /**
621 * @tc.number : SUB_UTILS_KV_STORE_2600
622 * @tc.name : Specification test when MaxCacheNum is 9
623 * @tc.desc : [C- SOFTWARE -0200]
624 */
625 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreCacheSize001, Function | MediumTest | Level1)
626 {
627 char key[] = "rw.sys.version";
628 char value[] = "It is never too old to learn";
629 BOOL ret = FALSE;
630
631 ret = SetKVFiles(MAX_CACHE_NUM_TEST-1, key, value);
632 if (ret != TRUE)
633 {
634 TEST_FAIL();
635 }
636 ret = ReadKVFiles(MAX_CACHE_NUM_TEST-1, key, value);
637 if (ret != TRUE)
638 {
639 TEST_FAIL();
640 }
641 ret = DeleteKVFiles(MAX_CACHE_NUM_TEST-1, key);
642 if (ret != TRUE)
643 {
644 TEST_FAIL();
645 }
646 };
647
648 /**
649 * @tc.number : SUB_UTILS_KV_STORE_2700
650 * @tc.name : Specification test when MaxCacheNum is 10
651 * @tc.desc : [C- SOFTWARE -0200]
652 */
653 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreCacheSize002, Function | MediumTest | Level1)
654 {
655 char key[] = "rw.sys.version";
656 char value[] = "It is never too old to learn";
657 BOOL ret = FALSE;
658
659 ret = SetKVFiles(MAX_CACHE_NUM_TEST, key, value);
660 if (ret != TRUE)
661 {
662 TEST_FAIL();
663 }
664 ret = ReadKVFiles(MAX_CACHE_NUM_TEST, key, value);
665 if (ret != TRUE)
666 {
667 TEST_FAIL();
668 }
669 ret = DeleteKVFiles(MAX_CACHE_NUM_TEST, key);
670 if (ret != TRUE)
671 {
672 TEST_FAIL();
673 }
674 };
675
676 /**
677 * @tc.number : SUB_UTILS_KV_STORE_2800
678 * @tc.name : Specification test when MaxCacheNum is 11
679 * @tc.desc : [C- SOFTWARE -0200]
680 */
681 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreCacheSize003, Function | MediumTest | Level1)
682 {
683 char key[] = "rw.sys.version";
684 char value[] = "It is never too old to learn";
685 BOOL ret = FALSE;
686
687 ret = SetKVFiles(MAX_CACHE_NUM_TEST+1, key, value);
688 if (ret != TRUE)
689 {
690 TEST_FAIL();
691 }
692 ret = ReadKVFiles(MAX_CACHE_NUM_TEST+1, key, value);
693 if (ret != TRUE)
694 {
695 TEST_FAIL();
696 }
697 ret = DeleteKVFiles(MAX_CACHE_NUM_TEST+1, key);
698 if (ret != TRUE)
699 {
700 TEST_FAIL();
701 }
702 };
703
704 /**
705 * @tc.number : SUB_UTILS_KV_STORE_2900
706 * @tc.name : Specification test when MaxKeyNum is 49
707 * @tc.desc : [C- SOFTWARE -0200]
708 */
709 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreMaxSize001, Function | MediumTest | Level1)
710 {
711 char key[] = "rw.sys.version";
712 char value[] = "It is never too old to learn";
713 BOOL ret = FALSE;
714
715 ret = SetKVFiles(MAX_KEY_NUM_TEST-1, key, value);
716 if (ret != TRUE)
717 {
718 TEST_FAIL();
719 }
720 ret = ReadKVFiles(MAX_KEY_NUM_TEST-1, key, value);
721 if (ret != TRUE)
722 {
723 TEST_FAIL();
724 }
725 ret = DeleteKVFiles(MAX_KEY_NUM_TEST-1, key);
726 if (ret != TRUE)
727 {
728 TEST_FAIL();
729 }
730 };
731
732 /**
733 * @tc.number : SUB_UTILS_KV_STORE_3000
734 * @tc.name : Specification test when MaxKeyNum is 50
735 * @tc.desc : [C- SOFTWARE -0200]
736 */
737 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreMaxSize002, Function | MediumTest | Level1)
738 {
739 char key[] = "rw.sys.version";
740 char value[] = "It is never too old to learn";
741 BOOL ret = FALSE;
742
743 ret = SetKVFiles(MAX_KEY_NUM_TEST, key, value);
744 if (ret != TRUE)
745 {
746 TEST_FAIL();
747 }
748 ret = ReadKVFiles(MAX_KEY_NUM_TEST, key, value);
749 if (ret != TRUE)
750 {
751 TEST_FAIL();
752 }
753 ret = DeleteKVFiles(MAX_KEY_NUM_TEST, key);
754 if (ret != TRUE)
755 {
756 TEST_FAIL();
757 }
758 };
759
760 /**
761 * @tc.number : SUB_UTILS_KV_STORE_3100
762 * @tc.name : Specification test when MaxKeyNum is 51
763 * @tc.desc : [C- SOFTWARE -0200]
764 */
765 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreMaxSize003, Function | MediumTest | Level1)
766 {
767 char key[] = "rw.sys.version";
768 char value[] = "It is never too old to learn";
769 BOOL ret = FALSE;
770
771 ret = SetKVFiles(MAX_KEY_NUM_TEST+1, key, value);
772 if (ret != TRUE)
773 {
774 TEST_FAIL();
775 }
776 ret = ReadKVFiles(MAX_KEY_NUM_TEST+1, key, value);
777 if (ret != TRUE)
778 {
779 TEST_FAIL();
780 }
781 ret = DeleteKVFiles(MAX_KEY_NUM_TEST+1, key);
782 if (ret != TRUE)
783 {
784 TEST_FAIL();
785 }
786 };
787
788 /**
789 * @tc.number : SUB_UTILS_KV_STORE_3200
790 * @tc.name : Specification test using InvalidKeyNum
791 * @tc.desc : [C- SOFTWARE -0200]
792 */
793 LITE_TEST_CASE(KvStoreFuncTestSuite, testKvStoreMaxSize004, Function | MediumTest | Level1)
794 {
795 char key[] = "rw.sys.version";
796 char value[] = "It is never too old to learn";
797 char value1[] = "Two tigers Two tigers two tiger running so fast \
798 running so fast one has no ears one has no tail How strange How strange";
799 BOOL ret = FALSE;
800
801 ret = SetKVFiles(INVALID_KEY_NUM, key, value);
802 if (ret != TRUE)
803 {
804 TEST_FAIL();
805 }
806 // Update operation
807 ret = SetKVFiles(INVALID_KEY_NUM, key, value1);
808 if (ret != TRUE)
809 {
810 TEST_FAIL();
811 }
812 ret = ReadKVFiles(INVALID_KEY_NUM, key, value1);
813 if (ret != TRUE)
814 {
815 TEST_FAIL();
816 }
817 ret = DeleteKVFiles(INVALID_KEY_NUM, key);
818 if (ret != TRUE)
819 {
820 TEST_FAIL();
821 }
822 };
823
824 RUN_TEST_SUITE(KvStoreFuncTestSuite);
825