diff --git a/examples/link/main.cpp b/examples/link/main.cpp index 281e523..76abcd0 100644 --- a/examples/link/main.cpp +++ b/examples/link/main.cpp @@ -43,8 +43,6 @@ #define APP_NAME "example_utilities" RNS::Reticulum reticulum; -RNS::FileSystem universal_filesystem(RNS::Type::NONE); -RNS::Interface udp_interface(RNS::Type::NONE); /* @@ -88,9 +86,9 @@ void client_connected(RNS::Link& link) { void server_loop(RNS::Destination& destination) { // Let the user know that everything is ready RNS::log( - "Link example "+ + "Link example <"+ destination.hash().toHex()+ - " running, waiting for a connection." + "> running, waiting for a connection." ); RNS::log("Hit enter to manually send an announce (Ctrl-C to quit)"); @@ -101,7 +99,6 @@ void server_loop(RNS::Destination& destination) { // know how to create messages directed towards it. while (true) { reticulum.loop(); - udp_interface.loop(); // Non-blocking input char ch; while (read(STDIN_FILENO, &ch, 1) > 0) { @@ -194,7 +191,6 @@ void client_loop() { RNS::log("Waiting for link to become active..."); while (!server_link) { reticulum.loop(); - udp_interface.loop(); RNS::Utilities::OS::sleep(0.1); } @@ -204,7 +200,6 @@ void client_loop() { bool should_quit = false; while (!should_quit) { reticulum.loop(); - udp_interface.loop(); // Non-blocking input char ch; @@ -274,7 +269,6 @@ void client(const char* destination_hexhash) { RNS::Transport::request_path(destination_hash); while (!RNS::Transport::has_path(destination_hash)) { reticulum.loop(); - udp_interface.loop(); RNS::Utilities::OS::sleep(0.1); } } @@ -350,12 +344,12 @@ int main(int argc, char *argv[]) { fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); // Initialize and register filesystem - universal_filesystem = new UniversalFileSystem(); + RNS::FileSystem universal_filesystem = new UniversalFileSystem(); universal_filesystem.init(); RNS::Utilities::OS::register_filesystem(universal_filesystem); // Initialize and register interface - udp_interface = new UDPInterface(); + RNS::Interface udp_interface = new UDPInterface(); udp_interface.mode(RNS::Type::Interface::MODE_GATEWAY); RNS::Transport::register_interface(udp_interface); udp_interface.start(); diff --git a/platformio.ini b/platformio.ini index dc4f634..c7f771a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,6 +29,8 @@ lib_deps = MsgPack@^0.4.2 https://github.com/attermann/Crypto.git test_framework = unity +; Following property enables Unity integration testing +test_build_src = true [env:native] platform = native diff --git a/src/FileSystem.h b/src/FileSystem.h index 41812c4..f224420 100644 --- a/src/FileSystem.h +++ b/src/FileSystem.h @@ -21,6 +21,7 @@ namespace RNS { protected: virtual bool init() { return true; } + virtual void loop() {} virtual bool file_exists(const char* file_path) = 0; virtual size_t read_file(const char* file_path, Bytes& data) = 0; virtual size_t write_file(const char* file_path, const Bytes& data) = 0; @@ -92,6 +93,7 @@ namespace RNS { public: inline bool init() { assert(_impl); return _impl->init(); } + inline void loop() { assert(_impl); return _impl->loop(); } inline bool file_exists(const char* file_path) { assert(_impl); return _impl->file_exists(file_path); } inline size_t read_file(const char* file_path, Bytes& data) { assert(_impl); return _impl->read_file(file_path, data); } inline size_t write_file(const char* file_path, const Bytes& data) { assert(_impl); return _impl->write_file(file_path, data); } diff --git a/src/Reticulum.cpp b/src/Reticulum.cpp index 88c678b..0e49c1b 100644 --- a/src/Reticulum.cpp +++ b/src/Reticulum.cpp @@ -207,11 +207,25 @@ void Reticulum::loop() { assert(_object); if (!_object->_is_connected_to_shared_instance) { + // Perform Reticulum housekeeping if (OS::time() > (_object->_jobs_last_run + JOB_INTERVAL)) { jobs(); _object->_jobs_last_run = OS::time(); } + // Perform interface processing + for (auto& [hash, interface] : Transport::get_interfaces()) { + interface.loop(); + } + + // Perform Filesystem processing + FileSystem& filesystem = OS::get_filesystem(); + if (filesystem) { + filesystem.loop(); + } + + + // Perform Transport processing RNS::Transport::loop(); } // Perform random number gnerator housekeeping diff --git a/src/Transport.h b/src/Transport.h index 231b95d..5150cc6 100644 --- a/src/Transport.h +++ b/src/Transport.h @@ -312,6 +312,7 @@ namespace RNS { static void handle_tunnel(const Bytes& tunnel_id, const Interface& interface); static void register_interface(Interface& interface); static void deregister_interface(const Interface& interface); + inline static const std::map get_interfaces() { return _interfaces; } static void register_destination(Destination& destination); static void deregister_destination(const Destination& destination); static void register_link(Link& link); diff --git a/src/Utilities/OS.h b/src/Utilities/OS.h index aac40db..1316dfc 100644 --- a/src/Utilities/OS.h +++ b/src/Utilities/OS.h @@ -135,6 +135,11 @@ namespace RNS { namespace Utilities { _filesystem = {Type::NONE}; } + inline static FileSystem& get_filesystem() { + return _filesystem; + } + + inline static bool file_exists(const char* file_path) { if (!_filesystem) { WARNING("file_exists: filesystem not registered"); diff --git a/src/main.cpp b/src/main.cpp index ed4a21a..f55c542 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +// Only include if building locally and NOT testing +#if defined(LIBRARY_TEST) && !defined(PIO_UNIT_TESTING) #include #ifdef ARDUINO @@ -14,3 +16,5 @@ int main(void) { } #endif + +#endif diff --git a/test/test_crypto/test_main.cpp b/test/test_crypto/test_main.cpp index 9d1eef0..abd22ec 100644 --- a/test/test_crypto/test_main.cpp +++ b/test/test_crypto/test_main.cpp @@ -25,7 +25,6 @@ void tearDown(void) { // clean stuff up here after each test } -/* void testHMAC() { { @@ -124,7 +123,6 @@ void testPKCS7() { TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len)); } } -*/ void testCrc8() { char data[32]; @@ -148,7 +146,6 @@ void testCrc8() { } -/* void testCrc32() { char data[16]; uint32_t crc; @@ -201,11 +198,15 @@ void testByteCrc32() { } TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc); } -*/ int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(testCrc8); + RUN_TEST(testHMAC); + RUN_TEST(testPKCS7); + RUN_TEST(testCrc8); + RUN_TEST(testCrc32); + RUN_TEST(testIncrementalCrc32); + RUN_TEST(testByteCrc32); return UNITY_END(); } diff --git a/test/test_general/test_main.cpp b/test/test_general/test_main.cpp index be5aa0c..1196290 100644 --- a/test/test_general/test_main.cpp +++ b/test/test_general/test_main.cpp @@ -15,8 +15,12 @@ void tearDown(void) { // clean stuff up here after each test } -#ifndef ARDUINO -unsigned long millis() { +#ifdef ARDUINO +unsigned long test_millis() { + return millis(); +} +#else +unsigned long test_millis() { timeval time; ::gettimeofday(&time, NULL); return (uint64_t)(time.tv_sec * 1000) + (uint64_t)(time.tv_usec / 1000); @@ -35,7 +39,7 @@ uint64_t timeOffset = 0; uint64_t ltime() { // handle roll-over of 32-bit millis (approx. 49 days) static uint32_t low32, high32; - uint32_t new_low32 = millis(); + uint32_t new_low32 = test_millis(); if (new_low32 < low32) high32++; low32 = new_low32; return ((uint64_t)high32 << 32 | low32) + timeOffset; diff --git a/test/test_msgpack/test_main.cpp b/test/test_msgpack/test_main.cpp index c62f18a..468b41c 100644 --- a/test/test_msgpack/test_main.cpp +++ b/test/test_msgpack/test_main.cpp @@ -1,5 +1,9 @@ #include + +#define MSGPACK_DEBUGLOG_ENABLE 0 +#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE #include +#include "Bytes.h" #include #include @@ -168,11 +172,82 @@ void test_msgpack_binary_content(void) { TEST_ASSERT_EQUAL_MEMORY(binaryData2, unpacked_buffer2.data(), 3); } +void test_pack_bytes_array_python() { + static const unsigned char binary[] = { + 0x93, 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, + 0xbe, 0x77, 0xc4, 0x06, 0x66, 0x69, 0x72, 0x73, + 0x74, 0xab, 0xc4, 0x07, 0x73, 0x65, 0x63, 0x6f, + 0x6e, 0x64, 0xcd, + }; + double time = 123.456; + // Test that array containing bytes matches python equivalent + const RNS::Bytes one("first\xab"); + const RNS::Bytes two("second\xcd"); + MsgPack::Packer packer; + packer.to_array(time, one, two); + RNS::Bytes data(packer.data(), packer.size()); + TEST_ASSERT_EQUAL_size_t(27, packer.size()); + TEST_ASSERT_EQUAL_MEMORY(binary, packer.data(), packer.size()); +} + +void test_unpack_bytes_array_python() { + static const unsigned char binary[] = { + 0x93, 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, + 0xbe, 0x77, 0xc4, 0x06, 0x66, 0x69, 0x72, 0x73, + 0x74, 0xab, 0xc4, 0x07, 0x73, 0x65, 0x63, 0x6f, + 0x6e, 0x64, 0xcd, + }; + MsgPack::Unpacker unpacker; + unpacker.feed(binary, sizeof(binary)); + double time; + // CBA NOTE: Can't currently deserialize directly into Bytes + //RNS::Bytes one; + //RNS::Bytes two; + //unpacker.from_array(time, one, two); + MsgPack::bin_t bin_one; + MsgPack::bin_t bin_two; + unpacker.from_array(time, bin_one, bin_two); + RNS::Bytes one(bin_one); + RNS::Bytes two(bin_two); + TEST_ASSERT_EQUAL_FLOAT(123.456, time); + TEST_ASSERT_EQUAL_MEMORY("first\xab", one.data(), one.size()); + TEST_ASSERT_EQUAL_MEMORY("second\xcd", two.data(), two.size()); +} + +void test_pack_float_python() { + static const unsigned char binary[] = { + 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, + 0x77 + }; + double time = 123.456; + MsgPack::Packer packer; + packer.serialize(time); + RNS::Bytes data(packer.data(), packer.size()); + TEST_ASSERT_EQUAL_size_t(9, packer.size()); + TEST_ASSERT_EQUAL_MEMORY(binary, packer.data(), packer.size()); +} + +void test_unpack_float_python() { + static const unsigned char binary[] = { + 0xcb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, + 0x77 + }; + MsgPack::Unpacker unpacker; + unpacker.feed(binary, sizeof(binary)); + double time; + unpacker.deserialize(time); + TEST_ASSERT_EQUAL_FLOAT(123.456, time); +} + int runUnityTests(void) { UNITY_BEGIN(); RUN_TEST(test_msgpack_array_packing); RUN_TEST(test_msgpack_empty_buffer); RUN_TEST(test_msgpack_binary_content); + RUN_TEST(test_pack_bytes_array_python); + RUN_TEST(test_unpack_bytes_array_python); + RUN_TEST(test_pack_float_python); + RUN_TEST(test_unpack_float_python); return UNITY_END(); }