StorageAPI: Add support for loading/storing empty strings/binaries

This commit is contained in:
Maschell 2024-02-24 13:16:41 +01:00
parent 46f4cf6496
commit 2262844544
2 changed files with 55 additions and 1 deletions

View File

@ -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;
}

View File

@ -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<std::string>("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<uint8_t> empty_vector;
res = WUPSStorageAPI::Store("test", empty_vector);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::vector<uint8_t> get_vector{0x13, 0x37};
res = WUPSStorageAPI::Get("test", get_vector);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(empty_vector.empty());
}