Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / persistent-storage / KeyValueStorageTest.cpp
1 /*
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
19 #include "KeyValueStorageTest.h"
20
21 #include <cstring>
22 #include <string>
23
24 #include <platform/KeyValueStoreManager.h>
25 #include <support/ErrorStr.h>
26 #include <support/logging/CHIPLogging.h>
27
28 using namespace chip::DeviceLayer::PersistedStorage;
29
30 #define RUN_TEST(test_result)                                                                                                      \
31     do                                                                                                                             \
32     {                                                                                                                              \
33         const CHIP_ERROR temp_test_result = test_result;                                                                           \
34         if (temp_test_result != CHIP_NO_ERROR)                                                                                     \
35         {                                                                                                                          \
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));    \
39         }                                                                                                                          \
40         else                                                                                                                       \
41         {                                                                                                                          \
42             ChipLogProgress(NotSpecified, "%s: PASSED", #test_result);                                                             \
43         }                                                                                                                          \
44     } while (0)
45
46 namespace chip {
47 namespace {
48
49 CHIP_ERROR TestEmptyString()
50 {
51     const char * kTestKey   = "str_key";
52     const char kTestValue[] = "";
53     char read_value[sizeof(kTestValue)];
54     size_t read_size;
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));
60     return CHIP_NO_ERROR;
61 }
62
63 CHIP_ERROR TestString()
64 {
65     const char * kTestKey   = "str_key";
66     const char kTestValue[] = "test_value";
67     char read_value[sizeof(kTestValue)];
68     size_t read_size;
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));
74     return CHIP_NO_ERROR;
75 }
76
77 CHIP_ERROR TestUint32()
78 {
79     const char * kTestKey = "uint32_key";
80     uint32_t kTestValue   = 5;
81     uint32_t read_value;
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));
86     return CHIP_NO_ERROR;
87 }
88
89 CHIP_ERROR TestArray()
90 {
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));
98     return CHIP_NO_ERROR;
99 }
100
101 CHIP_ERROR TestStruct()
102 {
103     struct SomeStruct
104     {
105         uint8_t value1;
106         uint32_t value2;
107     };
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;
117 }
118
119 CHIP_ERROR TestUpdateValue()
120 {
121     const char * kTestKey = "update_key";
122     uint32_t read_value;
123     for (size_t i = 0; i < 10; i++)
124     {
125         ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, i));
126         ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
127         ReturnErrorCodeIf(i != read_value, CHIP_ERROR_INTERNAL);
128     }
129     ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
130     return CHIP_NO_ERROR;
131 }
132
133 CHIP_ERROR TestMultiRead()
134 {
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++)
139     {
140         uint32_t read_value;
141         size_t read_size;
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);
147     }
148     ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
149     return CHIP_NO_ERROR;
150 }
151
152 } // namespace
153
154 void RunKvsTest(TestConfigurations test_config)
155 {
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)
163     {
164         RUN_TEST(TestMultiRead());
165     }
166 }
167
168 } // namespace chip