mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-12-06 15:34:49 +00:00
218 lines
11 KiB
C++
218 lines
11 KiB
C++
#include "test.h"
|
|
#include <TxtDataHelpers.h>
|
|
#include <ReplyEchoGuard.h>
|
|
#include <battery_curve.h>
|
|
#include <mesh/MeshCore.h>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
TEST(string_capacity, "UNIT-STRING-001", "String copies terminate and respect zero exact and short capacities") {
|
|
for (size_t size = 0; size <= 10; ++size) {
|
|
std::vector<char> out(size + 2, '#');
|
|
StrHelper::strncpy(out.data() + 1, "abcd", size);
|
|
CHECK(out.front() == '#' && out.back() == '#');
|
|
if (size) CHECK(std::string(out.data() + 1) == std::string("abcd").substr(0, size - 1));
|
|
std::fill(out.begin(), out.end(), '#');
|
|
StrHelper::strzcpy(out.data() + 1, "abcd", size);
|
|
CHECK(out.front() == '#' && out.back() == '#');
|
|
if (size) {
|
|
auto copied = std::min(size_t(4), size - 1);
|
|
CHECK(std::string(out.data() + 1) == std::string("abcd").substr(0, copied));
|
|
for (size_t i = copied; i < size; ++i) CHECK(out[i + 1] == 0);
|
|
}
|
|
}
|
|
}
|
|
TEST(string_normalization, "UNIT-STRING-002", "Decimal trimming and blank classification") {
|
|
const char *input[] = {"250.000", "62.500", "0.0010", "-10.000", "1000", "", "1.25"};
|
|
const char *expected[] = {"250", "62.5", "0.001", "-10", "1000", "", "1.25"};
|
|
for (unsigned i = 0; i < 7; ++i) {
|
|
char buffer[32]; std::strcpy(buffer, input[i]); StrHelper::stripTrailingZeros(buffer);
|
|
CHECK(std::strcmp(buffer, expected[i]) == 0);
|
|
}
|
|
CHECK(StrHelper::isBlank(nullptr) && StrHelper::isBlank("") && StrHelper::isBlank(" \t\r\n"));
|
|
CHECK(!StrHelper::isBlank(" a ") && !StrHelper::isBlank("0"));
|
|
}
|
|
TEST(text_then_padding, "UNIT-STRING-003", "A received text is printable characters followed only by zero padding") {
|
|
auto ok = [](std::vector<uint8_t> bytes, size_t pad = 0) {
|
|
bytes.insert(bytes.end(), pad, 0);
|
|
return StrHelper::isTextThenPadding(bytes.data(), bytes.size());
|
|
};
|
|
auto text = [](const char *s) { return std::vector<uint8_t>(s, s + std::strlen(s)); };
|
|
CHECK(ok(text("get name"), 3));
|
|
CHECK(ok(text("exactly-11!"))); // fills the block: no terminator, no padding
|
|
CHECK(ok(text("set name K\xC3\xA9kes \xF0\x9F\x93\xA1"), 5)); // UTF-8
|
|
CHECK(ok(text("two\nlines\r\n\tindented"), 2));
|
|
CHECK(ok({}, 11) && ok({})); // empty text
|
|
CHECK(!ok(text("get name\x01"), 2)); // control character inside the text
|
|
CHECK(!ok(text("get name\x1B[0m"), 2));
|
|
CHECK(!ok(text("get name\xFF"), 2));
|
|
std::vector<uint8_t> tail = text("get name"); // bytes after the terminator
|
|
tail.insert(tail.end(), {0, 0x01, 0x11, 0x22});
|
|
CHECK(!ok(tail, 4));
|
|
tail = text("get name");
|
|
tail.insert(tail.end(), {0, 0, 0, 0xFF});
|
|
CHECK(!ok(tail));
|
|
CHECK(!ok({0, 0x01}, 9)); // empty text, then data
|
|
// A plain text may carry its attempt number (4 and up) right after the terminator.
|
|
auto plain = [](std::vector<uint8_t> bytes, size_t pad = 0) {
|
|
bytes.insert(bytes.end(), pad, 0);
|
|
return StrHelper::isTextThenPadding(bytes.data(), bytes.size(), true);
|
|
};
|
|
std::vector<uint8_t> retry = text("hello");
|
|
retry.insert(retry.end(), {0, 4});
|
|
CHECK(plain(retry, 9) && plain(retry) && !ok(retry, 9));
|
|
retry.back() = 254;
|
|
CHECK(plain(retry, 9));
|
|
for (uint8_t bad : {uint8_t(1), uint8_t(3), uint8_t(0xFF)}) { // not attempt numbers
|
|
retry.back() = bad;
|
|
CHECK(!plain(retry, 9));
|
|
}
|
|
retry.back() = 7;
|
|
retry.push_back(0x55); // anything after the attempt number
|
|
CHECK(!plain(retry, 8));
|
|
CHECK(plain(text("hello"), 11) && !plain(text("hello\x01"), 2));
|
|
// The padding hint of zcpfs builds: NUL, attempt number or zero, 0x5A, then zeros.
|
|
std::vector<uint8_t> hint = text("hello");
|
|
hint.insert(hint.end(), {0, 0, 0x5A});
|
|
CHECK(plain(hint) && plain(hint, 8) && !ok(hint, 8));
|
|
hint[hint.size() - 2] = 9; // a late retry with the hint
|
|
CHECK(plain(hint, 8));
|
|
for (uint8_t bad : {uint8_t(1), uint8_t(3), uint8_t(0xFF)}) {
|
|
hint[hint.size() - 2] = bad;
|
|
CHECK(!plain(hint, 8));
|
|
}
|
|
hint[hint.size() - 2] = 0;
|
|
hint.push_back(0x01); // anything after the hint
|
|
CHECK(!plain(hint, 7));
|
|
std::vector<uint8_t> not_hint = text("hello");
|
|
not_hint.insert(not_hint.end(), {0, 0, 0x5B});
|
|
CHECK(!plain(not_hint, 8));
|
|
not_hint = text("hello");
|
|
not_hint.insert(not_hint.end(), {0, 0x5A}); // the hint byte where the attempt number goes: an attempt number
|
|
CHECK(plain(not_hint, 8));
|
|
}
|
|
TEST(login_password_field, "UNIT-STRING-004", "A path return whose path reaches the password field is not a login") {
|
|
// A login is [timestamp 4][password] (repeater) or [timestamp 4][sync_since 4][password] (room),
|
|
// zero padded to the AES block. Genuine ones pass.
|
|
auto login = [](std::vector<uint8_t> head, const char *pw) {
|
|
std::vector<uint8_t> pt = head;
|
|
pt.insert(pt.end(), pw, pw + std::strlen(pw));
|
|
pt.resize((pt.size() + 15) / 16 * 16, 0);
|
|
return pt;
|
|
};
|
|
for (const char *pw : {"", "password", "fifteen-chars!!"}) {
|
|
auto rep = login({1, 2, 3, 0x6A}, pw);
|
|
auto room = login({1, 2, 3, 0x6A, 9, 9, 9, 9}, pw);
|
|
CHECK(StrHelper::isTextThenPadding(&rep[4], rep.size() - 4));
|
|
CHECK(StrHelper::isTextThenPadding(&room[8], room.size() - 8));
|
|
}
|
|
// A path return is [path_len][path][extra_type][extra]. Whatever the attacker writes into the
|
|
// echoed path, the non-zero extra_type lands in or after the password field and fails it.
|
|
for (size_t offset : {size_t(4), size_t(8)}) {
|
|
for (size_t n = offset - 1; n <= 63; ++n) {
|
|
for (uint8_t extra_type : {uint8_t(0x01), uint8_t(0x03), uint8_t(0xFF)}) {
|
|
for (size_t nul = offset; nul <= 1 + n; ++nul) { // where the attacker ends the password
|
|
std::vector<uint8_t> pt(1 + n, 'a');
|
|
pt[0] = uint8_t(n);
|
|
for (size_t i = nul; i < pt.size(); ++i) pt[i] = 0;
|
|
pt.push_back(extra_type);
|
|
pt.insert(pt.end(), 4, 0); // extra: worst case all zero
|
|
pt.resize((pt.size() + 15) / 16 * 16, 0);
|
|
CHECK(!StrHelper::isTextThenPadding(&pt[offset], pt.size() - offset));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// A shorter path is not caught here: with a 4-byte extra (a reciprocal path, an ACK) the
|
|
// plaintext ends before the room's password field, which then reads as a blank password.
|
|
// The room's blank re-login therefore must not move last_timestamp (LLD 07 section 28).
|
|
for (size_t n = 0; n <= 2; ++n) {
|
|
std::vector<uint8_t> pt(1 + n, 0xEE);
|
|
pt[0] = uint8_t(n);
|
|
pt.push_back(0xFF);
|
|
pt.insert(pt.end(), 4, 0xA5);
|
|
pt.resize(16, 0);
|
|
CHECK(StrHelper::isTextThenPadding(&pt[8], pt.size() - 8));
|
|
}
|
|
}
|
|
TEST(keep_alive_padding, "UNIT-STRING-005", "A room keep-alive is 9 bytes, then zero padding") {
|
|
// Genuine: [timestamp 4][0x02][sync_since 4], zero padded to the AES block.
|
|
std::vector<uint8_t> ka = {1, 2, 3, 0x6A, 0x02, 9, 9, 9, 9};
|
|
ka.resize(16, 0);
|
|
CHECK(StrHelper::isZeroPadding(&ka[9], ka.size() - 9));
|
|
CHECK(StrHelper::isZeroPadding(ka.data(), 0));
|
|
// A path return read as a keep-alive: once the path reaches byte 4 (the request type) the
|
|
// extra_type, or the extra after it, lands at byte 9 or later.
|
|
for (size_t n = 8; n <= 63; ++n) {
|
|
for (uint8_t extra_type : {uint8_t(0x01), uint8_t(0x03), uint8_t(0xFF)}) {
|
|
std::vector<uint8_t> pt(1 + n, 0);
|
|
pt[0] = uint8_t(n);
|
|
pt[4] = 0x02;
|
|
pt.push_back(extra_type);
|
|
pt.insert(pt.end(), 4, 0); // extra: worst case all zero
|
|
pt.resize((pt.size() + 15) / 16 * 16, 0);
|
|
CHECK(!StrHelper::isZeroPadding(&pt[9], pt.size() - 9));
|
|
}
|
|
}
|
|
for (size_t n = 4; n <= 7; ++n) { // shorter: the extra's bytes land there
|
|
std::vector<uint8_t> pt(1 + n, 0);
|
|
pt[0] = uint8_t(n);
|
|
pt[4] = 0x02;
|
|
pt.push_back(0xFF);
|
|
pt.insert(pt.end(), 4, 0xA5);
|
|
pt.resize(16, 0);
|
|
CHECK(!StrHelper::isZeroPadding(&pt[9], pt.size() - 9));
|
|
}
|
|
}
|
|
TEST(reply_echo_guard, "UNIT-STRING-006", "A server's own CLI reply is recognised when it comes back") {
|
|
ReplyEchoGuard guard;
|
|
auto reply = [](uint32_t ts, const char *text) {
|
|
std::vector<uint8_t> pt(5);
|
|
std::memcpy(pt.data(), &ts, 4);
|
|
pt[4] = 1 << 2; // TXT_TYPE_CLI_DATA
|
|
pt.insert(pt.end(), text, text + std::strlen(text));
|
|
return pt;
|
|
};
|
|
auto first = reply(1000, "password now: hunter2");
|
|
CHECK(!guard.matches(first.data(), first.size())); // nothing sent yet
|
|
guard.note(first.data(), first.size());
|
|
CHECK(guard.matches(first.data(), first.size()));
|
|
auto other_time = reply(1001, "password now: hunter2"); // the admin typing the same text
|
|
auto other_text = reply(1000, "password now: hunter3");
|
|
CHECK(!guard.matches(other_time.data(), other_time.size()));
|
|
CHECK(!guard.matches(other_text.data(), other_text.size()));
|
|
for (uint32_t i = 0; i < ReplyEchoGuard::SLOTS - 1; ++i) { // still held with the table full
|
|
auto r = reply(2000 + i, "OK");
|
|
guard.note(r.data(), r.size());
|
|
}
|
|
CHECK(guard.matches(first.data(), first.size()));
|
|
auto last = reply(3000, "OK");
|
|
guard.note(last.data(), last.size()); // the oldest is replaced
|
|
CHECK(!guard.matches(first.data(), first.size()) && guard.matches(last.data(), last.size()));
|
|
}
|
|
TEST(rtc_unique, "UNIT-RTC-001", "Unique timestamps survive repeated and backward wall time") {
|
|
struct RTC : mesh::RTCClock {
|
|
uint32_t now = 100;
|
|
uint32_t getCurrentTime() override { return now; }
|
|
void setCurrentTime(uint32_t value) override { now = value; }
|
|
} rtc;
|
|
CHECK(rtc.getCurrentTimeUnique() == 100); CHECK(rtc.getCurrentTimeUnique() == 101);
|
|
rtc.now = 50; CHECK(rtc.getCurrentTimeUnique() == 102);
|
|
rtc.now = 200; CHECK(rtc.getCurrentTimeUnique() == 200);
|
|
}
|
|
TEST(battery_curve, "UNIT-BATTERY-001", "Battery endpoints interpolation multicell scaling and monotonicity") {
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 0) == 0);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 3100) == 0);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 4190) == 100);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 3720) == 50);
|
|
const uint16_t points[] = {4200, 3600, 3000};
|
|
battery_curve_t twoCell{points, 3, 2};
|
|
CHECK(battery_curve_lookup(&twoCell, 7800) == 75);
|
|
CHECK(battery_curve_lookup(&twoCell, 6600) == 25);
|
|
unsigned previous = 0;
|
|
for (unsigned mv = 0; mv <= 65535; ++mv) {
|
|
auto percent = battery_curve_lookup(&battery_curve_default, mv);
|
|
CHECK(percent >= previous && percent <= 100); previous = percent;
|
|
}
|
|
}
|