KvStoreTest.cpp 22.6 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "gtest/gtest.h"
#include <securec.h>

#include "utils_config.h"
#include "kv_store.h"
#include "kvstore_env.h"

using namespace std;
using namespace testing::ext;

const int  MAX_KEY_LEN_TEST = 32;    // Key length specifications
const int  MAX_VALUE_LEN_TEST = 128;  // Value length specifications
const int  MAX_KEY_NUM_TEST = 50;   // Maximum number of keys
const int  MAX_CACHE_NUM_TEST = 10;  // Minimum number of cache
const int  INVALID_PARAMETER = -9;   // Invalid parameter.

class KvStoreTest : public testing::Test {
protected:
M
mamingshuai 已提交
35 36
    // SetUpTestCase:测试套预置动作,在第一个TestCase之前执行
    static void SetUpTestCase(void)
W
wenjun 已提交
37 38 39 40 41 42 43
    {
        printf("----------test case with KvStoreTest start-------------\n");
        int ret = mkdir("/storage/com.huawei.kv", S_IRUSR | S_IWUSR);
        printf("/storage/com.huawei.kv ret = %d\n", ret);
        ret = UtilsSetEnv("/storage/com.huawei.kv");
        EXPECT_EQ(ret, 0);
    }
M
mamingshuai 已提交
44 45
    // TearDownTestCase:测试套清理动作,在最后一个TestCase之后执行
    static void TearDownTestCase(void)
W
wenjun 已提交
46 47 48 49 50 51 52
    {
        int ret = rmdir("/storage/com.huawei.kv/kvstore");
        printf("/storage/com.huawei.kv/kvstore ret = %d\n", ret);
        ret = rmdir("/storage/com.huawei.kv");
        printf("/storage/com.huawei.kv ret = %d\n", ret);
        printf("----------test case with KvStoreTest end-------------\n");
    }
M
mamingshuai 已提交
53
    // 用例的预置动作
W
wenjun 已提交
54
    virtual void SetUp() {}
M
mamingshuai 已提交
55
    // 用例的清理动作
W
wenjun 已提交
56
    virtual void TearDown() {}
M
mamingshuai 已提交
57 58 59
    bool TouchKVFiles (int num, const char* key, const char* value) const;
    bool DeleteKVFiles (int num, const char* key) const;
    bool ReadKVFiles (int num, const char* key, const char* value) const;
W
wenjun 已提交
60 61 62
};

/* Create files in batches. */
M
mamingshuai 已提交
63
bool KvStoreTest::TouchKVFiles (int num, const char* key, const char* value) const
W
wenjun 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
    int size = 0;
    int ret = 0;
    char keytemp[MAX_KEY_LEN_TEST] = {0};
    char valuetemp[MAX_VALUE_LEN_TEST] = {0};
    char temp[MAX_VALUE_LEN_TEST] = {0};
    if (num <= 0) {
        return false;
    }
    for (int i = 1; i <= num; i++) {
        size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, i);
        if (size < 0) {
            return false;
        }
        size = sprintf_s(valuetemp, MAX_VALUE_LEN_TEST, "%s_%d", value, i);
        if (size < 0) {
            return false;
M
mamingshuai 已提交
81
        }
W
wenjun 已提交
82 83 84 85 86 87 88 89 90 91 92 93
        ret = UtilsSetValue(keytemp, valuetemp);
        if (i <= MAX_KEY_NUM_TEST) {
            EXPECT_EQ(ret, 0);
        } else {
            EXPECT_EQ(ret, -1);
        }
        ret = UtilsGetValue(keytemp, temp, MAX_VALUE_LEN_TEST);
        if (i <= MAX_KEY_NUM_TEST) {
            EXPECT_EQ(ret, 0);
            EXPECT_STREQ(valuetemp, temp);
        } else {
            EXPECT_EQ(ret, -1);
M
mamingshuai 已提交
94
        }
W
wenjun 已提交
95 96 97 98 99 100 101 102
        memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
        memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
        memset_s(valuetemp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
    }
    return true;
}

/* Create files in batches. */
M
mamingshuai 已提交
103
bool KvStoreTest::ReadKVFiles (int num, const char* key, const char* value) const
W
wenjun 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
    int size = 0;
    int ret = 0;
    int i = 1;
    char keytemp[MAX_KEY_LEN_TEST] = {0};
    char valuetemp[MAX_VALUE_LEN_TEST] = {0};
    char temp[MAX_VALUE_LEN_TEST] = {0};
    if (num <= 0) {
        return false;
    }
    for (; num > 0; num--) {
        size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, num);
        if (size < 0) {
            return false;
        }
        size = sprintf_s(valuetemp, MAX_VALUE_LEN_TEST, "%s_%d", value, num);
        if (size < 0) {
            return false;
M
mamingshuai 已提交
122
        }
