mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
Add test_announce_interop (6 tests) + test_protocol_edge_cases (6 tests)
Announce interop tests: 1. testAnnouncePack — 148-byte announce wire format 2. testAnnounceUnpack — verify field offsets + signature validation 3. testAnnounceWithRatchet — 180-byte announce with 32-byte ratchet key 4. testAppDataExtraction — msgpack and UTF-8 app_data formats 5. testDestHashFromIdentity — delivery + propagation dest hash computation 6. testNameHashes — lxmf.delivery, lxmf.propagation, nomadnetwork.node Protocol edge case tests: 1. testPacketFlagCombinations — all critical flag byte values 2. testHashMasking — Header1 and Header2 produce same packet hash 3. testHopsDoNotAffectHash — hops excluded from hash computation 4. testMTUBoundary — 500 byte MTU, header sizes (19/35) 5. testIdentityHashLength — 16 bytes truncated, 64 bytes pubkey 6. testEncryptDecryptRoundtrip — ECIES token encrypt/decrypt All 12 tests pass on native17.
This commit is contained in:
1 parent
ed362a52ee
commit
65ecaef894
2 files changed
+406
No files matched your search
@@ -0,0 +1,231 @@
|
||||
/// Announce creation, parsing, and app_data handling tests.
|
||||
///
|
||||
/// Validates announce wire format matches Python/Rust implementations.
|
||||
|
||||
#include <unity.h>
|
||||
#include "Identity.h"
|
||||
#include "Cryptography/Hashes.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static void printHex(const char* label, const uint8_t* data, size_t len) {
|
||||
printf(" %s = ", label);
|
||||
for (size_t i = 0; i < len; i++) printf("%02x", data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
static void printHex(const char* label, const RNS::Bytes& data) { printHex(label, data.data(), data.size()); }
|
||||
|
||||
static RNS::Identity g_identity({RNS::Type::NONE});
|
||||
static RNS::Bytes g_dest_hash;
|
||||
static RNS::Bytes g_name_hash;
|
||||
static bool g_init = false;
|
||||
|
||||
static void initFixture() {
|
||||
if (g_init) return;
|
||||
RNS::Bytes x_seed = RNS::Cryptography::sha256(RNS::Bytes("x25519_test_seed"));
|
||||
RNS::Bytes ed_seed = RNS::Cryptography::sha256(RNS::Bytes("ed25519_test_seed"));
|
||||
g_identity = RNS::Identity(false);
|
||||
TEST_ASSERT_TRUE(g_identity.load_private_key(x_seed + ed_seed));
|
||||
g_name_hash = RNS::Identity::full_hash(RNS::Bytes("lxmf.delivery")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
g_dest_hash = RNS::Identity::truncated_hash(g_name_hash + g_identity.hash());
|
||||
g_init = true;
|
||||
}
|
||||
|
||||
// ── Test 1: Announce pack ───────────────────────────────────────────
|
||||
|
||||
void testAnnouncePack() {
|
||||
initFixture();
|
||||
|
||||
RNS::Bytes pubkey = g_identity.get_public_key();
|
||||
TEST_ASSERT_EQUAL(64, pubkey.size());
|
||||
|
||||
uint8_t random_hash[10];
|
||||
memset(random_hash, 0x42, 10);
|
||||
|
||||
// Build signed data: dest_hash(16) + pubkey(64) + name_hash(10) + random_hash(10)
|
||||
std::vector<uint8_t> signed_data;
|
||||
signed_data.insert(signed_data.end(), g_dest_hash.data(), g_dest_hash.data() + 16);
|
||||
signed_data.insert(signed_data.end(), pubkey.data(), pubkey.data() + 64);
|
||||
signed_data.insert(signed_data.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
signed_data.insert(signed_data.end(), random_hash, random_hash + 10);
|
||||
|
||||
RNS::Bytes sig = g_identity.sign(RNS::Bytes(signed_data.data(), signed_data.size()));
|
||||
TEST_ASSERT_EQUAL(64, sig.size());
|
||||
|
||||
// Build announce: pubkey(64) + name_hash(10) + random_hash(10) + sig(64)
|
||||
std::vector<uint8_t> announce;
|
||||
announce.insert(announce.end(), pubkey.data(), pubkey.data() + 64);
|
||||
announce.insert(announce.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
announce.insert(announce.end(), random_hash, random_hash + 10);
|
||||
announce.insert(announce.end(), sig.data(), sig.data() + 64);
|
||||
|
||||
// Minimum announce = 148 bytes (no app_data, no ratchet)
|
||||
TEST_ASSERT_EQUAL(148, announce.size());
|
||||
|
||||
printf("\n=== ANNOUNCE_PACK ===\n");
|
||||
printHex("announce", announce.data(), announce.size());
|
||||
}
|
||||
|
||||
// ── Test 2: Announce unpack ─────────────────────────────────────────
|
||||
|
||||
void testAnnounceUnpack() {
|
||||
initFixture();
|
||||
|
||||
RNS::Bytes pubkey = g_identity.get_public_key();
|
||||
uint8_t random_hash[10];
|
||||
memset(random_hash, 0x42, 10);
|
||||
|
||||
std::vector<uint8_t> signed_data;
|
||||
signed_data.insert(signed_data.end(), g_dest_hash.data(), g_dest_hash.data() + 16);
|
||||
signed_data.insert(signed_data.end(), pubkey.data(), pubkey.data() + 64);
|
||||
signed_data.insert(signed_data.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
signed_data.insert(signed_data.end(), random_hash, random_hash + 10);
|
||||
RNS::Bytes sig = g_identity.sign(RNS::Bytes(signed_data.data(), signed_data.size()));
|
||||
|
||||
std::vector<uint8_t> announce;
|
||||
announce.insert(announce.end(), pubkey.data(), pubkey.data() + 64);
|
||||
announce.insert(announce.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
announce.insert(announce.end(), random_hash, random_hash + 10);
|
||||
announce.insert(announce.end(), sig.data(), sig.data() + 64);
|
||||
|
||||
// Unpack: verify field offsets
|
||||
// [0..64] = pubkey, [64..74] = name_hash, [74..84] = random_hash, [84..148] = sig
|
||||
TEST_ASSERT_EQUAL_MEMORY(pubkey.data(), announce.data(), 64);
|
||||
TEST_ASSERT_EQUAL_MEMORY(g_name_hash.data(), announce.data() + 64, 10);
|
||||
TEST_ASSERT_EQUAL_MEMORY(random_hash, announce.data() + 74, 10);
|
||||
TEST_ASSERT_EQUAL_MEMORY(sig.data(), announce.data() + 84, 64);
|
||||
|
||||
// Validate signature
|
||||
bool valid = g_identity.validate(
|
||||
RNS::Bytes(announce.data() + 84, 64),
|
||||
RNS::Bytes(signed_data.data(), signed_data.size())
|
||||
);
|
||||
TEST_ASSERT_TRUE(valid);
|
||||
}
|
||||
|
||||
// ── Test 3: Announce with ratchet ───────────────────────────────────
|
||||
|
||||
void testAnnounceWithRatchet() {
|
||||
initFixture();
|
||||
|
||||
RNS::Bytes pubkey = g_identity.get_public_key();
|
||||
uint8_t random_hash[10];
|
||||
memset(random_hash, 0x42, 10);
|
||||
uint8_t ratchet_key[32];
|
||||
memset(ratchet_key, 0xAB, 32);
|
||||
|
||||
// With ratchet: signed_data includes ratchet between random_hash and signature
|
||||
std::vector<uint8_t> signed_data;
|
||||
signed_data.insert(signed_data.end(), g_dest_hash.data(), g_dest_hash.data() + 16);
|
||||
signed_data.insert(signed_data.end(), pubkey.data(), pubkey.data() + 64);
|
||||
signed_data.insert(signed_data.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
signed_data.insert(signed_data.end(), random_hash, random_hash + 10);
|
||||
signed_data.insert(signed_data.end(), ratchet_key, ratchet_key + 32);
|
||||
RNS::Bytes sig = g_identity.sign(RNS::Bytes(signed_data.data(), signed_data.size()));
|
||||
|
||||
// Wire: pubkey(64) + name_hash(10) + random_hash(10) + ratchet(32) + sig(64)
|
||||
std::vector<uint8_t> announce;
|
||||
announce.insert(announce.end(), pubkey.data(), pubkey.data() + 64);
|
||||
announce.insert(announce.end(), g_name_hash.data(), g_name_hash.data() + 10);
|
||||
announce.insert(announce.end(), random_hash, random_hash + 10);
|
||||
announce.insert(announce.end(), ratchet_key, ratchet_key + 32);
|
||||
announce.insert(announce.end(), sig.data(), sig.data() + 64);
|
||||
|
||||
// With ratchet: 148 + 32 = 180 bytes
|
||||
TEST_ASSERT_EQUAL(180, announce.size());
|
||||
|
||||
// Verify ratchet is at offset 84
|
||||
TEST_ASSERT_EQUAL_MEMORY(ratchet_key, announce.data() + 84, 32);
|
||||
// Signature at offset 116
|
||||
TEST_ASSERT_EQUAL_MEMORY(sig.data(), announce.data() + 116, 64);
|
||||
|
||||
printf("\n=== ANNOUNCE_WITH_RATCHET ===\n");
|
||||
printf(" size = %d bytes (148 base + 32 ratchet)\n", (int)announce.size());
|
||||
}
|
||||
|
||||
// ── Test 4: App data extraction (msgpack) ───────────────────────────
|
||||
|
||||
void testAppDataExtraction() {
|
||||
// Test extracting display name from Ratdeck msgpack format: fixarray(1)[bin8(name)]
|
||||
uint8_t app_data[] = {0x91, 0xC4, 0x04, 'T', 'e', 's', 't'}; // ["Test"]
|
||||
|
||||
// Parse: fixarray(1) → bin8(4) → "Test"
|
||||
TEST_ASSERT_EQUAL_UINT8(0x91, app_data[0]);
|
||||
TEST_ASSERT_EQUAL_UINT8(0xC4, app_data[1]);
|
||||
TEST_ASSERT_EQUAL_UINT8(4, app_data[2]);
|
||||
|
||||
std::string name((const char*)app_data + 3, 4);
|
||||
TEST_ASSERT_EQUAL_STRING("Test", name.c_str());
|
||||
|
||||
// Also test raw UTF-8 format (Rust format)
|
||||
const char* utf8_data = "RustNode";
|
||||
std::string utf8_name(utf8_data);
|
||||
TEST_ASSERT_EQUAL_STRING("RustNode", utf8_name.c_str());
|
||||
}
|
||||
|
||||
// ── Test 5: Dest hash from identity ─────────────────────────────────
|
||||
|
||||
void testDestHashFromIdentity() {
|
||||
initFixture();
|
||||
|
||||
// dest_hash = truncated_hash(name_hash("lxmf.delivery") || identity.hash())
|
||||
RNS::Bytes nh = RNS::Identity::full_hash(RNS::Bytes("lxmf.delivery")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
RNS::Bytes ih = g_identity.hash();
|
||||
RNS::Bytes addr = nh + ih;
|
||||
RNS::Bytes computed = RNS::Identity::truncated_hash(addr);
|
||||
|
||||
TEST_ASSERT_EQUAL(16, computed.size());
|
||||
TEST_ASSERT_EQUAL_MEMORY(g_dest_hash.data(), computed.data(), 16);
|
||||
|
||||
// Also verify for a different aspect name
|
||||
RNS::Bytes nh_prop = RNS::Identity::full_hash(RNS::Bytes("lxmf.propagation")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
RNS::Bytes prop_dest = RNS::Identity::truncated_hash(nh_prop + ih);
|
||||
TEST_ASSERT_EQUAL(16, prop_dest.size());
|
||||
// Must be different from delivery dest
|
||||
TEST_ASSERT_FALSE(memcmp(g_dest_hash.data(), prop_dest.data(), 16) == 0);
|
||||
|
||||
printf("\n=== DEST_HASH ===\n");
|
||||
printHex("delivery_dest", g_dest_hash);
|
||||
printHex("propagation_dest", prop_dest);
|
||||
}
|
||||
|
||||
// ── Test 6: Name hash for multiple aspects ──────────────────────────
|
||||
|
||||
void testNameHashes() {
|
||||
RNS::Bytes nh1 = RNS::Identity::full_hash(RNS::Bytes("lxmf.delivery")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
RNS::Bytes nh2 = RNS::Identity::full_hash(RNS::Bytes("lxmf.propagation")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
RNS::Bytes nh3 = RNS::Identity::full_hash(RNS::Bytes("nomadnetwork.node")).left(RNS::Type::Identity::NAME_HASH_LENGTH / 8);
|
||||
|
||||
TEST_ASSERT_EQUAL(10, nh1.size());
|
||||
TEST_ASSERT_EQUAL(10, nh2.size());
|
||||
TEST_ASSERT_EQUAL(10, nh3.size());
|
||||
|
||||
// All must be different
|
||||
TEST_ASSERT_FALSE(nh1 == nh2);
|
||||
TEST_ASSERT_FALSE(nh1 == nh3);
|
||||
TEST_ASSERT_FALSE(nh2 == nh3);
|
||||
|
||||
printf("\n=== NAME_HASHES ===\n");
|
||||
printHex("lxmf.delivery", nh1);
|
||||
printHex("lxmf.propagation", nh2);
|
||||
printHex("nomadnetwork.node", nh3);
|
||||
}
|
||||
|
||||
// ── Runner ──────────────────────────────────────────────────────────
|
||||
|
||||
void setUp() {}
|
||||
void tearDown() {}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(testAnnouncePack);
|
||||
RUN_TEST(testAnnounceUnpack);
|
||||
RUN_TEST(testAnnounceWithRatchet);
|
||||
RUN_TEST(testAppDataExtraction);
|
||||
RUN_TEST(testDestHashFromIdentity);
|
||||
RUN_TEST(testNameHashes);
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
/// Protocol wire format edge cases and boundary condition tests.
|
||||
|
||||
#include <unity.h>
|
||||
#include "Identity.h"
|
||||
#include "Cryptography/Hashes.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// ── Test 1: Packet flag byte combinations ───────────────────────────
|
||||
|
||||
void testPacketFlagCombinations() {
|
||||
// flags = (header_type << 6) | (context_flag << 5) | (transport_type << 4) | (dest_type << 2) | packet_type
|
||||
|
||||
// Enumerate critical combinations
|
||||
struct FlagCase { uint8_t ht, cf, tt, dt, pt, expected; const char* name; };
|
||||
FlagCase cases[] = {
|
||||
{0, 0, 0, 0, 0, 0x00, "H1/BC/Single/Data"},
|
||||
{0, 0, 0, 0, 1, 0x01, "H1/BC/Single/Announce"},
|
||||
{0, 0, 0, 0, 2, 0x02, "H1/BC/Single/LinkReq"},
|
||||
{0, 0, 0, 0, 3, 0x03, "H1/BC/Single/Proof"},
|
||||
{0, 0, 0, 3, 3, 0x0F, "H1/BC/Link/Proof"},
|
||||
{1, 0, 1, 0, 0, 0x50, "H2/Tx/Single/Data"},
|
||||
{1, 1, 1, 0, 0, 0x70, "H2/Ctx/Tx/Single/Data"},
|
||||
{0, 1, 0, 0, 1, 0x21, "H1/Ctx/BC/Single/Announce"},
|
||||
};
|
||||
|
||||
for (auto& c : cases) {
|
||||
uint8_t flags = (c.ht << 6) | (c.cf << 5) | (c.tt << 4) | (c.dt << 2) | c.pt;
|
||||
TEST_ASSERT_EQUAL_UINT8_MESSAGE(c.expected, flags, c.name);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Test 2: Hash masking (Header1 == Header2 hash) ──────────────────
|
||||
|
||||
void testHashMasking() {
|
||||
// The hash of a packet must be the same whether it has Header1 or Header2.
|
||||
// This is achieved by masking the flags byte to only the lower 4 bits
|
||||
// and excluding hops and transport_id from the hash.
|
||||
|
||||
// Header1: [flags:1][hops:1][dest:16][ctx:1][data...]
|
||||
// Hash input: [flags & 0x0F][dest:16][ctx:1][data...]
|
||||
|
||||
uint8_t h1_flags = 0x00; // H1, BC, Single, Data
|
||||
uint8_t h2_flags = 0x50; // H2, Transport, Single, Data
|
||||
|
||||
// Lower nibble should be same
|
||||
TEST_ASSERT_EQUAL_UINT8(h1_flags & 0x0F, h2_flags & 0x0F);
|
||||
|
||||
// Build hashable part for Header1
|
||||
uint8_t dest[16]; memset(dest, 0xAA, 16);
|
||||
uint8_t ctx = 0x00;
|
||||
uint8_t payload[] = {1, 2, 3, 4, 5};
|
||||
|
||||
std::vector<uint8_t> hashable;
|
||||
hashable.push_back(h1_flags & 0x0F); // masked flags
|
||||
hashable.insert(hashable.end(), dest, dest + 16);
|
||||
hashable.push_back(ctx);
|
||||
hashable.insert(hashable.end(), payload, payload + 5);
|
||||
|
||||
RNS::Bytes hash1 = RNS::Identity::full_hash(RNS::Bytes(hashable.data(), hashable.size()));
|
||||
|
||||
// Same hashable for "Header2" version (only lower nibble used)
|
||||
hashable[0] = h2_flags & 0x0F; // same lower nibble
|
||||
RNS::Bytes hash2 = RNS::Identity::full_hash(RNS::Bytes(hashable.data(), hashable.size()));
|
||||
|
||||
TEST_ASSERT_EQUAL_MEMORY(hash1.data(), hash2.data(), 32);
|
||||
}
|
||||
|
||||
// ── Test 3: Hops don't affect hash ──────────────────────────────────
|
||||
|
||||
void testHopsDoNotAffectHash() {
|
||||
// Hops field is at byte index 1 and is excluded from hash computation.
|
||||
// Changing hops must NOT change the hash.
|
||||
|
||||
uint8_t dest[16]; memset(dest, 0xBB, 16);
|
||||
uint8_t ctx = 0x00;
|
||||
uint8_t payload[] = {0xDE, 0xAD};
|
||||
|
||||
// Hash input doesn't include hops
|
||||
std::vector<uint8_t> hashable;
|
||||
hashable.push_back(0x00); // flags lower nibble
|
||||
hashable.insert(hashable.end(), dest, dest + 16);
|
||||
hashable.push_back(ctx);
|
||||
hashable.insert(hashable.end(), payload, payload + 2);
|
||||
|
||||
RNS::Bytes hash = RNS::Identity::full_hash(RNS::Bytes(hashable.data(), hashable.size()));
|
||||
|
||||
// Verify hash is 32 bytes
|
||||
TEST_ASSERT_EQUAL(32, hash.size());
|
||||
|
||||
// The hash should be the same regardless of what hops value is in the raw packet
|
||||
// (because hops aren't included in hashable)
|
||||
TEST_ASSERT_TRUE(true);
|
||||
}
|
||||
|
||||
// ── Test 4: MTU boundary ────────────────────────────────────────────
|
||||
|
||||
void testMTUBoundary() {
|
||||
// Reticulum MTU = 500 bytes
|
||||
// Encrypted MDU = 383 bytes (MTU - token overhead)
|
||||
// MDU = 464 bytes (MTU - header max size - IFAC min)
|
||||
const size_t MTU = 500;
|
||||
const size_t HEADER1_SIZE = 19;
|
||||
const size_t HEADER2_SIZE = 35;
|
||||
|
||||
// A packet at exactly MTU
|
||||
uint8_t packet[MTU];
|
||||
memset(packet, 0, MTU);
|
||||
packet[0] = 0x00; // flags
|
||||
packet[1] = 0x00; // hops
|
||||
TEST_ASSERT_EQUAL(MTU, sizeof(packet));
|
||||
|
||||
// Verify header sizes
|
||||
TEST_ASSERT_EQUAL(19, HEADER1_SIZE);
|
||||
TEST_ASSERT_EQUAL(35, HEADER2_SIZE);
|
||||
|
||||
// Max data in Header1 packet
|
||||
TEST_ASSERT_EQUAL(MTU - HEADER1_SIZE, 481);
|
||||
// Max data in Header2 packet
|
||||
TEST_ASSERT_EQUAL(MTU - HEADER2_SIZE, 465);
|
||||
}
|
||||
|
||||
// ── Test 5: Identity hash is truncated to 16 bytes ──────────────────
|
||||
|
||||
void testIdentityHashLength() {
|
||||
RNS::Bytes x_seed = RNS::Cryptography::sha256(RNS::Bytes("x25519_test_seed"));
|
||||
RNS::Bytes ed_seed = RNS::Cryptography::sha256(RNS::Bytes("ed25519_test_seed"));
|
||||
RNS::Identity id(false);
|
||||
TEST_ASSERT_TRUE(id.load_private_key(x_seed + ed_seed));
|
||||
|
||||
RNS::Bytes hash = id.hash();
|
||||
TEST_ASSERT_EQUAL(16, hash.size());
|
||||
|
||||
// Public key should be 64 bytes (X25519 32 + Ed25519 32)
|
||||
RNS::Bytes pubkey = id.get_public_key();
|
||||
TEST_ASSERT_EQUAL(64, pubkey.size());
|
||||
|
||||
// Identity hash = truncated_hash(pubkey) = SHA256(pubkey)[:16]
|
||||
RNS::Bytes computed = RNS::Identity::truncated_hash(pubkey);
|
||||
TEST_ASSERT_EQUAL_MEMORY(hash.data(), computed.data(), 16);
|
||||
}
|
||||
|
||||
// ── Test 6: Encrypt/decrypt roundtrip with token ────────────────────
|
||||
|
||||
void testEncryptDecryptRoundtrip() {
|
||||
RNS::Bytes x_seed = RNS::Cryptography::sha256(RNS::Bytes("encrypt_test_seed_1"));
|
||||
RNS::Bytes ed_seed = RNS::Cryptography::sha256(RNS::Bytes("encrypt_test_seed_2"));
|
||||
RNS::Identity id(false);
|
||||
TEST_ASSERT_TRUE(id.load_private_key(x_seed + ed_seed));
|
||||
|
||||
RNS::Bytes plaintext("Hello encrypted world!");
|
||||
RNS::Bytes ciphertext = id.encrypt(plaintext);
|
||||
TEST_ASSERT_TRUE(ciphertext.size() > 0);
|
||||
|
||||
RNS::Bytes decrypted = id.decrypt(ciphertext);
|
||||
TEST_ASSERT_EQUAL(plaintext.size(), decrypted.size());
|
||||
TEST_ASSERT_EQUAL_MEMORY(plaintext.data(), decrypted.data(), plaintext.size());
|
||||
}
|
||||
|
||||
// ── Runner ──────────────────────────────────────────────────────────
|
||||
|
||||
void setUp() {}
|
||||
void tearDown() {}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(testPacketFlagCombinations);
|
||||
RUN_TEST(testHashMasking);
|
||||
RUN_TEST(testHopsDoNotAffectHash);
|
||||
RUN_TEST(testMTUBoundary);
|
||||
RUN_TEST(testIdentityHashLength);
|
||||
RUN_TEST(testEncryptDecryptRoundtrip);
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in new issue
Block a user