Skip to content

Commit

Permalink
StorageAPI: Add support for loading/storing empty strings/binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Feb 24, 2024
1 parent 46f4cf6 commit 2262844
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion include/wups/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
51 changes: 51 additions & 0 deletions plugins/storage_test_plugin/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 2262844

Please sign in to comment.