W
wenjun 已提交
123 124 125 126 127 128 129 130 131 132 133
        ret = UtilsGetValue(keytemp, temp, MAX_VALUE_LEN_TEST);
        if (num <= MAX_KEY_NUM_TEST) {
            if (i <= MAX_CACHE_NUM_TEST) {
                EXPECT_EQ(ret, 0);
            } else {
                EXPECT_GT(ret, 0);
            }
            EXPECT_STREQ(valuetemp, temp);
            i++;
        } else {
            EXPECT_EQ(ret, -1);
M
mamingshuai 已提交
134
        }
W
wenjun 已提交
135 136 137 138 139 140 141 142
        memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
        memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
        memset_s(valuetemp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
    }
    return true;
}

/* Delete files in batches. */
M
mamingshuai 已提交
143
bool KvStoreTest::DeleteKVFiles (int num, const char* key) const
W
wenjun 已提交
144 145 146 147 148 149 150 151 152 153 154 155
{
    int size = 0;
    int ret = 0;
    char keytemp[MAX_KEY_LEN_TEST] = {0};

    if (num <= 0) {
        return false;
    }
    for (int i = 1; i <= num; i++) {
        size = sprintf_s(keytemp, MAX_KEY_LEN_TEST, "%s_%d", key, i);
        if (size < 0) {
            return false;
M
mamingshuai 已提交
156
        }
W
wenjun 已提交
157 158 159 160 161
        ret = UtilsDeleteValue(keytemp);
        if (i <= MAX_KEY_NUM_TEST) {
            EXPECT_EQ(ret, 0);
        } else {
            EXPECT_EQ(ret, -1);
M
mamingshuai 已提交
162 163
        }
        memset_s(keytemp, MAX_KEY_LEN_TEST, 0, MAX_KEY_LEN_TEST);
W
wenjun 已提交
164 165 166 167 168
    }
    return true;
}

