3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "KeyValueStorageTest.h"
24 #include <platform/KeyValueStoreManager.h>
25 #include <support/ErrorStr.h>
26 #include <support/logging/CHIPLogging.h>
28 using namespace chip::DeviceLayer::PersistedStorage;
30 #define RUN_TEST(test_result) \
33 const CHIP_ERROR temp_test_result = test_result; \
34 if (temp_test_result != CHIP_NO_ERROR) \
36 char error_str[255]; \
37 chip::FormatCHIPError(error_str, sizeof(error_str), temp_test_result); \
38 ChipLogError(NotSpecified, "%s: FAILED %d [%s]", #test_result, temp_test_result, chip::ErrorStr(temp_test_result)); \
42 ChipLogProgress(NotSpecified, "%s: PASSED", #test_result); \
49 CHIP_ERROR TestEmptyString()
51 const char * kTestKey = "str_key";
52 const char kTestValue[] = "";
53 char read_value[sizeof(kTestValue)];
55 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
56 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
57 ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
58 ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
59 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
63 CHIP_ERROR TestString()
65 const char * kTestKey = "str_key";
66 const char kTestValue[] = "test_value";
67 char read_value[sizeof(kTestValue)];
69 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
70 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
71 ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
72 ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
73 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
77 CHIP_ERROR TestUint32()
79 const char * kTestKey = "uint32_key";
80 uint32_t kTestValue = 5;
82 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
83 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
84 ReturnErrorCodeIf(kTestValue != read_value, CHIP_ERROR_INTERNAL);
85 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
89 CHIP_ERROR TestArray()
91 const char * kTestKey = "array_key";
92 uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
93 uint32_t read_value[5];
94 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
95 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
96 ReturnErrorCodeIf(memcmp(kTestValue, read_value, sizeof(kTestValue)) != 0, CHIP_ERROR_INTERNAL);
97 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
101 CHIP_ERROR TestStruct()
108 const char * kTestKey = "struct_key";
109 SomeStruct kTestValue{ value1 : 1, value2 : 2 };
110 SomeStruct read_value;
111 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
112 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
113 ReturnErrorCodeIf(kTestValue.value1 != read_value.value1, CHIP_ERROR_INTERNAL);
114 ReturnErrorCodeIf(kTestValue.value2 != read_value.value2, CHIP_ERROR_INTERNAL);
115 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
116 return CHIP_NO_ERROR;
119 CHIP_ERROR TestUpdateValue()
121 const char * kTestKey = "update_key";
123 for (size_t i = 0; i < 10; i++)
125 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, i));
126 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
127 ReturnErrorCodeIf(i != read_value, CHIP_ERROR_INTERNAL);
129 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
130 return CHIP_NO_ERROR;
133 CHIP_ERROR TestMultiRead()
135 const char * kTestKey = "multi_key";
136 uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
137 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
138 for (size_t i = 0; i < 5; i++)
142 // Returns buffer too small for all but the last read.
143 CHIP_ERROR error = KeyValueStoreMgr().Get(kTestKey, &read_value, sizeof(read_value), &read_size, i * sizeof(uint32_t));
144 ReturnErrorCodeIf(error != (i < 4 ? CHIP_ERROR_BUFFER_TOO_SMALL : CHIP_NO_ERROR), error);
145 ReturnErrorCodeIf(read_size != sizeof(read_value), CHIP_ERROR_INTERNAL);
146 ReturnErrorCodeIf(kTestValue[i] != read_value, CHIP_ERROR_INTERNAL);
148 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
149 return CHIP_NO_ERROR;
154 void RunKvsTest(TestConfigurations test_config)
156 RUN_TEST(TestEmptyString());
157 RUN_TEST(TestString());
158 RUN_TEST(TestUint32());
159 RUN_TEST(TestArray());
160 RUN_TEST(TestStruct());
161 RUN_TEST(TestUpdateValue());
162 if (test_config != SKIP_MULTI_READ_TEST)
164 RUN_TEST(TestMultiRead());