Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

9.1 KB
Raw Blame
// Runs the shared framing vectors against the firmware's own decoder, on a laptop.
//
//   devices/harness-device/firmware/test/run.sh
//
// This is the C half of the agreement described in main/cable_frame.h: the daemon's TypeScript decoder
// asserts against the same test/vectors/cable_frame.txt, generated by a third implementation. Nothing is
// mocked and nothing is reimplemented here — cable_frame.c is compiled as-is, which is possible because
// that file deliberately depends on no ESP-IDF header.
//
// Every stream case is replayed TWICE: once with the whole input handed over at once, and once a byte at
// a time. The second is not redundant on the day someone rewrites feed() to work in blocks — the split
// that matters is a frame's magic pair straddling two reads, and it only ever happens on a real cable.
#include "../main/cable_frame.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_HEX_BYTES 32768

static int failures;
static int checks;

static void fail(const char *name, const char *what)
{
    printf("  FAIL %-32s %s\n", name, what);
    failures++;
}

// ── hex ─────────────────────────────────────────────────────────────────────────────────────────────

static size_t unhex(const char *hex, uint8_t *out, size_t cap)
{
    if (strcmp(hex, "-") == 0) return 0;
    size_t n = 0;
    for (const char *p = hex; p[0] && p[1]; p += 2) {
        if (n >= cap) return n;
        char byte[3] = { p[0], p[1], 0 };
        out[n++] = (uint8_t)strtoul(byte, NULL, 16);
    }
    return n;
}

static void hexdump(const uint8_t *data, size_t n, char *out, size_t cap)
{
    size_t used = 0;
    for (size_t i = 0; i < n && used + 3 < cap; i++) used += (size_t)snprintf(out + used, cap - used, "%02x", data[i]);
    out[used < cap ? used : cap - 1] = 0;
}

// ── collecting decoded frames ───────────────────────────────────────────────────────────────────────

typedef struct {
    uint8_t  type[64];
    uint8_t  payload[64][CABLE_MAX_PAYLOAD];
    size_t   len[64];
    int      count;
} collected_t;

static void collect(uint8_t version, uint8_t type, const uint8_t *payload, size_t payload_len, void *ctx)
{
    collected_t *c = (collected_t *)ctx;
    if (version != CABLE_FRAME_VERSION) { printf("  (unexpected frame version %u)\n", version); failures++; }
    if (c->count >= 64) return;
    c->type[c->count] = type;
    c->len[c->count] = payload_len;
    if (payload_len) memcpy(c->payload[c->count], payload, payload_len);
    c->count++;
}

// ── the two case kinds ──────────────────────────────────────────────────────────────────────────────

static void run_encode(const char *name, int type, const char *payload_hex, const char *frame_hex)
{
    static uint8_t payload[MAX_HEX_BYTES], expected[MAX_HEX_BYTES], actual[MAX_HEX_BYTES];
    const size_t payload_len = unhex(payload_hex, payload, sizeof(payload));
    const size_t expected_len = unhex(frame_hex, expected, sizeof(expected));

    const int n = cable_frame_encode((uint8_t)type, payload, payload_len, actual, sizeof(actual));
    checks++;
    if (n < 0) { fail(name, "encode refused a payload the vectors say is legal"); return; }
    if ((size_t)n != expected_len || memcmp(actual, expected, expected_len) != 0) {
        static char got[MAX_HEX_BYTES * 2 + 1];
        hexdump(actual, (size_t)n, got, sizeof(got));
        printf("  FAIL %-32s bytes differ\n       want %s\n       got  %s\n", name, frame_hex, got);
        failures++;
    }
}

