// Replays recorded air traffic through the whole V2X_RX send path that needs no ESP32: gn_unwrap_its -> // v2x_rx_plan -> encode_v2x_rx -> split_fragments -> link::encode of every fragment -> reassembly, and checks // that what a phone would reassemble is byte for byte the body that was sent. Messages that fit one link message // must stay one message and be byte-identical to the unfragmented encoding. // // Usage: test_v2x_fragments capture.pcap [capture.pcap ...] // // The reassembly here is written independently of link::RequestReassembly (which is also run, for bodies it // accepts) and of the app's Kotlin LinkReassembly (checked by LinkReassemblyTest on the same fragments). #include #include #include #include #include #include extern "C" { #include "gn_unwrap.h" #include "test_util.h" } #include "link_fragment.hpp" #include "request_reassembly.hpp" #include "v2x_rx.hpp" using namespace microbu; using link::Bytes; namespace { constexpr int rx_frame_max_len = 2346; // c5_radio.cpp's Raw::bytes, as in test_replay.c struct Stats { long records = 0, accepted = 0, single = 0, fragmented = 0, dropped = 0, fragments = 0, failures = 0; std::map, long> by_port_size; }; FILE* dump = nullptr; // --dump out.tsv std::set> dumped; bool fail(Stats& s, const char* what, int index) { std::fprintf(stderr, "FAIL record %d: %s\n", index, what); ++s.failures; return false; } // What a phone does with the link messages of one body: independent of every other reassembler. bool reassemble(const std::vector& messages, Bytes& out) { out.clear(); std::size_t total = 0; for (std::size_t i = 0; i < messages.size(); ++i) { const auto& m = messages[i]; const auto& b = m.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); const std::uint8_t want = i == 0 ? 1 : (i + 1 == messages.size() ? 3 : 2); if (m.header.flags != want || m.header.opcode != link::Opcode::V2X_RX) 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; } void check(Stats& s, const gn_rx_t& rx, int index) { ++s.accepted; const auto plan = link::v2x_rx_plan(rx, false); if (link::v2x_rx_plan(rx, true) == link::V2xRxPlan::fragmented) { fail(s, "raw capture on still fragments", index); return; } if (plan == link::V2xRxPlan::drop) { ++s.dropped; return; } const Bytes body = link::encode_v2x_rx(rx, -60); if (body.size() != link::v2x_rx_prefix + static_cast(rx.payload_len)) { fail(s, "body size", index); return; } ++s.by_port_size[{rx.btp_dest_port, static_cast(rx.payload_len)}]; if (plan == link::V2xRxPlan::single) { ++s.single; std::vector none; Bytes wire; if (link::split_fragments(body, link::maximum_message, none)) { fail(s, "a single message was fragmented", index); return; } if (!link::encode(link::Message {{link::Opcode::V2X_RX, 0, 7}, body}, wire) || wire.size() != body.size() + link::header_size) fail(s, "single message does not encode", index); return; } ++s.fragmented; if (dump && dumped.insert({rx.btp_dest_port, static_cast(rx.payload_len)}).second) { // For the app's LinkReassemblyTest: the first message of each large size, its body and the link messages // (hex) both transports would carry. `port size limit body wire wire ...` for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) { std::vector fragments; if (!link::split_fragments(body, limit, fragments, 8)) continue; std::fprintf(dump, "%u\t%d\t%zu\t", rx.btp_dest_port, rx.payload_len, limit); for (const auto b : body) std::fprintf(dump, "%02x", b); for (const auto& f : fragments) { Bytes wire; link::encode(link::Message {{link::Opcode::V2X_RX, f.flags, 77}, f.body}, wire); std::fputc('\t', dump); for (const auto b : wire) std::fprintf(dump, "%02x", b); } std::fputc('\n', dump); } } // 512: serial and the full BLE MTU 517. 244: ATT MTU 247. Both must carry the message; 20 (MTU 23) must refuse it. for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) { std::vector fragments; if (!link::split_fragments(body, limit, fragments, 8)) { fail(s, "split refused", index); return; } std::vector messages; link::RequestReassembly oracle; std::optional done; for (std::size_t i = 0; i < fragments.size(); ++i) { link::Message m {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body}; Bytes wire; if (!link::encode(m, wire) || wire.size() > limit) { fail(s, "fragment exceeds the transport limit", index); return; } link::Message back; if (!link::decode(wire, back)) { fail(s, "fragment does not decode", index); return; } messages.push_back(back); done = oracle.feed(back, 0, i); ++s.fragments; } Bytes rebuilt; if (!reassemble(messages, rebuilt) || rebuilt != body) { fail(s, "independent reassembly differs", index); return; } if (body.size() <= link::maximum_request_body && (!done || done->body != body)) { fail(s, "RequestReassembly differs", index); return; } // One fragment lost anywhere but at the end, then the next message: nothing completes, nothing is poisoned. if (fragments.size() > 2) { link::RequestReassembly lossy; for (std::size_t i = 0; i < fragments.size(); ++i) { if (i == 1) continue; const auto got = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body}, 0, i); if (got) { fail(s, "a message with a lost fragment completed", index); return; } } if (body.size() <= link::maximum_request_body) { std::optional next; for (std::size_t i = 0; i < fragments.size(); ++i) next = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 10}, fragments[i].body}, 0, 100 + i); if (!next || next->body != body) { fail(s, "the message after a lost fragment did not complete", index); return; } } } } std::vector too_small; if (link::split_fragments(body, 20, too_small, 8)) fail(s, "MTU 23 should skip a large message", index); } } // namespace int main(int argc, char** argv) { int first = 1; if (argc > 2 && std::strcmp(argv[1], "--dump") == 0) { dump = std::fopen(argv[2], "w"); if (!dump) { std::fprintf(stderr, "cannot write %s\n", argv[2]); return 2; } first = 3; } if (argc <= first) { std::fprintf(stderr, "usage: test_v2x_fragments [--dump out.tsv] capture.pcap [capture.pcap ...]\n"); return 2; } tu_install_crash_handler(); tu_guard_init(); Stats stats; for (int a = first; a < argc; ++a) { struct Ctx { Stats* s; const char* path; } ctx {&stats, argv[a]}; const int n = tu_pcap_foreach(argv[a], [](const uint8_t* frame, int len, int index, void* c) { auto* x = static_cast(c); ++x->s->records; if (len > rx_frame_max_len) len = rx_frame_max_len; tu_set_context("fragments %s record %d (%d bytes)", x->path, index, len); gn_rx_t rx; if (gn_unwrap_its(tu_guarded(frame, len), len, &rx)) check(*x->s, rx, index); }, &ctx); if (n < 0) { std::fprintf(stderr, "cannot read %s as a pcap\n", argv[a]); return 2; } } std::printf("test_v2x_fragments: %ld records, %ld accepted: %ld single, %ld fragmented (%ld fragments built for " "two transports), %ld dropped, %ld failures\n", stats.records, stats.accepted, stats.single, stats.fragmented, stats.fragments, stats.dropped, stats.failures); for (const auto& [key, count] : stats.by_port_size) if (key.second > link::v2x_rx_max_payload) std::printf(" port %u, %zu bytes: %ld\n", key.first, key.second, count); return stats.failures ? 1 : 0; }