KeyValueStorageTest.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "KeyValueStorageTest.h"
  19. #include <cstring>
  20. #include <string>
  21. #include <lib/support/CodeUtils.h>
  22. #include <lib/support/ErrorStr.h>
  23. #include <lib/support/logging/CHIPLogging.h>
  24. #include <platform/KeyValueStoreManager.h>
  25. using namespace chip::DeviceLayer::PersistedStorage;
  26. #define RUN_TEST(test_result) \
  27. do \
  28. { \
  29. const CHIP_ERROR temp_test_result = test_result; \
  30. if (temp_test_result != CHIP_NO_ERROR) \
  31. { \
  32. char error_str[255]; \
  33. chip::FormatCHIPError(error_str, sizeof(error_str), temp_test_result); \
  34. ChipLogError(NotSpecified, "%s: FAILED [%s]", #test_result, chip::ErrorStr(temp_test_result)); \
  35. } \
  36. else \
  37. { \
  38. ChipLogProgress(NotSpecified, "%s: PASSED", #test_result); \
  39. } \
  40. } while (0)
  41. namespace chip {
  42. namespace {
  43. CHIP_ERROR TestEmptyString()
  44. {
  45. const char * kTestKey = "str_key";
  46. const char kTestValue[] = "";
  47. char read_value[sizeof(kTestValue)];
  48. size_t read_size;
  49. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  50. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
  51. ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
  52. ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
  53. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  54. return CHIP_NO_ERROR;
  55. }
  56. CHIP_ERROR TestString()
  57. {
  58. const char * kTestKey = "str_key";
  59. const char kTestValue[] = "test_value";
  60. char read_value[sizeof(kTestValue)];
  61. size_t read_size;
  62. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  63. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
  64. ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
  65. ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
  66. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  67. return CHIP_NO_ERROR;
  68. }
  69. CHIP_ERROR TestUint32()
  70. {
  71. const char * kTestKey = "uint32_key";
  72. uint32_t kTestValue = 5;
  73. uint32_t read_value;
  74. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  75. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
  76. ReturnErrorCodeIf(kTestValue != read_value, CHIP_ERROR_INTERNAL);
  77. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  78. return CHIP_NO_ERROR;
  79. }
  80. CHIP_ERROR TestArray()
  81. {
  82. const char * kTestKey = "array_key";
  83. uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
  84. uint32_t read_value[5];
  85. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  86. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
  87. ReturnErrorCodeIf(memcmp(kTestValue, read_value, sizeof(kTestValue)) != 0, CHIP_ERROR_INTERNAL);
  88. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  89. return CHIP_NO_ERROR;
  90. }
  91. CHIP_ERROR TestStruct()
  92. {
  93. struct SomeStruct
  94. {
  95. uint8_t value1;
  96. uint32_t value2;
  97. };
  98. const char * kTestKey = "struct_key";
  99. SomeStruct kTestValue{ value1 : 1, value2 : 2 };
  100. SomeStruct read_value;
  101. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  102. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
  103. ReturnErrorCodeIf(kTestValue.value1 != read_value.value1, CHIP_ERROR_INTERNAL);
  104. ReturnErrorCodeIf(kTestValue.value2 != read_value.value2, CHIP_ERROR_INTERNAL);
  105. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  106. return CHIP_NO_ERROR;
  107. }
  108. CHIP_ERROR TestUpdateValue()
  109. {
  110. const char * kTestKey = "update_key";
  111. uint32_t read_value;
  112. for (size_t i = 0; i < 10; i++)
  113. {
  114. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, i));
  115. ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
  116. ReturnErrorCodeIf(i != read_value, CHIP_ERROR_INTERNAL);
  117. }
  118. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  119. return CHIP_NO_ERROR;
  120. }
  121. CHIP_ERROR TestMultiRead()
  122. {
  123. const char * kTestKey = "multi_key";
  124. uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
  125. ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
  126. for (size_t i = 0; i < 5; i++)
  127. {
  128. uint32_t read_value;
  129. size_t read_size;
  130. // Returns buffer too small for all but the last read.
  131. CHIP_ERROR error = KeyValueStoreMgr().Get(kTestKey, &read_value, sizeof(read_value), &read_size, i * sizeof(uint32_t));
  132. ReturnErrorCodeIf(error != (i < 4 ? CHIP_ERROR_BUFFER_TOO_SMALL : CHIP_NO_ERROR), error);
  133. ReturnErrorCodeIf(read_size != sizeof(read_value), CHIP_ERROR_INTERNAL);
  134. ReturnErrorCodeIf(kTestValue[i] != read_value, CHIP_ERROR_INTERNAL);
  135. }
  136. ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
  137. return CHIP_NO_ERROR;
  138. }
  139. } // namespace
  140. void RunKvsTest(TestConfigurations test_config)
  141. {
  142. RUN_TEST(TestEmptyString());
  143. RUN_TEST(TestString());
  144. RUN_TEST(TestUint32());
  145. RUN_TEST(TestArray());
  146. RUN_TEST(TestStruct());
  147. RUN_TEST(TestUpdateValue());
  148. if (test_config != SKIP_MULTI_READ_TEST)
  149. {
  150. RUN_TEST(TestMultiRead());
  151. }
  152. }
  153. } // namespace chip