diff --git a/examples/common/universal_filesystem/UniversalFileSystem.h b/examples/common/universal_filesystem/UniversalFileSystem.h index eb25aba..3d00e44 100644 --- a/examples/common/universal_filesystem/UniversalFileSystem.h +++ b/examples/common/universal_filesystem/UniversalFileSystem.h @@ -158,7 +158,11 @@ protected: } assert(_file); int ch = fgetc(_file); - TRACEF("FileStream::read: %c", ch); + if (ch == EOF) { + return ch; + } + --_available; + //TRACEF("FileStream::read: %c", ch); return ch; } inline virtual int peek() { diff --git a/examples/unit_tests/.gitignore b/examples/unit_tests/.gitignore deleted file mode 100644 index 89cc49c..0000000 --- a/examples/unit_tests/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch diff --git a/examples/unit_tests/.vscode/extensions.json b/examples/unit_tests/.vscode/extensions.json deleted file mode 100644 index 080e70d..0000000 --- a/examples/unit_tests/.vscode/extensions.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ], - "unwantedRecommendations": [ - "ms-vscode.cpptools-extension-pack" - ] -} diff --git a/examples/unit_tests/.vscode/settings.json b/examples/unit_tests/.vscode/settings.json deleted file mode 100644 index e179302..0000000 --- a/examples/unit_tests/.vscode/settings.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "files.associations": { - "__bit_reference": "cpp", - "__config": "cpp", - "__hash_table": "cpp", - "__locale": "cpp", - "__node_handle": "cpp", - "__split_buffer": "cpp", - "__threading_support": "cpp", - "__tree": "cpp", - "__verbose_abort": "cpp", - "array": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "cfenv": "cpp", - "charconv": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "complex": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "execution": "cpp", - "forward_list": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "list": "cpp", - "locale": "cpp", - "map": "cpp", - "mutex": "cpp", - "new": "cpp", - "optional": "cpp", - "ostream": "cpp", - "queue": "cpp", - "ratio": "cpp", - "set": "cpp", - "shared_mutex": "cpp", - "source_location": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "string_view": "cpp", - "strstream": "cpp", - "tuple": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "variant": "cpp", - "vector": "cpp", - "algorithm": "cpp" - } -} \ No newline at end of file diff --git a/examples/unit_tests/.vscode/tasks.json b/examples/unit_tests/.vscode/tasks.json deleted file mode 100644 index 5318667..0000000 --- a/examples/unit_tests/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang++ build active file", - "command": "/usr/bin/clang++", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/examples/unit_tests/FileSystem.cpp b/examples/unit_tests/FileSystem.cpp deleted file mode 100644 index 33a1590..0000000 --- a/examples/unit_tests/FileSystem.cpp +++ /dev/null @@ -1,525 +0,0 @@ -#include "FileSystem.h" - -#include -#include - -bool FileSystem::init() { - TRACE("FileSystem initializing..."); - -#ifdef ARDUINO -#ifdef BOARD_ESP32 - // Setup FileSystem - INFO("SPIFFS mounting FileSystem"); - if (!SPIFFS.begin(true, "")){ - ERROR("SPIFFS FileSystem mount failed"); - return false; - } - uint32_t size = SPIFFS.totalBytes() / (1024 * 1024); - Serial.print("size: "); - Serial.print(size); - Serial.println(" MB"); - uint32_t used = SPIFFS.usedBytes() / (1024 * 1024); - Serial.print("used: "); - Serial.print(used); - Serial.println(" MB"); - // ensure FileSystem is writable and format if not - RNS::Bytes test("test"); - if (write_file("/test", test) < 4) { - INFO("SPIFFS FileSystem is being formatted, please wait..."); - SPIFFS.format(); - } - else { - remove_file("/test"); - } - DEBUG("SPIFFS FileSystem is ready"); -#elif BOARD_NRF52 - // Initialize Internal File System - INFO("InternalFS mounting FileSystem"); - InternalFS.begin(); - INFO("InternalFS FileSystem is ready"); -#endif -#endif - return true; -} - -#ifdef ARDUINO -void FileSystem::listDir(const char* dir) { - Serial.print("DIR: "); - Serial.println(dir); -#ifdef BOARD_ESP32 - File root = SPIFFS.open(dir); - if (!root) { - Serial.println("Failed to opend directory"); - return; - } - File file = root.openNextFile(); - while (file) { - if (file.isDirectory()) { - Serial.print(" DIR: "); - } - else { - Serial.print(" FILE: "); - } - Serial.println(file.name()); - file.close(); - file = root.openNextFile(); - } - root.close(); -#elif BOARD_NRF52 - File root = InternalFS.open(dir); - if (!root) { - Serial.println("Failed to opend directory"); - return; - } - File file = root.openNextFile(); - while (file) { - if (file.isDirectory()) { - Serial.print(" DIR: "); - } - else { - Serial.print(" FILE: "); - } - Serial.println(file.name()); - file.close(); - file = root.openNextFile(); - } - root.close(); -#endif -} -#else -void FileSystem::listDir(const char* dir) { -} -#endif - -/*virtual*/ bool FileSystem::file_exists(const char* file_path) { -#ifdef ARDUINO -#ifdef BOARD_ESP32 - File file = SPIFFS.open(file_path, FILE_READ); - if (file) { -#elif BOARD_NRF52 - File file(InternalFS); - if (file.open(file_path, FILE_O_READ)) { -#else - if (false) { -#endif -#else - // Native - FILE* file = fopen(file_path, "r"); - if (file != nullptr) { -#endif - //TRACE("file_exists: file exists, closing file"); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - file.close(); -#elif BOARD_NRF52 - file.close(); -#endif -#else - // Native - fclose(file); -#endif - return true; - } - else { - ERROR("file_exists: failed to open file " + std::string(file_path)); - return false; - } -} - -/*virtual*/ size_t FileSystem::read_file(const char* file_path, RNS::Bytes& data) { - size_t read = 0; -#ifdef ARDUINO -#ifdef BOARD_ESP32 - File file = SPIFFS.open(file_path, FILE_READ); - if (file) { - size_t size = file.size(); - read = file.readBytes((char*)data.writable(size), size); -#elif BOARD_NRF52 - //File file(InternalFS); - //if (file.open(file_path, FILE_O_READ)) { - File file = InternalFS.open(file_path, FILE_O_READ); - if (file) { - size_t size = file.size(); - read = file.readBytes((char*)data.writable(size), size); -#else - if (false) { - size_t size = 0; -#endif -#else - // Native - FILE* file = fopen(file_path, "r"); - if (file != nullptr) { - fseek(file, 0, SEEK_END); - size_t size = ftell(file); - rewind(file); - //size_t read = fread(data.writable(size), size, 1, file); - read = fread(data.writable(size), 1, size, file); -#endif - TRACE("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path)); - if (read != size) { - ERROR("read_file: failed to read file " + std::string(file_path)); - data.clear(); - } - //TRACE("read_file: closing input file"); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - file.close(); -#elif BOARD_NRF52 - file.close(); -#endif -#else - // Native - fclose(file); -#endif - } - else { - ERROR("read_file: failed to open input file " + std::string(file_path)); - } - return read; -} - -/*virtual*/ size_t FileSystem::write_file(const char* file_path, const RNS::Bytes& data) { - // CBA TODO Replace remove with working truncation - remove_file(file_path); - size_t wrote = 0; -#ifdef ARDUINO -#ifdef BOARD_ESP32 - File file = SPIFFS.open(file_path, FILE_WRITE); - if (file) { - wrote = file.write(data.data(), data.size()); -#elif BOARD_NRF52 - File file(InternalFS); - if (file.open(file_path, FILE_O_WRITE)) { - wrote = file.write(data.data(), data.size()); -#else - if (false) { -#endif -#else - // Native - FILE* file = fopen(file_path, "w"); - if (file != nullptr) { - //size_t wrote = fwrite(data.data(), data.size(), 1, file); - wrote = fwrite(data.data(), 1, data.size(), file); -#endif - TRACE("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path)); - if (wrote < data.size()) { - WARNING("write_file: not all data was written to file " + std::string(file_path)); - } - //TRACE("write_file: closing output file"); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - file.close(); -#elif BOARD_NRF52 - file.close(); -#endif -#else - // Native - fclose(file); -#endif - } - else { - ERROR("write_file: failed to open output file " + std::string(file_path)); - } - return wrote; -} - -/*virtual*/ RNS::FileStream FileSystem::open_file(const char* file_path, RNS::FileStream::MODE file_mode) { - TRACEF("open_file: opening file %s", file_path); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - const char* mode; - if (file_mode == RNS::FileStream::MODE_READ) { - mode = FILE_READ; - } - else if (file_mode == RNS::FileStream::MODE_WRITE) { - mode = FILE_WRITE; - } - else if (file_mode == RNS::FileStream::MODE_APPEND) { - mode = FILE_APPEND; - } - else { - ERRORF("open_file: unsupported mode %d", file_mode); - return {RNS::Type::NONE}; - } - TRACEF("open_file: opening file %s in mode %s", file_path, mode); - //// Using copy constructor to create a File* instead of local - //File file = SPIFFS.open(file_path, mode); - //if (!file) { - File* file = new File(SPIFFS.open(file_path, mode)); - if (file == nullptr || !(*file)) { - ERRORF("open_file: failed to open output file %s", file_path); - return {RNS::Type::NONE}; - } - TRACEF("open_file: successfully opened file %s", file_path); - return RNS::FileStream(new FileStreamImpl(file)); -#elif BOARD_NRF52 - //File file = File(InternalFS); - File* file = new File(InternalFS); - int mode; - if (file_mode == RNS::FileStream::MODE_READ) { - mode = FILE_O_READ; - } - else if (file_mode == RNS::FileStream::MODE_WRITE) { - mode = FILE_O_WRITE; - // CBA TODO Replace remove with working truncation - if (InternalFS.exists(file_path)) { - InternalFS.remove(file_path); - } - } - else if (file_mode == RNS::FileStream::MODE_APPEND) { - // CBA This is the default write mode for nrf52 littlefs - mode = FILE_O_WRITE; - } - else { - ERRORF("open_file: unsupported mode %d", file_mode); - return {RNS::Type::NONE}; - } - if (!file->open(file_path, mode)) { - ERRORF("open_file: failed to open output file %s", file_path); - return {RNS::Type::NONE}; - } - - // Seek to beginning to overwrite (this is failing on nrf52) - //if (file_mode == RNS::FileStream::MODE_WRITE) { - // file->seek(0); - // file->truncate(0); - //} - TRACEF("open_file: successfully opened file %s", file_path); - return RNS::FileStream(new FileStreamImpl(file)); -#else - #warning("unsuppoprted"); - return RNS::FileStream(RNS::Type::NONE); -#endif -#else // ARDUINO - // Native - const char* mode; - if (file_mode == RNS::FileStream::MODE_READ) { - mode = "r"; - } - else if (file_mode == RNS::FileStream::MODE_WRITE) { - mode = "w"; - } - else if (file_mode == RNS::FileStream::MODE_APPEND) { - mode = "a"; - } - else { - ERRORF("open_file: unsupported mode %d", file_mode); - return {RNS::Type::NONE}; - } - TRACEF("open_file: opening file %s in mode %s", file_path, mode); - FILE* file = fopen(file_path, mode); - if (file == nullptr) { - ERRORF("open_file: failed to open output file %s", file_path); - return {RNS::Type::NONE}; - } - TRACEF("open_file: successfully opened file %s", file_path); - return RNS::FileStream(new FileStreamImpl(file)); -#endif -} - -/*virtual*/ bool FileSystem::remove_file(const char* file_path) { -#ifdef ARDUINO -#ifdef BOARD_ESP32 - return SPIFFS.remove(file_path); -#elif BOARD_NRF52 - return InternalFS.remove(file_path); -#else - return false; -#endif -#else - // Native - return (remove(file_path) == 0); -#endif -} - -/*virtual*/ bool FileSystem::rename_file(const char* from_file_path, const char* to_file_path) { -#ifdef ARDUINO -#ifdef BOARD_ESP32 - return SPIFFS.rename(from_file_path, to_file_path); -#elif BOARD_NRF52 - return InternalFS.rename(from_file_path, to_file_path); -#else - return false; -#endif -#else - // Native - return (rename(from_file_path, to_file_path) == 0); -#endif -} - -/*virtua*/ bool FileSystem::directory_exists(const char* directory_path) { - TRACE("directory_exists: checking for existence of directory " + std::string(directory_path)); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - File file = SPIFFS.open(directory_path, FILE_READ); - if (file) { - bool is_directory = file.isDirectory(); - file.close(); - return is_directory; - } -#elif BOARD_NRF52 - File file(InternalFS); - if (file.open(directory_path, FILE_O_READ)) { - bool is_directory = file.isDirectory(); - file.close(); - return is_directory; - } -#else - if (false) { - return false; - } -#endif - else { - return false; - } -#else - // Native - return false; -#endif -} - -/*virtual*/ bool FileSystem::create_directory(const char* directory_path) { -#ifdef ARDUINO -#ifdef BOARD_ESP32 - if (!SPIFFS.mkdir(directory_path)) { - ERROR("create_directory: failed to create directorty " + std::string(directory_path)); - return false; - } - return true; -#elif BOARD_NRF52 - if (!InternalFS.mkdir(directory_path)) { - ERROR("create_directory: failed to create directorty " + std::string(directory_path)); - return false; - } - return true; -#else - return false; -#endif -#else - // Native - struct stat st = {0}; - if (stat(directory_path, &st) == 0) { - return true; - } - return (mkdir(directory_path, 0700) == 0); -#endif -} - -/*virtua*/ bool FileSystem::remove_directory(const char* directory_path) { - TRACE("remove_directory: removing directory " + std::string(directory_path)); -#ifdef ARDUINO -#ifdef BOARD_ESP32 - //if (!LittleFS.rmdir_r(directory_path)) { - if (!SPIFFS.rmdir(directory_path)) { - ERROR("remove_directory: failed to remove directorty " + std::string(directory_path)); - return false; - } - return true; -#elif BOARD_NRF52 - if (!InternalFS.rmdir_r(directory_path)) { - ERROR("remove_directory: failed to remove directory " + std::string(directory_path)); - return false; - } - return true; -#else - return false; -#endif -#else - // Native - return false; -#endif -} - -/*virtua*/ std::list FileSystem::list_directory(const char* directory_path) { - TRACE("list_directory: listing directory " + std::string(directory_path)); - std::list files; -#ifdef ARDUINO -#ifdef BOARD_ESP32 - File root = SPIFFS.open(directory_path); -#elif BOARD_NRF52 - File root = InternalFS.open(directory_path); -#endif - if (!root) { - ERROR("list_directory: failed to open directory " + std::string(directory_path)); - return files; - } - File file = root.openNextFile(); - while (file) { - if (!file.isDirectory()) { - char* name = (char*)file.name(); - files.push_back(name); - } - // CBA Following close required to avoid leaking memory - file.close(); - file = root.openNextFile(); - } - TRACE("list_directory: returning directory listing"); - root.close(); - return files; -#else - // Native - return files; -#endif -} - - -#ifdef ARDUINO - -#ifdef BOARD_ESP32 - -/*virtual*/ size_t FileSystem::storage_size() { - return SPIFFS.totalBytes(); -} - -/*virtual*/ size_t FileSystem::storage_available() { - return (SPIFFS.totalBytes() - SPIFFS.usedBytes()); -} - -#elif BOARD_NRF52 - -static int _countLfsBlock(void *p, lfs_block_t block){ - lfs_size_t *size = (lfs_size_t*) p; - *size += 1; - return 0; -} - -static lfs_ssize_t getUsedBlockCount() { - lfs_size_t size = 0; - lfs_traverse(InternalFS._getFS(), _countLfsBlock, &size); - return size; -} - -static int totalBytes() { - const lfs_config* config = InternalFS._getFS()->cfg; - return config->block_size * config->block_count; -} - -static int usedBytes() { - const lfs_config* config = InternalFS._getFS()->cfg; - const int usedBlockCount = getUsedBlockCount(); - return config->block_size * usedBlockCount; -} - -/*virtual*/ size_t FileSystem::storage_size() { - //return totalBytes(); - return InternalFS.totalBytes(); -} - -/*virtual*/ size_t FileSystem::storage_available() { - //return (totalBytes() - usedBytes()); - return (InternalFS.totalBytes() - InternalFS.usedBytes()); -} - -#endif - -#else - -/*virtual*/ size_t FileSystem::storage_size() { - return 0; -} - -/*virtual*/ size_t FileSystem::storage_available() { - return 0; -} - -#endif diff --git a/examples/unit_tests/FileSystem.h b/examples/unit_tests/FileSystem.h deleted file mode 100644 index b696371..0000000 --- a/examples/unit_tests/FileSystem.h +++ /dev/null @@ -1,184 +0,0 @@ -#pragma once - -#include -#include -#include - -#ifdef ARDUINO -#ifdef BOARD_ESP32 -//#include -#include -#elif BOARD_NRF52 -//#include -#include -using namespace Adafruit_LittleFS_Namespace; -#endif -#else -#include -#include -#include -#include -#include -#include -#endif - -class FileSystem : public RNS::FileSystemImpl { - -public: - FileSystem() {} - - bool init(); - -public: - static void listDir(const char* dir); - -public: - virtual bool file_exists(const char* file_path); - virtual size_t read_file(const char* file_path, RNS::Bytes& data); - virtual size_t write_file(const char* file_path, const RNS::Bytes& data); - virtual RNS::FileStream open_file(const char* file_path, RNS::FileStream::MODE file_mode); - virtual bool remove_file(const char* file_path); - virtual bool rename_file(const char* from_file_path, const char* to_file_path); - virtual bool directory_exists(const char* directory_path); - virtual bool create_directory(const char* directory_path); - virtual bool remove_directory(const char* directory_path); - virtual std::list list_directory(const char* directory_path); - virtual size_t storage_size(); - virtual size_t storage_available(); - -protected: - -#if defined(BOARD_ESP32) || defined(BOARD_NRF52) - class FileStreamImpl : public RNS::FileStreamImpl { - - private: - std::unique_ptr _file; - bool _closed = false; - - public: - FileStreamImpl(File* file) : RNS::FileStreamImpl(), _file(file) {} - virtual ~FileStreamImpl() { if (!_closed) close(); } - - public: - inline virtual const char* name() { return _file->name(); } - inline virtual size_t size() { return _file->size(); } - inline virtual void close() { _closed = true; _file->close(); } - - // Print overrides - inline virtual size_t write(uint8_t byte) { return _file->write(byte); } - inline virtual size_t write(const uint8_t *buffer, size_t size) { return _file->write(buffer, size); } - - // Stream overrides - inline virtual int available() { return _file->available(); } - inline virtual int read() { return _file->read(); } - inline virtual int peek() { return _file->peek(); } - inline virtual void flush() { _file->flush(); } - - }; -#else - class FileStreamImpl : public RNS::FileStreamImpl { - - private: - FILE* _file = nullptr; - bool _closed = false; - size_t _available = 0; - char _filename[1024]; - - public: - FileStreamImpl(FILE* file) : RNS::FileStreamImpl(), _file(file) { - _available = size(); - } - virtual ~FileStreamImpl() { if (!_closed) close(); } - - public: - inline virtual const char* name() { - assert(_file); -#if 0 - char proclnk[1024]; - snprintf(proclnk, sizeof(proclnk), "/proc/self/fd/%d", fileno(_file)); - int r = readlink(proclnk, _filename, sizeof(_filename)); - if (r < 0) { - return nullptr); - } - _filename[r] = '\0'; - return _filename; -#elif 0 - if (fcntl(fd, F_GETPATH, _filename) < 0) { - rerturn nullptr; - } - return _filename; -#else - return nullptr; -#endif - } - inline virtual size_t size() { - assert(_file); - struct stat st; - fstat(fileno(_file), &st); - return st.st_size; - } - inline virtual void close() { - assert(_file); - _closed = true; - fclose(_file); - _file = nullptr; - } - - // Print overrides - inline virtual size_t write(uint8_t byte) { - assert(_file); - int ch = fputc(byte, _file); - if (ch == EOF) { - return 0; - } - ++_available; - return 1; - } - inline virtual size_t write(const uint8_t *buffer, size_t size) { - assert(_file); - size_t wrote = fwrite(buffer, sizeof(uint8_t), size, _file); - _available += wrote; - return wrote; - } - - // Stream overrides - inline virtual int available() { -#if 0 - assert(_file); - int size = 0; - ioctl(fileno(_file), FIONREAD, &size); - TRACEF("FileStream::available: %d", size); - return size; -#else - return _available; -#endif - } - inline virtual int read() { - if (_available <= 0) { - return EOF; - } - assert(_file); - int ch = fgetc(_file); - TRACEF("FileStream::read: %c", ch); - return ch; - } - inline virtual int peek() { - if (_available <= 0) { - return EOF; - } - assert(_file); - int ch = fgetc(_file); - ungetc(ch, _file); - TRACEF("FileStream::peek: %c", ch); - return ch; - } - inline virtual void flush() { - assert(_file); - fflush(_file); - TRACE("FileStream::flush"); - } - - }; -#endif - -}; diff --git a/examples/unit_tests/Test.cpp b/examples/unit_tests/Test.cpp deleted file mode 100644 index ce44ffd..0000000 --- a/examples/unit_tests/Test.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "Test.h" - -void test() { - - testOS(); - testBytes(); - testCollections(); - testReference(); - testCrypto(); - testPersistence(); - testFileSystem(); - testReticulum(); - testObjects(); - -} diff --git a/examples/unit_tests/Test.h b/examples/unit_tests/Test.h deleted file mode 100644 index ababa15..0000000 --- a/examples/unit_tests/Test.h +++ /dev/null @@ -1,12 +0,0 @@ - -void test(); - -void testOS(); -void testBytes(); -void testCollections(); -void testReference(); -void testCrypto(); -void testPersistence(); -void testFileSystem(); -void testReticulum(); -void testObjects(); diff --git a/examples/unit_tests/TestCrypto.cpp b/examples/unit_tests/TestCrypto.cpp deleted file mode 100644 index 6c8c30c..0000000 --- a/examples/unit_tests/TestCrypto.cpp +++ /dev/null @@ -1,266 +0,0 @@ -//#include - -#include "Reticulum.h" -#include "Identity.h" -#include "Destination.h" -#include "Packet.h" -#include "Bytes.h" -#include "Log.h" -#include "Utilities/Crc.h" - -#include "Cryptography/HMAC.h" -#include "Cryptography/PKCS7.h" - -#include - -// Imported from Crypto.cpp -// Computes CRC-8/HITAG checksum -// CBA Doesn't appear to support incremental checksum building -extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); - -void testCryptoMain() { - HEAD("testCryptoMain", RNS::LOG_TRACE); - - RNS::Reticulum reticulum_crypto; - assert(reticulum_crypto); - - RNS::Identity identity; - assert(identity); - - RNS::Destination destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "appname", "aspects"); - //assert(encryptionPrivateKey().toHex().compare("") == ); - //assert(signingPrivateKey().toHex().compare("") == ); - //assert(encryptionPublicKey().toHex().compare("") == ); - //assert(signingPublicKey().toHex().compare("") == ); - - //Packet packet = destination.announce("appdata"); -} - -void testHMAC() { - HEAD("testHMAC", RNS::LOG_TRACE); - - { - const char keystr[] = "key"; - const char datastr[] = "The quick brown fox jumps over the lazy dog"; - const uint8_t hasharr[] = { - 0xf7, 0xbc, 0x83, 0xf4, 0x30, 0x53, 0x84, 0x24, - 0xb1, 0x32, 0x98, 0xe6, 0xaa, 0x6f, 0xb1, 0x43, - 0xef, 0x4d, 0x59, 0xa1, 0x49, 0x46, 0x17, 0x59, - 0x97, 0x47, 0x9d, 0xbc, 0x2d, 0x1a, 0x3c, 0xd8 - }; - RNS::Bytes key(keystr); - RNS::Bytes data(datastr); - RNS::Bytes hash(hasharr, sizeof(hasharr)); - TRACEF("expected hash: %s", hash.toHex().c_str()); - RNS::Cryptography::HMAC hmac(key, data); - RNS::Bytes result = hmac.digest(); - TRACEF("result hash: %s", result.toHex().c_str()); - assert(memcmp(hash.data(), result.data(), result.size()) == 0); - } -} - -void testPKCS7() { - HEAD("testPKCS7", RNS::LOG_TRACE); - - const uint8_t str[] = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; - - // test buffer of half blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE / 2; - RNS::Bytes bytes(str, len); - bytes = RNS::Cryptography::PKCS7::pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE); - - bytes = RNS::Cryptography::PKCS7::unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } - - // test buffer of one less blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE - 1; - RNS::Bytes bytes(str, len); - bytes = RNS::Cryptography::PKCS7::pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE); - - bytes = RNS::Cryptography::PKCS7::unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } - - // test buffer of blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE; - RNS::Bytes bytes(str, len); - bytes = RNS::Cryptography::PKCS7::pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE * 2); - - bytes = RNS::Cryptography::PKCS7::unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } - - // test inplace buffer of half blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE / 2; - RNS::Bytes bytes(str, len); - RNS::Cryptography::PKCS7::inplace_pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE); - - RNS::Cryptography::PKCS7::inplace_unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } - - // test inplace buffer of one less blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE - 1; - RNS::Bytes bytes(str, len); - RNS::Cryptography::PKCS7::inplace_pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE); - - RNS::Cryptography::PKCS7::inplace_unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } - - // test inplace buffer of blocksize - { - size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE; - RNS::Bytes bytes(str, len); - RNS::Cryptography::PKCS7::inplace_pad(bytes); - assert(bytes.size() == RNS::Cryptography::PKCS7::BLOCKSIZE * 2); - - RNS::Cryptography::PKCS7::inplace_unpad(bytes); - assert(bytes.size() == len); - assert(memcmp(bytes.data(), str, len) == 0); - } -} - -void testCrc8() { - HEAD("testCrc8", RNS::LOG_TRACE); - - char data[16]; - uint8_t crc; - - strcpy(data, "foo"); - crc = crypto_crc8(0, data, strlen(data)); - TRACEF("crc1: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x48, crc); - assert(0x48 == crc); - - strcpy(data, "bar"); - crc = crypto_crc8(0, data, strlen(data)); - TRACEF("crc2: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0xED, crc); - assert(0xED == crc); - - strcpy(data, "foo"); - crc = crypto_crc8(0, data, strlen(data)); - TRACEF("crc3: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x48, crc); - assert(0x48 == crc); - - strcpy(data, "foobarfoo"); - crc = crypto_crc8(0, data, strlen(data)); - TRACEF("crc4: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x00, crc); - assert(0x00 == crc); -} - -void testCrc32() { - HEAD("testCrc32", RNS::LOG_TRACE); - - char data[16]; - uint32_t crc; - - strcpy(data, "foo"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc1: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x8C736521, crc); - assert(0x8C736521 == crc); - - strcpy(data, "bar"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc2: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x76FF8CAA, crc); - assert(0x76FF8CAA == crc); - - strcpy(data, "foo"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc3: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x8C736521, crc); - assert(0x8C736521 == crc); - - strcpy(data, "foobarfoo"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc4: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0xEE2F4613, crc); - assert(0xEE2F4613 == crc); -} - -void testIncrementalCrc32() { - HEAD("testIncrementalCrc32", RNS::LOG_TRACE); - - char data[16]; - uint32_t crc; - - strcpy(data, "foo"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc1: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x8C736521, crc); - assert(0x8C736521 == crc); - - strcpy(data, "bar"); - crc = RNS::Utilities::Crc::crc32(crc, data); - TRACEF("crc1: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0x9EF61F95, crc); - assert(0x9EF61F95 == crc); - - strcpy(data, "foo"); - crc = RNS::Utilities::Crc::crc32(crc, data); - TRACEF("crc3: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0xEE2F4613, crc); - assert(0xEE2F4613 == crc); - - strcpy(data, "foobarfoo"); - crc = RNS::Utilities::Crc::crc32(0, data); - TRACEF("crc: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0xEE2F4613, crc); - assert(0xEE2F4613 == crc); -} - -void testByteCrc32() { - HEAD("testByteCrc32", RNS::LOG_TRACE); - - char data[16]; - strcpy(data, "foobarfoo"); - - uint32_t crc = 0; - for (int i = 0; i < strlen(data); i++) { - crc = RNS::Utilities::Crc::crc32(crc, data[i]); - } - TRACEF("crc: 0x%X", crc); - //TEST_ASSERT_EQUAL_UINT8(0xEE2F4613, crc); - assert(0xEE2F4613 == crc); -} - -void testCrypto() { - HEAD("Running testCrypto...", RNS::LOG_TRACE); - //testCryptoMain(); - testHMAC(); - testPKCS7(); - testCrc8(); - testCrc32(); - testIncrementalCrc32(); - testByteCrc32(); -} - -/* -int main(void) -{ - UNITY_BEGIN(); - RUN_TEST(testCrypto); - return UNITY_END(); -} -*/ diff --git a/examples/unit_tests/TestObjects.cpp b/examples/unit_tests/TestObjects.cpp deleted file mode 100644 index 5c4ac19..0000000 --- a/examples/unit_tests/TestObjects.cpp +++ /dev/null @@ -1,30 +0,0 @@ - -#include "A.h" -#include "B.h" - -#include - -void testObjects() { - - printf("Creating a...\n"); - A a = A(); - printf("Creating b...\n"); - B b = B(); - - // Following assignments are invalid because a and b are empty - //printf("Assigning b1...\n"); - //B b1 = a.getB(); - //printf("Assigning a1...\n"); - //A a1 = b.getA(); - - printf("Creating ab...\n"); - A ab = A(b); - printf("Creating ba...\n"); - B ba = B(a); - - printf("Assigning b2...\n"); - B b2 = ab.getB(); - printf("Assigning a2...\n"); - A a2 = ba.getA(); - -} diff --git a/examples/unit_tests/cache/40fe4ab8105b591cca1ef159d476a7b440fe4ab8105b591cca1ef159d476a7b4 b/examples/unit_tests/cache/40fe4ab8105b591cca1ef159d476a7b440fe4ab8105b591cca1ef159d476a7b4 deleted file mode 100644 index 30d74d2..0000000 --- a/examples/unit_tests/cache/40fe4ab8105b591cca1ef159d476a7b440fe4ab8105b591cca1ef159d476a7b4 +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/examples/unit_tests/cache/426f66f9aadf866933358b71295f59d0426f66f9aadf866933358b71295f59d0 b/examples/unit_tests/cache/426f66f9aadf866933358b71295f59d0426f66f9aadf866933358b71295f59d0 deleted file mode 100644 index 30d74d2..0000000 --- a/examples/unit_tests/cache/426f66f9aadf866933358b71295f59d0426f66f9aadf866933358b71295f59d0 +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/examples/unit_tests/cache/45c50662af11f1b26889efaab547942b45c50662af11f1b26889efaab547942b b/examples/unit_tests/cache/45c50662af11f1b26889efaab547942b45c50662af11f1b26889efaab547942b deleted file mode 100644 index 30d74d2..0000000 --- a/examples/unit_tests/cache/45c50662af11f1b26889efaab547942b45c50662af11f1b26889efaab547942b +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/examples/unit_tests/cache/c755609f4d0ab75b5119905a032eeb33c755609f4d0ab75b5119905a032eeb33 b/examples/unit_tests/cache/c755609f4d0ab75b5119905a032eeb33c755609f4d0ab75b5119905a032eeb33 deleted file mode 100644 index 30d74d2..0000000 --- a/examples/unit_tests/cache/c755609f4d0ab75b5119905a032eeb33c755609f4d0ab75b5119905a032eeb33 +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/examples/unit_tests/cache/test b/examples/unit_tests/cache/test deleted file mode 100644 index 30d74d2..0000000 --- a/examples/unit_tests/cache/test +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/examples/unit_tests/main.cpp b/examples/unit_tests/main.cpp deleted file mode 100644 index 5f2244f..0000000 --- a/examples/unit_tests/main.cpp +++ /dev/null @@ -1,91 +0,0 @@ -//#define NDEBUG - -#include "Test.h" - -#include "Log.h" -#include "Utilities/OS.h" - -#ifdef ARDUINO -#include -#else -#include -#include -#include -#endif - -#include -#include -#include -#include -#include -#include - -void setup() { - -#ifdef ARDUINO - Serial.begin(115200); - while (!Serial) { - if (millis() > 2000) - break; - } - Serial.print("Hello from T-Beam on PlatformIO!\n"); - delay(2000); -#endif - - RNS::loglevel(RNS::LOG_TRACE); - //RNS::loglevel(RNS::LOG_MEM); - - try { - - TRACE("Running tests..."); - RNS::LogLevel loglevel = RNS::loglevel(); -#if defined(MEM_LOG) - RNS::loglevel(RNS::LOG_MEM); -#else - //RNS::loglevel(RNS::LOG_WARNING); - RNS::loglevel(RNS::LOG_TRACE); -#endif - - //UNITY_BEGIN(); - - //RUN_TEST(test_request); - - //UNITY_END(); - - // all tests - //test(); - - // individiual tests - //testReference(); - //testCrypto(); - testBytes(); - //testFileSystem(); - //testPersistence(); - //testReticulum(); - //testObjects(); - - HEAD("All tests successful!", RNS::LOG_TRACE); - - RNS::loglevel(loglevel); - } - catch (std::exception& e) { - ERRORF("!!! Exception in test: %s", e.what()); - } - -#ifdef ARDUINO - //Serial.print("Goodbye from T-Beam on PlatformIO!\n"); -#endif -} - -void loop() { -} - -#ifndef ARDUINO -int main(void) { - printf("Hello from Native on PlatformIO!\n"); - - setup(); - - printf("Goodbye from Native on PlatformIO!\n"); -} -#endif diff --git a/examples/unit_tests/platformio.ini b/examples/unit_tests/platformio.ini deleted file mode 100644 index 5ff3566..0000000 --- a/examples/unit_tests/platformio.ini +++ /dev/null @@ -1,186 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html - -[platformio] -default_envs = native -include_dir = . -src_dir = . - -[env] -monitor_speed = 115200 -upload_speed = 921600 -build_type = debug -build_flags = - -DRNS_USE_ALLOCATOR=1 - -DRNS_USE_TLSF=1 - ;-DMEM_LOG - -DTEST_OBJECT_IMPL -lib_deps = - ArduinoJson@^6.21.3 - https://github.com/attermann/Crypto.git - https://github.com/hideakitai/MsgPack.git - microReticulum=symlink://../.. - -[env:native] -platform = native -monitor_port = none -build_flags = - ${env.build_flags} - -std=c++11 - -g3 - -ggdb - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -Wno-unused-parameter - -I. - -DNATIVE -lib_deps = - ${env.lib_deps} -lib_extra_dirs = ../ -lib_compat_mode = off -;debug_init_break = -;debug_init_break = tbreak -;debug_tool = - -[env:native17] -platform = native -build_unflags = -std=gnu++11 -build_flags = - ${env.build_flags} - -std=c++17 - -g3 - -ggdb - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -Wno-unused-parameter - -I. - -DNATIVE -lib_deps = - ${env.lib_deps} -lib_extra_dirs = ../ -lib_compat_mode = off - -[env:cubecell-gps] -framework = arduino -platform = heltec-cubecell -board = cubecell_gps -;build_flags = -Wl,-Map,output.map -build_flags = - ${env.build_flags} - -std=c++11 - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -I. - -DCUBECELL -lib_deps = - ${env.lib_deps} -; CBA NOTE must undef min and max from Arduino.h - -[env:heltec-tracker] -framework = arduino -platform = espressif32 -board = heltec_wireless_tracker -upload_protocol = esp-builtin -;board_build.partitions = no_ota.csv -;board_build.partitions = huge_app.csv -build_flags = - ${env.build_flags} - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -I variants/heltec_wireless_tracker - -I. - -DBOARD_ESP32 -lib_deps = - ${env.lib_deps} - lovyan03/LovyanGFX@^1.1.8 - -[env:ttgo-lora32-v21] -framework = arduino -platform = espressif32 -board = ttgo-lora32-v21 -upload_speed = 460800 -board_build.partitions = no_ota.csv -build_flags = - ${env.build_flags} - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -I. - -DBOARD_ESP32 -lib_deps = - ${env.lib_deps} -monitor_filters = esp32_exception_decoder - -[env:ttgo-t-beam] -framework = arduino -platform = espressif32 -board = ttgo-t-beam -board_build.partitions = no_ota.csv -;board_build.partitions = huge_app.csv -build_flags = - ${env.build_flags} - -Wall - -Wextra - -Wno-missing-field-initializers - -Wno-format - -I. - -DBOARD_ESP32 -lib_deps = - ${env.lib_deps} -monitor_filters = esp32_exception_decoder - -[env:wiscore_rak4631] -framework = arduino -platform = nordicnrf52 -board = wiscore_rak4631 -board_build.partitions = no_ota.csv -board_build.FileSystem = littlefs -build_flags = - ${env.build_flags} - -fexceptions - -Wall - ;-Wextra - -Wno-missing-field-initializers - -Wno-format - -I. - -DBOARD_NRF52 - -DRNS_USE_TLSF=1 -build_unflags = -fno-exceptions -lib_deps = - ${env.lib_deps} - -[env:nrf52840_dk_adafruit] -framework = arduino -platform = nordicnrf52 -board = nrf52840_dk_adafruit -monitor_speed = 115200 -build_flags = - ${env.build_flags} -lib_deps = - ${env.lib_deps} - -[env:nrf52_dk] -framework = arduino -;framework = zephyr -platform = nordicnrf52 -board = nrf52_dk -build_flags = - ${env.build_flags} -lib_deps = - ${env.lib_deps} diff --git a/examples/unit_tests/test_array b/examples/unit_tests/test_array deleted file mode 100644 index 301ee3f..0000000 --- a/examples/unit_tests/test_array +++ /dev/null @@ -1 +0,0 @@ -“Ë@^Ý/Ÿ¾wÄfirst«ÄsecondÍ \ No newline at end of file diff --git a/examples/unit_tests/test_array_2 b/examples/unit_tests/test_array_2 deleted file mode 100644 index 906d092..0000000 --- a/examples/unit_tests/test_array_2 +++ /dev/null @@ -1 +0,0 @@ -“Ë@^Ý/Ÿ¾w–firstÌÿ—secondÌÿ \ No newline at end of file diff --git a/examples/unit_tests/test_array_python b/examples/unit_tests/test_array_python deleted file mode 100644 index 301ee3f..0000000 --- a/examples/unit_tests/test_array_python +++ /dev/null @@ -1 +0,0 @@ -“Ë@^Ý/Ÿ¾wÄfirst«ÄsecondÍ \ No newline at end of file diff --git a/examples/unit_tests/test_destination_table b/examples/unit_tests/test_destination_table deleted file mode 100644 index 627e1e8..0000000 --- a/examples/unit_tests/test_destination_table +++ /dev/null @@ -1 +0,0 @@ -{"1111111111111111":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"2222222222222222":{"timestamp":2,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303031":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303032":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303033":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303034":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303035":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303036":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303037":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303038":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303039":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303130":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303131":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303132":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303133":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303134":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303135":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303136":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303137":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303138":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303139":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""},"31303030303030303030303030303230":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"interface_hash":"","packet_hash":""}} \ No newline at end of file diff --git a/examples/unit_tests/test_file b/examples/unit_tests/test_file deleted file mode 100644 index 0aa6fb5..0000000 --- a/examples/unit_tests/test_file +++ /dev/null @@ -1 +0,0 @@ -test data \ No newline at end of file diff --git a/examples/unit_tests/test_map b/examples/unit_tests/test_map deleted file mode 100644 index 7f681b5..0000000 --- a/examples/unit_tests/test_map +++ /dev/null @@ -1 +0,0 @@ -{"one":{"str":"one","vec":["foo","bar"]},"two":{"str":"two","vec":["foo","bar"]}} \ No newline at end of file diff --git a/examples/unit_tests/test_object b/examples/unit_tests/test_object deleted file mode 100644 index be461be..0000000 --- a/examples/unit_tests/test_object +++ /dev/null @@ -1 +0,0 @@ -‚£str¤test£vec’£one£two \ No newline at end of file diff --git a/examples/unit_tests/test_set b/examples/unit_tests/test_set deleted file mode 100644 index ec747fa..0000000 --- a/examples/unit_tests/test_set +++ /dev/null @@ -1 +0,0 @@ -null \ No newline at end of file diff --git a/examples/unit_tests/test_vector b/examples/unit_tests/test_vector deleted file mode 100644 index b350935..0000000 --- a/examples/unit_tests/test_vector +++ /dev/null @@ -1 +0,0 @@ -[{"str":"two","vec":["foo","bar"]},{"str":"one","vec":["foo","bar"]}] \ No newline at end of file diff --git a/examples/unit_tests/transport_identity b/examples/unit_tests/transport_identity deleted file mode 100644 index 41bb523..0000000 Binary files a/examples/unit_tests/transport_identity and /dev/null differ diff --git a/platformio.ini b/platformio.ini index c7f771a..8d4252f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -102,3 +102,10 @@ build_flags = -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} + +; Overrides for specific tests (not working!) +[test_env:test_objects] +build_flags = + ${env.build_flags} + ;-DTEST_OBJECT_DATA=1 + -DTEST_OBJECT_IMPL=1 diff --git a/test/common/filesystem/FileSystem.h b/test/common/filesystem/FileSystem.h new file mode 100644 index 0000000..b47a064 --- /dev/null +++ b/test/common/filesystem/FileSystem.h @@ -0,0 +1,691 @@ +#pragma once + +#include +#include +#include +#include +#include + +#ifdef ARDUINO +#ifdef BOARD_ESP32 +//#include +#include +#elif BOARD_NRF52 +//#include +#include +using namespace Adafruit_LittleFS_Namespace; +#endif +#else +#include +#include +#include +#include +#include +#include +#endif + +class FileSystem : public RNS::FileSystemImpl { + +public: + FileSystem() {} + + bool init() { + TRACE("FileSystem initializing..."); + + #ifdef ARDUINO + #ifdef BOARD_ESP32 + // Setup FileSystem + INFO("SPIFFS mounting FileSystem"); + if (!SPIFFS.begin(true, "")){ + ERROR("SPIFFS FileSystem mount failed"); + return false; + } + uint32_t size = SPIFFS.totalBytes() / (1024 * 1024); + Serial.print("size: "); + Serial.print(size); + Serial.println(" MB"); + uint32_t used = SPIFFS.usedBytes() / (1024 * 1024); + Serial.print("used: "); + Serial.print(used); + Serial.println(" MB"); + // ensure FileSystem is writable and format if not + RNS::Bytes test("test"); + if (write_file("/test", test) < 4) { + INFO("SPIFFS FileSystem is being formatted, please wait..."); + SPIFFS.format(); + } + else { + remove_file("/test"); + } + DEBUG("SPIFFS FileSystem is ready"); + #elif BOARD_NRF52 + // Initialize Internal File System + INFO("InternalFS mounting FileSystem"); + InternalFS.begin(); + INFO("InternalFS FileSystem is ready"); + #endif + #endif + return true; + } + +public: + static void listDir(const char* dir) { + #ifdef ARDUINO + Serial.print("DIR: "); + Serial.println(dir); + #ifdef BOARD_ESP32 + File root = SPIFFS.open(dir); + if (!root) { + Serial.println("Failed to opend directory"); + return; + } + File file = root.openNextFile(); + while (file) { + if (file.isDirectory()) { + Serial.print(" DIR: "); + } + else { + Serial.print(" FILE: "); + } + Serial.println(file.name()); + file.close(); + file = root.openNextFile(); + } + root.close(); + #elif BOARD_NRF52 + File root = InternalFS.open(dir); + if (!root) { + Serial.println("Failed to opend directory"); + return; + } + File file = root.openNextFile(); + while (file) { + if (file.isDirectory()) { + Serial.print(" DIR: "); + } + else { + Serial.print(" FILE: "); + } + Serial.println(file.name()); + file.close(); + file = root.openNextFile(); + } + root.close(); + #endif + #endif + } + +public: + virtual bool file_exists(const char* file_path) { + #ifdef ARDUINO + #ifdef BOARD_ESP32 + File file = SPIFFS.open(file_path, FILE_READ); + if (file) { + #elif BOARD_NRF52 + File file(InternalFS); + if (file.open(file_path, FILE_O_READ)) { + #else + if (false) { + #endif + #else + // Native + FILE* file = fopen(file_path, "r"); + if (file != nullptr) { + #endif + //TRACE("file_exists: file exists, closing file"); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + file.close(); + #elif BOARD_NRF52 + file.close(); + #endif + #else + // Native + fclose(file); + #endif + return true; + } + else { + ERROR("file_exists: failed to open file " + std::string(file_path)); + return false; + } + } + + virtual size_t read_file(const char* file_path, RNS::Bytes& data) { + size_t read = 0; + #ifdef ARDUINO + #ifdef BOARD_ESP32 + File file = SPIFFS.open(file_path, FILE_READ); + if (file) { + size_t size = file.size(); + read = file.readBytes((char*)data.writable(size), size); + #elif BOARD_NRF52 + //File file(InternalFS); + //if (file.open(file_path, FILE_O_READ)) { + File file = InternalFS.open(file_path, FILE_O_READ); + if (file) { + size_t size = file.size(); + read = file.readBytes((char*)data.writable(size), size); + #else + if (false) { + size_t size = 0; + #endif + #else + // Native + FILE* file = fopen(file_path, "r"); + if (file != nullptr) { + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + rewind(file); + //size_t read = fread(data.writable(size), size, 1, file); + read = fread(data.writable(size), 1, size, file); + #endif + TRACE("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path)); + if (read != size) { + ERROR("read_file: failed to read file " + std::string(file_path)); + data.clear(); + } + //TRACE("read_file: closing input file"); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + file.close(); + #elif BOARD_NRF52 + file.close(); + #endif + #else + // Native + fclose(file); + #endif + } + else { + ERROR("read_file: failed to open input file " + std::string(file_path)); + } + return read; + } + + virtual size_t write_file(const char* file_path, const RNS::Bytes& data) { + // CBA TODO Replace remove with working truncation + remove_file(file_path); + size_t wrote = 0; + #ifdef ARDUINO + #ifdef BOARD_ESP32 + File file = SPIFFS.open(file_path, FILE_WRITE); + if (file) { + wrote = file.write(data.data(), data.size()); + #elif BOARD_NRF52 + File file(InternalFS); + if (file.open(file_path, FILE_O_WRITE)) { + wrote = file.write(data.data(), data.size()); + #else + if (false) { + #endif + #else + // Native + FILE* file = fopen(file_path, "w"); + if (file != nullptr) { + //size_t wrote = fwrite(data.data(), data.size(), 1, file); + wrote = fwrite(data.data(), 1, data.size(), file); + #endif + TRACE("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path)); + if (wrote < data.size()) { + WARNING("write_file: not all data was written to file " + std::string(file_path)); + } + //TRACE("write_file: closing output file"); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + file.close(); + #elif BOARD_NRF52 + file.close(); + #endif + #else + // Native + fclose(file); + #endif + } + else { + ERROR("write_file: failed to open output file " + std::string(file_path)); + } + return wrote; + } + + virtual RNS::FileStream open_file(const char* file_path, RNS::FileStream::MODE file_mode) { + TRACEF("open_file: opening file %s", file_path); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + const char* mode; + if (file_mode == RNS::FileStream::MODE_READ) { + mode = FILE_READ; + } + else if (file_mode == RNS::FileStream::MODE_WRITE) { + mode = FILE_WRITE; + } + else if (file_mode == RNS::FileStream::MODE_APPEND) { + mode = FILE_APPEND; + } + else { + ERRORF("open_file: unsupported mode %d", file_mode); + return {RNS::Type::NONE}; + } + TRACEF("open_file: opening file %s in mode %s", file_path, mode); + //// Using copy constructor to create a File* instead of local + //File file = SPIFFS.open(file_path, mode); + //if (!file) { + File* file = new File(SPIFFS.open(file_path, mode)); + if (file == nullptr || !(*file)) { + ERRORF("open_file: failed to open output file %s", file_path); + return {RNS::Type::NONE}; + } + TRACEF("open_file: successfully opened file %s", file_path); + return RNS::FileStream(new FileStreamImpl(file)); + #elif BOARD_NRF52 + //File file = File(InternalFS); + File* file = new File(InternalFS); + int mode; + if (file_mode == RNS::FileStream::MODE_READ) { + mode = FILE_O_READ; + } + else if (file_mode == RNS::FileStream::MODE_WRITE) { + mode = FILE_O_WRITE; + // CBA TODO Replace remove with working truncation + if (InternalFS.exists(file_path)) { + InternalFS.remove(file_path); + } + } + else if (file_mode == RNS::FileStream::MODE_APPEND) { + // CBA This is the default write mode for nrf52 littlefs + mode = FILE_O_WRITE; + } + else { + ERRORF("open_file: unsupported mode %d", file_mode); + return {RNS::Type::NONE}; + } + if (!file->open(file_path, mode)) { + ERRORF("open_file: failed to open output file %s", file_path); + return {RNS::Type::NONE}; + } + + // Seek to beginning to overwrite (this is failing on nrf52) + //if (file_mode == RNS::FileStream::MODE_WRITE) { + // file->seek(0); + // file->truncate(0); + //} + TRACEF("open_file: successfully opened file %s", file_path); + return RNS::FileStream(new FileStreamImpl(file)); + #else + #warning("unsuppoprted"); + return RNS::FileStream(RNS::Type::NONE); + #endif + #else // ARDUINO + // Native + const char* mode; + if (file_mode == RNS::FileStream::MODE_READ) { + mode = "r"; + } + else if (file_mode == RNS::FileStream::MODE_WRITE) { + mode = "w"; + } + else if (file_mode == RNS::FileStream::MODE_APPEND) { + mode = "a"; + } + else { + ERRORF("open_file: unsupported mode %d", file_mode); + return {RNS::Type::NONE}; + } + TRACEF("open_file: opening file %s in mode %s", file_path, mode); + FILE* file = fopen(file_path, mode); + if (file == nullptr) { + ERRORF("open_file: failed to open output file %s", file_path); + return {RNS::Type::NONE}; + } + TRACEF("open_file: successfully opened file %s", file_path); + return RNS::FileStream(new FileStreamImpl(file)); + #endif + } + + virtual bool remove_file(const char* file_path) { + #ifdef ARDUINO + #ifdef BOARD_ESP32 + return SPIFFS.remove(file_path); + #elif BOARD_NRF52 + return InternalFS.remove(file_path); + #else + return false; + #endif + #else + // Native + return (remove(file_path) == 0); + #endif + } + + virtual bool rename_file(const char* from_file_path, const char* to_file_path) { + #ifdef ARDUINO + #ifdef BOARD_ESP32 + return SPIFFS.rename(from_file_path, to_file_path); + #elif BOARD_NRF52 + return InternalFS.rename(from_file_path, to_file_path); + #else + return false; + #endif + #else + // Native + return (rename(from_file_path, to_file_path) == 0); + #endif + } + + /*virtua*/ bool directory_exists(const char* directory_path) { + TRACE("directory_exists: checking for existence of directory " + std::string(directory_path)); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + File file = SPIFFS.open(directory_path, FILE_READ); + if (file) { + bool is_directory = file.isDirectory(); + file.close(); + return is_directory; + } + #elif BOARD_NRF52 + File file(InternalFS); + if (file.open(directory_path, FILE_O_READ)) { + bool is_directory = file.isDirectory(); + file.close(); + return is_directory; + } + #else + if (false) { + return false; + } + #endif + else { + return false; + } + #else + // Native + return false; + #endif + } + + virtual bool create_directory(const char* directory_path) { + #ifdef ARDUINO + #ifdef BOARD_ESP32 + if (!SPIFFS.mkdir(directory_path)) { + ERROR("create_directory: failed to create directorty " + std::string(directory_path)); + return false; + } + return true; + #elif BOARD_NRF52 + if (!InternalFS.mkdir(directory_path)) { + ERROR("create_directory: failed to create directorty " + std::string(directory_path)); + return false; + } + return true; + #else + return false; + #endif + #else + // Native + struct stat st = {0}; + if (stat(directory_path, &st) == 0) { + return true; + } + return (mkdir(directory_path, 0700) == 0); + #endif + } + + /*virtua*/ bool remove_directory(const char* directory_path) { + TRACE("remove_directory: removing directory " + std::string(directory_path)); + #ifdef ARDUINO + #ifdef BOARD_ESP32 + //if (!LittleFS.rmdir_r(directory_path)) { + if (!SPIFFS.rmdir(directory_path)) { + ERROR("remove_directory: failed to remove directorty " + std::string(directory_path)); + return false; + } + return true; + #elif BOARD_NRF52 + if (!InternalFS.rmdir_r(directory_path)) { + ERROR("remove_directory: failed to remove directory " + std::string(directory_path)); + return false; + } + return true; + #else + return false; + #endif + #else + // Native + return false; + #endif + } + + /*virtua*/ std::list list_directory(const char* directory_path) { + TRACE("list_directory: listing directory " + std::string(directory_path)); + std::list files; + #ifdef ARDUINO + #ifdef BOARD_ESP32 + File root = SPIFFS.open(directory_path); + #elif BOARD_NRF52 + File root = InternalFS.open(directory_path); + #endif + if (!root) { + ERROR("list_directory: failed to open directory " + std::string(directory_path)); + return files; + } + File file = root.openNextFile(); + while (file) { + if (!file.isDirectory()) { + char* name = (char*)file.name(); + files.push_back(name); + } + // CBA Following close required to avoid leaking memory + file.close(); + file = root.openNextFile(); + } + TRACE("list_directory: returning directory listing"); + root.close(); + return files; + #else + // Native + return files; + #endif + } + + + #ifdef ARDUINO + + #ifdef BOARD_ESP32 + + virtual size_t storage_size() { + return SPIFFS.totalBytes(); + } + + virtual size_t storage_available() { + return (SPIFFS.totalBytes() - SPIFFS.usedBytes()); + } + + #elif BOARD_NRF52 + + static int _countLfsBlock(void *p, lfs_block_t block){ + lfs_size_t *size = (lfs_size_t*) p; + *size += 1; + return 0; + } + + static lfs_ssize_t getUsedBlockCount() { + lfs_size_t size = 0; + lfs_traverse(InternalFS._getFS(), _countLfsBlock, &size); + return size; + } + + static int totalBytes() { + const lfs_config* config = InternalFS._getFS()->cfg; + return config->block_size * config->block_count; + } + + static int usedBytes() { + const lfs_config* config = InternalFS._getFS()->cfg; + const int usedBlockCount = getUsedBlockCount(); + return config->block_size * usedBlockCount; + } + + virtual size_t storage_size() { + //return totalBytes(); + return InternalFS.totalBytes(); + } + + virtual size_t storage_available() { + //return (totalBytes() - usedBytes()); + return (InternalFS.totalBytes() - InternalFS.usedBytes()); + } + + #endif + + #else + + virtual size_t storage_size() { + return 0; + } + + virtual size_t storage_available() { + return 0; + } + + #endif + +protected: + +#if defined(BOARD_ESP32) || defined(BOARD_NRF52) + class FileStreamImpl : public RNS::FileStreamImpl { + + private: + std::unique_ptr _file; + bool _closed = false; + + public: + FileStreamImpl(File* file) : RNS::FileStreamImpl(), _file(file) {} + virtual ~FileStreamImpl() { if (!_closed) close(); } + + public: + inline virtual const char* name() { return _file->name(); } + inline virtual size_t size() { return _file->size(); } + inline virtual void close() { _closed = true; _file->close(); } + + // Print overrides + inline virtual size_t write(uint8_t byte) { return _file->write(byte); } + inline virtual size_t write(const uint8_t *buffer, size_t size) { return _file->write(buffer, size); } + + // Stream overrides + inline virtual int available() { return _file->available(); } + inline virtual int read() { return _file->read(); } + inline virtual int peek() { return _file->peek(); } + inline virtual void flush() { _file->flush(); } + + }; +#else + class FileStreamImpl : public RNS::FileStreamImpl { + + private: + FILE* _file = nullptr; + bool _closed = false; + size_t _available = 0; + char _filename[1024]; + + public: + FileStreamImpl(FILE* file) : RNS::FileStreamImpl(), _file(file) { + _available = size(); + } + virtual ~FileStreamImpl() { if (!_closed) close(); } + + public: + inline virtual const char* name() { + assert(_file); +#if 0 + char proclnk[1024]; + snprintf(proclnk, sizeof(proclnk), "/proc/self/fd/%d", fileno(_file)); + int r = readlink(proclnk, _filename, sizeof(_filename)); + if (r < 0) { + return nullptr); + } + _filename[r] = '\0'; + return _filename; +#elif 0 + if (fcntl(fd, F_GETPATH, _filename) < 0) { + rerturn nullptr; + } + return _filename; +#else + return nullptr; +#endif + } + inline virtual size_t size() { + assert(_file); + struct stat st; + fstat(fileno(_file), &st); + return st.st_size; + } + inline virtual void close() { + assert(_file); + _closed = true; + fclose(_file); + _file = nullptr; + } + + // Print overrides + inline virtual size_t write(uint8_t byte) { + assert(_file); + int ch = fputc(byte, _file); + if (ch == EOF) { + return 0; + } + ++_available; + return 1; + } + inline virtual size_t write(const uint8_t *buffer, size_t size) { + assert(_file); + size_t wrote = fwrite(buffer, sizeof(uint8_t), size, _file); + _available += wrote; + return wrote; + } + + // Stream overrides + inline virtual int available() { +#if 0 + assert(_file); + int size = 0; + ioctl(fileno(_file), FIONREAD, &size); + TRACEF("FileStream::available: %d", size); + return size; +#else + return _available; +#endif + } + inline virtual int read() { + if (_available <= 0) { + return EOF; + } + assert(_file); + int ch = fgetc(_file); + if (ch == EOF) { + return ch; + } + --_available; + //TRACEF("FileStream::read: %c", ch); + return ch; + } + inline virtual int peek() { + if (_available <= 0) { + return EOF; + } + assert(_file); + int ch = fgetc(_file); + ungetc(ch, _file); + TRACEF("FileStream::peek: %c", ch); + return ch; + } + inline virtual void flush() { + assert(_file); + fflush(_file); + TRACE("FileStream::flush"); + } + + }; +#endif + +}; diff --git a/examples/unit_tests/TestBytes.cpp b/test/test_bytes/test_main.cpp similarity index 52% rename from examples/unit_tests/TestBytes.cpp rename to test/test_bytes/test_main.cpp index fb865c6..2fe8260 100644 --- a/examples/unit_tests/TestBytes.cpp +++ b/test/test_bytes/test_main.cpp @@ -1,16 +1,13 @@ -//#include +#include #include #include "Bytes.h" #include "Log.h" -#include - -//void testBytesDefault(const RNS::Bytes& bytes = {RNS::Bytes::NONE}) { void testBytesDefault(const RNS::Bytes& bytes = {}) { - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_NULL(bytes.data()); } RNS::Bytes ref("Test"); @@ -24,32 +21,32 @@ const RNS::Bytes& testBytesReference() { void testBytesMain() { RNS::Bytes bytes; - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.empty() == true); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_TRUE(bytes.empty()); + TEST_ASSERT_NULL(bytes.data()); const uint8_t prestr[] = "Hello"; const uint8_t poststr[] = " World"; const RNS::Bytes prebuf(prestr, 5); - assert(prebuf); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); - assert(!(bytes == prebuf)); - assert(bytes != prebuf); - assert(bytes < prebuf); + TEST_ASSERT_TRUE(prebuf); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); + TEST_ASSERT_FALSE(bytes == prebuf); + TEST_ASSERT_TRUE(bytes != prebuf); + TEST_ASSERT_TRUE(bytes < prebuf); const RNS::Bytes postbuf(poststr, 6); - assert(postbuf); - assert(postbuf.size() == 6); - assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0); - assert(!(postbuf == bytes)); - assert(postbuf != bytes); - assert(postbuf > bytes); + TEST_ASSERT_TRUE(postbuf); + TEST_ASSERT_EQUAL_size_t(6, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", postbuf.data(), postbuf.size()); + TEST_ASSERT_FALSE(postbuf == bytes); + TEST_ASSERT_TRUE(postbuf != bytes); + TEST_ASSERT_TRUE(postbuf > bytes); - assert(!(prebuf == postbuf)); - assert(prebuf != postbuf); + TEST_ASSERT_FALSE(prebuf == postbuf); + TEST_ASSERT_TRUE(prebuf != postbuf); if (prebuf == postbuf) { TRACE("bytess are the same"); @@ -60,84 +57,84 @@ void testBytesMain() { // test string assignment bytes = "Foo"; - assert(bytes.size() == 3); - assert(memcmp(bytes.data(), "Foo", bytes.size()) == 0); + TEST_ASSERT_EQUAL_size_t(3, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Foo", bytes.data(), bytes.size()); // test string concat with addition bytes += prebuf + postbuf; - assert(bytes.size() == 14); - assert(memcmp(bytes.data(), "FooHello World", bytes.size()) == 0); + TEST_ASSERT_EQUAL_size_t(14, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("FooHello World", bytes.data(), bytes.size()); TRACE("assign bytes: " + bytes.toString()); TRACE("assign prebuf: " + prebuf.toString()); TRACE("assign postbuf: " + postbuf.toString()); // test string assignment with addition bytes = prebuf + postbuf; - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); // test string constructor { RNS::Bytes str("Foo"); - assert(str.size() == 3); - assert(memcmp(str.data(), "Foo", str.size()) == 0); + TEST_ASSERT_EQUAL_size_t(3, str.size()); + TEST_ASSERT_EQUAL_MEMORY("Foo", str.data(), str.size()); } // test left in range { RNS::Bytes left(bytes.left(5)); TRACE("left: " + left.toString()); - assert(left.size() == 5); - assert(memcmp(left.data(), "Hello", left.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, left.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", left.data(), left.size()); } // test left oob { RNS::Bytes left(bytes.left(20)); TRACE("oob left: " + left.toString()); - assert(left.size() == 11); - assert(memcmp(left.data(), "Hello World", left.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, left.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", left.data(), left.size()); } // test right in range { RNS::Bytes right(bytes.right(5)); TRACE("right: " + right.toString()); - assert(right.size() == 5); - assert(memcmp(right.data(), "World", right.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, right.size()); + TEST_ASSERT_EQUAL_MEMORY("World", right.data(), right.size()); } // test right oob { RNS::Bytes right(bytes.right(20)); TRACE("oob right: " + right.toString()); - assert(right.size() == 11); - assert(memcmp(right.data(), "Hello World", right.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, right.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", right.data(), right.size()); } // test mid in range { RNS::Bytes mid(bytes.mid(3, 5)); TRACE("mid: " + mid.toString()); - assert(mid.size() == 5); - assert(memcmp(mid.data(), "lo Wo", mid.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, mid.size()); + TEST_ASSERT_EQUAL_MEMORY("lo Wo", mid.data(), mid.size()); } // test mid oob pos { RNS::Bytes mid(bytes.mid(20, 5)); TRACE("oob pos mid: " + mid.toString()); - assert(!mid); - assert(mid.size() == 0); + TEST_ASSERT_FALSE(mid); + TEST_ASSERT_EQUAL_size_t(0, mid.size()); } // test mid oob pos { RNS::Bytes mid(bytes.mid(3, 20)); TRACE("oob len mid: " + mid.toString()); - assert(mid.size() == 8); - assert(memcmp(mid.data(), "lo World", mid.size()) == 0); + TEST_ASSERT_EQUAL_size_t(8, mid.size()); + TEST_ASSERT_EQUAL_MEMORY("lo World", mid.data(), mid.size()); } // test mid to end variant { RNS::Bytes mid(bytes.mid(3)); TRACE("end mid: " + mid.toString()); - assert(mid.size() == 8); - assert(memcmp(mid.data(), "lo World", mid.size()) == 0); + TEST_ASSERT_EQUAL_size_t(8, mid.size()); + TEST_ASSERT_EQUAL_MEMORY("lo World", mid.data(), mid.size()); } // test resize @@ -146,8 +143,8 @@ void testBytesMain() { RNS::Bytes shrink(bytes); shrink.resize(5); TRACE("shrink: " + shrink.toString()); - assert(shrink.size() == 5); - assert(memcmp(shrink.data(), "Hello", shrink.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, shrink.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", shrink.data(), shrink.size()); } // test trim @@ -155,44 +152,44 @@ void testBytesMain() { { RNS::Bytes bytes("Hello World"); TRACE("orig: " + bytes.toString()); - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); bytes = bytes.left(5); TRACE("trim: " + bytes.toString()); - assert(bytes.size() == 5); - assert(memcmp(bytes.data(), "Hello", bytes.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes.data(), bytes.size()); } // test creating bytes from default { RNS::Bytes bytes; - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_NULL(bytes.data()); } // test creating bytes from nullptr { RNS::Bytes bytes = nullptr; - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_NULL(bytes.data()); } // test creating bytes from NONE { RNS::Bytes bytes(RNS::Bytes::NONE); - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_NULL(bytes.data()); } // test creating bytes from empty { RNS::Bytes bytes = {}; - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.data() == nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_NULL(bytes.data()); } // function default argument @@ -204,9 +201,9 @@ void testBytesMain() { { RNS::Bytes test = testBytesReference(); TRACE("returned"); - assert(test); - assert(test.size() == 4); - assert(memcmp(test.data(), "Test", test.size()) == 0); + TEST_ASSERT_TRUE(test); + TEST_ASSERT_EQUAL_size_t(4, test.size()); + TEST_ASSERT_EQUAL_MEMORY("Test", test.data(), test.size()); } // TODO test comparison @@ -216,18 +213,18 @@ void testBytesMain() { void testCowBytes() { RNS::Bytes bytes1("1"); - assert(bytes1.size() == 1); - assert(memcmp(bytes1.data(), "1", bytes1.size()) == 0); + TEST_ASSERT_EQUAL_size_t(1, bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY("1", bytes1.data(), bytes1.size()); RNS::Bytes bytes2(bytes1); - assert(bytes2.size() == 1); - assert(memcmp(bytes2.data(), "1", bytes2.size()) == 0); - assert(bytes2.data() == bytes1.data()); + TEST_ASSERT_EQUAL_size_t(1, bytes2.size()); + TEST_ASSERT_EQUAL_MEMORY("1", bytes2.data(), bytes2.size()); + TEST_ASSERT_EQUAL_PTR(bytes1.data(), bytes2.data()); RNS::Bytes bytes3(bytes2); - assert(bytes3.size() == 1); - assert(memcmp(bytes3.data(), "1", bytes3.size()) == 0); - assert(bytes3.data() == bytes2.data()); + TEST_ASSERT_EQUAL_size_t(1, bytes3.size()); + TEST_ASSERT_EQUAL_MEMORY("1", bytes3.data(), bytes3.size()); + TEST_ASSERT_EQUAL_PTR(bytes2.data(), bytes3.data()); TRACE("pre bytes1 ptr: " + std::to_string((uintptr_t)bytes1.data()) + " data: " + bytes1.toString()); TRACE("pre bytes2 ptr: " + std::to_string((uintptr_t)bytes2.data()) + " data: " + bytes2.toString()); @@ -239,14 +236,14 @@ void testCowBytes() { //assert(bytes1.data() != bytes2.data()); bytes2.append("mississippi"); - assert(bytes2.size() == 12); - assert(memcmp(bytes2.data(), "1mississippi", bytes2.size()) == 0); - assert(bytes2.data() != bytes1.data()); + TEST_ASSERT_EQUAL_size_t(12, bytes2.size()); + TEST_ASSERT_EQUAL_MEMORY("1mississippi", bytes2.data(), bytes2.size()); + TEST_ASSERT_NOT_EQUAL(bytes1.data(), bytes2.data()); bytes3.assign("mississippi"); - assert(bytes3.size() == 11); - assert(memcmp(bytes3.data(), "mississippi", bytes3.size()) == 0); - assert(bytes3.data() != bytes2.data()); + TEST_ASSERT_EQUAL_size_t(11, bytes3.size()); + TEST_ASSERT_EQUAL_MEMORY("mississippi", bytes3.data(), bytes3.size()); + TEST_ASSERT_NOT_EQUAL(bytes2.data(), bytes3.data()); TRACE("post bytes1 ptr: " + std::to_string((uintptr_t)bytes1.data()) + " data: " + bytes1.toString()); TRACE("post bytes2 ptr: " + std::to_string((uintptr_t)bytes2.data()) + " data: " + bytes2.toString()); @@ -259,15 +256,15 @@ void testBytesConversion() { RNS::Bytes bytes("Hello World"); std::string hex = bytes.toHex(true); TRACE("text: \"" + bytes.toString() + "\" upper hex: \"" + hex + "\""); - assert(hex.length() == 22); - assert(hex.compare("48656C6C6F20576F726C64") == 0); + TEST_ASSERT_EQUAL_size_t(22, hex.length()); + TEST_ASSERT_EQUAL_STRING("48656C6C6F20576F726C64", hex.c_str()); } { RNS::Bytes bytes("Hello World"); std::string hex = bytes.toHex(false); TRACE("text: \"" + bytes.toString() + "\" lower hex: \"" + hex + "\""); - assert(hex.length() == 22); - assert(hex.compare("48656c6c6f20576f726c64") == 0); + TEST_ASSERT_EQUAL_size_t(22, hex.length()); + TEST_ASSERT_EQUAL_STRING("48656c6c6f20576f726c64", hex.c_str()); } { std::string hex("48656C6C6F20576F726C64"); @@ -275,8 +272,8 @@ void testBytesConversion() { bytes.assignHex(hex.c_str()); std::string text = bytes.toString(); TRACE("hex: \"" + hex + "\" text: \"" + text + "\""); - assert(text.length() == 11); - assert(text.compare("Hello World") == 0); + TEST_ASSERT_EQUAL_size_t(11, text.length()); + TEST_ASSERT_EQUAL_STRING("Hello World", text.c_str()); } { std::string hex("48656c6c6f20576f726c64"); @@ -284,22 +281,22 @@ void testBytesConversion() { bytes.assignHex(hex.c_str()); std::string text = bytes.toString(); TRACE("hex: \"" + hex + "\" text: \"" + text + "\""); - assert(text.length() == 11); - assert(text.compare("Hello World") == 0); + TEST_ASSERT_EQUAL_size_t(11, text.length()); + TEST_ASSERT_EQUAL_STRING("Hello World", text.c_str()); // overwrite bytes.assignHex(hex.c_str()); text = bytes.toString(); TRACE("hex: \"" + hex + "\" text: \"" + text + "\""); - assert(text.length() == 11); - assert(text.compare("Hello World") == 0); + TEST_ASSERT_EQUAL_size_t(11, text.length()); + TEST_ASSERT_EQUAL_STRING("Hello World", text.c_str()); // apend bytes.appendHex(hex.c_str()); text = bytes.toString(); TRACE("hex: \"" + hex + "\" text: \"" + text + "\""); - assert(text.length() == 22); - assert(text.compare("Hello WorldHello World") == 0); + TEST_ASSERT_EQUAL_size_t(22, text.length()); + TEST_ASSERT_EQUAL_STRING("Hello WorldHello World", text.c_str()); } } @@ -308,21 +305,21 @@ void testBytesResize() { TRACE("Testing downsize of Bytes with initial capacity"); RNS::Bytes bytes(1024); - assert(!bytes); - assert(bytes.size() == 0); - assert(bytes.capacity() == 1024); - assert(bytes.data() != nullptr); + TEST_ASSERT_FALSE(bytes); + TEST_ASSERT_EQUAL_size_t(0, bytes.size()); + TEST_ASSERT_EQUAL_size_t(1024, bytes.capacity()); + TEST_ASSERT_NOT_NULL(bytes.data()); uint8_t* buffer = bytes.writable(bytes.capacity()); - assert(bytes); - assert(bytes.size() == 1024); - assert(bytes.capacity() == 1024); + TEST_ASSERT_TRUE(bytes); + TEST_ASSERT_EQUAL_size_t(1024, bytes.size()); + TEST_ASSERT_EQUAL_size_t(1024, bytes.capacity()); - assert(buffer != nullptr); + TEST_ASSERT_NOT_NULL(buffer); memcpy(buffer, "Hello World", 11); bytes.resize(11); - assert(bytes.size() == 11); - assert(bytes.capacity() == 1024); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_size_t(1024, bytes.capacity()); } void testBytesStream() { @@ -331,14 +328,14 @@ void testBytesStream() { const uint8_t poststr[] = " World"; const RNS::Bytes prebuf(prestr, 5); - assert(prebuf); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); + TEST_ASSERT_TRUE(prebuf); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); const RNS::Bytes postbuf(poststr, 6); - assert(postbuf); - assert(postbuf.size() == 6); - assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0); + TEST_ASSERT_TRUE(postbuf); + TEST_ASSERT_EQUAL_size_t(6, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", postbuf.data(), postbuf.size()); // stream into empty bytes HEAD("TestBytes: stream into empty bytes", RNS::LOG_TRACE); @@ -349,12 +346,12 @@ void testBytesStream() { TRACEF("stream prebuf: %s", prebuf.toString().c_str()); TRACEF("stream postbuf: %s", postbuf.toString().c_str()); TRACEF("stream strmbuf: %s", strmbuf.toString().c_str()); - assert(strmbuf.size() == 11); - assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); - assert(postbuf.size() == 6); - assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, strmbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", strmbuf.data(), strmbuf.size()); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); + TEST_ASSERT_EQUAL_size_t(6, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", postbuf.data(), postbuf.size()); } // stream into reserved empty bytes @@ -368,33 +365,33 @@ void testBytesStream() { //TRACEF("stream prebuf: %s", prebuf.toString().c_str()); //TRACEF("stream postbuf: %s", postbuf.toString().c_str()); //TRACEF("stream strmbuf: %s", strmbuf.toString().c_str()); - assert(strmbuf.size() == 11); - assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); - assert(postbuf.size() == 6); - assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, strmbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", strmbuf.data(), strmbuf.size()); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); + TEST_ASSERT_EQUAL_size_t(6, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", postbuf.data(), postbuf.size()); } // stream into populated bytes HEAD("TestBytes: stream into populated bytes", RNS::LOG_TRACE); { RNS::Bytes strmbuf("Stream "); - assert(strmbuf); - assert(strmbuf.size() == 7); - assert(memcmp(strmbuf.data(), "Stream ", strmbuf.size()) == 0); + TEST_ASSERT_TRUE(strmbuf); + TEST_ASSERT_EQUAL_size_t(7, strmbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Stream ", strmbuf.data(), strmbuf.size()); strmbuf << prebuf << postbuf; TRACE("Results:"); TRACEF("stream prebuf: %s", prebuf.toString().c_str()); TRACEF("stream postbuf: %s", postbuf.toString().c_str()); TRACEF("stream strmbuf: %s", strmbuf.toString().c_str()); - assert(strmbuf.size() == 18); - assert(memcmp(strmbuf.data(), "Stream Hello World", strmbuf.size()) == 0); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); - assert(postbuf.size() == 6); - assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(18, strmbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Stream Hello World", strmbuf.data(), strmbuf.size()); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); + TEST_ASSERT_EQUAL_size_t(6, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", postbuf.data(), postbuf.size()); } // stream with assignment @@ -406,20 +403,20 @@ void testBytesStream() { { // NOTE pre must be volatile in order for below stream with assignment to work (since it gets modified) RNS::Bytes pre("Hello"); - assert(pre.size() == 5); + TEST_ASSERT_EQUAL_size_t(5, pre.size()); const RNS::Bytes post(" World"); - assert(post.size() == 6); + TEST_ASSERT_EQUAL_size_t(6, post.size()); RNS::Bytes strmbuf = pre << post; TRACE("Results:"); TRACEF("stream pre: %s", prebuf.toString().c_str()); TRACEF("stream post: %s", postbuf.toString().c_str()); TRACEF("stream strmbuf: %s", strmbuf.toString().c_str()); - assert(strmbuf.size() == 11); - assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0); - assert(pre.size() == 11); - assert(memcmp(pre.data(), "Hello World", pre.size()) == 0); - assert(post.size() == 6); - assert(memcmp(post.data(), " World", post.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, strmbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", strmbuf.data(), strmbuf.size()); + TEST_ASSERT_EQUAL_size_t(11, pre.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", pre.data(), pre.size()); + TEST_ASSERT_EQUAL_size_t(6, post.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", post.data(), post.size()); } } @@ -434,8 +431,8 @@ void testBytesReserve() { dst << hops; dst << src.mid(2); TRACEF("non-reserve: %s", dst.toString().c_str()); - assert(dst.size() == 5); - assert(memcmp(dst.data(), "H llo", dst.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, dst.size()); + TEST_ASSERT_EQUAL_MEMORY("H llo", dst.data(), dst.size()); } { @@ -444,8 +441,8 @@ void testBytesReserve() { dst << hops; dst << src.mid(2); TRACEF("reserve: %s", dst.toString().c_str()); - assert(dst.size() == 5); - assert(memcmp(dst.data(), "H llo", dst.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, dst.size()); + TEST_ASSERT_EQUAL_MEMORY("H llo", dst.data(), dst.size()); } } @@ -453,14 +450,14 @@ void testBytesReserve() { void testFind() { HEAD("testFind:", RNS::LOG_TRACE); RNS::Bytes src("Hello"); - assert(0 == src.find("H")); - assert(-1 == src.find(1, "H")); - assert(2 == src.find("ll")); - assert(2 == src.find(1, "ll")); - assert(2 == src.find(2, "ll")); - assert(-1 == src.find(3, "ll")); - assert(-1 == src.find(32, "ll")); - assert(-1 == src.find("foo")); + TEST_ASSERT_EQUAL_INT(0, src.find("H")); + TEST_ASSERT_EQUAL_INT(-1, src.find(1, "H")); + TEST_ASSERT_EQUAL_INT(2, src.find("ll")); + TEST_ASSERT_EQUAL_INT(2, src.find(1, "ll")); + TEST_ASSERT_EQUAL_INT(2, src.find(2, "ll")); + TEST_ASSERT_EQUAL_INT(-1, src.find(3, "ll")); + TEST_ASSERT_EQUAL_INT(-1, src.find(32, "ll")); + TEST_ASSERT_EQUAL_INT(-1, src.find("foo")); } void testCompare() { @@ -468,10 +465,10 @@ void testCompare() { RNS::Bytes bytes("Hello\x20World"); //RNS::Bytes bytes("Hello World"); TRACEF("testCompare: %s", bytes.toString().c_str()); - assert(bytes == RNS::Bytes("Hello World")); - assert(bytes == RNS::Bytes("Hello\x20World")); - assert(bytes == "Hello World"); - assert(bytes == "Hello\x20World"); + TEST_ASSERT_TRUE(bytes == RNS::Bytes("Hello World")); + TEST_ASSERT_TRUE(bytes == RNS::Bytes("Hello\x20World")); + TEST_ASSERT_TRUE(bytes == "Hello World"); + TEST_ASSERT_TRUE(bytes == "Hello\x20World"); } void testConcat() { @@ -492,10 +489,10 @@ void testConcat() { RNS::Bytes bytes("Prefix "); bytes += bytes1 + bytes2; //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 18); - assert(memcmp(bytes.data(), "Prefix Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(18, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Prefix Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } // CBA NOTE: Extreme care must be excercised when using stream insertion since intermediate @@ -505,20 +502,20 @@ void testConcat() { RNS::Bytes bytes("Prefix "); bytes << bytes1 << bytes2; //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 18); - assert(memcmp(bytes.data(), "Prefix Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(18, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Prefix Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } { TRACE("testConcat: assignment with addition"); const RNS::Bytes bytes = bytes1 + bytes2; //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } // CBA NOTE: Following construct with assignment using stream insertion is NOT WORKING!!! @@ -530,10 +527,10 @@ void testConcat() { TRACE("testConcat: assignment with stream"); const RNS::Bytes bytes = bytes1 << bytes2; //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } */ @@ -543,20 +540,20 @@ void testConcat() { bytes << bytes1; bytes << bytes2; //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } { TRACE("testConcat: construct with addition"); const RNS::Bytes bytes(bytes1 + bytes2); //TRACEF("testConcat: bytes: %s", bytes.toString().c_str()); - assert(bytes.size() == 11); - assert(memcmp(bytes.data(), "Hello World", bytes.size()) == 0); - assert(memcmp(bytes1.data(), "Hello", bytes1.size()) == 0); - assert(memcmp(bytes2.data(), " World", bytes2.size()) == 0); + TEST_ASSERT_EQUAL_size_t(11, bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", bytes1.data(), bytes1.size()); + TEST_ASSERT_EQUAL_MEMORY(" World", bytes2.data(), bytes2.size()); } } @@ -565,60 +562,49 @@ void testIndex() { HEAD("testConcat:", RNS::LOG_TRACE); const RNS::Bytes bytes("Hello"); - assert(bytes[0] == 'H'); - assert(bytes[1] == 'e'); - assert(bytes[2] == 'l'); - assert(bytes[3] == 'l'); - assert(bytes[4] == 'o'); + TEST_ASSERT_EQUAL_UINT8('H', bytes[0]); + TEST_ASSERT_EQUAL_UINT8('e', bytes[1]); + TEST_ASSERT_EQUAL_UINT8('l', bytes[2]); + TEST_ASSERT_EQUAL_UINT8('l', bytes[3]); + TEST_ASSERT_EQUAL_UINT8('o', bytes[4]); } -void testBytes() { - HEAD("Running testBytes...", RNS::LOG_TRACE); - RNS::Utilities::OS::dump_heap_stats(); - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testBytes: pre-mem: %u", pre_memory); - - testBytesMain(); - testCowBytes(); - testBytesConversion(); - testBytesResize(); - testBytesStream(); - testBytesReserve(); - testFind(); - testCompare(); - testConcat(); - testIndex(); - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testBytes: post-mem: %u", post_memory); - TRACEF("testBytes: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - -#if defined(RNS_USE_ALLOCATOR) - RNS::Utilities::OS::dump_allocator_stats(); -#endif - - RNS::Utilities::OS::dump_heap_stats(); -} - -/* void setUp(void) { - // set stuff up here + // set stuff up here before each test } void tearDown(void) { - // clean stuff up here + // clean stuff up here after each test } int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testBytes); + + // Suite-level setup + RNS::Utilities::OS::dump_heap_stats(); + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testBytes: pre-mem: %u", pre_memory); + + // Run tests + RUN_TEST(testBytesMain); RUN_TEST(testCowBytes); RUN_TEST(testBytesConversion); - RUN_TEST(testMap); + RUN_TEST(testBytesResize); + RUN_TEST(testBytesStream); + RUN_TEST(testBytesReserve); + RUN_TEST(testFind); + RUN_TEST(testCompare); + RUN_TEST(testConcat); + RUN_TEST(testIndex); + + // Suite-level teardown + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testBytes: post-mem: %u", post_memory); + TRACEF("testBytes: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + return UNITY_END(); } @@ -627,18 +613,19 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/examples/unit_tests/TestCollections.cpp b/test/test_collections/test_main.cpp similarity index 68% rename from examples/unit_tests/TestCollections.cpp rename to test/test_collections/test_main.cpp index 517fafa..db47653 100644 --- a/examples/unit_tests/TestCollections.cpp +++ b/test/test_collections/test_main.cpp @@ -1,11 +1,10 @@ -//#include +#include #include "Bytes.h" #include "Log.h" #include #include -#include void testBytesMap() { @@ -13,46 +12,46 @@ void testBytesMap() const uint8_t poststr[] = "World"; RNS::Bytes prebuf(prestr, 5); - assert(prebuf.size() == 5); - assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, prebuf.size()); + TEST_ASSERT_EQUAL_MEMORY("Hello", prebuf.data(), prebuf.size()); RNS::Bytes postbuf(poststr, 5); - assert(postbuf.size() == 5); - assert(memcmp(postbuf.data(), "World", postbuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, postbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("World", postbuf.data(), postbuf.size()); std::map map; map.insert({prebuf, "hello"}); map.insert({postbuf, "world"}); - assert(map.size() == 2); + TEST_ASSERT_EQUAL_size_t(2, map.size()); auto preit = map.find(prebuf); - assert(preit != map.end()); - assert((*preit).second.compare("hello") == 0); + TEST_ASSERT_NOT_EQUAL(map.end(), preit); + TEST_ASSERT_EQUAL_STRING("hello", (*preit).second.c_str()); if (preit != map.end()) { TRACE(std::string("found prebuf: ") + (*preit).second); } auto postit = map.find(postbuf); - assert(postit != map.end()); - assert((*postit).second.compare("world") == 0); + TEST_ASSERT_NOT_EQUAL(map.end(), postit); + TEST_ASSERT_EQUAL_STRING("world", (*postit).second.c_str()); if (postit != map.end()) { TRACE(std::string("found postbuf: ") + (*postit).second); } const uint8_t newstr[] = "World"; RNS::Bytes newbuf(newstr, 5); - assert(newbuf.size() == 5); - assert(memcmp(newbuf.data(), "World", newbuf.size()) == 0); + TEST_ASSERT_EQUAL_size_t(5, newbuf.size()); + TEST_ASSERT_EQUAL_MEMORY("World", newbuf.data(), newbuf.size()); auto newit = map.find(newbuf); - assert(newit != map.end()); - assert((*newit).second.compare("world") == 0); + TEST_ASSERT_NOT_EQUAL(map.end(), newit); + TEST_ASSERT_EQUAL_STRING("world", (*newit).second.c_str()); if (newit != map.end()) { TRACE(std::string("found newbuf: ") + (*newit).second); } std::string str = map["World"]; - assert(str.size() == 5); - assert(str.compare("world") == 0); + TEST_ASSERT_EQUAL_size_t(5, str.size()); + TEST_ASSERT_EQUAL_STRING("world", str.c_str()); } class Entry { @@ -78,8 +77,8 @@ void testOldMap() { other_entries.insert({some_other_entry._hash, some_other_entry}); } - assert(entries.size() == 1); - assert(other_entries.size() == 2); + TEST_ASSERT_EQUAL_size_t(1, entries.size()); + TEST_ASSERT_EQUAL_size_t(2, other_entries.size()); for (auto& pair : other_entries) { if (entries.find(pair.first) == entries.end()) { entries[pair.first] = other_entries[pair.first]; @@ -88,8 +87,8 @@ void testOldMap() { for (auto& pair : entries) { TRACE("entries: " + pair.second._hash.toString()); } - assert(entries.size() == 2); - assert(other_entries.size() == 2); + TEST_ASSERT_EQUAL_size_t(2, entries.size()); + TEST_ASSERT_EQUAL_size_t(2, other_entries.size()); } @@ -106,8 +105,8 @@ void testNewMap() { other_entries.insert({some_other_entry._hash, some_other_entry}); } - assert(entries.size() == 1); - assert(other_entries.size() == 2); + TEST_ASSERT_EQUAL_size_t(1, entries.size()); + TEST_ASSERT_EQUAL_size_t(2, other_entries.size()); for (auto& [hash, entry] : other_entries) { if (entries.find(hash) == entries.end()) { entries[hash] = other_entries[hash]; @@ -116,30 +115,25 @@ void testNewMap() { for (auto& [hash, entry] : entries) { TRACE("entries: " + entry._hash.toString()); } - assert(entries.size() == 2); - assert(other_entries.size() == 2); + TEST_ASSERT_EQUAL_size_t(2, entries.size()); + TEST_ASSERT_EQUAL_size_t(2, other_entries.size()); } -void testCollections() { - HEAD("Running testCollections...", RNS::LOG_TRACE); - testBytesMap(); - testOldMap(); - testNewMap(); -} -/* void setUp(void) { - // set stuff up here + // set stuff up here before each test } void tearDown(void) { - // clean stuff up here + // clean stuff up here after each test } int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testCollections); + RUN_TEST(testBytesMap); + RUN_TEST(testOldMap); + RUN_TEST(testNewMap); return UNITY_END(); } @@ -148,18 +142,19 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/test/test_crypto/test_main.cpp b/test/test_crypto/test_main.cpp index abd22ec..d1e52c8 100644 --- a/test/test_crypto/test_main.cpp +++ b/test/test_crypto/test_main.cpp @@ -17,14 +17,6 @@ // CBA Doesn't appear to support incremental checksum building extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); -void setUp(void) { - // set stuff up here before each test -} - -void tearDown(void) { - // clean stuff up here after each test -} - void testHMAC() { { @@ -199,6 +191,15 @@ void testByteCrc32() { TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc); } + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + int runUnityTests(void) { UNITY_BEGIN(); RUN_TEST(testHMAC); diff --git a/test/test_example/test_main.cpp b/test/test_example/test_main.cpp index ccf603d..bc23232 100644 --- a/test/test_example/test_main.cpp +++ b/test/test_example/test_main.cpp @@ -1,5 +1,11 @@ #include +void testExample() { + bool success = true; + TEST_ASSERT_EQUAL(true, success); +} + + void setUp(void) { // set stuff up here before each test } @@ -8,15 +14,16 @@ void tearDown(void) { // clean stuff up here after each test } -void testExample() { - bool success = true; - TEST_ASSERT_EQUAL(true, success); -} - - int runUnityTests(void) { UNITY_BEGIN(); + + // Suite-level setup + + // Run tests RUN_TEST(testExample); + + // Suite-level teardown + return UNITY_END(); } diff --git a/examples/unit_tests/TestFilesystem.cpp b/test/test_filesystem/test_main.cpp similarity index 69% rename from examples/unit_tests/TestFilesystem.cpp rename to test/test_filesystem/test_main.cpp index d6afa30..36077df 100644 --- a/examples/unit_tests/TestFilesystem.cpp +++ b/test/test_filesystem/test_main.cpp @@ -1,17 +1,166 @@ -//#include +#include -#include "FileSystem.h" +#include "../common/filesystem/FileSystem.h" #include #include #include - -#include #include #include #include -RNS::FileSystem test_filesystem(RNS::Type::NONE); +#ifdef ARDUINO +const char test_file_path[] = "/test_file"; +const char test_stream_path[] = "/test_stream"; +#else +const char test_file_path[] = "test_file"; +const char test_stream_path[] = "test_stream"; +#endif + + +void writeFile(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testWriteFile: pre-mem: %u", pre_memory); + + { + RNS::Bytes data("test"); + size_t wrote = RNS::Utilities::OS::write_file(file_path, data); + TEST_ASSERT_EQUAL_size_t(4, wrote); + } + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testWriteFile: post-mem: %u", post_memory); + TRACEF("testWriteFile: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + +void readFile(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testReadFile: pre-mem: %u", pre_memory); + + { + RNS::Bytes data; + size_t read = RNS::Utilities::OS::read_file(file_path, data); + TEST_ASSERT_EQUAL_size_t(4, read); + TEST_ASSERT_EQUAL_STRING("test", data.toString().c_str()); + } + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testReadFile: post-mem: %u", post_memory); + TRACEF("testReadFile: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + +void overwriteFile(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testOverwriteFile: pre-mem: %u", pre_memory); + + // overwrite + { + RNS::Bytes data("test"); + size_t wrote = RNS::Utilities::OS::write_file(file_path, data); + TEST_ASSERT_EQUAL_size_t(4, wrote); + } + + // read + { + RNS::Bytes data; + size_t read = RNS::Utilities::OS::read_file(file_path, data); + TEST_ASSERT_EQUAL_size_t(4, read); + TEST_ASSERT_EQUAL_STRING("test", data.toString().c_str()); + } + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testOverwriteFile: post-mem: %u", post_memory); + TRACEF("testOverwriteFile: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + +void removeFile(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testRemoveFile: pre-mem: %u", pre_memory); + + TEST_ASSERT_TRUE(RNS::Utilities::OS::remove_file(file_path)); + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testRemoveFile: post-mem: %u", post_memory); + TRACEF("testRemoveFile: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + +void writeFileStream(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testWriteFileStream: pre-mem: %u", pre_memory); + + { + RNS::Bytes data("stream"); + RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_WRITE); + TEST_ASSERT_TRUE(stream); + TRACE("testWriteFileStream: writing to file..."); + size_t wrote = stream.write(data.data(), data.size()); + TRACEF("testWriteFileStream: wrote: %u", wrote); + // Auto-closes + //stream.close(); + TEST_ASSERT_EQUAL_size_t(6, wrote); + uint32_t crc = RNS::Utilities::Crc::crc32(0, data.data(), data.size()); + TRACEF("testWriteFileStream: crc: 0x%X", crc); + TRACEF("testWriteFileStream: stream crc: 0x%X", stream.crc()); + TEST_ASSERT_EQUAL_UINT32(crc, stream.crc()); + } + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testWriteFileStream: post-mem: %u", post_memory); + TRACEF("testWriteFileStream: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + +void readFileStream(const char* file_path) { + + size_t pre_memory = RNS::Utilities::OS::heap_available(); + TRACEF("testReadFileStream: pre-mem: %u", pre_memory); + + { + RNS::Bytes data; + RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_READ); + TEST_ASSERT_TRUE(stream); + size_t size = stream.size(); + TRACEF("testReadFileStream: size: %u", size); + TRACE("testReadFileStream: reading from file..."); + size_t read = stream.readBytes(data.writable(size), size); + TRACEF("testReadFileStream: read: %u", read); + // Auto-closes + //stream.close(); + TEST_ASSERT_EQUAL_size_t(6, read); + TEST_ASSERT_EQUAL_INT(0, data.compare("stream")); + uint32_t crc = RNS::Utilities::Crc::crc32(0, data.data(), data.size()); + TRACEF("testWriteFileStream: crc: 0x%X", crc); + TRACEF("testWriteFileStream: stream crc: 0x%X", stream.crc()); + TEST_ASSERT_EQUAL_UINT32(crc, stream.crc()); + } + + size_t post_memory = RNS::Utilities::OS::heap_available(); + size_t diff_memory = (int)pre_memory - (int)post_memory; + TRACEF("testReadFileStream: post-mem: %u", post_memory); + TRACEF("testReadFileStream: diff-mem: %u", diff_memory); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); + +} + void testListDirectory() { @@ -32,89 +181,23 @@ void testListDirectory() { size_t diff_memory = (int)pre_memory - (int)post_memory; INFO("testListDirectory: post-mem: " + std::to_string(post_memory)); INFO("testListDirectory: diff-mem: " + std::to_string(diff_memory)); - assert(diff_memory == 0); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); } -void testWriteFile(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testWriteFile: pre-mem: %u", pre_memory); - - { - RNS::Bytes data("test"); - size_t wrote = RNS::Utilities::OS::write_file(file_path, data); - assert(wrote == 4); - } - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testWriteFile: post-mem: %u", post_memory); - TRACEF("testWriteFile: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - +void testWriteFile() { + writeFile(test_file_path); } -void testReadFile(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testReadFile: pre-mem: %u", pre_memory); - - { - RNS::Bytes data; - size_t read = RNS::Utilities::OS::read_file(file_path, data); - assert(read == 4); - assert(data.toString().compare("test") == 0); - } - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testReadFile: post-mem: %u", post_memory); - TRACEF("testReadFile: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - +void testReadFile() { + readFile(test_file_path); } -void testOverwriteFile(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testOverwriteFile: pre-mem: %u", pre_memory); - - // overwrite - { - RNS::Bytes data("test"); - size_t wrote = RNS::Utilities::OS::write_file(file_path, data); - assert(wrote == 4); - } - - // read - { - RNS::Bytes data; - size_t read = RNS::Utilities::OS::read_file(file_path, data); - assert(read == 4); - assert(data.toString().compare("test") == 0); - } - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testOverwriteFile: post-mem: %u", post_memory); - TRACEF("testOverwriteFile: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - +void testOverwriteFile() { + overwriteFile(test_file_path); } void testRemoveFile(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testRemoveFile: pre-mem: %u", pre_memory); - - assert(RNS::Utilities::OS::remove_file(file_path)); - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testRemoveFile: post-mem: %u", post_memory); - TRACEF("testRemoveFile: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - + removeFile(test_file_path); } void testReadNonexistantFile() { @@ -125,14 +208,14 @@ void testReadNonexistantFile() { { RNS::Bytes data; size_t read = RNS::Utilities::OS::read_file("./foo", data); - assert(read == 0); + TEST_ASSERT_EQUAL_size_t(0, read); } size_t post_memory = RNS::Utilities::OS::heap_available(); size_t diff_memory = (int)pre_memory - (int)post_memory; TRACEF("testReadNonexistantFile: post-mem: %u", post_memory); TRACEF("testReadNonexistantFile: diff-mem: %u", diff_memory); - assert(diff_memory == 0); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); } @@ -144,76 +227,23 @@ void testWriteFail() { { RNS::Bytes data("test"); size_t wrote = RNS::Utilities::OS::write_file("", data); - assert(wrote == 4); + TEST_ASSERT_EQUAL_size_t(4, wrote); } size_t post_memory = RNS::Utilities::OS::heap_available(); size_t diff_memory = (int)pre_memory - (int)post_memory; TRACEF("testWriteFail: post-mem: %u", post_memory); TRACEF("testWriteFail: diff-mem: %u", diff_memory); - assert(diff_memory == 0); + TEST_ASSERT_EQUAL_size_t(0, diff_memory); } -void testWriteFileStream(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testWriteFileStream: pre-mem: %u", pre_memory); - - { - RNS::Bytes data("stream"); - RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_WRITE); - assert(stream); - TRACE("testWriteFileStream: writing to file..."); - size_t wrote = stream.write(data.data(), data.size()); - TRACEF("testWriteFileStream: wrote: %u", wrote); - // Auto-closes - //stream.close(); - assert(wrote == 6); - uint32_t crc = RNS::Utilities::Crc::crc32(0, data.data(), data.size()); - TRACEF("testWriteFileStream: crc: 0x%X", crc); - TRACEF("testWriteFileStream: stream crc: 0x%X", stream.crc()); - assert(crc == stream.crc()); - } - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testWriteFileStream: post-mem: %u", post_memory); - TRACEF("testWriteFileStream: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - +void testWriteFileStream() { + writeFileStream(test_stream_path); } -void testReadFileStream(const char* file_path) { - - size_t pre_memory = RNS::Utilities::OS::heap_available(); - TRACEF("testReadFileStream: pre-mem: %u", pre_memory); - - { - RNS::Bytes data; - RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_READ); - assert(stream); - size_t size = stream.size(); - TRACEF("testReadFileStream: size: %u", size); - TRACE("testReadFileStream: reading from file..."); - size_t read = stream.readBytes(data.writable(size), size); - TRACEF("testReadFileStream: read: %u", read); - // Auto-closes - //stream.close(); - assert(read == 6); - assert(data.compare("stream") == 0); - uint32_t crc = RNS::Utilities::Crc::crc32(0, data.data(), data.size()); - TRACEF("testWriteFileStream: crc: 0x%X", crc); - TRACEF("testWriteFileStream: stream crc: 0x%X", stream.crc()); - assert(crc == stream.crc()); - } - - size_t post_memory = RNS::Utilities::OS::heap_available(); - size_t diff_memory = (int)pre_memory - (int)post_memory; - TRACEF("testReadFileStream: post-mem: %u", post_memory); - TRACEF("testReadFileStream: diff-mem: %u", diff_memory); - assert(diff_memory == 0); - +void testReadFileStream() { + readFileStream(test_stream_path); } void testStdStream() { @@ -223,7 +253,7 @@ void testStdStream() { file.flush(); file.close(); } - assert(RNS::Utilities::OS::file_exists("./stream")); + TEST_ASSERT_TRUE(RNS::Utilities::OS::file_exists("./stream")); RNS::Utilities::OS::remove_file("./stream"); } @@ -233,32 +263,33 @@ void testCacheWrite() { if (!RNS::Utilities::OS::directory_exists("/cache")) { RNS::Utilities::OS::create_directory("/cache"); } - testWriteFile("./cache/test"); - testWriteFile("./cache/45c50662af11f1b26889efaab547942b45c50662af11f1b26889efaab547942b"); - testWriteFile("./cache/40fe4ab8105b591cca1ef159d476a7b440fe4ab8105b591cca1ef159d476a7b4"); - testWriteFile("./cache/c755609f4d0ab75b5119905a032eeb33c755609f4d0ab75b5119905a032eeb33"); - testWriteFile("./cache/426f66f9aadf866933358b71295f59d0426f66f9aadf866933358b71295f59d0"); - -} - -void testFilesystemSize() { - - TRACEF("testFilesystemSize: size: %u", test_filesystem.storage_size()); - TRACEF("testFilesystemSize: available: %u", test_filesystem.storage_available()); + writeFile("./cache/test"); + writeFile("./cache/45c50662af11f1b26889efaab547942b45c50662af11f1b26889efaab547942b"); + writeFile("./cache/40fe4ab8105b591cca1ef159d476a7b440fe4ab8105b591cca1ef159d476a7b4"); + writeFile("./cache/c755609f4d0ab75b5119905a032eeb33c755609f4d0ab75b5119905a032eeb33"); + writeFile("./cache/426f66f9aadf866933358b71295f59d0426f66f9aadf866933358b71295f59d0"); } -void testFileSystem() { - HEAD("Running testFileSystem...", RNS::LOG_TRACE); +void setUp(void) { + // set stuff up here before each test +} - test_filesystem = new FileSystem(); +void tearDown(void) { + // clean stuff up here after each test +} + +int runUnityTests(void) { + UNITY_BEGIN(); + + // Suite-level setup + RNS::FileSystem test_filesystem = new FileSystem(); ((FileSystem*)test_filesystem.get())->init(); FileSystem::listDir("/"); FileSystem::listDir("/cache"); RNS::Utilities::OS::register_filesystem(test_filesystem); - size_t pre_memory = RNS::Utilities::OS::heap_available(); TRACEF("testFileSystem: pre-mem: %u", pre_memory); @@ -274,40 +305,25 @@ void testFileSystem() { TRACEF("testFileSystem: post-mem: %u", post_memory); TRACEF("testFileSystem: diff-mem: %u", diff_memory); + // Run tests + RUN_TEST(testListDirectory); + RUN_TEST(testWriteFile); + RUN_TEST(testReadFile); + RUN_TEST(testOverwriteFile); + RUN_TEST(testReadNonexistantFile); + //RUN_TEST(testWriteFail(); + RUN_TEST(testWriteFileStream); + RUN_TEST(testReadFileStream); + //RUN_TEST(testStdStream); + //RUN_TEST(testCacheWrite); - testListDirectory(); - testWriteFile("./test1"); - testReadFile("./test1"); - testOverwriteFile("./test1"); - testRemoveFile("./test1"); - testReadNonexistantFile(); - //testWriteFail(); - testWriteFileStream("./stream1"); - testReadFileStream("./stream1"); - testRemoveFile("./stream1"); - //testStdStream(); - //testCacheWrite(); - testFilesystemSize(); - + // Suite-level teardown + removeFile(test_file_path); + removeFile(test_stream_path); + TRACEF("testFileSystem: size: %u", test_filesystem.storage_size()); + TRACEF("testFileSystem: available: %u", test_filesystem.storage_available()); RNS::Utilities::OS::deregister_filesystem(); -} - -/* -void setUp(void) { - // set stuff up here -} - -void tearDown(void) { - // clean stuff up here -} - -int runUnityTests(void) { - UNITY_BEGIN(); - RUN_TEST(testBytes); - RUN_TEST(testCowBytes); - RUN_TEST(testBytesConversion); - RUN_TEST(testMap); return UNITY_END(); } @@ -316,18 +332,19 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/test/test_general/test_main.cpp b/test/test_general/test_main.cpp index 1196290..ec8a7fe 100644 --- a/test/test_general/test_main.cpp +++ b/test/test_general/test_main.cpp @@ -7,14 +7,6 @@ #include #include -void setUp(void) { - // set stuff up here before each test -} - -void tearDown(void) { - // clean stuff up here after each test -} - #ifdef ARDUINO unsigned long test_millis() { return millis(); @@ -74,6 +66,15 @@ void testConvert() { TEST_ASSERT_EQUAL_UINT64(1234567890, newnum); } + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + int runUnityTests(void) { UNITY_BEGIN(); RUN_TEST(testTime); diff --git a/test/test_msgpack/test_main.cpp b/test/test_msgpack/test_main.cpp index 468b41c..47984fe 100644 --- a/test/test_msgpack/test_main.cpp +++ b/test/test_msgpack/test_main.cpp @@ -11,14 +11,6 @@ // Use MsgPack's native binary type using BinaryBuffer = MsgPack::bin_t; -void setUp(void) { - // set stuff up here before each test -} - -void tearDown(void) { - // clean stuff up here after each test -} - void test_msgpack_array_packing(void) { // Test data setup float testFloat = 3.14159f; @@ -239,6 +231,15 @@ void test_unpack_float_python() { TEST_ASSERT_EQUAL_FLOAT(123.456, time); } + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + int runUnityTests(void) { UNITY_BEGIN(); RUN_TEST(test_msgpack_array_packing); diff --git a/examples/unit_tests/A.cpp b/test/test_objects/A.cpp similarity index 53% rename from examples/unit_tests/A.cpp rename to test/test_objects/A.cpp index 18c811d..bc6215b 100644 --- a/examples/unit_tests/A.cpp +++ b/test/test_objects/A.cpp @@ -8,25 +8,25 @@ #include "AData.h" A::A() { - printf("A() A=%x\n", getId()); + printf("A() this=0x%x A.Id=%x\n", this, getId()); } A::A(const A& ref) : _data(ref._data) { - printf("A(A) A=%x\n", getId()); + printf("A(A) this=0x%x A.Id=%x\n", this, getId()); } A::A(const B& b) : _data(new AData(b)) { - printf("A(B) A=%x B=%x\n", getId(), _data->_b.getId()); + printf("A(B) this=0x%x A.Id=%x B.Id=%x\n", this, getId(), _data->_b.getId()); } A& A::operator = (const A& ref) { _data = ref._data; - printf("=(A) A=%x\n", getId()); + printf("=(A) this=0x%x A.Id=%x\n", this, getId()); return *this; } B A::getB() { - printf("getB() A=%x B=%x\n", getId(), _data->_b.getId()); + printf("getB() this=0x%x A.Id=%x B.Id=%x\n", this, getId(), _data->_b.getId()); return _data->_b; } @@ -40,27 +40,27 @@ long A::getId() { A::A() { //A::A(AImpl::Ptr impl) : _impl(impl) { - printf("A() A=%x\n", getId()); + printf("A() this=0x%x A.Id=%x\n", this, getId()); } A::A(const A& ref) : _impl(ref._impl) { - printf("A(A) A=%x\n", getId()); + printf("A(A) this=0x%x A.Id=%x\n", this, getId()); } A::A(const B& b) : _impl(new AImpl(b)) { - //printf("A(B) A=%x B=%x\n", getId(), _impl->_b.getId()); - printf("A(B) A=%x\n", getId()); + //printf("A(B) A.Id=%x B.Id=%x\n", getId(), _impl->_b.getId()); + printf("A(B) this=0x%x A.Id=%x\n", this, getId()); } A& A::operator = (const A& ref) { _impl = ref._impl; - printf("=(A) A=%x\n", getId()); + printf("=(A) this=0x%x A.Id=%x\n", this, getId()); return *this; } B A::getB() { - //printf("getB() A=%x B=%x\n", getId(), _impl->_b.getId()); - printf("getB() A=%x\n", getId()); + //printf("getB() A.Id=%x B.Id=%x\n", getId(), _impl->_b.getId()); + printf("getB() this=0x%x A.Id=%x\n", this, getId()); return _impl->getB(); } diff --git a/examples/unit_tests/A.h b/test/test_objects/A.h similarity index 72% rename from examples/unit_tests/A.h rename to test/test_objects/A.h index 63bea72..27c8683 100644 --- a/examples/unit_tests/A.h +++ b/test/test_objects/A.h @@ -1,5 +1,7 @@ #pragma once +//#define TEST_OBJECT_DATA 1 +#define TEST_OBJECT_IMPL 1 #if TEST_OBJECT_DATA #include @@ -13,6 +15,9 @@ public: A(const B& b); A(const A& ref); A& operator = (const A& ref); + operator bool() const { + return _data.get() != nullptr; + } B getB(); long getId(); protected: @@ -32,6 +37,9 @@ public: A(const B& b); A(const A& ref); A& operator = (const A& ref); + operator bool() const { + return _impl.get() != nullptr; + } B getB(); long getId(); protected: @@ -46,6 +54,9 @@ class A { public: A(); A(const B& b); + operator bool() const { + return true; + } B getB(); long getId(); protected: diff --git a/examples/unit_tests/AData.h b/test/test_objects/AData.h similarity index 100% rename from examples/unit_tests/AData.h rename to test/test_objects/AData.h diff --git a/examples/unit_tests/AImpl.cpp b/test/test_objects/AImpl.cpp similarity index 100% rename from examples/unit_tests/AImpl.cpp rename to test/test_objects/AImpl.cpp diff --git a/examples/unit_tests/AImpl.h b/test/test_objects/AImpl.h similarity index 100% rename from examples/unit_tests/AImpl.h rename to test/test_objects/AImpl.h diff --git a/examples/unit_tests/B.cpp b/test/test_objects/B.cpp similarity index 52% rename from examples/unit_tests/B.cpp rename to test/test_objects/B.cpp index b95f54d..2ee33c7 100644 --- a/examples/unit_tests/B.cpp +++ b/test/test_objects/B.cpp @@ -8,25 +8,25 @@ #include "BData.h" B::B() { - printf("B() B=%x\n", getId()); + printf("B() this=0x%x B.Id=%x\n", this, getId()); } B::B(const B& ref) : _data(ref._data) { - printf("B(B) B=%x\n", getId()); + printf("B(B) this=0x%x B.Id=%x\n", this, getId()); } B::B(const A& a) : _data(new BData(a)) { - printf("B(A) B=%x A=%x\n", getId(), _data->_a.getId()); + printf("B(A) this=0x%x B.Id=%x A.Id=%x\n", this, getId(), _data->_a.getId()); } B& B::operator = (const B& ref) { _data = ref._data; - printf("=(B) B=%x\n", getId()); + printf("=(B) this=0x%x B.Id=%x\n", this, getId()); return *this; } A B::getA() { - printf("getA() B=%x A=%x\n", getId(), _data->_a.getId()); + printf("getA() this=0x%x B.Id=%x A.Id=%x\n", this, getId(), _data->_a.getId()); return _data->_a; } @@ -39,27 +39,27 @@ long B::getId() { #include "BImpl.h" B::B() { - printf("B() B=%x\n", getId()); + printf("B() this=0x%x B.Id=%x\n", this, getId()); } B::B(const B& ref) : _impl(ref._impl) { - printf("B(B) B=%x\n", getId()); + printf("B(B) this=0x%x B.Id=%x\n", this, getId()); } B::B(const A& a) : _impl(new BImpl(a)) { - //printf("B(A) B=%x A=%x\n", getId(), _impl->_a.getId()); - printf("B(A) B=%x\n", getId()); + //printf("B(A) B.Id=%x A.Id=%x\n", getId(), _impl->_a.getId()); + printf("B(A) this=0x%x B.Id=%x\n", this, getId()); } B& B::operator = (const B& ref) { _impl = ref._impl; - printf("=(B) B=%x\n", getId()); + printf("=(B) this=0x%x B.Id=%x\n", this, getId()); return *this; } A B::getA() { - //printf("getA() B=%x A=%x\n", getId(), _impl->_a.getId()); - printf("getA() B=%x\n", getId()); + //printf("getA() B.Id=%x A.Id=%x\n", getId(), _impl->_a.getId()); + printf("getA() this=0x%x B.Id=%x\n", this, getId()); return _impl->getA(); } diff --git a/examples/unit_tests/B.h b/test/test_objects/B.h similarity index 72% rename from examples/unit_tests/B.h rename to test/test_objects/B.h index 55a542b..d27111a 100644 --- a/examples/unit_tests/B.h +++ b/test/test_objects/B.h @@ -1,5 +1,7 @@ #pragma once +//#define TEST_OBJECT_DATA 1 +#define TEST_OBJECT_IMPL 1 #if TEST_OBJECT_DATA #include @@ -13,6 +15,9 @@ public: B(const A& a); B(const B& ref); B& operator = (const B& ref); + operator bool() const { + return _data.get() != nullptr; + } A getA(); long getId(); protected: @@ -32,6 +37,9 @@ public: B(const A& a); B(const B& ref); B& operator = (const B& ref); + operator bool() const { + return _impl.get() != nullptr; + } A getA(); long getId(); protected: @@ -46,6 +54,9 @@ class B { public: B(); B(const A& a); + operator bool() const { + return true; + } A getA(); long getId(); protected: diff --git a/examples/unit_tests/BData.h b/test/test_objects/BData.h similarity index 100% rename from examples/unit_tests/BData.h rename to test/test_objects/BData.h diff --git a/examples/unit_tests/BImpl.cpp b/test/test_objects/BImpl.cpp similarity index 100% rename from examples/unit_tests/BImpl.cpp rename to test/test_objects/BImpl.cpp diff --git a/examples/unit_tests/BImpl.h b/test/test_objects/BImpl.h similarity index 100% rename from examples/unit_tests/BImpl.h rename to test/test_objects/BImpl.h diff --git a/test/test_objects/test_main.cpp b/test/test_objects/test_main.cpp new file mode 100644 index 0000000..0b9731e --- /dev/null +++ b/test/test_objects/test_main.cpp @@ -0,0 +1,71 @@ +#include + +#include "A.h" +#include "B.h" + +#include + +void testObjects() { + + printf("Creating a...\n"); + A a = A(); + + printf("Creating b...\n"); + B b = B(); + + // Following assignments are invalid because a and b are empty + //printf("Assigning b1...\n"); + //B b1 = a.getB(); + //printf("Assigning a1...\n"); + //A a1 = b.getA(); + + printf("Creating ab...\n"); + A ab = A(b); + + printf("Creating ba...\n"); + B ba = B(a); + + printf("Assigning b2...\n"); + B b2 = ab.getB(); + + printf("Assigning a2...\n"); + A a2 = ba.getA(); + +} + + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + +int runUnityTests(void) { + UNITY_BEGIN(); + RUN_TEST(testObjects); + return UNITY_END(); +} + +// For native dev-platform or for some embedded frameworks +int main(void) { + return runUnityTests(); +} + +#ifdef ARDUINO +// For Arduino framework +void setup() { + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runUnityTests(); +} +void loop() {} +#endif + +// For ESP-IDF framework +void app_main() { + runUnityTests(); +} diff --git a/examples/unit_tests/TestOS.cpp b/test/test_os/test_main.cpp similarity index 71% rename from examples/unit_tests/TestOS.cpp rename to test/test_os/test_main.cpp index 74806b0..96b0634 100644 --- a/examples/unit_tests/TestOS.cpp +++ b/test/test_os/test_main.cpp @@ -1,10 +1,8 @@ -//#include +#include #include "Log.h" #include "Utilities/OS.h" -#include - using namespace RNS::Utilities; void testTime() @@ -23,13 +21,13 @@ void testTime() diff_time = (double)(end_ltime - start_ltime) / 1000.0; TRACE(std::string("ltime diff: ") + std::to_string(diff_time)); - assert(diff_time > sleep_time * 0.99); - assert(diff_time < sleep_time * 1.01); + TEST_ASSERT_TRUE(diff_time > sleep_time * 0.99); + TEST_ASSERT_TRUE(diff_time < sleep_time * 1.01); diff_time = end_dtime - start_dtime; TRACE(std::string("dtime diff: ") + std::to_string(diff_time)); - assert(diff_time > sleep_time * 0.99); - assert(diff_time < sleep_time * 1.01); + TEST_ASSERT_TRUE(diff_time > sleep_time * 0.99); + TEST_ASSERT_TRUE(diff_time < sleep_time * 1.01); } @@ -38,20 +36,17 @@ void testConvert() { //printf("time: %lld\n", time); uint64_t num = 1234567890; - //TEST_ASSERT_EQUAL_UINT64(1234567890, num); - assert(1234567890 == num); + TEST_ASSERT_EQUAL_UINT64(1234567890, num); char str[16]; snprintf(str, 16, "%lld", num); - //TEST_ASSERT_EQUAL_STRING("1234567890", str); - assert(strncmp("1234567890", str, sizeof(str)) == 0); + TEST_ASSERT_EQUAL_STRING("1234567890", str); char* buf = (char*)# //uint64_t newnum = (uint64_t)(*buf); uint64_t newnum = *(uint64_t*)buf; - //TEST_ASSERT_EQUAL_UINT64(1234567890, newnum); - assert(1234567890 == newnum); + TEST_ASSERT_EQUAL_UINT64(1234567890, newnum); } void testOS() { @@ -60,18 +55,20 @@ void testOS() { testConvert(); } -/* + void setUp(void) { - // set stuff up here + // set stuff up here before each test } void tearDown(void) { - // clean stuff up here + // clean stuff up here after each test } int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testCollections); + RUN_TEST(testTime); + RUN_TEST(testConvert); + RUN_TEST(testOS); return UNITY_END(); } @@ -80,19 +77,20 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/examples/unit_tests/TestPersistence.cpp b/test/test_persistence/test_main.cpp similarity index 82% rename from examples/unit_tests/TestPersistence.cpp rename to test/test_persistence/test_main.cpp index 5642b0f..af67f64 100644 --- a/examples/unit_tests/TestPersistence.cpp +++ b/test/test_persistence/test_main.cpp @@ -1,6 +1,6 @@ -//#include +#include -#include "FileSystem.h" +#include "../common/filesystem/FileSystem.h" #include #include @@ -16,11 +16,8 @@ #include #include #include -#include #include -RNS::FileSystem persistence_filesystem(RNS::Type::NONE); - class TestInterface : public RNS::InterfaceImpl { public: @@ -89,7 +86,7 @@ void testWrite() { } else { TRACE("write failed"); - assert(false); + TEST_FAIL(); } } @@ -98,13 +95,14 @@ void testRead() { if (RNS::Utilities::OS::read_file(test_file_path, data) > 0) { TRACE("read: " + std::to_string(data.size()) + " bytes"); TRACE("data: " + data.toString()); - assert(data.size() == strlen(test_file_data)); - assert(memcmp(data.data(), test_file_data, strlen(test_file_data)) == 0); + TEST_ASSERT_EQUAL_size_t(strlen(test_file_data), data.size()); + TEST_ASSERT_NOT_NULL(data.data()); + TEST_ASSERT_EQUAL_MEMORY(test_file_data, data.data(), strlen(test_file_data)); //assert(RNS::Utilities::OS::remove_file(test_file_path)); } else { TRACE("read failed"); - assert(false); + TEST_FAIL(); } } @@ -125,7 +123,7 @@ void testSerializeObject() { Test test("bar", "fum"); StaticJsonDocument<1024> doc; - doc = test; + doc.set(test); RNS::Bytes data; size_t size = 1024; @@ -141,12 +139,12 @@ void testSerializeObject() { } else { TRACE("write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -171,13 +169,13 @@ void testDeserializeObject() { } else { TRACE("testDeserializeObject: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_object_path)); } else { TRACE("read failed"); - assert(false); + TEST_FAIL(); } } @@ -189,7 +187,7 @@ void testSerializeObject() { TRACE("testSerializeObject: test: " + test.toString()); StaticJsonDocument<1024> doc; - doc = test; + doc.set(test); RNS::Bytes data; size_t size = 1024; @@ -205,12 +203,12 @@ void testSerializeObject() { } else { TRACE("testSerializeObject: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testSerializeObject: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -236,13 +234,13 @@ void testDeserializeObject() { } else { TRACE("testDeserializeObject: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_object_path)); } else { TRACE("testDeserializeObject: read failed"); - assert(false); + TEST_FAIL(); } } @@ -266,7 +264,7 @@ void testSerializeVector() { //copyArray(vector, doc.createNestedArray("vector")); //doc["vector"] = vector; //copyArray(vector, doc); - doc = vector; + doc.set(vector); //JsonVariant dst; //dst = vector; @@ -302,12 +300,12 @@ void testSerializeVector() { } else { TRACE("testSerializeVector: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testSerializeVector: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -344,13 +342,13 @@ void testDeserializeVector() { } else { TRACE("testDeserializeVector: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_vector_path)); } else { TRACE("testDeserializeVector: read failed"); - assert(false); + TEST_FAIL(); } } @@ -374,7 +372,7 @@ void testSerializeSet() { //copyArray(set, doc.createNestedArray("set")); //doc["set"] = set; //copyArray(set, doc); - //doc = set; + //doc.set(set); //std::string test; //RNS::Bytes test; @@ -398,12 +396,12 @@ void testSerializeSet() { } else { TRACE("testSerializeSet: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testSerializeSet: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -440,13 +438,13 @@ void testDeserializeSet() { } else { TRACE("testDeserializeSet: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_set_path)); } else { TRACE("testDeserializeSet: read failed"); - assert(false); + TEST_FAIL(); } } @@ -471,7 +469,7 @@ void testSerializeMap() { //copyArray(map, doc.createNestedArray("map")); //doc["map"] = map; //copyArray(map, doc); - doc = map; + doc.set(map); RNS::Bytes data; size_t size = 4096; @@ -487,12 +485,12 @@ void testSerializeMap() { } else { TRACE("testSerializeMap: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testSerializeMap: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -529,21 +527,23 @@ void testDeserializeMap() { } else { TRACE("testDeserializeMap: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_map_path)); } else { TRACE("testDeserializeMap: read failed"); - assert(false); + TEST_FAIL(); } } #ifdef ARDUINO const char test_destination_table_path[] = "/test_destination_table"; +const char test_empty_destination_table_path[] = "/test_empty_destination_table"; #else const char test_destination_table_path[] = "test_destination_table"; +const char test_empty_destination_table_path[] = "test_empty_destination_table"; #endif void testSerializeDestinationTable() { @@ -595,7 +595,7 @@ void testSerializeDestinationTable() { } //StaticJsonDocument<1024> doc; - //doc = map; + //doc.set(map); TRACE("testSerializeDestinationTable: JSON_OBJECT_SIZE " + std::to_string(JSON_OBJECT_SIZE(1)) + " bytes"); TRACE("testSerializeDestinationTable: JSON_ARRAY_SIZE " + std::to_string(JSON_ARRAY_SIZE(1)) + " bytes"); int calcsize = 0; @@ -620,9 +620,10 @@ void testSerializeDestinationTable() { TRACE("testSerializeDestinationTable: doc memory " + std::to_string(doc.memoryUsage())); RNS::Bytes data; - // /size_t size = 1024; - //size_t size = calcsize; - size_t size = doc.memoryUsage(); + //size_t size = 32768; + //size_t size = BUFFER_SIZE; + size_t size = calcsize; + //size_t size = doc.memoryUsage(); TRACE("testSerializeDestinationTable: buffer size " + std::to_string(size) + " bytes"); size_t length = serializeJson(doc, data.writable(size), size); //size_t length = serializeMsgPack(doc, data.writable(size), size); @@ -637,12 +638,12 @@ void testSerializeDestinationTable() { } else { TRACE("testSerializeDestinationTable: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testSerializeDestinationTable: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -668,6 +669,7 @@ void testDeserializeDestinationTable() { TRACE("json: " + data.toString()); static std::map map(doc.as>()); + TEST_ASSERT_EQUAL_size_t(22, map.size()); for (auto& [hash, test] : map) { TRACE("testDeserializeDestinationTable: entry: " + hash.toHex() + " = " + test.debugString()); } @@ -679,109 +681,65 @@ void testDeserializeDestinationTable() { } else { TRACE("testDeserializeDestinationTable: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_destination_table_path)); } else { TRACE("testDeserializeDestinationTable: read failed"); - assert(false); + TEST_FAIL(); } } -void testPersistenceSerializeDestinationTable() { - HEAD("testPersistenceSerializeDestinationTable", RNS::LOG_TRACE); +void testDeserializeEmptyDestinationTable() { - //RNS::Bytes empty; - //std::set blobs; - //RNS::Interface interface({RNS::Type::NONE}); - RNS::Interface test_interface(new TestInterface()); - //RNS::Packet packet({RNS::Type::NONE}); - static std::map map; - //DestinationEntry(double time, const Bytes& received_from, uint8_t announce_hops, double expires, const std::set& random_blobs, Interface& receiving_interface, const Packet& packet) : - //RNS::Transport::DestinationEntry entry_one(1.0, empty, 1, 0.0, blobs, interface, packet); - RNS::Bytes received; - received.assignHex("deadbeef"); - RNS::Bytes blob; - blob.assignHex("b10bb10b"); - std::set blobs({received, blob}); - RNS::Transport::DestinationEntry entry_one; - entry_one._timestamp = 1.0; - entry_one._received_from = received; - entry_one._receiving_interface = test_interface.get_hash(); - entry_one._random_blobs = blobs; - RNS::Bytes one; - one.assignHex("1111111111111111"); - map.insert({one, entry_one}); - //RNS::Transport::DestinationEntry entry_two(2.0, empty, 1, 0.0, blobs, interface, packet); - RNS::Transport::DestinationEntry entry_two; - entry_two._timestamp = 2.0; - entry_two._received_from = received; - entry_two._receiving_interface = test_interface.get_hash(); - entry_two._random_blobs = blobs; - RNS::Bytes two; - two.assignHex("2222222222222222"); - map.insert({two, entry_two}); + // Write empty JSON destination table + RNS::Bytes emptyData("{}"); + TEST_ASSERT_EQUAL_size_t(2, RNS::Utilities::OS::write_file(test_empty_destination_table_path, emptyData)); - for (int n = 0; n < 20; n++) { - RNS::Transport::DestinationEntry entry; - entry._timestamp = 1.0; - entry._received_from = received; - entry._receiving_interface = test_interface.get_hash(); - entry._random_blobs = blobs; - RNS::Bytes hash; - hash.assign(std::to_string(1000000000000001 + n).c_str()); - map.insert({hash, entry}); + RNS::Bytes data; + if (RNS::Utilities::OS::read_file(test_empty_destination_table_path, data) > 0) { + TRACE("testDeserializeEmptyDestinationTable: read: " + std::to_string(data.size()) + " bytes"); + //StaticJsonDocument<8192> doc; + //DynamicJsonDocument doc(1024); + int calcsize = data.size() * 2.0; + TRACE("testDeserializeEmptyDestinationTable: calcsize " + std::to_string(calcsize) + " bytes"); + DynamicJsonDocument doc(calcsize); + + //TRACE("testDeserializeVector: data: " + data.toString()); + DeserializationError error = deserializeJson(doc, data.data()); + //DeserializationError error = deserializeMsgPack(doc, data.data()); + TRACE("testDeserializeEmptyDestinationTable: doc size " + std::to_string(doc.size())); + TRACE("testDeserializeEmptyDestinationTable: doc memory " + std::to_string(doc.memoryUsage())); + if (!error) { + TRACE("testDeserializeEmptyDestinationTable: successfully deserialized " + std::to_string(data.size()) + " bytes"); + TRACE("json: " + data.toString()); + + static std::map map(doc.as>()); + TEST_ASSERT_EQUAL_size_t(0, map.size()); + for (auto& [hash, test] : map) { + TRACE("testDeserializeEmptyDestinationTable: entry: " + hash.toHex() + " = " + test.debugString()); + } + + //JsonObject root = doc.as(); + //for (JsonPair kv : root) { + // TRACE("testDeserializeEmptyDestinationTable: entry: " + std::string(kv.key().c_str()) + " = " + kv.value().as().toString()); + //} + } + else { + TRACE("testDeserializeEmptyDestinationTable: failed to deserialize"); + TEST_FAIL(); + } + //assert(RNS::Utilities::OS::remove_file(test_empty_destination_table_path)); } - - TRACEF("testPersistenceSerializeDestinationTable: map contains %d entries", map.size()); - assert(22 == map.size()); - for (auto& [hash, test] : map) { - TRACEF("testPersistenceSerializeDestinationTable: entry: %s = %s", hash.toHex().c_str(), test.debugString().c_str()); + else { + TRACE("testDeserializeEmptyDestinationTable: read failed"); + TEST_FAIL(); } - uint32_t stream_crc; - size_t wrote = RNS::Persistence::serialize(map, test_destination_table_path, stream_crc); - TRACEF("testPersistenceSerializeDestinationTable: wrote: %d bytes", wrote); - if (wrote == 0) { - TRACE("testPersistenceSerializeDestinationTable: write failed"); - assert(false); - } - - uint32_t crc = RNS::Persistence::crc(map); - TRACEF("testPersistenceSerializeDestinationTable: crc: 0x%X", crc); - TRACEF("testPersistenceSerializeDestinationTable: stream crc: 0x%X", stream_crc); - assert(0xFC979601 == crc); - assert(0xFC979601 == stream_crc); - } -void testPersistenceDeserializeDestinationTable() { - HEAD("testPersistenceDeserializeDestinationTable", RNS::LOG_TRACE); - - std::map map; - uint32_t stream_crc; - size_t read = RNS::Persistence::deserialize(map, test_destination_table_path, stream_crc); - TRACEF("testPersistenceDeserializeDestinationTable: deserialized %d bytes", read); - if (read == 0) { - TRACE("testPersistenceDeserializeDestinationTable: failed to deserialize"); - assert(false); - } - - TRACEF("testPersistenceDeserializeDestinationTable: map contains %d entries", map.size()); - assert(22 == map.size()); - for (auto& [hash, test] : map) { - TRACEF("testPersistenceDeserializeDestinationTable: entry: %s = %s", hash.toHex().c_str(), test.debugString().c_str()); - } - - uint32_t crc = RNS::Persistence::crc(map); - TRACEF("testPersistenceDeserializeDestinationTable: crc: 0x%X", crc); - TRACEF("testPersistenceDeserializeDestinationTable: stream crc: 0x%X", stream_crc); - assert(0xFC979601 == crc); - assert(0xFC979601 == stream_crc); - -} void testSerializeTimeOffset() { uint64_t offset = RNS::Utilities::OS::ltime(); @@ -814,7 +772,7 @@ void testJsonMsgpackSerializeObject() { TRACE("testJsonMsgpackSerializeObject: test: " + test.toString()); StaticJsonDocument<1024> doc; - doc = test; + doc.set(test); RNS::Bytes data; size_t size = 1024; @@ -829,12 +787,12 @@ void testJsonMsgpackSerializeObject() { } else { TRACE("testJsonMsgpackSerializeObject: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testJsonMsgpackSerializeObject: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -859,13 +817,13 @@ void testJsonMsgpackDeserializeObject() { } else { TRACE("testJsonMsgpackDeserializeObject: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_object_path)); } else { TRACE("testJsonMsgpackDeserializeObject: read failed"); - assert(false); + TEST_FAIL(); } } @@ -913,12 +871,12 @@ void testJsonMsgpackSerializeArray() { } else { TRACE("testJsonMsgpackSerializeArray: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testJsonMsgpackSerializeArray: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -943,18 +901,20 @@ void testJsonMsgpackDeserializeArray() { RNS::Bytes two(array[2].as>()); TRACEF("testJsonMsgpackDeserializeArray: time=%f one=%s two=%s", time, one.toString().c_str(), two.toString().c_str()); //TRACEF("testJsonMsgpackDeserializeArray: time=%f one=%s two=%s", time, one.c_str(), two.c_str()); - assert(one == "first\xab"); - assert(two == "second\xcd"); + TEST_ASSERT_NOT_NULL(one.data()); + TEST_ASSERT_EQUAL_MEMORY("first\xab", one.data(), one.size()); + TEST_ASSERT_NOT_NULL(two.data()); + TEST_ASSERT_EQUAL_MEMORY("second\xcd", two.data(), two.size()); } else { TRACE("testJsonMsgpackDeserializeArray: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_array_path)); } else { TRACE("testJsonMsgpackDeserializeArray: read failed"); - assert(false); + TEST_FAIL(); } } @@ -967,7 +927,7 @@ void testMsgpackSerializeObject() { TRACE("testMsgpackSerializeObject: test: " + test.toString()); StaticJsonDocument<1024> doc; - doc = test; + doc.set(test); RNS::Bytes data; size_t size = 1024; @@ -982,12 +942,12 @@ void testMsgpackSerializeObject() { } else { TRACE("testMsgpackSerializeObject: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testMsgpackSerializeObject: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -1012,13 +972,13 @@ void testMsgpackDeserializeObject() { } else { TRACE("testMsgpackDeserializeObject: failed to deserialize"); - assert(false); + TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_object_path)); } else { TRACE("testMsgpackDeserializeObject: read failed"); - assert(false); + TEST_FAIL(); } } @@ -1055,12 +1015,12 @@ void testMsgpackSerializeArray() { } else { TRACE("testMsgpackSerializeArray: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testMsgpackSerializeArray: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -1088,12 +1048,14 @@ void testMsgpackDeserializeArray() { TRACEF("testMsgpackDeserializeArray: time=%f one=%s two=%s", time, one.toString().c_str(), two.toString().c_str()); //TRACEF("testMsgpackDeserializeArray: time=%f one=%s two=%s", time, one.c_str(), two.c_str()); //assert(RNS::Utilities::OS::remove_file(test_array_path)); - assert(one == "first\xab"); - assert(two == "second\xcd"); + TEST_ASSERT_NOT_NULL(one.data()); + TEST_ASSERT_EQUAL_MEMORY("first\xab", one.data(), one.size()); + TEST_ASSERT_NOT_NULL(two.data()); + TEST_ASSERT_EQUAL_MEMORY("second\xcd", two.data(), two.size()); } else { TRACE("testMsgpackDeserializeArray: read failed"); - assert(false); + TEST_FAIL(); } } @@ -1104,15 +1066,9 @@ void testMsgpackSerializeSeries() { double time = 123.456; // CBA Need this version writing byte-arrays to match the python version also writing byte-arrays (27 bytes) - //const RNS::Bytes one("first\xab"); - //const RNS::Bytes two("second\xcd"); - //MsgPack::bin_t one("first\xab"); - //MsgPack::bin_t two("second\xcd"); + const RNS::Bytes one("first\xab"); + const RNS::Bytes two("second\xcd"); //TRACEF("testMsgpackSerializeSeries: time=%f one=%s two=%s", time, one.toString().c_str(), two.toString().c_str()); - // CBA This version writing strings exactly matches the python version also writing strings (23 bytes) - const std::string one("first"); - const std::string two("second"); - //TRACEF("testMsgpackSerializeSeries: time=%f one=%s two=%s", time, one.c_str(), two.c_str()); MsgPack::Packer packer; packer.serialize(time, one, two); @@ -1126,12 +1082,12 @@ void testMsgpackSerializeSeries() { } else { TRACE("testMsgpackSerializeSeries: write failed"); - assert(false); + TEST_FAIL(); } } else { TRACE("testMsgpackSerializeSeries: failed to serialize"); - assert(false); + TEST_FAIL(); } } @@ -1155,82 +1111,76 @@ void testMsgpackDeserializeSeries() { TRACEF("testMsgpackDeserializeSeries: time=%f one=%s two=%s", time, one.toString().c_str(), two.toString().c_str()); //TRACEF("testMsgpackDeserializeSeries: time=%f one=%s two=%s", time, one.c_str(), two.c_str()); //assert(RNS::Utilities::OS::remove_file(test_series_path)); - assert(one == "first\xab"); - assert(two == "second\xcd"); + TEST_ASSERT_NOT_NULL(one.data()); + TEST_ASSERT_EQUAL_MEMORY("first\xab", one.data(), one.size()); + TEST_ASSERT_NOT_NULL(two.data()); + TEST_ASSERT_EQUAL_MEMORY("second\xcd", two.data(), two.size()); } else { TRACE("testMsgpackDeserializeSeries: read failed"); - assert(false); + TEST_FAIL(); } } -void testPersistence() { - HEAD("Running testPersistence...", RNS::LOG_TRACE); - - persistence_filesystem = new FileSystem(); - ((FileSystem*)persistence_filesystem.get())->init(); - RNS::Utilities::OS::register_filesystem(persistence_filesystem); - -/* - testWrite(); - testRead(); - testSerializeObject(); - testDeserializeObject(); - testSerializeVector(); - testDeserializeVector(); - testSerializeSet(); - testDeserializeSet(); - testSerializeMap(); - testDeserializeMap(); -*/ - -/* - testSerializeTimeOffset(); - - testSerializeDestinationTable(); - testDeserializeDestinationTable(); - - // CBA Following not currently working on native due to incomplete streaming implementation -#ifdef ARDUINO - testPersistenceSerializeDestinationTable(); -#endif - - // CBA Following not currently working on native due to incomplete streaming implementation -#ifdef ARDUINO - testPersistenceDeserializeDestinationTable(); -#endif -*/ - - //testJsonMsgpackSerializeObject(); - //testJsonMsgpackDeserializeObject(); - //testJsonMsgpackSerializeArray(); - //testJsonMsgpackDeserializeArray(); - - //testMsgpackSerializeObject(); - //testMsgpackDeserializeObject(); - testMsgpackSerializeArray(); - testMsgpackDeserializeArray(); - //testMsgpackSerializeSeries(); - //testMsgpackDeserializeSeries(); - - RNS::Utilities::OS::deregister_filesystem(); - -} - -/* void setUp(void) { - // set stuff up here + // set stuff up here before each test } void tearDown(void) { - // clean stuff up here + // clean stuff up here after each test } int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testCollections); + + // Suite-level setup + RNS::FileSystem persistence_filesystem = new FileSystem(); + ((FileSystem*)persistence_filesystem.get())->init(); + RNS::Utilities::OS::register_filesystem(persistence_filesystem); + + // Run tests + RUN_TEST(testWrite); + RUN_TEST(testRead); + RUN_TEST(testSerializeObject); + RUN_TEST(testDeserializeObject); + RUN_TEST(testSerializeVector); + RUN_TEST(testDeserializeVector); + RUN_TEST(testSerializeSet); + RUN_TEST(testDeserializeSet); + RUN_TEST(testSerializeMap); + RUN_TEST(testDeserializeMap); + + RUN_TEST(testSerializeTimeOffset); + + RUN_TEST(testSerializeDestinationTable); + RUN_TEST(testDeserializeDestinationTable); + RUN_TEST(testDeserializeEmptyDestinationTable); + + RUN_TEST(testJsonMsgpackSerializeObject); + RUN_TEST(testJsonMsgpackDeserializeObject); + RUN_TEST(testJsonMsgpackSerializeArray); + RUN_TEST(testJsonMsgpackDeserializeArray); + + RUN_TEST(testMsgpackSerializeObject); + RUN_TEST(testMsgpackDeserializeObject); + RUN_TEST(testMsgpackSerializeArray); + RUN_TEST(testMsgpackDeserializeArray); + RUN_TEST(testMsgpackSerializeSeries); + RUN_TEST(testMsgpackDeserializeSeries); + + // Suite-level teardown + RNS::Utilities::OS::remove_file(test_file_path); + RNS::Utilities::OS::remove_file(test_object_path); + RNS::Utilities::OS::remove_file(test_map_path); + RNS::Utilities::OS::remove_file(test_vector_path); + RNS::Utilities::OS::remove_file(test_set_path); + RNS::Utilities::OS::remove_file(test_array_path); + RNS::Utilities::OS::remove_file(test_destination_table_path); + RNS::Utilities::OS::remove_file(test_series_path); + RNS::Utilities::OS::deregister_filesystem(); + return UNITY_END(); } @@ -1239,18 +1189,19 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/examples/unit_tests/TestReference.cpp b/test/test_reference/test_main.cpp similarity index 82% rename from examples/unit_tests/TestReference.cpp rename to test/test_reference/test_main.cpp index 9a7234b..a79a78d 100644 --- a/examples/unit_tests/TestReference.cpp +++ b/test/test_reference/test_main.cpp @@ -1,24 +1,22 @@ -//#include +#include #include "Reticulum.h" #include "Bytes.h" -#include - void testReference() { HEAD("Running testReference...", RNS::LOG_TRACE); RNS::Reticulum reticulum_default; - assert(reticulum_default); + TEST_ASSERT_TRUE(reticulum_default); RNS::Reticulum reticulum_none({RNS::Type::NONE}); - assert(!reticulum_none); + TEST_ASSERT_FALSE(reticulum_none); RNS::Reticulum reticulum_default_copy(reticulum_default); - assert(reticulum_default_copy); + TEST_ASSERT_TRUE(reticulum_default_copy); RNS::Reticulum reticulum_none_copy(reticulum_none); - assert(!reticulum_none_copy); + TEST_ASSERT_FALSE(reticulum_none_copy); /* RNS::loglevel(RNS::LOG_TRACE); @@ -117,11 +115,39 @@ void testReference() { } -/* -int main(void) -{ + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + +int runUnityTests(void) { UNITY_BEGIN(); RUN_TEST(testReference); return UNITY_END(); } -*/ + +// For native dev-platform or for some embedded frameworks +int main(void) { + return runUnityTests(); +} + +#ifdef ARDUINO +// For Arduino framework +void setup() { + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runUnityTests(); +} +void loop() {} +#endif + +// For ESP-IDF framework +void app_main() { + runUnityTests(); +} diff --git a/examples/unit_tests/TestReticulum.cpp b/test/test_rns_loopback/test_main.cpp similarity index 92% rename from examples/unit_tests/TestReticulum.cpp rename to test/test_rns_loopback/test_main.cpp index a46286e..770000a 100644 --- a/examples/unit_tests/TestReticulum.cpp +++ b/test/test_rns_loopback/test_main.cpp @@ -1,13 +1,11 @@ -//#include +#include -#include "FileSystem.h" +#include "../common/filesystem/FileSystem.h" #include #include #include -#include - RNS::FileSystem reticulum_filesystem(RNS::Type::NONE); class InInterface : public RNS::InterfaceImpl { @@ -21,14 +19,7 @@ public: } // Incoming interface only, send_outgoing not currently implemented virtual void send_outgoing(const RNS::Bytes &data) {} - // Incoming shoujld be handled automatically by InterfaceImpl::handle_incoming called via Interface::handle_incoming from OutInterface -/* - void on_incoming(const RNS::Bytes &data) { - HEAD("InInterface.handle_incoming: data: " + data.toHex(), RNS::LOG_TRACE); - // Pass received data on to transport - InterfaceImpl::handle_incoming(data); - } -*/ + // Incoming is be handled automatically by InterfaceImpl::handle_incoming called via Interface::handle_incoming from OutInterface }; class OutInterface : public RNS::InterfaceImpl { @@ -44,6 +35,8 @@ public: HEAD("OutInterface.send_outgoing: data: " + data.toHex(), RNS::LOG_TRACE); // Loop data back to InInterface for testing + // NOTE: This sends the incoming data directory to RNS::Interface (which relays to Transport) + // and does not actually go through InInterface. _in_interface.handle_incoming(data); // Perform post-send housekeeping @@ -173,24 +166,22 @@ void testReticulum() { destination.announce(RNS::bytesFromString("test")); // 23.9% (+0.8%) + HEAD("Destroying Reticulum instance...", RNS::LOG_TRACE); reticulum = {RNS::Type::NONE}; } -/* + void setUp(void) { - // set stuff up here + // set stuff up here before each test } void tearDown(void) { - // clean stuff up here + // clean stuff up here after each test } int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testBytes); - RUN_TEST(testCowBytes); - RUN_TEST(testBytesConversion); - RUN_TEST(testMap); + RUN_TEST(testReticulum); return UNITY_END(); } @@ -199,18 +190,19 @@ int main(void) { return runUnityTests(); } +#ifdef ARDUINO // For Arduino framework void setup() { // Wait ~2 seconds before the Unity test runner // establishes connection with a board Serial interface delay(2000); - + runUnityTests(); } void loop() {} +#endif // For ESP-IDF framework void app_main() { runUnityTests(); } -*/ diff --git a/test/test_rns_persistence/test_main.cpp b/test/test_rns_persistence/test_main.cpp new file mode 100644 index 0000000..f13722b --- /dev/null +++ b/test/test_rns_persistence/test_main.cpp @@ -0,0 +1,200 @@ +#include + +#include "../common/filesystem/FileSystem.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +class TestInterface : public RNS::InterfaceImpl { + +public: + TestInterface(const char* name = "TestInterface") : InterfaceImpl(name) {} + virtual ~TestInterface() {} + +private: + virtual void send_outgoing(const RNS::Bytes& data) { + DEBUG("TestInterface.send_outgoing: data: " + data.toHex()); + // Perform post-send housekeeping + InterfaceImpl::handle_outgoing(data); + } + void on_incoming(const RNS::Bytes& data) { + DEBUG("TestInterface.on_incoming: data: " + data.toHex()); + // Pass received data on to transport + InterfaceImpl::handle_incoming(data); + } + +}; + +#ifdef ARDUINO +const char test_destination_table_path[] = "/test_destination_table"; +const char test_empty_destination_table_path[] = "/test_empty_destination_table"; +#else +const char test_destination_table_path[] = "test_destination_table"; +const char test_empty_destination_table_path[] = "test_empty_destination_table"; +#endif + +void testSerializeDestinationTable() { + HEAD("testSerializeDestinationTable", RNS::LOG_TRACE); + + //RNS::Bytes empty; + //std::set blobs; + //RNS::Interface interface({RNS::Type::NONE}); + RNS::Interface test_interface(new TestInterface()); + //RNS::Packet packet({RNS::Type::NONE}); + static std::map map; + //DestinationEntry(double time, const Bytes& received_from, uint8_t announce_hops, double expires, const std::set& random_blobs, Interface& receiving_interface, const Packet& packet) : + //RNS::Transport::DestinationEntry entry_one(1.0, empty, 1, 0.0, blobs, interface, packet); + RNS::Bytes received; + received.assignHex("deadbeef"); + RNS::Bytes blob; + blob.assignHex("b10bb10b"); + std::set blobs({received, blob}); + RNS::Transport::DestinationEntry entry_one; + entry_one._timestamp = 1.0; + entry_one._received_from = received; + entry_one._receiving_interface = test_interface.get_hash(); + entry_one._random_blobs = blobs; + RNS::Bytes one; + one.assignHex("1111111111111111"); + map.insert({one, entry_one}); + //RNS::Transport::DestinationEntry entry_two(2.0, empty, 1, 0.0, blobs, interface, packet); + RNS::Transport::DestinationEntry entry_two; + entry_two._timestamp = 2.0; + entry_two._received_from = received; + entry_two._receiving_interface = test_interface.get_hash(); + entry_two._random_blobs = blobs; + RNS::Bytes two; + two.assignHex("2222222222222222"); + map.insert({two, entry_two}); + + for (int n = 0; n < 20; n++) { + RNS::Transport::DestinationEntry entry; + entry._timestamp = 1.0; + entry._received_from = received; + entry._receiving_interface = test_interface.get_hash(); + entry._random_blobs = blobs; + RNS::Bytes hash; + hash.assign(std::to_string(1000000000000001 + n).c_str()); + map.insert({hash, entry}); + } + + TRACEF("testSerializeDestinationTable: map contains %d entries", map.size()); + TEST_ASSERT_EQUAL_size_t(22, map.size()); + for (auto& [hash, test] : map) { + TRACEF("testSerializeDestinationTable: entry: %s = %s", hash.toHex().c_str(), test.debugString().c_str()); + } + + uint32_t stream_crc; + size_t wrote = RNS::Persistence::serialize(map, test_destination_table_path, stream_crc); + TRACEF("testSerializeDestinationTable: wrote: %d bytes", wrote); + if (wrote == 0) { + TRACE("testSerializeDestinationTable: write failed"); + TEST_FAIL(); + } + + uint32_t crc = RNS::Persistence::crc(map); + TRACEF("testSerializeDestinationTable: crc: 0x%X", crc); + TRACEF("testSerializeDestinationTable: stream crc: 0x%X", stream_crc); + TEST_ASSERT_EQUAL_UINT32(0xFC979601, crc); + TEST_ASSERT_EQUAL_UINT32(0xFC979601, stream_crc); + +} + +void testDeserializeDestinationTable() { + HEAD("testDeserializeDestinationTable", RNS::LOG_TRACE); + + std::map map; + uint32_t stream_crc; + size_t read = RNS::Persistence::deserialize(map, test_destination_table_path, stream_crc); + TRACEF("testDeserializeDestinationTable: deserialized %d bytes", read); + if (read == 0) { + TRACE("testDeserializeDestinationTable: failed to deserialize"); + TEST_FAIL(); + } + + TRACEF("testDeserializeDestinationTable: map contains %d entries", map.size()); + TEST_ASSERT_EQUAL_size_t(22, map.size()); + for (auto& [hash, test] : map) { + TRACEF("testDeserializeDestinationTable: entry: %s = %s", hash.toHex().c_str(), test.debugString().c_str()); + } + + uint32_t crc = RNS::Persistence::crc(map); + TRACEF("testDeserializeDestinationTable: crc: 0x%X", crc); + TRACEF("testDeserializeDestinationTable: stream crc: 0x%X", stream_crc); + TEST_ASSERT_EQUAL_UINT32(0xFC979601, crc); + TEST_ASSERT_EQUAL_UINT32(0xFC979601, stream_crc); + +} + +void testDeserializeEmptyDestinationTable() { + + // Write empty JSON destination table + RNS::Bytes emptyData("{}"); + TEST_ASSERT_EQUAL_size_t(2, RNS::Utilities::OS::write_file(test_empty_destination_table_path, emptyData)); + + std::map destination_table; + RNS::Persistence::deserialize(destination_table, test_empty_destination_table_path); + TEST_ASSERT_EQUAL_size_t(0, destination_table.size()); +} + + +void setUp(void) { + // set stuff up here before each test +} + +void tearDown(void) { + // clean stuff up here after each test +} + +int runUnityTests(void) { + UNITY_BEGIN(); + + // Suite-level setup + RNS::FileSystem persistence_filesystem = new FileSystem(); + ((FileSystem*)persistence_filesystem.get())->init(); + RNS::Utilities::OS::register_filesystem(persistence_filesystem); + + // Run tests + RUN_TEST(testSerializeDestinationTable); + RUN_TEST(testDeserializeDestinationTable); + RUN_TEST(testDeserializeEmptyDestinationTable); + + // Suite-level teardown + RNS::Utilities::OS::remove_file(test_destination_table_path); + RNS::Utilities::OS::remove_file(test_empty_destination_table_path); + RNS::Utilities::OS::deregister_filesystem(); + + return UNITY_END(); +} + +// For native dev-platform or for some embedded frameworks +int main(void) { + return runUnityTests(); +} + +#ifdef ARDUINO +// For Arduino framework +void setup() { + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runUnityTests(); +} +void loop() {} +#endif + +// For ESP-IDF framework +void app_main() { + runUnityTests(); +}