/**
M
mamingshuai 已提交
169 170
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0100
 * @tc.name      : UtilsSetValue parameter legal test Lowercase alphanumeric, dot
W
wenjun 已提交
171 172
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
173
HWTEST_F(KvStoreTest, testUtilsSetValue001, Function | MediumTest | Level0)
W
wenjun 已提交
174 175 176 177 178 179 180 181 182 183 184
{
    char key[] = "rw.sys.version";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
185 186
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0200
 * @tc.name      : UtilsSetValue parameter legal test Lowercase alphanumeric, underscore, dot
W
wenjun 已提交
187 188
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
189
HWTEST_F(KvStoreTest, testUtilsSetValue002, Function | MediumTest | Level1)
W
wenjun 已提交
190 191 192 193 194 195 196
{
    char key[] = "rw.sys.version_100";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
M
mamingshuai 已提交
197
    EXPECT_EQ(ret, 0);
W
wenjun 已提交
198 199 200
}

/**
M
mamingshuai 已提交
201 202
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0300
 * @tc.name      : UtilsSetValue parameter legal test Lowercase alphanumeric
W
wenjun 已提交
203 204
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
205
HWTEST_F(KvStoreTest, testUtilsSetValue003, Function | MediumTest | Level1)
W
wenjun 已提交
206 207 208 209 210 211 212
{
    char key[] = "100";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
M
mamingshuai 已提交
213
    EXPECT_EQ(ret, 0);
W
wenjun 已提交
214 215 216
}

/**
M
mamingshuai 已提交
217 218
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0400
 * @tc.name      : UtilsSetValue parameter legal test key length is 31 Byte
W
wenjun 已提交
219 220
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
221
HWTEST_F(KvStoreTest, testUtilsSetValue004, Function | MediumTest | Level2)
W
wenjun 已提交
222 223 224 225
{
    char key[] = "rw.sys.version.utilskvparameter";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
226
    EXPECT_EQ(ret, 0);
W
wenjun 已提交
227 228

    ret = UtilsDeleteValue(key);
M
mamingshuai 已提交
229
    EXPECT_EQ(ret, 0);
W
wenjun 已提交
230 231 232
}

/**
M
mamingshuai 已提交
233 234
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0500
 * @tc.name      : UtilsSetValue parameter Illegal test key length is 32 Byte
W
wenjun 已提交
235 236
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
237
HWTEST_F(KvStoreTest, testUtilsSetValue005, Function | MediumTest | Level2)
W
wenjun 已提交
238 239 240 241
{
    char key[] = "rw.sys.version.utilskvparameter1";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
242
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
243 244 245
}

/**
M
mamingshuai 已提交
246 247
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0600
 * @tc.name      : UtilsSetValue parameter Illegal test key length is 33 Byte
W
wenjun 已提交
248 249
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
250
HWTEST_F(KvStoreTest, testUtilsSetValue006, Function | MediumTest | Level2)
W
wenjun 已提交
251 252 253 254
{
    char key[] = "rw.sys.version.utilskvparameter12";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
255
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
256 257 258
}

/**
M
mamingshuai 已提交
259 260
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0700
 * @tc.name      : UtilsSetValue parameter Illegal test key length is 41 Byte
W
wenjun 已提交
261 262
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
263
HWTEST_F(KvStoreTest, testUtilsSetValue007, Function | MediumTest | Level2)
W
wenjun 已提交
264 265 266 267
{
    char key[] = "rw.sys.version.utilskvparameterforillegal";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
268
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
269 270 271
}

/**
M
mamingshuai 已提交
272 273
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0800
 * @tc.name      : UtilsSetValue parameter Illegal test key is an invalid character
W
wenjun 已提交
274 275
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
276
HWTEST_F(KvStoreTest, testUtilsSetValue008, Function | MediumTest | Level2)
W
wenjun 已提交
277 278 279 280 281 282 283 284
{
    char key[] = "Rw.sys.version";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, INVALID_PARAMETER);
}

/**
M
mamingshuai 已提交
285 286
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_0900
 * @tc.name      : UtilsSetValue parameter Illegal test key is an invalid character with minus
W
wenjun 已提交
287 288
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
289
HWTEST_F(KvStoreTest, testUtilsSetValue009, Function | MediumTest | Level2)
W
wenjun 已提交
290 291 292 293
{
    char key[] = "rw.sys.version-r3";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
294
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
295 296 297
}

/**
M
mamingshuai 已提交
298 299
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1000
 * @tc.name      : UtilsSetValue parameter Illegal test key is an invalid character with plus
W
wenjun 已提交
300 301
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
302
HWTEST_F(KvStoreTest, testUtilsSetValue010, Function | MediumTest | Level2)
W
wenjun 已提交
303 304 305 306
{
    char key[] = "RE+R3";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
307
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
308 309 310
}

/**
M
mamingshuai 已提交
311 312
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1100
 * @tc.name      : UtilsSetValue parameter Illegal test key is an invalid character with multiplication
W
wenjun 已提交
313 314
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
315
HWTEST_F(KvStoreTest, testUtilsSetValue0011, Function | MediumTest | Level2)
W
wenjun 已提交
316 317 318 319
{
    char key[] = "rw.sys.version*r3";
    char value[] = "Hello world !";
    int ret = UtilsSetValue(key, value);
M
mamingshuai 已提交
320
    EXPECT_EQ(ret, INVALID_PARAMETER);
W
wenjun 已提交
321 322 323
}

/**
M
mamingshuai 已提交
324 325
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1200
 * @tc.name      : UtilsSetValue parameter legal test Value is equal to 121 characters
W
wenjun 已提交
326 327
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
328
HWTEST_F(KvStoreTest, testUtilsSetValue012, Function | MediumTest | Level0)
W
wenjun 已提交
329 330 331 332 333 334 335 336 337 338 339 340
{
    char key[] = "rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange How strange ";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
341 342
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1300
 * @tc.name      : UtilsSetValue parameter legal test Value is equal to 126 characters
W
wenjun 已提交
343 344
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
345
HWTEST_F(KvStoreTest, testUtilsSetValue013, Function | MediumTest | Level1)
W
wenjun 已提交
346 347 348 349 350 351 352 353 354 355 356 357
{
    char key[] = "rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange Howstrangesleeping";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
358 359
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1400
 * @tc.name      : UtilsSetValue parameter legal test Value is equal to 127 characters
W
wenjun 已提交
360 361
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
362
HWTEST_F(KvStoreTest, testUtilsSetValue014, Function | MediumTest | Level1)
W
wenjun 已提交
363 364 365 366 367 368 369 370 371 372 373 374
{
    char key[] = "rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange How strangesleeping";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
375 376
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1500
 * @tc.name      : UtilsSetValue parameter Illegal test Value is equal to 128 characters
W
wenjun 已提交
377 378
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
379
HWTEST_F(KvStoreTest, testUtilsSetValue015, Function | MediumTest | Level1)
W
wenjun 已提交
380 381 382 383 384 385 386 387 388
{
    char key[] = "rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange How strange sleeping";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, INVALID_PARAMETER);
}

/**
M
mamingshuai 已提交
389 390
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1600
 * @tc.name      : UtilsSetValue parameter Illegal test Value greater than 128 characters
W
wenjun 已提交
391 392
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
393
HWTEST_F(KvStoreTest, testUtilsSetValue016, Function | MediumTest | Level2)
W
wenjun 已提交
394 395 396 397 398 399 400 401 402
{
    char key[] = "rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange How strange  Are you sleeping";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, INVALID_PARAMETER);
}

/**
M
mamingshuai 已提交
403
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1700
W
wenjun 已提交
404 405 406
 * @tc.name      : Value greater than 128 characters and key is an invalid character
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
407
HWTEST_F(KvStoreTest, testUtilsSetValue017, Function | MediumTest | Level2)
W
wenjun 已提交
408 409 410 411 412 413 414 415 416
{
    char key[] = "Rw.sys.version";
    char value[] = "Two tigers Two tigers two tiger running so fast \
running so fast one has no ears one has no tail How strange How strange  Are you sleeping";
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, INVALID_PARAMETER);
}

/**
M
mamingshuai 已提交
417 418
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1800
 * @tc.name      : Use the interface UtilsGetValue to get the kv value cache with key is alpha and dot
W
wenjun 已提交
419 420
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
421
HWTEST_F(KvStoreTest, testUtilsGetValue001, Function | MediumTest | Level0)
W
wenjun 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    char temp[MAX_VALUE_LEN_TEST] = {0};
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_EQ(ret, 0);
    EXPECT_STREQ(value, temp);
    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
437 438
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_1900
 * @tc.name      : Use the interface UtilsGetValue to get the kv value cache with key is digit
W
wenjun 已提交
439 440
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
441
HWTEST_F(KvStoreTest, testUtilsGetValue002, Function | MediumTest | Level1)
W
wenjun 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
{
    char key[] = "100";
    char value[] = "!@#¥%……&*()——+~《》?,。、“‘;:、12345767890";
    char temp[MAX_VALUE_LEN_TEST] = {0};
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);

    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_EQ(ret, 0);
    EXPECT_STREQ(value, temp);
    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
457 458
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2000
 * @tc.name      : Use the interface UtilsGetValue to get the kv value cache with key is dot and alpha
W
wenjun 已提交
459 460
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
461
HWTEST_F(KvStoreTest, testUtilsGetValue003, Function | MediumTest | Level1)
W
wenjun 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
    char key[] = "rw.sys.version";
    char temp[MAX_VALUE_LEN_TEST] = {0};
    // Update the value of key
    char value1[] = "Two tigers,Two tigers,two tiger,running so fast";
    int ret = UtilsSetValue(key, value1);
    EXPECT_EQ(ret, 0);

    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_EQ(ret, 0);
    EXPECT_STREQ(value1, temp);
    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

#ifdef FEATURE_KV_CACHE
/**
M
mamingshuai 已提交
479 480
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2100
 * @tc.name      : Use the interface ClearKVCache to clear cache with key is alpha and dot
W
wenjun 已提交
481 482
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
483
HWTEST_F(KvStoreTest, testClearKVCache001, Function | MediumTest | Level0)
W
wenjun 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    char temp[MAX_VALUE_LEN_TEST] = {0};
    int ret = UtilsSetValue(key, value);
    EXPECT_EQ(ret, 0);
    // Get the value of key
    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_EQ(ret, 0);
    EXPECT_STREQ(value, temp);
    // Clear cache
    ret = ClearKVCache();
    EXPECT_EQ(ret, 0);
    // Get the value of key
M
mamingshuai 已提交
498
    memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
W
wenjun 已提交
499 500 501 502 503 504 505 506 507
    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_GT(ret, 0);
    EXPECT_STREQ(value, temp);
    // Clear key
    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}

/**
M
mamingshuai 已提交
508 509
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2200
 * @tc.name      : Use the interface ClearKVCache to clear cache with key is dot and alpha
W
wenjun 已提交
510 511
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
512
HWTEST_F(KvStoreTest, testClearKVCache002, Function | MediumTest | Level1)
W
wenjun 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
{
    char key[] = "rw.sys.version";
    char temp[MAX_VALUE_LEN_TEST] = {0};
    // Update the value of key
    char value1[] = "Two tigers,Two tigers,two tiger,running so fast";
    int ret = UtilsSetValue(key, value1);
    EXPECT_EQ(ret, 0);
    // Get the value of key
    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_EQ(ret, 0);
    EXPECT_STREQ(value1, temp);
    // Clear cache
    ret = ClearKVCache();
    EXPECT_EQ(ret, 0);
    // Get the value of key
    memset_s(temp, MAX_VALUE_LEN_TEST, 0, MAX_VALUE_LEN_TEST);
    ret = UtilsGetValue(key, temp, MAX_VALUE_LEN_TEST);
    EXPECT_GT(ret, 0);
    EXPECT_STREQ(value1, temp);
    // Clear key
    ret = UtilsDeleteValue(key);
    EXPECT_EQ(ret, 0);
}
#endif

/**
M
mamingshuai 已提交
539 540
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2300
 * @tc.name      : Specification test with cache num is less than max
W
wenjun 已提交
541 542
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
543
HWTEST_F(KvStoreTest, testKVCacheCacheSize001, Function | MediumTest | Level1)
W
wenjun 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_CACHE_NUM_TEST-1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_CACHE_NUM_TEST-1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
564
    }
W
wenjun 已提交
565 566 567 568 569 570 571
    ret = DeleteKVFiles(MAX_CACHE_NUM_TEST-1, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
572
    }
W
wenjun 已提交
573 574 575
}

/**
M
mamingshuai 已提交
576 577
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2400
 * @tc.name      : Specification test with cache num is equal to max
W
wenjun 已提交
578 579
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
580
HWTEST_F(KvStoreTest, testKVCacheCacheSize002, Function | MediumTest | Level1)
W
wenjun 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_CACHE_NUM_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_CACHE_NUM_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
601
    }
W
wenjun 已提交
602 603 604 605 606 607 608
    ret = DeleteKVFiles(MAX_CACHE_NUM_TEST, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
609
    }
W
wenjun 已提交
610 611 612
}

/**
M
mamingshuai 已提交
613 614
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2500
 * @tc.name      : Specification test with cache num is greater than max
W
wenjun 已提交
615 616
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
617
HWTEST_F(KvStoreTest, testKVCacheCacheSize003, Function | MediumTest | Level1)
W
wenjun 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_CACHE_NUM_TEST+1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_CACHE_NUM_TEST+1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
638
    }
W
wenjun 已提交
639 640 641 642 643 644 645
    ret = DeleteKVFiles(MAX_CACHE_NUM_TEST+1, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
M
mamingshuai 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    }
}

/**
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2600
 * @tc.name      : Specification test with key num is less than max
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(KvStoreTest, testKVStoreSize001, Function | MediumTest | Level1)
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_KEY_NUM_TEST-1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_KEY_NUM_TEST-1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = DeleteKVFiles(MAX_KEY_NUM_TEST-1, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
}

/**
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2700
 * @tc.name      : Specification test with key num is equal to max
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(KvStoreTest, testKVStoreSize002, Function | MediumTest | Level1)
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_KEY_NUM_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_KEY_NUM_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = DeleteKVFiles(MAX_KEY_NUM_TEST, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
}

/**
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2800
 * @tc.name      : Specification test with key num is greater than max
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(KvStoreTest, testKVStoreSize003, Function | MediumTest | Level1)
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_KEY_NUM_TEST+1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_KEY_NUM_TEST+1, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = DeleteKVFiles(MAX_KEY_NUM_TEST+1, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
}

/**
 * @tc.number    : SUB_UTILS_KV_STORE_POSIX_2900
 * @tc.name      : Specification test with value num is equal to max
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(KvStoreTest, testKVStoreSize004, Function | MediumTest | Level1)
{
    char key[] = "rw.sys.version";
    char value[] = "It is never too old to learn";
    bool ret = false;

    ret = TouchKVFiles(MAX_VALUE_LEN_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = ReadKVFiles(MAX_VALUE_LEN_TEST, key, value);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
    ret = DeleteKVFiles(MAX_VALUE_LEN_TEST, key);
    if (ret == true)
    {
        SUCCEED();
    }else
    {
        ADD_FAILURE();
    }
}