From c3f5bd8d7bbfb2734d96e1e6f834c9950b4533fe Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 24 Feb 2024 13:16:41 +0100 Subject: [PATCH] StorageAPI: Add support for loading/storing empty strings/binaries --- include/wups/storage.h | 5 ++- plugins/storage_test_plugin/src/test.cpp | 51 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/wups/storage.h b/include/wups/storage.h index de86ea3..1d2e8f0 100644 --- a/include/wups/storage.h +++ b/include/wups/storage.h @@ -437,7 +437,7 @@ inline WUPSStorageError WUPSStorageAPI_StoreDouble(wups_storage_item parent, con * \see WUPSStorageAPI_StoreItem */ inline WUPSStorageError WUPSStorageAPI_StoreBinary(wups_storage_item parent, const char *key, const void *data, uint32_t size) { - if (data == NULL) { + if (data == NULL && size > 0) { return WUPS_STORAGE_ERROR_INVALID_ARGS; } return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_BINARY, (void *) data, size); @@ -1062,6 +1062,9 @@ namespace WUPSStorageAPI { auto r = WUPSStorageAPI_GetItemSize(parent, key.data(), WUPS_STORAGE_ITEM_BINARY, &resizeToSize); if (r == WUPS_STORAGE_ERROR_SUCCESS) { outValue.resize(resizeToSize); + if (resizeToSize == 0) { + return WUPS_STORAGE_ERROR_SUCCESS; + } } else { return r; } diff --git a/plugins/storage_test_plugin/src/test.cpp b/plugins/storage_test_plugin/src/test.cpp index a194b59..04db25b 100644 --- a/plugins/storage_test_plugin/src/test.cpp +++ b/plugins/storage_test_plugin/src/test.cpp @@ -927,4 +927,55 @@ TEST_CASE("Load stored item from deleted sub-item should fail") { res = subItem1->Get(itemKey, readValue); REQUIRE(res == WUPS_STORAGE_ERROR_NOT_FOUND); REQUIRE(readValue == 0); +} + +TEST_CASE("Test GetOrStoreDefault with no existing value") { + // make sure storage is closed + auto res = WUPSStorageAPI::WipeStorage(); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::string target = "will be discarded"; + std::string default_value = "new_default"; + res = WUPSStorageAPI::GetOrStoreDefault("test", target, default_value); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + REQUIRE(target == default_value); +} + +TEST_CASE("Test GetOrStoreDefault with existing value") { + // make sure storage is closed + auto res = WUPSStorageAPI::WipeStorage(); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::string original_value = "original_value"; + std::string target = original_value; + res = WUPSStorageAPI::Store("test", target); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::string default_value = "new_default"; + res = WUPSStorageAPI::GetOrStoreDefault("test", target, default_value); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + REQUIRE(target == original_value); +} + +TEST_CASE("Store empty string works") { + // make sure storage is closed + auto res = WUPSStorageAPI::WipeStorage(); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::string empty_string; + res = WUPSStorageAPI::Store("test", empty_string); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::string get_test = "_"; + res = WUPSStorageAPI::Get("test", get_test); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + REQUIRE(get_test.empty()); +} + +TEST_CASE("Store empty binary works") { + // make sure storage is closed + auto res = WUPSStorageAPI::WipeStorage(); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::vector empty_vector; + res = WUPSStorageAPI::Store("test", empty_vector); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + std::vector get_vector{0x13, 0x37}; + res = WUPSStorageAPI::Get("test", get_vector); + REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS); + REQUIRE(empty_vector.empty()); } \ No newline at end of file