WIP: Migrated a few unit tests to Unity

This commit is contained in:
Chad Attermann committed 2025-08-26 10:49:37 -06:00
1 parent bac53b9e6d
commit fe687d391b
10 files changed
+120 -18

No files matched your search

+4 -10
View File
@@ -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();
+2
View File
@@ -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
+2
View File
@@ -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); }
+14
View File
@@ -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
+1
View File
@@ -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<Bytes, Interface&> get_interfaces() { return _interfaces; }
static void register_destination(Destination& destination);
static void deregister_destination(const Destination& destination);
static void register_link(Link& link);
+5
View File
@@ -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");
+4
View File
@@ -1,3 +1,5 @@
// Only include if building locally and NOT testing
#if defined(LIBRARY_TEST) && !defined(PIO_UNIT_TESTING)
#include <stdio.h>
#ifdef ARDUINO
@@ -14,3 +16,5 @@ int main(void) {
}
#endif
#endif
+6 -5
View File
@@ -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();
}
+7 -3
View File
@@ -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;
+75
View File
@@ -1,5 +1,9 @@
#include <unity.h>
#define MSGPACK_DEBUGLOG_ENABLE 0
#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
#include <MsgPack.h>
#include "Bytes.h"
#include <string.h>
#include <stdint.h>
@@ -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<uint8_t> bin_one;
MsgPack::bin_t<uint8_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();
}