diff --git a/ion/Makefile b/ion/Makefile index 02cc6af5a..c61f8e087 100644 --- a/ion/Makefile +++ b/ion/Makefile @@ -29,4 +29,5 @@ tests += $(addprefix ion/test/,\ crc32.cpp\ events.cpp\ keyboard.cpp\ + storage.cpp\ ) diff --git a/ion/include/ion/storage.h b/ion/include/ion/storage.h index a4bfe5d47..41d95a0ee 100644 --- a/ion/include/ion/storage.h +++ b/ion/include/ion/storage.h @@ -2,6 +2,7 @@ #define ION_STORAGE_H #include +#include namespace Ion { diff --git a/ion/test/storage.cpp b/ion/test/storage.cpp new file mode 100644 index 000000000..a6082746f --- /dev/null +++ b/ion/test/storage.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include + +using namespace Ion; + +Storage::Record::ErrorStatus putRecordInSharedStorage(const char * baseName, const char * extension, const char * data) { + size_t dataSize = strlen(data); + return Storage::sharedStorage()->createRecordWithExtension(baseName, extension, data, dataSize); +} + +QUIZ_CASE(ion_storage_store_and_destroy_record) { + size_t initialStorageAvailableStage = Storage::sharedStorage()->availableSize(); + + const char * baseNameRecord = "ionTestStorage"; + const char * extensionRecord = "record1"; + const char * dataRecord = "This is a test to ensure one can create, retreive, modify and delete records in ion's shared storage."; + + // Put a record in the store + Storage::Record::ErrorStatus error = putRecordInSharedStorage(baseNameRecord, extensionRecord, dataRecord); + quiz_assert(error == Storage::Record::ErrorStatus::None); + + // Retreive the record + Storage::Record retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + size_t dataRecordSize = strlen(dataRecord); + quiz_assert(retreivedRecord.value().size == dataRecordSize); + quiz_assert(strcmp(dataRecord, static_cast(retreivedRecord.value().buffer)) == 0); + + // Destroy it + retreivedRecord.destroy(); + retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + quiz_assert(retreivedRecord == Storage::Record()); + quiz_assert(Storage::sharedStorage()->availableSize() == initialStorageAvailableStage); +} + + +QUIZ_CASE(ion_storage_put_record_twice) { + size_t initialStorageAvailableStage = Storage::sharedStorage()->availableSize(); + + const char * baseNameRecord = "ionTestStorage"; + const char * extensionRecord = "record"; + const char * dataRecord = "This is a test to ensure one can create, retreive, modify and delete records in ion's shared storage."; + + // Put a record in the store + Storage::Record::ErrorStatus error = putRecordInSharedStorage(baseNameRecord, extensionRecord, dataRecord); + quiz_assert(error == Storage::Record::ErrorStatus::None); + + // Put the same record again: an error should be issued + error = Storage::sharedStorage()->createRecordWithExtension(baseNameRecord, extensionRecord, dataRecord, strlen(dataRecord)); + quiz_assert(error == Storage::Record::ErrorStatus::NameTaken); + + // Retreive the record + Storage::Record retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + size_t dataRecordSize = strlen(dataRecord); + quiz_assert(retreivedRecord.value().size == dataRecordSize); + quiz_assert(strcmp(dataRecord, static_cast(retreivedRecord.value().buffer)) == 0); + + // Destroy it + retreivedRecord.destroy(); + retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + quiz_assert(retreivedRecord == Storage::Record()); + quiz_assert(Storage::sharedStorage()->availableSize() == initialStorageAvailableStage); +} + +QUIZ_CASE(ion_storage_invalid_renaming) { + size_t initialStorageAvailableStage = Storage::sharedStorage()->availableSize(); + + const char * baseNameRecord = "ionTestStorage"; + const char * extensionRecord = "record1"; + const char * dataRecord = "This is a test to ensure one can create, retreive, modify and delete records in ion's shared storage."; + + // Put a record in the store + Storage::Record::ErrorStatus error = putRecordInSharedStorage(baseNameRecord, extensionRecord, dataRecord); + quiz_assert(error == Storage::Record::ErrorStatus::None); + + // Retreive the record + Storage::Record retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + size_t dataRecordSize = strlen(dataRecord); + quiz_assert(retreivedRecord.value().size == dataRecordSize); + quiz_assert(strcmp(dataRecord, static_cast(retreivedRecord.value().buffer)) == 0); + + // Rename the record with an invalid name + const char * fullNameRecord2 = "invalidNameWithoutDot"; + error = retreivedRecord.setName(fullNameRecord2); + quiz_assert(error == Storage::Record::ErrorStatus::NonCompliantName); + + // Destroy it + retreivedRecord.destroy(); + retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + quiz_assert(retreivedRecord == Storage::Record()); + quiz_assert(Storage::sharedStorage()->availableSize() == initialStorageAvailableStage); +} + +QUIZ_CASE(ion_storage_valid_renaming) { + size_t initialStorageAvailableStage = Storage::sharedStorage()->availableSize(); + + const char * baseNameRecord = "ionTestStorage"; + const char * extensionRecord = "record1"; + const char * dataRecord = "This is a test to ensure one can create, retreive, modify and delete records in ion's shared storage."; + + // Put a record in the store + Storage::Record::ErrorStatus error = putRecordInSharedStorage(baseNameRecord, extensionRecord, dataRecord); + quiz_assert(error == Storage::Record::ErrorStatus::None); + + // Retreive the record + Storage::Record retreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + size_t dataRecordSize = strlen(dataRecord); + quiz_assert(retreivedRecord.value().size == dataRecordSize); + quiz_assert(strcmp(dataRecord, static_cast(retreivedRecord.value().buffer)) == 0); + + // Rename the record with a valid name + const char * newFullNameRecord = "testStorage.record2"; + error = retreivedRecord.setName(newFullNameRecord); + quiz_assert(error == Storage::Record::ErrorStatus::None); + + // Retreive the previous record + Storage::Record oldRetreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + quiz_assert(oldRetreivedRecord == Storage::Record()); + + // Retreive the new record + Storage::Record newRetreivedRecord = Storage::sharedStorage()->recordNamed(newFullNameRecord); + quiz_assert(strcmp(dataRecord, static_cast(newRetreivedRecord.value().buffer)) == 0); + + // Destroy it + newRetreivedRecord.destroy(); + newRetreivedRecord = Storage::sharedStorage()->recordBaseNamedWithExtension(baseNameRecord, extensionRecord); + quiz_assert(newRetreivedRecord == Storage::Record()); + quiz_assert(Storage::sharedStorage()->availableSize() == initialStorageAvailableStage); +}