rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 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 | |
rgoliver | 6ce4044 | 2021-02-05 15:13:18 -0500 | [diff] [blame] | 19 | #include "KeyValueStorageTest.h" |
| 20 | |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 21 | #include <cstring> |
| 22 | #include <string> |
| 23 | |
Boris Zbarsky | 9dee693 | 2023-09-28 22:49:28 -0400 | [diff] [blame] | 24 | #include <lib/core/ErrorStr.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 25 | #include <lib/support/CodeUtils.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 26 | #include <lib/support/logging/CHIPLogging.h> |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 27 | #include <platform/KeyValueStoreManager.h> |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 28 | |
| 29 | using 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 Schoedel | 0e54870 | 2021-07-16 18:34:23 -0400 | [diff] [blame] | 39 | ChipLogError(NotSpecified, "%s: FAILED [%s]", #test_result, chip::ErrorStr(temp_test_result)); \ |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 40 | } \ |
| 41 | else \ |
| 42 | { \ |
| 43 | ChipLogProgress(NotSpecified, "%s: PASSED", #test_result); \ |
| 44 | } \ |
| 45 | } while (0) |
| 46 | |
| 47 | namespace chip { |
| 48 | namespace { |
| 49 | |
| 50 | CHIP_ERROR TestEmptyString() |
| 51 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 52 | static const char kTestKey[] = "str_key"; |
| 53 | static const char kTestValue[] = ""; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 54 | 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 | |
| 64 | CHIP_ERROR TestString() |
| 65 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 66 | static const char kTestKey[] = "str_key"; |
| 67 | static const char kTestValue[] = "test_value"; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 68 | 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 | |
| 78 | CHIP_ERROR TestUint32() |
| 79 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 80 | static const char kTestKey[] = "uint32_key"; |
| 81 | uint32_t kTestValue = 5; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 82 | 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 | |
| 90 | CHIP_ERROR TestArray() |
| 91 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 92 | static const char kTestKey[] = "array_key"; |
| 93 | uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 }; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 94 | 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 | |
| 102 | CHIP_ERROR TestStruct() |
| 103 | { |
| 104 | struct SomeStruct |
| 105 | { |
| 106 | uint8_t value1; |
| 107 | uint32_t value2; |
| 108 | }; |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 109 | static const char kTestKey[] = "struct_key"; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 110 | 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 | |
| 120 | CHIP_ERROR TestUpdateValue() |
| 121 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 122 | static const char kTestKey[] = "update_key"; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 123 | 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 | |
| 134 | CHIP_ERROR TestMultiRead() |
| 135 | { |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 136 | static const char kTestKey[] = "multi_key"; |
| 137 | uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 }; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 138 | 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 | |
rgoliver | 6ce4044 | 2021-02-05 15:13:18 -0500 | [diff] [blame] | 155 | void RunKvsTest(TestConfigurations test_config) |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 156 | { |
| 157 | RUN_TEST(TestEmptyString()); |
| 158 | RUN_TEST(TestString()); |
| 159 | RUN_TEST(TestUint32()); |
| 160 | RUN_TEST(TestArray()); |
| 161 | RUN_TEST(TestStruct()); |
| 162 | RUN_TEST(TestUpdateValue()); |
rgoliver | 6ce4044 | 2021-02-05 15:13:18 -0500 | [diff] [blame] | 163 | if (test_config != SKIP_MULTI_READ_TEST) |
| 164 | { |
| 165 | RUN_TEST(TestMultiRead()); |
| 166 | } |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace chip |