Skip to content

mesa: add test for encoding failure #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ fn buildBinary(
.file = "pkg/vere/benchmarks.c",
.deps = vere_test_deps,
},
.{
.name = "pact_test",
.file = "pkg/vere/io/mesa/pact_test.c",
.deps = vere_test_deps,
},
};

for (tests) |tst| {
Expand Down
4 changes: 2 additions & 2 deletions pkg/vere/io/mesa/mesa.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ typedef struct _u3_mesa_data_meta {

typedef enum {
AUTH_SIGN = 0,
AUTH_HMAC = 1,
AUTH_NONE = 2,
AUTH_NONE = 1,
AUTH_HMAC = 2,
AUTH_PAIR = 3,
} u3_mesa_auth_type;

Expand Down
79 changes: 48 additions & 31 deletions pkg/vere/io/mesa/pact.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ _mesa_rank(u3_ship who_u)
static c3_y
_mesa_make_chub_tag(c3_d tot_d)
{
return (tot_d <= 0xff)? 0b00 :
(tot_d <= 0xffff)? 0b01 :
(tot_d <= 0xffffff)? 0b10 :
0b11;
return (tot_d <= 0xff)? 0b00 :
(tot_d <= 0xffff)? 0b01 :
(tot_d <= 0xffffffff)? 0b10 :
0b11;
}

/*
Expand Down Expand Up @@ -618,14 +618,16 @@ _mesa_etch_name(u3_etcher *ech_u, u3_mesa_name* nam_u)
_etch_bits(ech_u, 2, met_u.gaf_y);
c3_y her_y = 2 << met_u.ran_y; // XX confirm
_etch_ship(ech_u, nam_u->her_u, her_y);
// log_name(nam_u);
_etch_var_word(ech_u, nam_u->rif_w, met_u.rif_y + 1);
_etch_byte(ech_u, nam_u->boq_y);

if ( met_u.nit_y ) {
// init packet
}
else {
_etch_var_word(ech_u, nam_u->fra_d, 1 << met_u.gaf_y);
// log_name(nam_u);
_etch_var_chub(ech_u, nam_u->fra_d, 1 << met_u.gaf_y);
}

_etch_short(ech_u, nam_u->pat_s);
Expand Down Expand Up @@ -659,7 +661,7 @@ _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u)
nam_u->fra_d = 0;
}
else {
nam_u->fra_d = _sift_var_word(sif_u, 1 << met_u.gaf_y);
nam_u->fra_d = _sift_var_chub(sif_u, 1 << met_u.gaf_y);
}

nam_u->pat_s = _sift_short(sif_u);
Expand Down Expand Up @@ -1172,13 +1174,13 @@ _test_cmp_name(u3_mesa_name* hav_u, u3_mesa_name* ned_u)
{
c3_i ret_i = 0;

cmp_buffer(her_d, sizeof(ned_u->her_u), "name: ships differ");
cmp_buffer(her_u, sizeof(ned_u->her_u), "name: ships differ");

cmp_scalar(rif_w, "name: rifts", "%u");
cmp_scalar(boq_y, "name: bloqs", "%u");
cmp_scalar(nit_o, "name: inits", "%u");
cmp_scalar(aut_o, "name: auths", "%u");
cmp_scalar(fra_w, "name: fragments", "%u");
cmp_scalar(fra_d, "name: fragments", "%" PRIu64);
cmp_scalar(pat_s, "name: path-lengths", "%u");

cmp_string(pat_c, ned_u->pat_s, "name: paths");
Expand All @@ -1191,13 +1193,16 @@ _test_cmp_data(u3_mesa_data* hav_u, u3_mesa_data* ned_u)
{
c3_i ret_i = 0;

cmp_scalar(tot_w, "data: total packets", "%u");
cmp_scalar(tob_d, "data: total packets", "%" PRIu64);

cmp_scalar(aut_u.typ_e, "data: auth-types", "%u");
cmp_buffer(aut_u.sig_y, 64, "data: sig|hmac|null");

cmp_scalar(aut_u.len_y, "data: hash-lengths", "%u");
cmp_buffer(aut_u.has_y, ned_u->aut_u.len_y, "data: hashes");
// cmp_scalar(aut_u.len_y, "data: hash-lengths", "%u");

if ( AUTH_PAIR == ned_u->aut_u.typ_e ) {
cmp_buffer(aut_u.has_y, 2, "data: hashes");
}

cmp_scalar(len_w, "data: fragments-lengths", "%u");
cmp_buffer(fra_y, ned_u->len_w, "data: fragments");
Expand All @@ -1209,7 +1214,7 @@ static c3_i
_test_pact(u3_mesa_pact* pac_u)
{
c3_y* buf_y = c3_calloc(PACT_SIZE);
c3_w len_w = mesa_etch_pact(buf_y, pac_u);
c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u);
c3_i ret_i = 0;
c3_i bot_i = 0;
c3_w sif_w;
Expand All @@ -1226,8 +1231,12 @@ _test_pact(u3_mesa_pact* pac_u)
ret_i = 1; goto done;
}

if ( len_w != (sif_w = _mesa_sift_pact(&nex_u, buf_y, len_w)) ) {
fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_w);
u3_sifter sif_u;
sifter_init(&sif_u, buf_y, len_w);
_mesa_sift_pact(&sif_u, &nex_u);

if ( sif_u.rem_w && !sif_u.err_c ) {
fprintf(stderr, "pact: sift failed len=%u sif=%u\r\n", len_w, sif_u.rem_w);
_log_buf(buf_y, len_w);
ret_i = 1; goto done;
}
Expand Down Expand Up @@ -1319,6 +1328,14 @@ _test_rand_word(void* ptr_v)
return (hig_w << 16) ^ (low_w & ((1 << 16) - 1));
}

static c3_d
_test_rand_chub(void* ptr_v)
{
c3_w low_w = _test_rand_word(ptr_v);
c3_w hig_w = _test_rand_word(ptr_v);
return ((c3_d)hig_w << 32) ^ low_w;
}

static c3_y
_test_rand_gulf_y(void* ptr_v, c3_y top_y)
{
Expand Down Expand Up @@ -1397,7 +1414,7 @@ _test_rand_path(void* ptr_v, c3_s len_s, c3_c* pat_c)
static void
_test_make_head(void* ptr_v, u3_mesa_head* hed_u)
{
hed_u->nex_y = NEXH_NONE; // XX
hed_u->nex_y = HOP_NONE; // XX
hed_u->pro_y = 1;
hed_u->typ_y = _test_rand_gulf_y(ptr_v, 3) + 1;
hed_u->hop_y = _test_rand_gulf_y(ptr_v, 8);
Expand All @@ -1421,43 +1438,43 @@ _test_make_name(void* ptr_v, c3_s pat_s, u3_mesa_name* nam_u)

if ( c3y == nam_u->nit_o ) {
nam_u->aut_o = c3n;
nam_u->fra_w = 0;
nam_u->fra_d = 0;
}
else {
nam_u->aut_o = _test_rand_bits(ptr_v, 1);
nam_u->fra_w = _test_rand_word(ptr_v);
nam_u->fra_d = _test_rand_chub(ptr_v);
}
}

static void
_test_make_data(void* ptr_v, u3_mesa_data* dat_u)
{
dat_u->tot_w = _test_rand_word(ptr_v);
dat_u->tob_d = _test_rand_chub(ptr_v);

memset(dat_u->aut_u.sig_y, 0, 64);
dat_u->aut_u.len_y = 0;
// dat_u->aut_u.len_y = 0;
memset(dat_u->aut_u.has_y, 0, sizeof(dat_u->aut_u.has_y));

switch ( dat_u->aut_u.typ_e = _test_rand_bits(ptr_v, 2) ) {
case AUTH_NEXT: {
dat_u->aut_u.len_y = 2;
} break;

case AUTH_SIGN: {
dat_u->aut_u.len_y = _test_rand_gulf_y(ptr_v, 3);
_test_rand_bytes(ptr_v, 64, dat_u->aut_u.sig_y);
} break;

case AUTH_HMAC: {
dat_u->aut_u.len_y = _test_rand_gulf_y(ptr_v, 3);
_test_rand_bytes(ptr_v, 16, dat_u->aut_u.mac_y);
} break;

default: break;
}
case AUTH_NONE: {

} break;

for ( c3_w i_w = 0; i_w < dat_u->aut_u.len_y; i_w++ ) {
_test_rand_bytes(ptr_v, 32, dat_u->aut_u.has_y[i_w]);
case AUTH_PAIR: {
_test_rand_bytes(ptr_v, 32, dat_u->aut_u.has_y[0]);
_test_rand_bytes(ptr_v, 32, dat_u->aut_u.has_y[1]);
} break;

default: break;
}

dat_u->len_w = _test_rand_gulf_w(ptr_v, 1024);
Expand Down Expand Up @@ -1531,12 +1548,12 @@ _test_sift_page()
nam_u->pat_c = "foo/bar";
nam_u->pat_s = strlen(nam_u->pat_c);
nam_u->boq_y = 13;
nam_u->fra_w = 54;
nam_u->fra_d = 54;
nam_u->nit_o = c3n;

u3_mesa_data* dat_u = &pac_u.pag_u.dat_u;
dat_u->aut_u.typ_e = AUTH_NONE;
dat_u->tot_w = 1000;
dat_u->tob_d = 1000;
dat_u->len_w = 1024;
dat_u->fra_y = c3_calloc(1024);
// dat_u->fra_y[1023] = 1;
Expand All @@ -1553,7 +1570,7 @@ _test_encode_path(c3_c* pat_c)
{
u3_noun wan = u3do("stab", u3dt("cat", 3, '/', u3i_string(pat_c)));
u3_noun hav = _mesa_encode_path(strlen(pat_c), (c3_y*)pat_c);

// u3m_p("hav", hav);
if ( c3n == u3r_sing(wan, hav) ) {
u3l_log(RED_TEXT);
u3l_log("path encoding mismatch");
Expand Down
3 changes: 3 additions & 0 deletions pkg/vere/io/mesa/pact_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define PACT_TEST

#include "pact.c"