blob: 5aab299dc014ac74795fdf489c64427ec2ae4ffa [file] [log] [blame]
rgoliverbf60cc22021-01-26 21:15:59 -05001/*
2 *
3 * Copyright (c) 2021 Project CHIP Authors
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
rgoliver6ce40442021-02-05 15:13:18 -050019#include "KeyValueStorageTest.h"
20
rgoliverbf60cc22021-01-26 21:15:59 -050021#include <cstring>
22#include <string>
23
Boris Zbarsky9dee6932023-09-28 22:49:28 -040024#include <lib/core/ErrorStr.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080025#include <lib/support/CodeUtils.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080026#include <lib/support/logging/CHIPLogging.h>
rgoliverbf60cc22021-01-26 21:15:59 -050027#include <platform/KeyValueStoreManager.h>
rgoliverbf60cc22021-01-26 21:15:59 -050028
29using namespace chip::DeviceLayer::PersistedStorage;
30
31#define RUN_TEST(test_result) \
32 do \
33 { \
34 const CHIP_ERROR temp_test_result = test_result; \
35 if (temp_test_result != CHIP_NO_ERROR) \
36 { \
37 char error_str[255]; \
38 chip::FormatCHIPError(error_str, sizeof(error_str), temp_test_result); \
Kevin Schoedel0e548702021-07-16 18:34:23 -040039 ChipLogError(NotSpecified, "%s: FAILED [%s]", #test_result, chip::ErrorStr(temp_test_result)); \
rgoliverbf60cc22021-01-26 21:15:59 -050040 } \
41 else \
42 { \
43 ChipLogProgress(NotSpecified, "%s: PASSED", #test_result); \
44 } \
45 } while (0)
46
47namespace chip {
48namespace {
49
50CHIP_ERROR TestEmptyString()
51{
Michael Spang547b5872023-12-05 09:25:54 -050052 static const char kTestKey[] = "str_key";
53 static const char kTestValue[] = "";
rgoliverbf60cc22021-01-26 21:15:59 -050054 char read_value[sizeof(kTestValue)];
55 size_t read_size;
56 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
57 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
58 ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
59 ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
60 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
61 return CHIP_NO_ERROR;
62}
63
64CHIP_ERROR TestString()
65{
Michael Spang547b5872023-12-05 09:25:54 -050066 static const char kTestKey[] = "str_key";
67 static const char kTestValue[] = "test_value";
rgoliverbf60cc22021-01-26 21:15:59 -050068 char read_value[sizeof(kTestValue)];
69 size_t read_size;
70 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
71 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
72 ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
73 ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
74 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
75 return CHIP_NO_ERROR;
76}
77
78CHIP_ERROR TestUint32()
79{
Michael Spang547b5872023-12-05 09:25:54 -050080 static const char kTestKey[] = "uint32_key";
81 uint32_t kTestValue = 5;
rgoliverbf60cc22021-01-26 21:15:59 -050082 uint32_t read_value;
83 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
84 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
85 ReturnErrorCodeIf(kTestValue != read_value, CHIP_ERROR_INTERNAL);
86 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
87 return CHIP_NO_ERROR;
88}
89
90CHIP_ERROR TestArray()
91{
Michael Spang547b5872023-12-05 09:25:54 -050092 static const char kTestKey[] = "array_key";
93 uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
rgoliverbf60cc22021-01-26 21:15:59 -050094 uint32_t read_value[5];
95 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
96 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
97 ReturnErrorCodeIf(memcmp(kTestValue, read_value, sizeof(kTestValue)) != 0, CHIP_ERROR_INTERNAL);
98 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
99 return CHIP_NO_ERROR;
100}
101
102CHIP_ERROR TestStruct()
103{
104 struct SomeStruct
105 {
106 uint8_t value1;
107 uint32_t value2;
108 };
Michael Spang547b5872023-12-05 09:25:54 -0500109 static const char kTestKey[] = "struct_key";
rgoliverbf60cc22021-01-26 21:15:59 -0500110 SomeStruct kTestValue{ value1 : 1, value2 : 2 };
111 SomeStruct read_value;
112 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
113 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
114 ReturnErrorCodeIf(kTestValue.value1 != read_value.value1, CHIP_ERROR_INTERNAL);
115 ReturnErrorCodeIf(kTestValue.value2 != read_value.value2, CHIP_ERROR_INTERNAL);
116 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
117 return CHIP_NO_ERROR;
118}
119
120CHIP_ERROR TestUpdateValue()
121{
Michael Spang547b5872023-12-05 09:25:54 -0500122 static const char kTestKey[] = "update_key";
rgoliverbf60cc22021-01-26 21:15:59 -0500123 uint32_t read_value;
124 for (size_t i = 0; i < 10; i++)
125 {
126 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, i));
127 ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
128 ReturnErrorCodeIf(i != read_value, CHIP_ERROR_INTERNAL);
129 }
130 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
131 return CHIP_NO_ERROR;
132}
133
134CHIP_ERROR TestMultiRead()
135{
Michael Spang547b5872023-12-05 09:25:54 -0500136 static const char kTestKey[] = "multi_key";
137 uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
rgoliverbf60cc22021-01-26 21:15:59 -0500138 ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
139 for (size_t i = 0; i < 5; i++)
140 {
141 uint32_t read_value;
142 size_t read_size;
143 // Returns buffer too small for all but the last read.
144 CHIP_ERROR error = KeyValueStoreMgr().Get(kTestKey, &read_value, sizeof(read_value), &read_size, i * sizeof(uint32_t));
145 ReturnErrorCodeIf(error != (i < 4 ? CHIP_ERROR_BUFFER_TOO_SMALL : CHIP_NO_ERROR), error);
146 ReturnErrorCodeIf(read_size != sizeof(read_value), CHIP_ERROR_INTERNAL);
147 ReturnErrorCodeIf(kTestValue[i] != read_value, CHIP_ERROR_INTERNAL);
148 }
149 ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
150 return CHIP_NO_ERROR;
151}
152
153} // namespace
154
rgoliver6ce40442021-02-05 15:13:18 -0500155void RunKvsTest(TestConfigurations test_config)
rgoliverbf60cc22021-01-26 21:15:59 -0500156{
157 RUN_TEST(TestEmptyString());
158 RUN_TEST(TestString());
159 RUN_TEST(TestUint32());
160 RUN_TEST(TestArray());
161 RUN_TEST(TestStruct());
162 RUN_TEST(TestUpdateValue());
rgoliver6ce40442021-02-05 15:13:18 -0500163 if (test_config != SKIP_MULTI_READ_TEST)
164 {
165 RUN_TEST(TestMultiRead());
166 }
rgoliverbf60cc22021-01-26 21:15:59 -0500167}
168
169} // namespace chip