// `expected` is "type:payload-hex,..." or "-".
static void compare(const char *name, const char *how, const collected_t *c,
                    const char *expected, uint32_t discarded, uint32_t corrupt,
                    uint32_t got_discarded, uint32_t got_corrupt)
{
    char label[160];
    snprintf(label, sizeof(label), "%s (%s)", name, how);

    int want_count = 0;
    if (strcmp(expected, "-") != 0) {
        want_count = 1;
        for (const char *p = expected; *p; p++) if (*p == ',') want_count++;
    }
    if (c->count != want_count) {
        char msg[96];
        snprintf(msg, sizeof(msg), "expected %d frame(s), decoded %d", want_count, c->count);
        fail(label, msg);
        return;
    }

    const char *p = expected;
    for (int i = 0; i < want_count; i++) {
        const int want_type = atoi(p);
        const char *colon = strchr(p, ':');
        const char *comma = strchr(p, ',');
        if (!colon) { fail(label, "malformed vector"); return; }
        static uint8_t want_payload[MAX_HEX_BYTES];
        char hex[MAX_HEX_BYTES * 2 + 1];
        const size_t hex_len = comma ? (size_t)(comma - colon - 1) : strlen(colon + 1);
        if (hex_len >= sizeof(hex)) { fail(label, "vector payload too long for the test"); return; }
        memcpy(hex, colon + 1, hex_len);
        hex[hex_len] = 0;
        const size_t want_len = unhex(hex, want_payload, sizeof(want_payload));

        if (c->type[i] != want_type || c->len[i] != want_len ||
            memcmp(c->payload[i], want_payload, want_len) != 0) {
            char msg[128];
            snprintf(msg, sizeof(msg), "frame %d: want type %d/%zu bytes, got type %u/%zu bytes",
                     i, want_type, want_len, c->type[i], c->len[i]);
            fail(label, msg);
            return;
        }
        if (!comma) break;
        p = comma + 1;
    }

    // The counters are asserted, not merely printed: they are the only evidence that resync stepped over
    // exactly what it should have. A decoder that swallows a good frame along with the noise in front of
    // it passes every payload check and fails here.
    if (got_discarded != discarded || got_corrupt != corrupt) {
        char msg[128];
        snprintf(msg, sizeof(msg), "counters: want discarded=%u corrupt=%u, got discarded=%u corrupt=%u",
                 discarded, corrupt, got_discarded, got_corrupt);
        fail(label, msg);
    }
}

static void run_stream(const char *name, const char *input_hex, const char *expected,
                       uint32_t discarded, uint32_t corrupt)
{
    static uint8_t input[MAX_HEX_BYTES];
    const size_t n = unhex(input_hex, input, sizeof(input));

    static collected_t c;
    static cable_decoder_t d;

    memset(&c, 0, sizeof(c));
    cable_decoder_init(&d);
    cable_decoder_feed(&d, input, n, collect, &c);
    checks++;
    compare(name, "bulk", &c, expected, discarded, corrupt, d.discarded_bytes, d.corrupt_frames);

    memset(&c, 0, sizeof(c));
    cable_decoder_init(&d);
    for (size_t i = 0; i < n; i++) cable_decoder_feed(&d, input + i, 1, collect, &c);
    checks++;
    compare(name, "byte at a time", &c, expected, discarded, corrupt, d.discarded_bytes, d.corrupt_frames);
}

// ── driver ──────────────────────────────────────────────────────────────────────────────────────────

// Split on tabs in place. Returns the field count.
static int split(char *line, char **fields, int max)
{
    int n = 0;
    char *p = line;
    while (n < max) {
        fields[n++] = p;
        char *tab = strchr(p, '\t');
        if (!tab) break;
        *tab = 0;
        p = tab + 1;
    }
    return n;
}

int main(int argc, char **argv)
{
    const char *path = argc > 1 ? argv[1] : "test/vectors/cable_frame.txt";
    FILE *f = fopen(path, "r");
    if (!f) { fprintf(stderr, "cannot open %s\n", path); return 2; }

    // The published check value. If this is wrong every vector is wrong, and the failures below would
    // read as a hundred unrelated bugs.
    if (cable_crc16((const uint8_t *)"123456789", 9) != 0x29B1) {
        fprintf(stderr, "cable_crc16 is not CRC-16/CCITT-FALSE\n");
        return 2;
    }

    printf("vectors: %s\n", path);
    char *line = NULL;
    size_t cap = 0;
    ssize_t len;
    while ((len = getline(&line, &cap, f)) > 0) {
        while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) line[--len] = 0;
        if (len == 0 || line[0] == '#') continue;

        char *field[8];
        const int n = split(line, field, 8);
        if (n >= 5 && strcmp(field[0], "encode") == 0) {
            run_encode(field[1], atoi(field[2]), field[3], field[4]);
        } else if (n >= 6 && strcmp(field[0], "stream") == 0) {
            run_stream(field[1], field[2], field[3], (uint32_t)strtoul(field[4], NULL, 10),
                       (uint32_t)strtoul(field[5], NULL, 10));
        } else {
            fprintf(stderr, "unreadable vector line: %s\n", field[0]);
            failures++;
        }
    }
    free(line);
    fclose(f);

    printf("%s: %d checks, %d failure(s)\n", failures ? "FAILED" : "ok", checks, failures);
    return failures ? 1 : 0;
}