The firmware dropped every received ITS message over 494 bytes (one link message), so a road RSU's 888-byte SPATEM and 1198-byte MAPEM never reached the phone. V2X_RX now goes out in link fragments up to 2300 bytes, best effort and non-blocking; messages up to 494 bytes are unchanged. The app's SPATEM decoder then refused those messages because they carry intersection and movement names and advisory speed lists. It now reads past both, checked against asn1tools on 718 distinct messages. Adds the pure fragmenter with host tests and a pcap replay, a Kotlin cross-check on the firmware's own fragments, tools/rx_size_compare.py, docs/08-large-message-reception.md and station-link revision 3.
670 lines
33 KiB
C++
670 lines
33 KiB
C++
// Host unit tests of the obu-firmware logic that does not need an ESP32: the station-link codec
|
|
// (byte-exact against the app's StationLinkTest and docs/station-link.tex), request reassembly, the
|
|
// V2X_RX extension, and the pure policies (radio operating points, BLE TX guard, ITS clock, CCA, BLE
|
|
// security, battery). Everything is compiled from ../../main unmodified. Build and run: `make firmware`.
|
|
//
|
|
// What this cannot test: anything that touches the radio, BLE, NVS or the USB peripheral. Those are on
|
|
// the bench checklist in TODO.md.
|
|
#include "ble_security_policy.hpp"
|
|
#include "battery_policy.hpp"
|
|
#include "cca_window.hpp"
|
|
#include "its_clock.hpp"
|
|
#include "link_fragment.hpp"
|
|
#include "link_protocol.hpp"
|
|
#include "radio_policy.hpp"
|
|
#include "raw_capture_policy.hpp"
|
|
#include "request_reassembly.hpp"
|
|
#include "tx_policy.hpp"
|
|
#include "v2x_rx.hpp"
|
|
|
|
extern "C" {
|
|
#include "dot11p.h"
|
|
#include "geonet.h"
|
|
}
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace microbu;
|
|
using link::Bytes;
|
|
|
|
// ---- a very small test framework -------------------------------------------------------------
|
|
struct TestCase { const char* name; std::function<void()> fn; };
|
|
static std::vector<TestCase>& registry() { static std::vector<TestCase> r; return r; }
|
|
static int failed_checks = 0;
|
|
static std::vector<std::string> failures;
|
|
struct Register { Register(const char* n, std::function<void()> f) { registry().push_back({n, std::move(f)}); } };
|
|
#define TEST(name) static void name(); static Register reg_##name(#name, name); static void name()
|
|
#define CHECK(cond) do { if (!(cond)) { char b[300]; std::snprintf(b, sizeof b, "line %d: %s", __LINE__, #cond); \
|
|
failures.emplace_back(b); ++failed_checks; } } while (0)
|
|
#define CHECK_STR(a, b) do { const std::string va = (a), vb = (b); if (va != vb) { char s[600]; \
|
|
std::snprintf(s, sizeof s, "line %d: %s | got %s | expected %s", __LINE__, #a, va.c_str(), vb.c_str()); \
|
|
failures.emplace_back(s); ++failed_checks; } } while (0)
|
|
#define CHECK_EQ(a, b) do { const auto va = (a); const auto vb = (b); if (!(va == vb)) { char s[300]; \
|
|
std::snprintf(s, sizeof s, "line %d: %s == %s (%lld vs %lld)", __LINE__, #a, #b, (long long)va, (long long)vb); \
|
|
failures.emplace_back(s); ++failed_checks; } } while (0)
|
|
|
|
static Bytes hex(const char* s) {
|
|
Bytes out;
|
|
for (; s[0] && s[1]; s += 2) { unsigned v; std::sscanf(s, "%2x", &v); out.push_back(static_cast<std::uint8_t>(v)); }
|
|
return out;
|
|
}
|
|
static std::string to_hex(const Bytes& b) {
|
|
std::string s; char t[3];
|
|
for (auto v : b) { std::snprintf(t, sizeof t, "%02x", v); s += t; }
|
|
return s;
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Link message framing
|
|
// ================================================================================================
|
|
TEST(link_header_is_little_endian) {
|
|
link::Message m {{link::Opcode::STATION_CONFIGURE, 0, 0x1234}, {0xAA}};
|
|
Bytes out;
|
|
CHECK(link::encode(m, out));
|
|
CHECK_STR(to_hex(out), std::string("01003412aa"));
|
|
link::Message back;
|
|
CHECK(link::decode(out, back));
|
|
CHECK_EQ(back.header.sequence, 0x1234);
|
|
CHECK(back.header.opcode == link::Opcode::STATION_CONFIGURE);
|
|
}
|
|
|
|
TEST(link_message_size_limits) {
|
|
Bytes out;
|
|
link::Message m {{link::Opcode::POTI_UPDATE, 0, 1}, Bytes(link::maximum_message - link::header_size)};
|
|
CHECK(link::encode(m, out));
|
|
CHECK_EQ(out.size(), link::maximum_message);
|
|
m.body.push_back(0);
|
|
CHECK(!link::encode(m, out)); // 513 octets
|
|
link::Message back;
|
|
CHECK(!link::decode(Bytes(3), back)); // shorter than the header
|
|
CHECK(!link::decode(Bytes(link::maximum_message + 1), back));
|
|
}
|
|
|
|
TEST(opcode_numbers_follow_the_spec_and_V2X_RX_is_collision_free) {
|
|
CHECK_EQ(static_cast<int>(link::Opcode::CONFIRM), 0x80);
|
|
CHECK_EQ(static_cast<int>(link::Opcode::BTP_DATA_INDICATION), 0x81);
|
|
CHECK_EQ(static_cast<int>(link::Opcode::RAW_FRAME), 0x85);
|
|
CHECK_EQ(static_cast<int>(link::Opcode::STATUS_REQUEST), 0x0C);
|
|
CHECK_EQ(static_cast<int>(link::Opcode::V2X_RX), 0x90); // MicrOBU extension; the app's LinkOpcode.V2X_RX
|
|
const link::Opcode all[] = {
|
|
link::Opcode::STATION_CONFIGURE, link::Opcode::POTI_UPDATE, link::Opcode::BTP_DATA_REQUEST,
|
|
link::Opcode::CREDENTIALS_PROVISION, link::Opcode::CREDENTIALS_ERASE, link::Opcode::SF_IDCHANGE_SUBSCRIBE,
|
|
link::Opcode::SF_IDCHANGE_UNSUBSCRIBE, link::Opcode::SF_IDCHANGE_EVENT_RESPONSE, link::Opcode::SF_IDCHANGE_TRIGGER,
|
|
link::Opcode::SF_ID_LOCK, link::Opcode::SF_ID_UNLOCK, link::Opcode::STATUS_REQUEST, link::Opcode::RAW_CAPTURE,
|
|
link::Opcode::PKI_IDENTITY, link::Opcode::PKI_ENROL_REQUEST, link::Opcode::PKI_AUTH_REQUEST, link::Opcode::PKI_RESPONSE,
|
|
link::Opcode::PKI_RETRY, link::Opcode::PKI_CANCEL, link::Opcode::PKI_STATUS, link::Opcode::PKI_TRUST_BOOTSTRAP,
|
|
link::Opcode::PKI_TRUST_UPDATE, link::Opcode::CONFIRM, link::Opcode::BTP_DATA_INDICATION, link::Opcode::SF_IDCHANGE_EVENT,
|
|
link::Opcode::RAW_FRAME, link::Opcode::PKI_REQUEST_DATA, link::Opcode::BATTERY_STATE, link::Opcode::V2X_RX};
|
|
for (std::size_t i = 0; i < sizeof all / sizeof all[0]; ++i)
|
|
for (std::size_t j = i + 1; j < sizeof all / sizeof all[0]; ++j)
|
|
CHECK(all[i] != all[j]);
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Bodies, byte-exact against the app's StationLinkTest (the hex strings are the same)
|
|
// ================================================================================================
|
|
TEST(station_configure_is_the_six_octet_form_the_app_sends) {
|
|
link::StationConfigure c;
|
|
c.station_type = 2; c.channel_number = 180; c.transmit_power_dbm = 20; c.security = 1; c.beaconing = 0;
|
|
CHECK_STR(to_hex(link::encode(c)), std::string("02b400140100"));
|
|
link::StationConfigure d;
|
|
CHECK(link::decode(hex("02b400140100"), d));
|
|
CHECK_EQ(d.station_type, 2); CHECK_EQ(d.channel_number, 180); CHECK_EQ(d.transmit_power_dbm, 20);
|
|
CHECK_EQ(d.security, 1); CHECK_EQ(d.beaconing, 0);
|
|
// the retired fields keep the spec's defaults: transmit and receive, anonymous address, DP2, 1 s
|
|
CHECK_EQ(d.radio, 2); CHECK_EQ(d.address_configuration, 1); CHECK_EQ(d.default_traffic_class, 2);
|
|
CHECK_EQ(d.default_lifetime, 0x05);
|
|
link::StationConfigure bad;
|
|
CHECK(!link::decode(hex("02b4001401"), bad)); // five octets
|
|
}
|
|
|
|
TEST(station_configure_with_a_named_mac_is_the_sixteen_octet_form) {
|
|
// the app's form for a ride (rotating pseudonym MAC) and for the bench pinger (fixed MAC)
|
|
link::StationConfigure c;
|
|
CHECK(link::decode(hex("02010002112233445500b40014020205"), c));
|
|
CHECK_EQ(c.station_type, 2); CHECK_EQ(c.security, 1);
|
|
CHECK_EQ(c.address_configuration, 0); // AUTO: the MID below names the address
|
|
CHECK_STR(to_hex(Bytes(c.mid, c.mid + 6)), std::string("021122334455"));
|
|
CHECK_EQ(c.channel_number, 180); CHECK_EQ(c.transmit_power_dbm, 20);
|
|
CHECK_EQ(c.radio, 2); CHECK_EQ(c.default_traffic_class, 2); CHECK_EQ(c.default_lifetime, 0x05);
|
|
// and the station's own encoder produces the same bytes for the same values
|
|
CHECK_STR(to_hex(link::encode(c)), std::string("02010002112233445500b40014020205"));
|
|
link::StationConfigure bad;
|
|
CHECK(!link::decode(hex("02010002112233445500b4001402"), bad)); // cut short
|
|
CHECK(!link::decode(hex("02010202112233445500b40014020205"), bad)); // address_configuration 2 does not exist
|
|
}
|
|
|
|
TEST(poti_update_decodes_the_apps_31_octets) {
|
|
link::PotiUpdate p;
|
|
CHECK(link::decode(hex("fbb2b7ffa60000002bcbeb1f914bf905e601e60100000e000000001f02d204"), p));
|
|
CHECK_EQ(p.timestamp_ms, 717254800123ULL);
|
|
CHECK_EQ(p.latitude, 535546667);
|
|
CHECK_EQ(p.longitude, 100223889);
|
|
CHECK_EQ(p.semi_major_cm, 486); CHECK_EQ(p.semi_minor_cm, 486);
|
|
CHECK(!p.has_altitude()); CHECK(p.has_speed()); CHECK(p.has_heading()); CHECK(p.pai());
|
|
CHECK_EQ(p.speed_cm_s, 543); CHECK_EQ(p.heading_deci_degree, 1234);
|
|
CHECK_STR(to_hex(link::encode(p)), std::string("fbb2b7ffa60000002bcbeb1f914bf905e601e60100000e000000001f02d204"));
|
|
CHECK(!link::decode(hex("fbb2b7ffa6000000"), p)); // truncated
|
|
}
|
|
|
|
TEST(btp_data_request_signed_cam_from_the_app) {
|
|
link::BtpDataRequest q;
|
|
CHECK(link::decode(hex("01d1070000010102ffff000000000024000000030100000005000102030405"), q));
|
|
CHECK_EQ(q.btp_type, 1); CHECK_EQ(q.destination_port, 2001);
|
|
CHECK_EQ(q.gn_packet_transport_type, 1); // SHB
|
|
CHECK_EQ(q.gn_communication_profile, 1); // ITS-G5
|
|
CHECK_EQ(q.gn_security_profile, 2); // secured
|
|
CHECK_EQ(q.gn_traffic_class, 0xFF); CHECK_EQ(q.gn_maximum_packet_lifetime, 0xFF);
|
|
CHECK_EQ(q.its_aid, 36);
|
|
CHECK_STR(to_hex(q.permissions), std::string("010000"));
|
|
CHECK_STR(to_hex(q.fl_sdu), std::string("0102030405"));
|
|
CHECK(!q.generated_at_ms.has_value());
|
|
CHECK(!q.area.has_value());
|
|
}
|
|
|
|
TEST(btp_data_request_unsecured_profile_is_decoded_not_dropped) {
|
|
// the MicrOBU extension needs the security profile 1 to reach Station::btp_request
|
|
link::BtpDataRequest q;
|
|
CHECK(link::decode(hex("01d1070000010101ffff000000000024000000030100000005000102030405"), q));
|
|
CHECK_EQ(q.gn_security_profile, 1);
|
|
}
|
|
|
|
TEST(btp_data_request_truncated_is_malformed) {
|
|
link::BtpDataRequest q;
|
|
CHECK(!link::decode(hex("01d1070000010102ffff000000000024000000030100000005000102"), q));
|
|
CHECK(!link::decode(Bytes(), q));
|
|
}
|
|
|
|
TEST(credentials_provision_segment_from_the_app) {
|
|
link::CredentialsProvision c;
|
|
CHECK(link::decode(hex("b702f00003ababab"), c));
|
|
CHECK_EQ(c.total_length, 695); CHECK_EQ(c.offset, 240);
|
|
CHECK_EQ(c.segment.size(), 3u);
|
|
}
|
|
|
|
TEST(confirm_roundtrip_and_configure_detail) {
|
|
link::Confirm c; c.code = link::Code::accepted; c.detail = Bytes(18, 7);
|
|
const auto body = link::encode(c);
|
|
CHECK_EQ(body.size(), 20u);
|
|
CHECK_EQ(body[0], 0); CHECK_EQ(body[1], 18);
|
|
link::Confirm d;
|
|
CHECK(link::decode(body, d));
|
|
CHECK(d.code == link::Code::accepted);
|
|
CHECK_EQ(d.detail.size(), 18u);
|
|
CHECK(!link::decode(hex("0012"), d)); // length says 18, nothing follows
|
|
}
|
|
|
|
TEST(confirm_codes_match_the_apps_names) {
|
|
CHECK_EQ(static_cast<int>(link::Code::time_regression), 7); // app: LinkResultCode.TIME_REGRESSION
|
|
CHECK_EQ(static_cast<int>(link::Code::not_configured), 0x12); // app: LinkResultCode.NOT_CONFIGURED
|
|
CHECK_EQ(static_cast<int>(link::Code::no_credentials), 0x14);
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Status snapshot: 131 octets, offsets from docs/station-link.tex, as the app's StationStatus.decode reads them
|
|
// ================================================================================================
|
|
TEST(status_is_131_octets_with_the_spec_offsets) {
|
|
link::Status s;
|
|
s.uptime_ms = 0x11223344; s.configured = 1; s.tickets = 3;
|
|
s.signed_messages = 1001; s.refused_no_ticket = 1002; s.refused_change_pending = 1003;
|
|
s.refused_permission = 1004; s.sign_failed = 1005; s.verified = 1006; s.rejected = 1007;
|
|
s.requests_accepted = 1008; s.requests_refused = 1009; s.indications = 1010;
|
|
s.radio_submitted = 1011; s.radio_failed = 1012; s.radio_received = 1013; s.radio_dropped = 1014;
|
|
s.link_rx_frames = 1015; s.link_crc_errors = 1016; s.link_malformed = 1017; s.poti_updates = 1018;
|
|
s.its_time_ms = 0x0102030405060708ULL;
|
|
s.heap_free = 123456; s.battery_mv = 3871;
|
|
const auto b = link::encode(s);
|
|
CHECK_EQ(b.size(), 131u);
|
|
auto u32 = [&](std::size_t o) { return b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (std::uint32_t(b[o + 3]) << 24); };
|
|
CHECK_EQ(u32(0), 0x11223344u); // uptime_ms
|
|
CHECK_EQ(b[4], 1); // configured
|
|
CHECK_EQ(b[22], 3); // tickets
|
|
const std::size_t counters[] = {23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, 83, 87, 91};
|
|
for (int i = 0; i < 18; ++i) CHECK_EQ(u32(counters[i]), 1001u + i);
|
|
CHECK_EQ(b[95], 0x08); CHECK_EQ(b[102], 0x01); // its_time_ms u64
|
|
CHECK_EQ(u32(111), 123456u); // heap_free
|
|
CHECK_EQ(b[129] | (b[130] << 8), 3871); // battery_mv: the app reads octets 129-130
|
|
link::Status back;
|
|
CHECK(link::decode(b, back));
|
|
CHECK_EQ(back.battery_mv, 3871); CHECK_EQ(back.signed_messages, 1001u); CHECK_EQ(back.poti_updates, 1018u);
|
|
}
|
|
|
|
TEST(status_unavailable_markers) {
|
|
link::Status s;
|
|
CHECK_EQ(s.cbr_local, 0xFFFF); CHECK_EQ(s.cbr_global, 0xFFFF); CHECK_EQ(s.dcc_delta, 0xFFFF);
|
|
CHECK_EQ(s.board_temperature_deci_deg, 0x7FFF);
|
|
CHECK_EQ(s.battery_mv, 0); // 0 = no cell / no sensor: never a shutdown
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Fragmented requests (spec: request maximum 1536, 8 pending, 2 s idle timeout)
|
|
// ================================================================================================
|
|
static link::Message fragment(std::uint8_t flags, std::uint16_t seq, std::uint16_t total, std::uint16_t offset, const Bytes& data) {
|
|
Bytes body {std::uint8_t(total), std::uint8_t(total >> 8), std::uint8_t(offset), std::uint8_t(offset >> 8)};
|
|
body.insert(body.end(), data.begin(), data.end());
|
|
return link::Message {{link::Opcode::PKI_RESPONSE, flags, seq}, body};
|
|
}
|
|
|
|
TEST(a_complete_message_passes_through) {
|
|
link::RequestReassembly r;
|
|
link::Message m {{link::Opcode::POTI_UPDATE, 0, 5}, Bytes(31, 1)};
|
|
const auto out = r.feed(m, 0, 0);
|
|
CHECK(out.has_value());
|
|
CHECK_EQ(out->body.size(), 31u);
|
|
}
|
|
|
|
TEST(three_fragments_reassemble_in_order) {
|
|
link::RequestReassembly r;
|
|
CHECK(!r.feed(fragment(1, 9, 6, 0, {1, 2}), 0, 0));
|
|
CHECK(!r.feed(fragment(2, 9, 6, 2, {3, 4}), 0, 10));
|
|
const auto out = r.feed(fragment(3, 9, 6, 4, {5, 6}), 0, 20);
|
|
CHECK(out.has_value());
|
|
CHECK_STR(to_hex(out->body), std::string("010203040506"));
|
|
CHECK_EQ(out->header.flags, 0);
|
|
}
|
|
|
|
TEST(gaps_wrong_order_and_changed_lengths_are_dropped) {
|
|
link::RequestReassembly r;
|
|
CHECK(!r.feed(fragment(1, 1, 6, 0, {1, 2}), 0, 0));
|
|
CHECK(!r.feed(fragment(3, 1, 6, 4, {5, 6}), 0, 1)); // skipped the middle
|
|
CHECK(!r.feed(fragment(3, 1, 6, 2, {3, 4}), 0, 2)); // the stream was discarded: nothing completes
|
|
link::RequestReassembly r2;
|
|
CHECK(!r2.feed(fragment(1, 2, 6, 0, {1, 2}), 0, 0));
|
|
CHECK(!r2.feed(fragment(2, 2, 8, 2, {3, 4}), 0, 1)); // total changed
|
|
link::RequestReassembly r3;
|
|
CHECK(!r3.feed(fragment(2, 3, 6, 2, {3, 4}), 0, 0)); // middle without a first
|
|
}
|
|
|
|
TEST(an_idle_stream_times_out_after_2_seconds) {
|
|
link::RequestReassembly r;
|
|
CHECK(!r.feed(fragment(1, 4, 4, 0, {1, 2}), 0, 0));
|
|
CHECK(!r.feed(fragment(3, 4, 4, 2, {3, 4}), 0, 2500)); // the first fragment is gone
|
|
}
|
|
|
|
TEST(oversize_requests_are_refused_and_flagged) {
|
|
link::RequestReassembly r;
|
|
CHECK(!r.feed(fragment(1, 6, 1537, 0, {1}), 0, 0));
|
|
CHECK(r.take_overflow());
|
|
CHECK(!r.take_overflow());
|
|
link::Message big {{link::Opcode::POTI_UPDATE, 0, 7}, Bytes(1537)};
|
|
CHECK(!r.feed(big, 0, 0));
|
|
CHECK(r.take_overflow());
|
|
}
|
|
|
|
TEST(usb_and_ble_streams_are_kept_apart) {
|
|
link::RequestReassembly r;
|
|
CHECK(!r.feed(fragment(1, 8, 4, 0, {1, 2}), /*usb*/ 0, 0));
|
|
CHECK(!r.feed(fragment(3, 8, 4, 2, {3, 4}), /*ble*/ 1, 1)); // same opcode+sequence, other origin
|
|
const auto out = r.feed(fragment(3, 8, 4, 2, {3, 4}), 0, 2);
|
|
CHECK(out.has_value());
|
|
}
|
|
|
|
// ================================================================================================
|
|
// V2X_RX (MicrOBU extension): TX code -> air frame -> raw RX path -> link body
|
|
// ================================================================================================
|
|
static bool air_frame(const Bytes& payload, std::uint16_t port, Bytes& frame) {
|
|
gn_lpv_t lpv {};
|
|
const std::uint8_t mac[6] = {0x02, 0x11, 0x22, 0x33, 0x44, 0x55};
|
|
std::memcpy(lpv.mac, mac, 6);
|
|
lpv.station_type = 2; lpv.pai = true; lpv.tst_ms = 1234567; lpv.lat_tenmicrodeg = 535546667;
|
|
lpv.lon_tenmicrodeg = 100223889; lpv.speed_cms = 543; lpv.heading_decideg = 1234;
|
|
std::vector<std::uint8_t> gn(payload.size() + 64), f(payload.size() + 128);
|
|
const int n = geonet_wrap_shb(payload.data(), static_cast<int>(payload.size()), &lpv, port, gn.data(), gn.size());
|
|
if (n <= 0) return false;
|
|
const int m = dot11p_build_frame(gn.data(), n, mac, f.data(), f.size(), true);
|
|
if (m <= 0) return false;
|
|
frame.assign(f.begin(), f.begin() + m);
|
|
return true;
|
|
}
|
|
|
|
TEST(a_sent_unsigned_cam_comes_back_as_a_v2x_rx_body) {
|
|
Bytes cam(60); for (std::size_t i = 0; i < cam.size(); ++i) cam[i] = static_cast<std::uint8_t>(i * 7);
|
|
Bytes frame;
|
|
CHECK(air_frame(cam, 2001, frame));
|
|
gn_rx_t rx;
|
|
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
|
CHECK(link::v2x_rx_fits(rx));
|
|
const auto body = link::encode_v2x_rx(rx, -61);
|
|
CHECK_EQ(body.size(), link::v2x_rx_prefix + cam.size());
|
|
CHECK_EQ(body[0] | (body[1] << 8), 2001); // port
|
|
CHECK_EQ(static_cast<std::int8_t>(body[2]), -61); // rssi
|
|
CHECK_EQ(body[3], 0); // no GBC area, not signed
|
|
CHECK_STR(to_hex(Bytes(body.begin() + 14, body.end())), to_hex(cam));
|
|
}
|
|
|
|
TEST(v2x_rx_layout_matches_the_documented_prefix) {
|
|
// port 2018 (VAM), rssi -70, flags 0x03 (area + signed), lat/lon of the bench crossroads, 150 m, payload ab cd
|
|
const std::uint8_t payload[] = {0xAB, 0xCD};
|
|
gn_rx_t rx {};
|
|
rx.btp_dest_port = 2018; rx.payload = payload; rx.payload_len = 2; rx.has_geo_area = true;
|
|
rx.geo_area_lat_tenmicrodeg = 535546980; rx.geo_area_lon_tenmicrodeg = 100225480; rx.geo_area_distance_a_m = 150;
|
|
rx.signed_unverified = true;
|
|
CHECK_STR(to_hex(link::encode_v2x_rx(rx, -70)), std::string("e207ba0364cceb1fc851f9059600abcd"));
|
|
}
|
|
|
|
TEST(rssi_is_clamped_to_a_signed_octet) {
|
|
const std::uint8_t payload[] = {1};
|
|
gn_rx_t rx {}; rx.btp_dest_port = 2001; rx.payload = payload; rx.payload_len = 1;
|
|
CHECK_EQ(static_cast<std::int8_t>(link::encode_v2x_rx(rx, -300)[2]), -128);
|
|
CHECK_EQ(static_cast<std::int8_t>(link::encode_v2x_rx(rx, 300)[2]), 127);
|
|
}
|
|
|
|
TEST(the_494_octet_limit_is_exact) {
|
|
CHECK_EQ(link::v2x_rx_max_payload, 494u);
|
|
for (int len : {493, 494, 495}) {
|
|
Bytes cam(len, 0x5A), frame;
|
|
CHECK(air_frame(cam, 2003, frame));
|
|
gn_rx_t rx;
|
|
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
|
CHECK_EQ(link::v2x_rx_fits(rx) ? 1 : 0, len <= 494 ? 1 : 0);
|
|
if (link::v2x_rx_fits(rx)) {
|
|
link::Message m {{link::Opcode::V2X_RX, 0, 1}, link::encode_v2x_rx(rx, -50)};
|
|
Bytes out;
|
|
CHECK(link::encode(m, out)); // really fits one 512-octet link message
|
|
CHECK(out.size() <= link::maximum_message);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(v2x_rx_plan_keeps_small_messages_single_and_fragments_large_ones) {
|
|
gn_rx_t rx {};
|
|
const std::uint8_t byte = 0;
|
|
rx.payload = &byte;
|
|
const auto plan = [&](int len, bool raw_on, bool truncated = false) {
|
|
rx.payload_len = len; rx.truncated = truncated;
|
|
return link::v2x_rx_plan(rx, raw_on);
|
|
};
|
|
using P = link::V2xRxPlan;
|
|
CHECK(plan(1, false) == P::single);
|
|
CHECK(plan(58, false) == P::single);
|
|
CHECK(plan(494, false) == P::single);
|
|
CHECK(plan(495, false) == P::fragmented);
|
|
CHECK(plan(888, false) == P::fragmented);
|
|
CHECK(plan(1198, false) == P::fragmented);
|
|
CHECK(plan(2300, false) == P::fragmented);
|
|
CHECK(plan(2301, false) == P::drop);
|
|
CHECK(plan(0, false) == P::drop);
|
|
CHECK(plan(-1, false) == P::drop);
|
|
CHECK(plan(600, false, true) == P::drop); // truncated is never forwarded
|
|
// raw capture on: the app reads RAW_FRAME, so a large message is not sent twice; small ones are as before
|
|
CHECK(plan(494, true) == P::single);
|
|
CHECK(plan(495, true) == P::covered_by_raw);
|
|
CHECK(plan(1198, true) == P::covered_by_raw);
|
|
CHECK(plan(2301, true) == P::drop);
|
|
}
|
|
|
|
// What a phone does with the fragments, written here without link::RequestReassembly.
|
|
static bool put_back(const std::vector<link::Fragment>& fragments, Bytes& out) {
|
|
out.clear();
|
|
std::size_t total = 0;
|
|
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
|
const auto& b = fragments[i].body;
|
|
if (b.size() <= link::fragment_header_size) return false;
|
|
const std::size_t t = b[0] | (std::size_t(b[1]) << 8), offset = b[2] | (std::size_t(b[3]) << 8);
|
|
if (fragments[i].flags != (i == 0 ? 1 : i + 1 == fragments.size() ? 3 : 2)) return false;
|
|
if (i == 0) total = t;
|
|
if (t != total || offset != out.size()) return false;
|
|
out.insert(out.end(), b.begin() + link::fragment_header_size, b.end());
|
|
}
|
|
return out.size() == total;
|
|
}
|
|
|
|
TEST(fragments_of_a_large_v2x_rx_reassemble_exactly_on_both_transports) {
|
|
for (const int len : {495, 504, 888, 1198, 2300}) {
|
|
Bytes mapem(len);
|
|
for (std::size_t i = 0; i < mapem.size(); ++i) mapem[i] = static_cast<std::uint8_t>(i * 31 + len);
|
|
Bytes frame;
|
|
CHECK(air_frame(mapem, 2003, frame));
|
|
gn_rx_t rx;
|
|
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
|
CHECK_EQ(rx.payload_len, len);
|
|
const auto body = link::encode_v2x_rx(rx, -48);
|
|
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) { // serial / MTU 517, MTU 247
|
|
std::vector<link::Fragment> fragments;
|
|
const bool split = link::split_fragments(body, limit, fragments, 16);
|
|
CHECK(split);
|
|
if (!split) continue;
|
|
CHECK(fragments.size() >= 2);
|
|
Bytes rebuilt;
|
|
CHECK(put_back(fragments, rebuilt));
|
|
CHECK(rebuilt == body);
|
|
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
|
Bytes wire;
|
|
CHECK(link::encode(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 5}, fragments[i].body}, wire));
|
|
CHECK(wire.size() <= limit);
|
|
if (i + 1 < fragments.size()) CHECK_EQ(wire.size(), limit); // full except the last
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(fragment_counts_for_the_bench_messages_at_the_full_mtu) {
|
|
// 14 octets of prefix + the UPER payload; a fragment carries 512 - 4 - 4 = 504 octets of it
|
|
std::vector<link::Fragment> f;
|
|
CHECK(link::split_fragments(Bytes(14 + 888), 512, f)); CHECK_EQ(f.size(), 2u);
|
|
CHECK(link::split_fragments(Bytes(14 + 1198), 512, f)); CHECK_EQ(f.size(), 3u);
|
|
CHECK(link::split_fragments(Bytes(14 + 495), 512, f)); CHECK_EQ(f.size(), 2u);
|
|
CHECK_STR(to_hex(Bytes(f[0].body.begin(), f[0].body.begin() + 4)), std::string("fd010000")); // total 509, offset 0
|
|
CHECK_STR(to_hex(Bytes(f[1].body.begin(), f[1].body.begin() + 4)), std::string("fd01f801")); // total 509, offset 504
|
|
}
|
|
|
|
TEST(a_message_that_fits_one_link_message_is_never_fragmented) {
|
|
std::vector<link::Fragment> f;
|
|
CHECK(!link::split_fragments(Bytes(508), 512, f)); // 508 + 4 = 512: exactly one message
|
|
CHECK(f.empty());
|
|
CHECK(link::split_fragments(Bytes(509), 512, f));
|
|
CHECK(!link::split_fragments(Bytes(240), 244, f)); // MTU 247: 240 + 4 = 244
|
|
CHECK(link::split_fragments(Bytes(241), 244, f));
|
|
}
|
|
|
|
TEST(fragmenting_refuses_what_a_transport_cannot_carry_sensibly) {
|
|
std::vector<link::Fragment> f;
|
|
CHECK(!link::split_fragments(Bytes(1212), 20, f, 8)); // ATT MTU 23: 12 octets a fragment, 101 fragments
|
|
CHECK(f.empty());
|
|
CHECK(!link::split_fragments(Bytes(1212), 8, f)); // no room for data at all
|
|
CHECK(!link::split_fragments(Bytes(65536), 512, f)); // total is a u16
|
|
CHECK(link::split_fragments(Bytes(65535), 512, f));
|
|
CHECK(link::split_fragments(Bytes(1212), 244, f, 6)); // exactly 6 fragments of 236
|
|
CHECK(!link::split_fragments(Bytes(1212), 244, f, 5));
|
|
}
|
|
|
|
TEST(a_lost_fragment_loses_that_message_only) {
|
|
std::vector<link::Fragment> a, b;
|
|
Bytes first(1212, 0xA1), second(1212, 0xB2);
|
|
CHECK(link::split_fragments(first, 512, a));
|
|
CHECK(link::split_fragments(second, 512, b));
|
|
link::RequestReassembly phone;
|
|
std::optional<link::Message> done;
|
|
for (std::size_t i = 0; i < a.size(); ++i) {
|
|
if (i == 1) continue; // the middle fragment never arrives
|
|
done = phone.feed(link::Message {{link::Opcode::V2X_RX, a[i].flags, 1}, a[i].body}, 1, i);
|
|
CHECK(!done);
|
|
}
|
|
for (std::size_t i = 0; i < b.size(); ++i) done = phone.feed(link::Message {{link::Opcode::V2X_RX, b[i].flags, 2}, b[i].body}, 1, 10 + i);
|
|
CHECK(done.has_value());
|
|
if (done) CHECK(done->body == second);
|
|
}
|
|
|
|
TEST(ports_outside_the_its_set_are_not_forwarded) {
|
|
Bytes frame;
|
|
CHECK(air_frame(Bytes(20, 1), 1234, frame));
|
|
gn_rx_t rx;
|
|
CHECK(!gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
|
for (std::uint16_t port : {2001, 2002, 2003, 2004, 2018}) {
|
|
CHECK(air_frame(Bytes(20, 1), port, frame));
|
|
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
|
CHECK_EQ(rx.btp_dest_port, port);
|
|
}
|
|
}
|
|
|
|
TEST(garbage_is_ignored_without_crashing) {
|
|
gn_rx_t rx;
|
|
const std::uint8_t junk[40] = {0x08, 0x01};
|
|
CHECK(!gn_unwrap_its(junk, sizeof junk, &rx));
|
|
CHECK(!gn_unwrap_its(junk, 3, &rx));
|
|
CHECK(!gn_unwrap_its(junk, 0, &rx));
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Policies
|
|
// ================================================================================================
|
|
TEST(the_apps_default_operating_point_is_allowed) {
|
|
CHECK(radio_policy::operating_point_allowed(180, 20.0)); // StationConfigure(180, 20 dBm)
|
|
CHECK(radio_policy::operating_point_allowed(180, 10.0));
|
|
CHECK(!radio_policy::operating_point_allowed(180, 20.25)); // above the measured mask limit
|
|
CHECK(!radio_policy::operating_point_allowed(180, 1.75)); // below the lowest power setting
|
|
CHECK(!radio_policy::operating_point_allowed(180, 10.1)); // not a 0.25 dB driver step
|
|
CHECK(radio_policy::operating_point_allowed(176, 16.5));
|
|
CHECK(!radio_policy::operating_point_allowed(176, 18.0));
|
|
CHECK(!radio_policy::operating_point_allowed(172, 10.0)); // undeclared carrier: no transmission
|
|
CHECK(radio_policy::receive_channel_allowed(172)); // but receiving is fine
|
|
CHECK(!radio_policy::receive_channel_allowed(171));
|
|
CHECK(!radio_policy::receive_channel_allowed(186));
|
|
}
|
|
|
|
TEST(ble_tx_guard_delays_only_inside_the_20_ms_window) {
|
|
CHECK_EQ(tx_policy::guard_delay_us(0, 20000), 0);
|
|
CHECK_EQ(tx_policy::guard_delay_us(12499, 20000), 0);
|
|
CHECK_EQ(tx_policy::guard_delay_us(12500, 20000), 3500); // start of the lossy window: wait to its end
|
|
CHECK_EQ(tx_policy::guard_delay_us(15999, 20000), 1);
|
|
CHECK_EQ(tx_policy::guard_delay_us(16000, 20000), 0);
|
|
CHECK_EQ(tx_policy::guard_delay_us(20000 * 7 + 13000, 20000), 3000); // periodic
|
|
CHECK_EQ(tx_policy::guard_delay_us(13000, 30000), 0); // an uncalibrated interval transmits unguarded
|
|
CHECK_EQ(tx_policy::guard_delay_us(-1, 20000), 0);
|
|
CHECK_EQ(tx_policy::guard_delay_us(5, 0), 0);
|
|
CHECK(tx_policy::guard_windows(30000).empty());
|
|
}
|
|
|
|
TEST(stale_requests_are_older_than_one_packet_lifetime) {
|
|
CHECK(!tx_policy::is_stale(1000000, 1900000, 1000000));
|
|
CHECK(!tx_policy::is_stale(1000000, 2000000, 1000000)); // exactly 1 s is still fresh
|
|
CHECK(tx_policy::is_stale(1000000, 2000001, 1000000));
|
|
}
|
|
|
|
TEST(its_clock_follows_the_phone) {
|
|
its_clock::Regression reg; its_clock::Lag lag;
|
|
CHECK(its_clock::next_step(false, 0, 0, reg, lag) == its_clock::Step::start);
|
|
CHECK(its_clock::next_step(true, 50000, 1000, reg, lag) == its_clock::Step::forward);
|
|
CHECK(its_clock::next_step(true, -300000, 2000, reg, lag) == its_clock::Step::hold); // < 1 s behind
|
|
CHECK(its_clock::next_step(true, -3000000, 3000, reg, lag) == its_clock::Step::refuse_stale); // a late update
|
|
}
|
|
|
|
TEST(its_clock_restarts_only_after_a_consistent_step_back) {
|
|
its_clock::Regression reg; its_clock::Lag lag;
|
|
std::int64_t t = 0;
|
|
its_clock::Step step = its_clock::Step::refuse_stale;
|
|
for (int i = 0; i < 4; ++i) { step = its_clock::next_step(true, -5000000, t, reg, lag); t += 600000; }
|
|
CHECK(step == its_clock::Step::refuse_stale); // 4 updates are not enough
|
|
step = its_clock::next_step(true, -5000000, t, reg, lag); // 5th, 2.4 s after the first
|
|
CHECK(step == its_clock::Step::restart);
|
|
}
|
|
|
|
TEST(its_clock_slews_back_when_it_runs_ahead) {
|
|
its_clock::Regression reg; its_clock::Lag lag;
|
|
its_clock::Step step = its_clock::Step::hold;
|
|
std::int64_t t = 0;
|
|
for (int i = 0; i < 5; ++i) { step = its_clock::next_step(true, -80000, t, reg, lag); t += 600000; }
|
|
CHECK(step == its_clock::Step::slew_back);
|
|
CHECK_EQ(lag.slew_us, 80000);
|
|
CHECK_EQ(its_clock::slew_applied_us(80000, 100000), 10000); // 10 % per elapsed time
|
|
CHECK_EQ(its_clock::slew_applied_us(5000, 1000000), 5000); // never more than pending
|
|
}
|
|
|
|
TEST(cca_counters_wrap_at_27_bits) {
|
|
CHECK_EQ(cca::delta27(100, 40), 60u);
|
|
CHECK_EQ(cca::delta27(10, cca::counter_range - 90), 100u); // across the wrap
|
|
CHECK_EQ(cca::wrap_us, 3355443);
|
|
const auto half = cca::window_cbr(2000, 1000, 1000, 500, 100000);
|
|
CHECK(half.has_value() && *half > 0.499 && *half < 0.501);
|
|
CHECK(!cca::window_cbr(2000, 1000, 2000, 500, 100000).has_value()); // no cycle elapsed
|
|
CHECK(!cca::window_cbr(2000, 1000, 1000, 500, cca::wrap_us).has_value());
|
|
CHECK(!cca::window_cbr(2000, 1000, 1000, 500, 0).has_value());
|
|
CHECK_EQ(*cca::window_cbr(2000, 9000, 1000, 500, 100000), 1.0); // clamped
|
|
}
|
|
|
|
TEST(ble_needs_a_passkey_and_an_authenticated_encrypted_link) {
|
|
CHECK(ble::pairing_allowed(123456));
|
|
CHECK(ble::pairing_allowed(999999));
|
|
CHECK(!ble::pairing_allowed(0)); // 0 disables BLE
|
|
CHECK(!ble::pairing_allowed(1000000));
|
|
CHECK(ble::station_link_security_ready(true, true, 16));
|
|
CHECK(!ble::station_link_security_ready(true, false, 16));
|
|
CHECK(!ble::station_link_security_ready(false, true, 16));
|
|
CHECK(!ble::station_link_security_ready(true, true, 8));
|
|
}
|
|
|
|
TEST(raw_capture_only_accepts_the_three_defined_bits) {
|
|
CHECK(capture_policy::valid_mode(0x07));
|
|
CHECK(capture_policy::valid_mode(0x00));
|
|
CHECK(!capture_policy::valid_mode(0x10));
|
|
CHECK_EQ(capture_policy::effective_mode(0x0F), 0x07); // older clients may set bit 3
|
|
}
|
|
|
|
// ---- battery protection (battery_policy.hpp) ----------------------------------------------------
|
|
static void feed(BatteryPolicy& p, std::uint16_t mv, int n, bool usb, std::uint32_t& t) {
|
|
for (int i = 0; i < n; ++i) { p.update(mv, usb, t); t += 5000; }
|
|
}
|
|
|
|
TEST(battery_no_cell_never_shuts_down) {
|
|
BatteryPolicy p; std::uint32_t t = 100000;
|
|
feed(p, 0, 50, false, t);
|
|
CHECK(!p.shutdown_due()); CHECK_EQ(p.millivolts(), 0);
|
|
CHECK(p.resume_ok(0)); // cannot judge: run
|
|
}
|
|
|
|
TEST(battery_shuts_down_after_30_s_below_3300_without_usb) {
|
|
BatteryPolicy p; std::uint32_t t = 100000; // past the 60 s grace after boot
|
|
feed(p, 3250, 5, false, t);
|
|
CHECK(!p.shutdown_due()); // 25 s
|
|
feed(p, 3250, 1, false, t);
|
|
CHECK(p.shutdown_due()); // 30 s: 6 samples
|
|
}
|
|
|
|
TEST(battery_is_not_shut_down_while_a_phone_is_on_usb_or_just_after_boot) {
|
|
BatteryPolicy p; std::uint32_t t = 100000;
|
|
feed(p, 3200, 40, true, t);
|
|
CHECK(!p.shutdown_due());
|
|
BatteryPolicy q; std::uint32_t boot = 0;
|
|
feed(q, 3200, 10, false, boot); // t < 60 s: grace
|
|
CHECK(!q.shutdown_due());
|
|
}
|
|
|
|
TEST(battery_warning_has_hysteresis_and_resume_needs_3600) {
|
|
BatteryPolicy p; std::uint32_t t = 100000;
|
|
feed(p, 3600, 4, false, t); CHECK(!p.low());
|
|
feed(p, 3450, 20, false, t); CHECK(p.low());
|
|
feed(p, 3550, 20, false, t); CHECK(p.low()); // between 3500 and 3600: stays low
|
|
feed(p, 3700, 30, false, t); CHECK(!p.low());
|
|
CHECK(!p.resume_ok(3599)); CHECK(p.resume_ok(3600));
|
|
}
|
|
|
|
TEST(battery_implausible_readings_count_as_no_cell) {
|
|
BatteryPolicy p; std::uint32_t t = 100000;
|
|
feed(p, 3800, 4, false, t);
|
|
p.update(5200, false, t); // above 4.5 V: a floating pin, not a cell
|
|
CHECK_EQ(p.millivolts(), 0);
|
|
}
|
|
|
|
// ================================================================================================
|
|
int main() {
|
|
int passed = 0, failed = 0;
|
|
std::printf("obu-firmware host unit tests (%zu tests)\n", registry().size());
|
|
for (const auto& t : registry()) {
|
|
const int before = failed_checks;
|
|
failures.clear();
|
|
t.fn();
|
|
if (failed_checks == before) { ++passed; std::printf(" PASS %s\n", t.name); }
|
|
else {
|
|
++failed; std::printf(" FAIL %s\n", t.name);
|
|
for (const auto& f : failures) std::printf(" %s\n", f.c_str());
|
|
}
|
|
}
|
|
std::printf("\n%d passed, %d failed, %d checks failed\n", passed, failed, failed_checks);
|
|
std::printf(failed == 0 ? "RESULT: PASS\n" : "RESULT: FAIL\n");
|
|
return failed == 0 ? 0 : 1;
|
|
}
|