aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / pi-spark-task / tools / pst_verify.c
| 1 | /* pst_verify.c: reference checker for PST1, the Pi-Spark task message. |
| 2 | * Spec: ../SPEC.md (contract 0.1.0, wire version 1): sections 4 and 7 for one |
| 3 | * message, section 10 for a recorded session, section 11 for tool-call tasks. |
| 4 | * |
| 5 | * Self-contained C99: its own SHA-256 (FIPS 180-4), libc only, no heap |
| 6 | * anywhere (static buffers), no clock. The decoder pst_decode() is a pure |
| 7 | * function of one read-only buffer; the encoder pst_encode() is a pure |
| 8 | * function of one message struct. It exists to pin the format with vectors, |
| 9 | * not to be linked into a runtime. |
| 10 | * |
| 11 | * Usage: |
| 12 | * pst_verify FILE print one verdict line |
| 13 | * pst_verify --corpus DIR check every DIR/expected.txt line, re-encode |
| 14 | * every accepted message and require the same |
| 15 | * bytes, then every DIR/sessions/expected.txt line; |
| 16 | * print PST1_CONFORMANCE impl=c pass=N fail=M |
| 17 | * pst_verify --session FILE check one recorded session (spec section 10) |
| 18 | * pst_verify --make-vectors DIR write the vector files, DIR/expected.txt, |
| 19 | * DIR/sessions/ (the directory must exist) and |
| 20 | * DIR/interplane-tool-request.json, the INTERPLANE |
| 21 | * canonical request the tool-call vectors carry |
| 22 | * pst_verify --session-fuzz SEED COUNT FILE... |
| 23 | * COUNT seeded reorderings, drops and duplications |
| 24 | * of the given sessions; prints the verdict counts |
| 25 | * and one digest of all verdict lines |
| 26 | * |
| 27 | * Verdict line (the format expected.txt pins): |
| 28 | * ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64 |
| 29 | * parent=H32 task=H32 idem=H32 ordinal=N effect=E len=L digest=H64 |
| 30 | * refuse CODE (CODE is a name from SPEC.md section 7) |
| 31 | * (one line; Hn is n lower-case hex digits). |
| 32 | * Session verdict line: ok steps=N tasks=T, or refuse CODE step=N. |
| 33 | * |
| 34 | * Exit status: 0 on accept / all corpus lines agree, 1 otherwise, 2 on usage |
| 35 | * or I/O error. |
| 36 | * |
| 37 | * Mutation hooks: building with -DPST_MUTANT=K disables check K (the CHK_* |
| 38 | * numbers below). tools/mutate-verifier.sh builds every mutant and requires |
| 39 | * the vectors to fail each one, which shows every check is exercised. |
| 40 | */ |
| 41 | #define _POSIX_C_SOURCE 200809L /* open, fdopen, close under -std=c99 */ |
| 42 | #include <fcntl.h> |
| 43 | #include <stdint.h> |
| 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include <unistd.h> |
| 48 | |
| 49 | #ifndef PST_MUTANT |
| 50 | #define PST_MUTANT 0 |
| 51 | #endif |
| 52 | #define CHECK(k) (PST_MUTANT != (k)) |
| 53 | |
| 54 | enum { |
| 55 | CHK_MAX_TOTAL = 1, CHK_MAGIC, CHK_DOWNGRADE, CHK_VERSION, CHK_TYPE, |
| 56 | CHK_RESERVED0, CHK_RESERVED1, CHK_PAYLOAD_SHORT, CHK_TRAILING, CHK_EFFECT, |
| 57 | CHK_CAUSE, CHK_TASK, CHK_IDEM, CHK_OBJECTIVE, CHK_DIGEST, CHK_TOOL_HEAD_LEN, CHK_TOOL_HEAD_BOUND, |
| 58 | /* session rules (spec section 10.3) */ |
| 59 | CHK_S_SIDE, CHK_S_SUBJECT, CHK_S_IDEM_OTHER, CHK_S_IDEM_SAME, CHK_S_UNKNOWN_TASK, |
| 60 | CHK_S_CONTEXT, CHK_S_REPEAT_REQUEST, CHK_S_GAP, CHK_S_REPEAT, CHK_S_SECOND_FINAL, |
| 61 | CHK_S_ORDER, |
| 62 | /* tool-call tasks (spec section 11) */ |
| 63 | CHK_S_NOT_TOOL, CHK_S_TOOL_CHUNK_SIZE, CHK_S_TOOL_DIGEST, CHK_S_TOOL_INCOMPLETE, CHK_LAST |
| 64 | }; |
| 65 | |
| 66 | /* ---- refusal codes (spec section 7) ------------------------------------- */ |
| 67 | enum { |
| 68 | PST_OK = 0, PST_BAD_MAGIC, PST_BAD_VERSION, PST_DOWNGRADE, PST_BAD_TYPE, |
| 69 | PST_WRONG_SIDE, PST_BAD_LENGTH, PST_TRAILING_BYTES, PST_RESERVED_NONZERO, |
| 70 | PST_NO_CAUSE, PST_NO_TASK, PST_NO_OBJECTIVE, PST_DIGEST_MISMATCH, |
| 71 | PST_BAD_EFFECT_CLASS, PST_SUBJECT_MISMATCH, PST_BAD_ORDER, PST_SECOND_FINAL, |
| 72 | PST_ORDINAL_GAP, PST_ORDINAL_REPEAT, PST_IDEMPOTENCE_CONFLICT, PST_UNKNOWN_TASK, |
| 73 | PST_CONTEXT_MISMATCH, PST_SESSION_FULL, PST_BAD_RECORDING, PST_BAD_TOOL_HEAD, |
| 74 | PST_TOOL_CHUNK_SIZE, PST_TOOL_DIGEST_MISMATCH, PST_TOOL_INCOMPLETE, PST_NOT_TOOL_TASK, PST_CODE_COUNT |
| 75 | }; |
| 76 | static const char *const code_names[PST_CODE_COUNT] = { |
| 77 | "OK", "PST_BAD_MAGIC", "PST_BAD_VERSION", "PST_DOWNGRADE", "PST_BAD_TYPE", |
| 78 | "PST_WRONG_SIDE", "PST_BAD_LENGTH", "PST_TRAILING_BYTES", "PST_RESERVED_NONZERO", |
| 79 | "PST_NO_CAUSE", "PST_NO_TASK", "PST_NO_OBJECTIVE", "PST_DIGEST_MISMATCH", |
| 80 | "PST_BAD_EFFECT_CLASS", "PST_SUBJECT_MISMATCH", "PST_BAD_ORDER", "PST_SECOND_FINAL", |
| 81 | "PST_ORDINAL_GAP", "PST_ORDINAL_REPEAT", "PST_IDEMPOTENCE_CONFLICT", "PST_UNKNOWN_TASK", |
| 82 | "PST_CONTEXT_MISMATCH", "PST_SESSION_FULL", "PST_BAD_RECORDING", "PST_BAD_TOOL_HEAD", |
| 83 | "PST_TOOL_CHUNK_SIZE", "PST_TOOL_DIGEST_MISMATCH", "PST_TOOL_INCOMPLETE", "PST_NOT_TOOL_TASK" |
| 84 | }; |
| 85 | |
| 86 | /* ---- SHA-256 ------------------------------------------------------------ */ |
| 87 | typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256; |
| 88 | |
| 89 | static const uint32_t K256[64] = { |
| 90 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
| 91 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
| 92 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
| 93 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, |
| 94 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, |
| 95 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, |
| 96 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, |
| 97 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
| 98 | }; |
| 99 | #define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r)))) |
| 100 | |
| 101 | static void sha_block(sha256 *s, const uint8_t *p) { |
| 102 | uint32_t w[64], a, b, c, d, e, f, g, h; |
| 103 | for (int i = 0; i < 16; i++) |
| 104 | w[i] = (uint32_t)p[4*i] << 24 | (uint32_t)p[4*i+1] << 16 | (uint32_t)p[4*i+2] << 8 | p[4*i+3]; |
| 105 | for (int i = 16; i < 64; i++) { |
| 106 | uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3); |
| 107 | uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10); |
| 108 | w[i] = w[i-16] + s0 + w[i-7] + s1; |
| 109 | } |
| 110 | a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; |
| 111 | e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7]; |
| 112 | for (int i = 0; i < 64; i++) { |
| 113 | uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i]; |
| 114 | uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); |
| 115 | h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; |
| 116 | } |
| 117 | s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; |
| 118 | s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h; |
| 119 | } |
| 120 | static void sha_init(sha256 *s) { |
| 121 | static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a, |
| 122 | 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 }; |
| 123 | memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0; |
| 124 | } |
| 125 | static void sha_update(sha256 *s, const void *data, size_t len) { |
| 126 | const uint8_t *p = data; |
| 127 | s->n += len; |
| 128 | while (len) { |
| 129 | uint32_t k = 64 - s->used; |
| 130 | if (k > len) k = (uint32_t)len; |
| 131 | memcpy(s->b + s->used, p, k); |
| 132 | s->used += k; p += k; len -= k; |
| 133 | if (s->used == 64) { sha_block(s, s->b); s->used = 0; } |
| 134 | } |
| 135 | } |
| 136 | static void sha_final(sha256 *s, uint8_t out[32]) { |
| 137 | uint64_t bits = s->n * 8; |
| 138 | uint8_t pad = 0x80, z = 0, l[8]; |
| 139 | sha_update(s, &pad, 1); |
| 140 | while (s->used != 56) sha_update(s, &z, 1); |
| 141 | for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i)); |
| 142 | sha_update(s, l, 8); |
| 143 | for (int i = 0; i < 8; i++) { |
| 144 | out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16); |
| 145 | out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i]; |
| 146 | } |
| 147 | } |
| 148 | static void sha_once(const void *p, size_t n, uint8_t out[32]) { |
| 149 | sha256 s; sha_init(&s); sha_update(&s, p, n); sha_final(&s, out); |
| 150 | } |
| 151 | |
| 152 | /* ---- big-endian helpers -------------------------------------------------- */ |
| 153 | static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] << 8 | p[1]); } |
| 154 | static uint32_t g32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; } |
| 155 | static void p16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } |
| 156 | static void p32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; } |
| 157 | static int all_zero(const uint8_t *p, size_t n) { while (n--) if (*p++) return 0; return 1; } |
| 158 | |
| 159 | /* ---- the format (spec section 4) ---------------------------------------- */ |
| 160 | #define PST_HDR 224u |
| 161 | #define PST_MAX_PAY 800u |
| 162 | #define PST_MAX_MSG 1024u |
| 163 | #define PST_VERSION 1u |
| 164 | #define PST_MAX_TYPE 16u |
| 165 | #define T_REQUEST 1u |
| 166 | #define T_TOOL_REQ 15u /* tool_call_request (spec section 11) */ |
| 167 | #define T_TOOL_CHUNK 16u /* tool_call_chunk */ |
| 168 | #define PST_TOOL_HEAD 36u /* INTERPLANE digest (32) + canonical length (4) */ |
| 169 | #define PST_TOOL_MAX 102400u /* 128 chunks of 800 bytes */ |
| 170 | |
| 171 | enum { |
| 172 | O_MAGIC = 0, O_VERSION = 4, O_TYPE = 5, O_RES0 = 6, O_CAUSE = 8, O_ORIGIN = 40, |
| 173 | O_OBJECTIVE = 72, O_SUBJECT = 104, O_PARENT = 136, O_TASK = 152, O_IDEM = 168, |
| 174 | O_ORDINAL = 184, O_EFFECT = 188, O_RES1 = 189, O_PLEN = 190, O_DIGEST = 192 |
| 175 | }; |
| 176 | |
| 177 | typedef struct { |
| 178 | uint8_t version, type; |
| 179 | uint8_t cause[32], origin[32], objective[32], subject[32]; |
| 180 | uint8_t parent[16], task[16], idem[16]; |
| 181 | uint32_t ordinal; |
| 182 | uint8_t effect; |
| 183 | uint16_t payload_len; |
| 184 | uint8_t digest[32]; |
| 185 | const uint8_t *payload; /* points into the decoded buffer */ |
| 186 | } pst_msg; |
| 187 | |
| 188 | /* Decode and check one message (spec section 4.1 check order). Reads only |
| 189 | * b[0..n); writes only *m. Returns PST_OK or the first refusal code. */ |
| 190 | static int pst_decode(const uint8_t *b, size_t n, pst_msg *m) { |
| 191 | size_t body; |
| 192 | uint8_t d[32]; |
| 193 | |
| 194 | memset(m, 0, sizeof *m); |
| 195 | /* 1. length bounds (the short bound is not a mutation point: it guards every read) */ |
| 196 | if (n < PST_HDR) return PST_BAD_LENGTH; |
| 197 | if (CHECK(CHK_MAX_TOTAL) && n > PST_MAX_MSG) return PST_BAD_LENGTH; |
| 198 | /* 2. magic */ |
| 199 | if (CHECK(CHK_MAGIC) && memcmp(b + O_MAGIC, "PST1", 4) != 0) return PST_BAD_MAGIC; |
| 200 | /* 3. version */ |
| 201 | if (CHECK(CHK_DOWNGRADE) && b[O_VERSION] < PST_VERSION) return PST_DOWNGRADE; |
| 202 | if (CHECK(CHK_VERSION) && b[O_VERSION] > PST_VERSION) return PST_BAD_VERSION; |
| 203 | /* 4. type */ |
| 204 | if (CHECK(CHK_TYPE) && (b[O_TYPE] == 0 || b[O_TYPE] > PST_MAX_TYPE)) return PST_BAD_TYPE; |
| 205 | /* 5. reserved */ |
| 206 | if (CHECK(CHK_RESERVED0) && !all_zero(b + O_RES0, 2)) return PST_RESERVED_NONZERO; |
| 207 | if (CHECK(CHK_RESERVED1) && b[O_RES1] != 0) return PST_RESERVED_NONZERO; |
| 208 | /* 6, 7. payload length against the bytes present */ |
| 209 | m->payload_len = g16(b + O_PLEN); |
| 210 | body = n - PST_HDR; |
| 211 | if (CHECK(CHK_PAYLOAD_SHORT) && body < m->payload_len) return PST_BAD_LENGTH; |
| 212 | if (CHECK(CHK_TRAILING) && body > m->payload_len) return PST_TRAILING_BYTES; |
| 213 | /* 8..11. field rules */ |
| 214 | if (CHECK(CHK_EFFECT) && b[O_EFFECT] > 2) return PST_BAD_EFFECT_CLASS; |
| 215 | if (CHECK(CHK_CAUSE) && all_zero(b + O_CAUSE, 32)) return PST_NO_CAUSE; |
| 216 | if (CHECK(CHK_TASK) && all_zero(b + O_TASK, 16)) return PST_NO_TASK; |
| 217 | if (CHECK(CHK_IDEM) && all_zero(b + O_IDEM, 16)) return PST_NO_TASK; |
| 218 | if (CHECK(CHK_OBJECTIVE) && (b[O_TYPE] == T_REQUEST || b[O_TYPE] == T_TOOL_REQ) && all_zero(b + O_OBJECTIVE, 32)) |
| 219 | return PST_NO_OBJECTIVE; |
| 220 | /* 12. payload digest (over the bytes present, so a mutant never reads past n) */ |
| 221 | sha_once(b + PST_HDR, body < m->payload_len ? body : m->payload_len, d); |
| 222 | if (CHECK(CHK_DIGEST) && memcmp(d, b + O_DIGEST, 32) != 0) return PST_DIGEST_MISMATCH; |
| 223 | /* 13. tool-call head (spec section 11.2): exactly 36 bytes, length 1..102400 */ |
| 224 | if (b[O_TYPE] == T_TOOL_REQ) { |
| 225 | if (CHECK(CHK_TOOL_HEAD_LEN) && m->payload_len != PST_TOOL_HEAD) return PST_BAD_TOOL_HEAD; |
| 226 | if (body >= PST_TOOL_HEAD && CHECK(CHK_TOOL_HEAD_BOUND)) { |
| 227 | uint32_t len = g32(b + PST_HDR + 32); |
| 228 | if (len == 0 || len > PST_TOOL_MAX) return PST_BAD_TOOL_HEAD; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | m->version = b[O_VERSION]; |
| 233 | m->type = b[O_TYPE]; |
| 234 | memcpy(m->cause, b + O_CAUSE, 32); |
| 235 | memcpy(m->origin, b + O_ORIGIN, 32); |
| 236 | memcpy(m->objective, b + O_OBJECTIVE, 32); |
| 237 | memcpy(m->subject, b + O_SUBJECT, 32); |
| 238 | memcpy(m->parent, b + O_PARENT, 16); |
| 239 | memcpy(m->task, b + O_TASK, 16); |
| 240 | memcpy(m->idem, b + O_IDEM, 16); |
| 241 | m->ordinal = g32(b + O_ORDINAL); |
| 242 | m->effect = b[O_EFFECT]; |
| 243 | memcpy(m->digest, b + O_DIGEST, 32); |
| 244 | m->payload = b + PST_HDR; |
| 245 | return PST_OK; |
| 246 | } |
| 247 | |
| 248 | /* Encode one message. Writes the fields exactly as given (the digest is not |
| 249 | * recomputed, so a bad digest can be encoded on purpose). Returns the byte |
| 250 | * count, or 0 if it does not fit in cap or the payload is over the cap. */ |
| 251 | static size_t pst_encode(const pst_msg *m, uint8_t *out, size_t cap) { |
| 252 | size_t n = PST_HDR + (size_t)m->payload_len; |
| 253 | if (m->payload_len > PST_MAX_PAY || n > cap) return 0; |
| 254 | memset(out, 0, PST_HDR); |
| 255 | memcpy(out + O_MAGIC, "PST1", 4); |
| 256 | out[O_VERSION] = m->version; |
| 257 | out[O_TYPE] = m->type; |
| 258 | memcpy(out + O_CAUSE, m->cause, 32); |
| 259 | memcpy(out + O_ORIGIN, m->origin, 32); |
| 260 | memcpy(out + O_OBJECTIVE, m->objective, 32); |
| 261 | memcpy(out + O_SUBJECT, m->subject, 32); |
| 262 | memcpy(out + O_PARENT, m->parent, 16); |
| 263 | memcpy(out + O_TASK, m->task, 16); |
| 264 | memcpy(out + O_IDEM, m->idem, 16); |
| 265 | p32(out + O_ORDINAL, m->ordinal); |
| 266 | out[O_EFFECT] = m->effect; |
| 267 | p16(out + O_PLEN, m->payload_len); |
| 268 | memcpy(out + O_DIGEST, m->digest, 32); |
| 269 | if (m->payload_len) memcpy(out + PST_HDR, m->payload, m->payload_len); |
| 270 | return n; |
| 271 | } |
| 272 | |
| 273 | /* ---- session rules (spec section 10) --------------------------------------- */ |
| 274 | #define SIDE_PI 1u |
| 275 | #define SIDE_SPARK 2u |
| 276 | #define MAX_TASKS 16u |
| 277 | /* ST_COLLECTING: a tool-call task whose canonical bytes are not all in yet (section 11.3). */ |
| 278 | enum { ST_COLLECTING = 1, ST_REQUESTED, ST_ACCEPTED, ST_QUEUED, ST_RUNNING, ST_FINAL }; |
| 279 | |
| 280 | typedef struct { |
| 281 | uint8_t task[16], idem[16], req_digest[32]; |
| 282 | uint8_t cause[32], origin[32], objective[32], parent[16], effect; |
| 283 | int state; |
| 284 | uint64_t next_ordinal[3]; /* indexed by side; [0] unused */ |
| 285 | uint8_t opener; /* T_REQUEST (chat) or T_TOOL_REQ (tool call) */ |
| 286 | uint8_t tool_digest[32]; /* from the tool-call head */ |
| 287 | uint32_t tool_len, tool_got; |
| 288 | sha256 tool_sha; /* running SHA-256 of the chunks so far */ |
| 289 | } task_slot; |
| 290 | |
| 291 | typedef struct { int code; unsigned long step, tasks; } session_verdict; |
| 292 | |
| 293 | static int side_of(uint8_t type) { |
| 294 | return type == 1 || type == 13 || type == 14 || type == T_TOOL_REQ || type == T_TOOL_CHUNK ? SIDE_PI : SIDE_SPARK; |
| 295 | } |
| 296 | static int is_opener(uint8_t type) { return type == T_REQUEST || type == T_TOOL_REQ; } |
| 297 | |
| 298 | static int same_context(const task_slot *t, const pst_msg *m) { |
| 299 | return memcmp(t->cause, m->cause, 32) == 0 && memcmp(t->origin, m->origin, 32) == 0 && |
| 300 | memcmp(t->objective, m->objective, 32) == 0 && memcmp(t->parent, m->parent, 16) == 0 && |
| 301 | memcmp(t->idem, m->idem, 16) == 0 && t->effect == m->effect; |
| 302 | } |
| 303 | |
| 304 | /* Allowed step for one task (spec section 10.2). Returns the new state, or |
| 305 | * 0 if the type is not allowed in state s. */ |
| 306 | static int next_state(int s, uint8_t type) { |
| 307 | switch (type) { |
| 308 | case 2: return s == ST_REQUESTED ? ST_ACCEPTED : 0; |
| 309 | case 3: return s == ST_ACCEPTED ? ST_QUEUED : 0; |
| 310 | case 4: return s == ST_ACCEPTED || s == ST_QUEUED ? ST_RUNNING : 0; |
| 311 | case 5: return s == ST_RUNNING ? ST_RUNNING : 0; |
| 312 | case 7: return s == ST_RUNNING ? ST_FINAL : 0; |
| 313 | case 8: case 9: return s != ST_FINAL ? ST_FINAL : 0; |
| 314 | case 13: case 14: return s; |
| 315 | default: return 0; /* types 6, 10, 11, 12: no session rule yet */ |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static int srefuse(session_verdict *v, int code, unsigned long step) { v->code = code; v->step = step; return code; } |
| 320 | |
| 321 | /* Check one recorded session (spec section 10.4 container). Reads only |
| 322 | * b[0..n); keeps its task table on the stack. Returns PST_OK or the first |
| 323 | * refusal code, with the 1-based step in v->step. */ |
| 324 | static int session_check(const uint8_t *b, size_t n, session_verdict *v) { |
| 325 | task_slot tasks[MAX_TASKS]; |
| 326 | unsigned ntasks = 0; |
| 327 | uint8_t subject[32]; |
| 328 | size_t o = 4; |
| 329 | unsigned long step = 0; |
| 330 | |
| 331 | memset(v, 0, sizeof *v); |
| 332 | memset(tasks, 0, sizeof tasks); |
| 333 | if (n < 4 || memcmp(b, "PSS1", 4) != 0) return srefuse(v, PST_BAD_RECORDING, 0); |
| 334 | while (o < n) { |
| 335 | pst_msg m; |
| 336 | task_slot *t = NULL; |
| 337 | unsigned side, len, i; |
| 338 | int code, ns, repeat = 0; |
| 339 | |
| 340 | step++; |
| 341 | /* the container (a guard, not a protocol rule) */ |
| 342 | if (n - o < 3) return srefuse(v, PST_BAD_RECORDING, step); |
| 343 | side = b[o]; |
| 344 | len = g16(b + o + 1); |
| 345 | if ((side != SIDE_PI && side != SIDE_SPARK) || n - o - 3 < len) return srefuse(v, PST_BAD_RECORDING, step); |
| 346 | /* 1. the single-message checks */ |
| 347 | code = pst_decode(b + o + 3, len, &m); |
| 348 | o += 3 + (size_t)len; |
| 349 | if (code != PST_OK) return srefuse(v, code, step); |
| 350 | /* 2. side */ |
| 351 | if (CHECK(CHK_S_SIDE) && side_of(m.type) != (int)side) return srefuse(v, PST_WRONG_SIDE, step); |
| 352 | /* 3. subject */ |
| 353 | if (step == 1) memcpy(subject, m.subject, 32); |
| 354 | else if (CHECK(CHK_S_SUBJECT) && memcmp(subject, m.subject, 32) != 0) return srefuse(v, PST_SUBJECT_MISMATCH, step); |
| 355 | /* 4. task */ |
| 356 | for (i = 0; i < ntasks; i++) |
| 357 | if (memcmp(tasks[i].task, m.task, 16) == 0) { t = &tasks[i]; break; } |
| 358 | if (is_opener(m.type) && !t) { |
| 359 | for (i = 0; i < ntasks; i++) |
| 360 | if (CHECK(CHK_S_IDEM_OTHER) && memcmp(tasks[i].idem, m.idem, 16) == 0) |
| 361 | return srefuse(v, PST_IDEMPOTENCE_CONFLICT, step); |
| 362 | if (ntasks == MAX_TASKS) return srefuse(v, PST_SESSION_FULL, step); |
| 363 | t = &tasks[ntasks++]; |
| 364 | memcpy(t->task, m.task, 16); memcpy(t->idem, m.idem, 16); memcpy(t->req_digest, m.digest, 32); |
| 365 | memcpy(t->cause, m.cause, 32); memcpy(t->origin, m.origin, 32); |
| 366 | memcpy(t->objective, m.objective, 32); memcpy(t->parent, m.parent, 16); |
| 367 | t->effect = m.effect; |
| 368 | t->opener = m.type; |
| 369 | t->state = ST_REQUESTED; |
| 370 | if (m.type == T_TOOL_REQ && m.payload_len >= PST_TOOL_HEAD) { /* section 11.3: collect the bytes first */ |
| 371 | memcpy(t->tool_digest, m.payload, 32); |
| 372 | t->tool_len = g32(m.payload + 32); |
| 373 | sha_init(&t->tool_sha); |
| 374 | t->state = ST_COLLECTING; |
| 375 | } |
| 376 | } else if (is_opener(m.type)) { |
| 377 | repeat = 1; |
| 378 | if (CHECK(CHK_S_IDEM_SAME) && (memcmp(t->idem, m.idem, 16) != 0 || memcmp(t->req_digest, m.digest, 32) != 0 || |
| 379 | t->opener != m.type)) |
| 380 | return srefuse(v, PST_IDEMPOTENCE_CONFLICT, step); |
| 381 | } else if (!t) { |
| 382 | if (CHECK(CHK_S_UNKNOWN_TASK)) return srefuse(v, PST_UNKNOWN_TASK, step); |
| 383 | continue; /* mutant only: ignore the message */ |
| 384 | } |
| 385 | /* 5. context; a recognised repeat of the request stops here */ |
| 386 | if (CHECK(CHK_S_CONTEXT) && !same_context(t, &m)) return srefuse(v, PST_CONTEXT_MISMATCH, step); |
| 387 | if (repeat && CHECK(CHK_S_REPEAT_REQUEST)) continue; |
| 388 | /* 6. ordinal, per task and side */ |
| 389 | if (CHECK(CHK_S_GAP) && m.ordinal > t->next_ordinal[side]) return srefuse(v, PST_ORDINAL_GAP, step); |
| 390 | if (CHECK(CHK_S_REPEAT) && m.ordinal < t->next_ordinal[side]) return srefuse(v, PST_ORDINAL_REPEAT, step); |
| 391 | t->next_ordinal[side] = (uint64_t)m.ordinal + 1; |
| 392 | /* 7. tool-call body (section 11.3), then order */ |
| 393 | if (is_opener(m.type)) continue; /* the new task is REQUESTED, or COLLECTING */ |
| 394 | if (m.type == T_TOOL_CHUNK && (t->opener == T_TOOL_REQ || CHECK(CHK_S_NOT_TOOL))) { |
| 395 | uint32_t left = t->tool_len - t->tool_got, want = left < PST_MAX_PAY ? left : PST_MAX_PAY; |
| 396 | uint8_t d[32]; |
| 397 | if (t->opener != T_TOOL_REQ) return srefuse(v, PST_NOT_TOOL_TASK, step); |
| 398 | if (t->state != ST_COLLECTING) { |
| 399 | if (CHECK(CHK_S_ORDER)) return srefuse(v, PST_BAD_ORDER, step); |
| 400 | continue; /* mutant only */ |
| 401 | } |
| 402 | if (CHECK(CHK_S_TOOL_CHUNK_SIZE) && m.payload_len != want) return srefuse(v, PST_TOOL_CHUNK_SIZE, step); |
| 403 | sha_update(&t->tool_sha, m.payload, m.payload_len); |
| 404 | t->tool_got += m.payload_len; |
| 405 | if (t->tool_got != t->tool_len) continue; /* more chunks to come */ |
| 406 | sha_final(&t->tool_sha, d); |
| 407 | if (CHECK(CHK_S_TOOL_DIGEST) && memcmp(d, t->tool_digest, 32) != 0) return srefuse(v, PST_TOOL_DIGEST_MISMATCH, step); |
| 408 | t->state = ST_REQUESTED; /* the whole request is in and matches its digest */ |
| 409 | continue; |
| 410 | } |
| 411 | if (CHECK(CHK_S_TOOL_INCOMPLETE) && t->state == ST_COLLECTING && side == SIDE_SPARK && m.type != 8 && m.type != 9) |
| 412 | return srefuse(v, PST_TOOL_INCOMPLETE, step); |
| 413 | ns = next_state(t->state, m.type); |
| 414 | if (!ns) { |
| 415 | if (CHECK(CHK_S_SECOND_FINAL) && t->state == ST_FINAL && (m.type == 7 || m.type == 8 || m.type == 9)) |
| 416 | return srefuse(v, PST_SECOND_FINAL, step); |
| 417 | if (CHECK(CHK_S_ORDER)) return srefuse(v, PST_BAD_ORDER, step); |
| 418 | continue; /* mutant only: keep the state */ |
| 419 | } |
| 420 | t->state = ns; |
| 421 | } |
| 422 | v->step = step; |
| 423 | v->tasks = ntasks; |
| 424 | return v->code = PST_OK; |
| 425 | } |
| 426 | |
| 427 | /* ---- verdict line ---------------------------------------------------------- */ |
| 428 | static char *hexw(char *o, const uint8_t *p, size_t n) { |
| 429 | static const char hx[] = "0123456789abcdef"; |
| 430 | for (size_t i = 0; i < n; i++) { *o++ = hx[p[i] >> 4]; *o++ = hx[p[i] & 15]; } |
| 431 | *o = 0; |
| 432 | return o; |
| 433 | } |
| 434 | static void verdict_line(int code, const pst_msg *m, char *buf, size_t cap) { |
| 435 | char c[65], o[65], ob[65], s[65], pa[33], t[33], k[33], d[65]; |
| 436 | if (code != PST_OK) { |
| 437 | snprintf(buf, cap, "refuse %s", code > 0 && code < PST_CODE_COUNT ? code_names[code] : "PST_UNKNOWN_CODE"); |
| 438 | return; |
| 439 | } |
| 440 | hexw(c, m->cause, 32); hexw(o, m->origin, 32); hexw(ob, m->objective, 32); hexw(s, m->subject, 32); |
| 441 | hexw(pa, m->parent, 16); hexw(t, m->task, 16); hexw(k, m->idem, 16); hexw(d, m->digest, 32); |
| 442 | snprintf(buf, cap, "ok type=%u version=%u cause=%s origin=%s objective=%s subject=%s parent=%s task=%s idem=%s ordinal=%lu effect=%u len=%u digest=%s", |
| 443 | m->type, m->version, c, o, ob, s, pa, t, k, (unsigned long)m->ordinal, m->effect, m->payload_len, d); |
| 444 | } |
| 445 | static void session_line(const session_verdict *v, char *buf, size_t cap) { |
| 446 | if (v->code == PST_OK) snprintf(buf, cap, "ok steps=%lu tasks=%lu", v->step, v->tasks); |
| 447 | else snprintf(buf, cap, "refuse %s step=%lu", v->code > 0 && v->code < PST_CODE_COUNT ? code_names[v->code] : "PST_UNKNOWN_CODE", v->step); |
| 448 | } |
| 449 | |
| 450 | /* ---- file I/O (outside the decoder) --------------------------------------- */ |
| 451 | #define FILE_CAP 65536u |
| 452 | static uint8_t file_buf[FILE_CAP]; |
| 453 | |
| 454 | /* Reads the whole file into file_buf. Returns 0, or -1 on I/O error or a |
| 455 | * file larger than FILE_CAP (no vector is that large). */ |
| 456 | static int slurp(const char *path, size_t *n) { |
| 457 | FILE *f = fopen(path, "rb"); |
| 458 | size_t k; |
| 459 | if (!f) return -1; |
| 460 | k = fread(file_buf, 1, FILE_CAP, f); |
| 461 | if (k == FILE_CAP && fgetc(f) != EOF) { fclose(f); return -1; } |
| 462 | fclose(f); |
| 463 | *n = k; |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int check_file(const char *path, char *line, size_t cap, int *roundtrip_ok) { |
| 468 | static uint8_t again[PST_MAX_MSG]; |
| 469 | pst_msg m; |
| 470 | size_t n, k; |
| 471 | int code; |
| 472 | if (slurp(path, &n)) { fprintf(stderr, "cannot read %s\n", path); return -1; } |
| 473 | code = pst_decode(file_buf, n, &m); |
| 474 | verdict_line(code, &m, line, cap); |
| 475 | *roundtrip_ok = 1; |
| 476 | if (code == PST_OK) { |
| 477 | k = pst_encode(&m, again, sizeof again); |
| 478 | *roundtrip_ok = k == n && memcmp(again, file_buf, n) == 0; |
| 479 | } |
| 480 | return code; |
| 481 | } |
| 482 | |
| 483 | static void rstrip(char *s) { size_t n = strlen(s); while (n && (s[n-1] == '\n' || s[n-1] == '\r')) s[--n] = 0; } |
| 484 | |
| 485 | /* A corpus line names a file in the corpus folder itself: no path separator, |
| 486 | * no leading dot, so expected.txt can never point outside the folder. */ |
| 487 | static int plain_name(const char *s) { return s[0] && s[0] != '.' && !strchr(s, '/'); } |
| 488 | |
| 489 | /* Create (or truncate) a file readable by all but writable only by its owner. */ |
| 490 | static FILE *create_file(const char *path, const char *mode) { |
| 491 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 492 | FILE *f; |
| 493 | if (fd < 0) return NULL; |
| 494 | if (!(f = fdopen(fd, mode))) close(fd); |
| 495 | return f; |
| 496 | } |
| 497 | |
| 498 | static int corpus(const char *dir) { |
| 499 | char path[4096], line[1024], got[1024]; |
| 500 | unsigned pass = 0, fail = 0; |
| 501 | FILE *f; |
| 502 | |
| 503 | snprintf(path, sizeof path, "%s/expected.txt", dir); |
| 504 | if (!(f = fopen(path, "r"))) { fprintf(stderr, "cannot read %s\n", path); return 2; } |
| 505 | while (fgets(line, sizeof line, f)) { |
| 506 | char *sp; |
| 507 | int rt; |
| 508 | rstrip(line); |
| 509 | if (!line[0] || line[0] == '#') continue; |
| 510 | if (!(sp = strchr(line, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; } |
| 511 | *sp = 0; |
| 512 | if (!plain_name(line)) { fprintf(stderr, "not a plain file name: %s\n", line); fail++; continue; } |
| 513 | snprintf(path, sizeof path, "%s/%s", dir, line); |
| 514 | if (check_file(path, got, sizeof got, &rt) < 0) { fail++; continue; } |
| 515 | if (strcmp(got, sp + 1) != 0) { fail++; printf("MISMATCH %s\n expected: %s\n got: %s\n", line, sp + 1, got); } |
| 516 | else if (!rt) { fail++; printf("ROUNDTRIP_MISMATCH %s\n", line); } |
| 517 | else pass++; |
| 518 | } |
| 519 | fclose(f); |
| 520 | |
| 521 | /* recorded sessions (spec section 10), when the directory has them */ |
| 522 | snprintf(path, sizeof path, "%s/sessions/expected.txt", dir); |
| 523 | if ((f = fopen(path, "r"))) { |
| 524 | while (fgets(line, sizeof line, f)) { |
| 525 | char *sp; |
| 526 | size_t n; |
| 527 | session_verdict v; |
| 528 | rstrip(line); |
| 529 | if (!line[0] || line[0] == '#') continue; |
| 530 | if (!(sp = strchr(line, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; } |
| 531 | *sp = 0; |
| 532 | if (!plain_name(line)) { fprintf(stderr, "not a plain file name: %s\n", line); fail++; continue; } |
| 533 | snprintf(path, sizeof path, "%s/sessions/%s", dir, line); |
| 534 | if (slurp(path, &n)) { fprintf(stderr, "cannot read %s\n", path); fail++; continue; } |
| 535 | session_check(file_buf, n, &v); |
| 536 | session_line(&v, got, sizeof got); |
| 537 | if (strcmp(got, sp + 1) != 0) { fail++; printf("MISMATCH sessions/%s\n expected: %s\n got: %s\n", line, sp + 1, got); } |
| 538 | else pass++; |
| 539 | } |
| 540 | fclose(f); |
| 541 | } |
| 542 | printf("PST1_CONFORMANCE impl=c pass=%u fail=%u\n", pass, fail); |
| 543 | return fail || !pass ? 1 : 0; |
| 544 | } |
| 545 | |
| 546 | /* ---- seeded random sessions (reorder, drop, duplicate) -------------------- */ |
| 547 | #define FUZZ_FILES 8u |
| 548 | #define FUZZ_RECS 64u |
| 549 | static uint8_t fuzz_src[FUZZ_FILES][8192]; |
| 550 | static size_t fuzz_len[FUZZ_FILES]; |
| 551 | static uint8_t fuzz_out[8192 + PST_MAX_MSG + 3]; |
| 552 | |
| 553 | static uint64_t splitmix(uint64_t *s) { |
| 554 | uint64_t z = (*s += 0x9E3779B97F4A7C15ull); |
| 555 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 556 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 557 | return z ^ (z >> 31); |
| 558 | } |
| 559 | |
| 560 | /* Record boundaries of a well-formed recording; returns the count. */ |
| 561 | static unsigned records(const uint8_t *b, size_t n, size_t *off, size_t *len) { |
| 562 | size_t o = 4; |
| 563 | unsigned k = 0; |
| 564 | while (o + 3 <= n && k < FUZZ_RECS) { |
| 565 | size_t l = 3 + (size_t)g16(b + o + 1); |
| 566 | if (l > n - o) break; |
| 567 | off[k] = o; len[k] = l; k++; |
| 568 | o += l; |
| 569 | } |
| 570 | return k; |
| 571 | } |
| 572 | |
| 573 | static int session_fuzz(uint64_t seed, unsigned long count, int nfiles, char **files) { |
| 574 | static const char *const ops[3] = { "reorder", "drop", "duplicate" }; |
| 575 | unsigned long by_op[3] = { 0, 0, 0 }, ok = 0, refused = 0, i; |
| 576 | uint64_t rng = seed; |
| 577 | sha256 all; |
| 578 | uint8_t d[32]; |
| 579 | char hx[65], line[128]; |
| 580 | |
| 581 | if (nfiles < 1 || nfiles > (int)FUZZ_FILES) { fprintf(stderr, "need 1 to %u files\n", FUZZ_FILES); return 2; } |
| 582 | for (int k = 0; k < nfiles; k++) { |
| 583 | size_t n; |
| 584 | if (slurp(files[k], &n) || n > sizeof fuzz_src[k]) { fprintf(stderr, "cannot read %s\n", files[k]); return 2; } |
| 585 | memcpy(fuzz_src[k], file_buf, n); |
| 586 | fuzz_len[k] = n; |
| 587 | } |
| 588 | sha_init(&all); |
| 589 | for (i = 0; i < count; i++) { |
| 590 | size_t off[FUZZ_RECS], len[FUZZ_RECS], o = 4; |
| 591 | unsigned f = (unsigned)(splitmix(&rng) % (uint64_t)nfiles), nr, op, a, b2, r; |
| 592 | const uint8_t *src = fuzz_src[f]; |
| 593 | session_verdict v; |
| 594 | |
| 595 | nr = records(src, fuzz_len[f], off, len); |
| 596 | if (nr < 2) { fprintf(stderr, "%s: fewer than 2 records\n", files[f]); return 2; } |
| 597 | op = (unsigned)(splitmix(&rng) % 3); |
| 598 | a = (unsigned)(splitmix(&rng) % nr); |
| 599 | b2 = (unsigned)(splitmix(&rng) % (nr - 1)); |
| 600 | if (b2 >= a) b2++; /* b2 != a */ |
| 601 | memcpy(fuzz_out, "PSS1", 4); |
| 602 | for (r = 0; r < nr; r++) { |
| 603 | unsigned take = r; |
| 604 | if (op == 0) take = r == a ? b2 : r == b2 ? a : r; /* swap a and b2 */ |
| 605 | if (op == 1 && r == a) continue; /* drop a */ |
| 606 | memcpy(fuzz_out + o, src + off[take], len[take]); o += len[take]; |
| 607 | if (op == 2 && r == b2) { memcpy(fuzz_out + o, src + off[a], len[a]); o += len[a]; } /* copy of a after b2 */ |
| 608 | } |
| 609 | session_check(fuzz_out, o, &v); |
| 610 | session_line(&v, line, sizeof line); |
| 611 | if (v.code == PST_OK) ok++; else refused++; |
| 612 | by_op[op]++; |
| 613 | sha_update(&all, line, strlen(line)); |
| 614 | sha_update(&all, "\n", 1); |
| 615 | } |
| 616 | sha_final(&all, d); |
| 617 | hexw(hx, d, 32); |
| 618 | printf("PST1_SESSION_FUZZ seed=%llu count=%lu %s=%lu %s=%lu %s=%lu ok=%lu refused=%lu verdicts=%s\n", |
| 619 | (unsigned long long)seed, count, ops[0], by_op[0], ops[1], by_op[1], ops[2], by_op[2], ok, refused, hx); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /* ---- vector generator ------------------------------------------------------ */ |
| 624 | /* Every vector starts from one well-formed message and changes one thing. The |
| 625 | * expected line for an accepted vector is built from the generator's own |
| 626 | * struct, not from pst_decode, so the decoder is checked against the intent. */ |
| 627 | static uint8_t gen_payload[PST_MAX_PAY + 8]; |
| 628 | |
| 629 | static void fill(uint8_t *p, size_t n, uint8_t seed) { for (size_t i = 0; i < n; i++) p[i] = (uint8_t)(seed + 7 * i); } |
| 630 | |
| 631 | static void base(pst_msg *m, uint8_t type, const char *text) { |
| 632 | size_t len = strlen(text); |
| 633 | memset(m, 0, sizeof *m); |
| 634 | m->version = PST_VERSION; |
| 635 | m->type = type; |
| 636 | fill(m->cause, 32, 0xC1); |
| 637 | fill(m->origin, 32, 0x0A); |
| 638 | fill(m->objective, 32, 0x0B); |
| 639 | fill(m->subject, 32, 0x5B); |
| 640 | fill(m->task, 16, 0x7A); |
| 641 | fill(m->idem, 16, 0x1D); |
| 642 | m->ordinal = type == T_REQUEST ? 0 : 1; |
| 643 | m->effect = 1; |
| 644 | memcpy(gen_payload, text, len); |
| 645 | m->payload = gen_payload; |
| 646 | m->payload_len = (uint16_t)len; |
| 647 | sha_once(gen_payload, len, m->digest); |
| 648 | } |
| 649 | |
| 650 | static FILE *gen_expected; |
| 651 | static int gen_fail; |
| 652 | |
| 653 | static void emit(const char *dir, const char *name, const uint8_t *b, size_t n, const char *expect) { |
| 654 | char path[4096]; |
| 655 | FILE *f; |
| 656 | snprintf(path, sizeof path, "%s/%s", dir, name); |
| 657 | if (!(f = create_file(path, "wb")) || fwrite(b, 1, n, f) != n || fclose(f) != 0) { |
| 658 | fprintf(stderr, "cannot write %s\n", path); |
| 659 | gen_fail = 1; |
| 660 | return; |
| 661 | } |
| 662 | fprintf(gen_expected, "%s %s\n", name, expect); |
| 663 | } |
| 664 | |
| 665 | static void emit_ok(const char *dir, const char *name, const pst_msg *m) { |
| 666 | static uint8_t out[PST_MAX_MSG]; |
| 667 | char line[1024]; |
| 668 | size_t n = pst_encode(m, out, sizeof out); |
| 669 | verdict_line(PST_OK, m, line, sizeof line); |
| 670 | emit(dir, name, out, n, line); |
| 671 | } |
| 672 | |
| 673 | static void emit_bad(const char *dir, const char *name, const pst_msg *m, int at, int value, long extra, const char *code) { |
| 674 | static uint8_t out[PST_MAX_MSG + 64]; |
| 675 | char line[128]; |
| 676 | size_t n = pst_encode(m, out, sizeof out); |
| 677 | if (at >= 0) out[at] = (uint8_t)value; |
| 678 | if (extra < 0) n -= (size_t)(-extra); /* drop bytes from the end */ |
| 679 | else for (long i = 0; i < extra; i++) out[n++] = (uint8_t)(0xE0 + i); |
| 680 | snprintf(line, sizeof line, "refuse %s", code); |
| 681 | emit(dir, name, out, n, line); |
| 682 | } |
| 683 | |
| 684 | /* ---- tool-call vectors (spec section 11) ---------------------------------- */ |
| 685 | /* The one INTERPLANE canonical tool request the vectors carry: RFC 8785 bytes |
| 686 | * written by interplane-core canonicalize() at aien-dev/interplane-mv commit |
| 687 | * 83df0f074b2c572473cd535c89f767df48933f4a, using |
| 688 | * tools/interplane-request (1743 bytes, so three chunks: 800, 800, 143). |
| 689 | * INTERPLANE digest: |
| 690 | * sha256:78dbef7ee689b20ad9ae530fd7fa8417fb671778667b27f910484156776990d6 */ |
| 691 | static const char IPR_REQ[] = |
| 692 | "{\"arguments\":{\"currency_hint\":\"\302\243\",\"fields\":[\"merchant\",\"date\",\"total" |
| 693 | "\",\"items\"],\"instructions\":\"Find every receipt from the last week that mentions the " |
| 694 | "garden hose, the rain barrel or the drip line. Summarise each one in a single plain sente" |
| 695 | "nce: what was bought, where, and for how much. Leave out card numbers and addresses. If t" |
| 696 | "wo receipts look like the same purchase, say so instead of counting it twice.\\nFind ever" |
| 697 | "y receipt from the last week that mentions the garden hose, the rain barrel or the drip l" |
| 698 | "ine. Summarise each one in a single plain sentence: what was bought, where, and for how m" |
| 699 | "uch. Leave out card numbers and addresses. If two receipts look like the same purchase, s" |
| 700 | "ay so instead of counting it twice.\\nFind every receipt from the last week that mentions" |
| 701 | " the garden hose, the rain barrel or the drip line. Summarise each one in a single plain " |
| 702 | "sentence: what was bought, where, and for how much. Leave out card numbers and addresses." |
| 703 | " If two receipts look like the same purchase, say so instead of counting it twice.\\nFind" |
| 704 | " every receipt from the last week that mentions the garden hose, the rain barrel or the d" |
| 705 | "rip line. Summarise each one in a single plain sentence: what was bought, where, and for " |
| 706 | "how much. Leave out card numbers and addresses. If two receipts look like the same purcha" |
| 707 | "se, say so instead of counting it twice.\\n\",\"limit\":25,\"locale\":\"en-GB\",\"note\":" |
| 708 | "\"caf\303\251 \342\230\225 \360\237\214\247\",\"query\":\"garden hose OR rain barrel OR d" |
| 709 | "rip line\",\"redact\":[\"card_number\",\"address\"],\"since_days\":7},\"kind\":\"tool_req" |
| 710 | "uest\",\"provenance\":{\"dialect\":\"openai\",\"model\":\"pi-local-router\",\"parser_vers" |
| 711 | "ion\":\"1.0.0\",\"source_call_id\":\"call_pst1_c6\",\"source_turn\":7},\"request_id\":\"p" |
| 712 | "st1-c6-vector-1\",\"tool\":{\"name\":\"search\",\"namespace\":\"aien.receipts\"}}"; |
| 713 | static const char IPR_DIGEST_HEX[] = "78dbef7ee689b20ad9ae530fd7fa8417fb671778667b27f910484156776990d6"; |
| 714 | #define IPR_LEN (sizeof IPR_REQ - 1) |
| 715 | |
| 716 | /* Writes a file that has no expected.txt line (the carried request itself). */ |
| 717 | static void emit_raw(const char *dir, const char *name, const uint8_t *b, size_t n) { |
| 718 | char path[4096]; |
| 719 | FILE *f; |
| 720 | snprintf(path, sizeof path, "%s/%s", dir, name); |
| 721 | if (!(f = create_file(path, "wb")) || fwrite(b, 1, n, f) != n || fclose(f) != 0) { |
| 722 | fprintf(stderr, "cannot write %s\n", path); |
| 723 | gen_fail = 1; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* Sets m's payload to p[0..n) (copied into gen_payload) and its digest. */ |
| 728 | static void set_payload(pst_msg *m, const uint8_t *p, size_t n) { |
| 729 | memmove(gen_payload, p, n); |
| 730 | m->payload = gen_payload; |
| 731 | m->payload_len = (uint16_t)n; |
| 732 | sha_once(gen_payload, n, m->digest); |
| 733 | } |
| 734 | |
| 735 | /* A tool-call head: the INTERPLANE digest, the canonical length, then `extra` |
| 736 | * further bytes (only for refusal vectors). */ |
| 737 | static void set_tool_head(pst_msg *m, const uint8_t digest[32], uint32_t len, size_t extra) { |
| 738 | uint8_t h[PST_TOOL_HEAD + 8]; |
| 739 | memcpy(h, digest, 32); |
| 740 | p32(h + 32, len); |
| 741 | for (size_t i = 0; i < extra; i++) h[PST_TOOL_HEAD + i] = (uint8_t)(0x03 + i); |
| 742 | set_payload(m, h, PST_TOOL_HEAD + extra); |
| 743 | } |
| 744 | |
| 745 | static uint8_t ipr_digest[32]; |
| 746 | |
| 747 | /* Computes the INTERPLANE digest of IPR_REQ and checks it against the pinned |
| 748 | * value, so the embedded bytes cannot drift from what interplane-core wrote. */ |
| 749 | static int ipr_init(void) { |
| 750 | char hx[65]; |
| 751 | sha_once(IPR_REQ, IPR_LEN, ipr_digest); |
| 752 | hexw(hx, ipr_digest, 32); |
| 753 | if (strcmp(hx, IPR_DIGEST_HEX) != 0) { fprintf(stderr, "embedded INTERPLANE request digest drifted\n"); return -1; } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /* ---- recorded-session generator (spec section 10.4) ---------------------- */ |
| 758 | static uint8_t sess[16384]; |
| 759 | static size_t sess_n; |
| 760 | |
| 761 | static void s_begin(void) { memcpy(sess, "PSS1", 4); sess_n = 4; } |
| 762 | |
| 763 | /* One message of task number tn (task id and idempotence key derive from tn). */ |
| 764 | static void mk(pst_msg *m, uint8_t type, unsigned tn, uint32_t ordinal, const char *text) { |
| 765 | base(m, type, text); |
| 766 | fill(m->task, 16, (uint8_t)(0x7A + 16 * tn)); |
| 767 | fill(m->idem, 16, (uint8_t)(0x1D + 16 * tn)); |
| 768 | m->task[15] ^= (uint8_t)tn; /* keeps task 16 apart from task 0 */ |
| 769 | m->idem[15] ^= (uint8_t)tn; |
| 770 | m->ordinal = ordinal; |
| 771 | } |
| 772 | |
| 773 | static void s_add(unsigned side, const pst_msg *m) { |
| 774 | size_t n = pst_encode(m, sess + sess_n + 3, sizeof sess - sess_n - 3); |
| 775 | if (!n) { gen_fail = 1; return; } |
| 776 | sess[sess_n] = (uint8_t)side; |
| 777 | p16(sess + sess_n + 1, (uint16_t)n); |
| 778 | sess_n += 3 + n; |
| 779 | } |
| 780 | |
| 781 | /* Shorthand: build and add one message. */ |
| 782 | static void s_msg(unsigned side, uint8_t type, unsigned tn, uint32_t ordinal, const char *text) { |
| 783 | pst_msg m; |
| 784 | mk(&m, type, tn, ordinal, text); |
| 785 | s_add(side, &m); |
| 786 | } |
| 787 | |
| 788 | static void s_emit(const char *dir, const char *name, const char *expect) { |
| 789 | char sub[4096]; |
| 790 | snprintf(sub, sizeof sub, "%s/sessions", dir); |
| 791 | emit(sub, name, sess, sess_n, expect); |
| 792 | } |
| 793 | |
| 794 | #define PI SIDE_PI |
| 795 | #define SP SIDE_SPARK |
| 796 | static const char RESULT32[] = "result digest stand-in: 32 bytes"; /* 32 bytes of payload */ |
| 797 | |
| 798 | /* request, accepted, running: the opening of a normal task (Spark ordinals 0, 1). */ |
| 799 | static void s_open(unsigned tn) { |
| 800 | s_msg(PI, 1, tn, 0, "Write a short poem about rain."); |
| 801 | s_msg(SP, 2, tn, 0, ""); |
| 802 | s_msg(SP, 4, tn, 1, ""); |
| 803 | } |
| 804 | |
| 805 | /* Tool-call task tn: the head (ordinal 0), then chunks carrying canonical bytes. */ |
| 806 | static void s_tool_open(unsigned tn, const uint8_t digest[32]) { |
| 807 | pst_msg m; |
| 808 | mk(&m, T_TOOL_REQ, tn, 0, ""); |
| 809 | set_tool_head(&m, digest, (uint32_t)IPR_LEN, 0); |
| 810 | s_add(SIDE_PI, &m); |
| 811 | } |
| 812 | static void s_tool_chunk(unsigned tn, uint32_t ordinal, const uint8_t *p, size_t n) { |
| 813 | pst_msg m; |
| 814 | mk(&m, T_TOOL_CHUNK, tn, ordinal, ""); |
| 815 | set_payload(&m, p, n); |
| 816 | s_add(SIDE_PI, &m); |
| 817 | } |
| 818 | /* All of p[0..n) in 800-byte chunks, ordinals 1, 2, ... */ |
| 819 | static void s_tool_chunks(unsigned tn, const uint8_t *p, size_t n) { |
| 820 | uint32_t ord = 1; |
| 821 | for (size_t at = 0; at < n; at += PST_MAX_PAY, ord++) |
| 822 | s_tool_chunk(tn, ord, p + at, n - at < PST_MAX_PAY ? n - at : PST_MAX_PAY); |
| 823 | } |
| 824 | |
| 825 | static int make_sessions(const char *dir) { |
| 826 | char path[4096]; |
| 827 | pst_msg m; |
| 828 | snprintf(path, sizeof path, "%s/sessions/expected.txt", dir); |
| 829 | if (!(gen_expected = create_file(path, "w"))) { fprintf(stderr, "cannot write %s\n", path); return 2; } |
| 830 | fprintf(gen_expected, |
| 831 | "# PST1 recorded sessions. Spec: ../../SPEC.md section 10 (rules) and 10.4 (container).\n" |
| 832 | "# Line: <file> ok steps=N tasks=T | <file> refuse <code> step=N (1-based message position)\n" |
| 833 | "# Generated by tools/pst_verify.c --make-vectors; do not edit by hand.\n"); |
| 834 | |
| 835 | /* accepted sessions */ |
| 836 | s_begin(); s_open(0); |
| 837 | s_msg(SP, 5, 0, 2, "Rain on the "); s_msg(SP, 5, 0, 3, "tin roof, "); s_msg(SP, 5, 0, 4, "soft and steady."); |
| 838 | s_msg(SP, 7, 0, 5, RESULT32); |
| 839 | s_emit(dir, "ok-normal-life.pss", "ok steps=7 tasks=1"); |
| 840 | |
| 841 | s_begin(); |
| 842 | s_msg(PI, 1, 0, 0, "Index the new receipts."); |
| 843 | s_msg(PI, 1, 1, 0, "Rebuild the map."); |
| 844 | s_msg(SP, 2, 0, 0, ""); |
| 845 | s_msg(SP, 8, 1, 0, "busy"); |
| 846 | s_msg(SP, 3, 0, 1, ""); |
| 847 | s_msg(PI, 13, 0, 1, ""); |
| 848 | s_msg(SP, 9, 0, 2, ""); |
| 849 | s_msg(PI, 14, 0, 2, ""); |
| 850 | s_emit(dir, "ok-two-tasks-queued-cancelled.pss", "ok steps=8 tasks=2"); |
| 851 | |
| 852 | s_begin(); |
| 853 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 854 | s_msg(SP, 2, 0, 0, ""); |
| 855 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); /* the Pi sends the same request again */ |
| 856 | s_msg(SP, 4, 0, 1, ""); |
| 857 | s_msg(SP, 7, 0, 2, RESULT32); |
| 858 | s_emit(dir, "ok-repeated-request.pss", "ok steps=5 tasks=1"); |
| 859 | |
| 860 | /* refused sessions: the six of the frozen first cut, then the other rules */ |
| 861 | s_begin(); |
| 862 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 863 | s_msg(SP, 7, 0, 0, RESULT32); |
| 864 | s_emit(dir, "bad-completed-before-accepted.pss", "refuse PST_BAD_ORDER step=2"); |
| 865 | |
| 866 | s_begin(); s_open(0); |
| 867 | s_msg(SP, 7, 0, 2, RESULT32); s_msg(SP, 8, 0, 3, "late failure"); |
| 868 | s_emit(dir, "bad-second-final.pss", "refuse PST_SECOND_FINAL step=5"); |
| 869 | |
| 870 | s_begin(); s_open(0); |
| 871 | s_msg(SP, 5, 0, 2, "Rain on the "); s_msg(SP, 5, 0, 4, "soft and steady."); |
| 872 | s_emit(dir, "bad-chunk-skipped.pss", "refuse PST_ORDINAL_GAP step=5"); |
| 873 | |
| 874 | s_begin(); s_open(0); |
| 875 | s_msg(SP, 5, 0, 2, "Rain on the "); s_msg(SP, 5, 0, 2, "Rain on the "); |
| 876 | s_emit(dir, "bad-chunk-repeated.pss", "refuse PST_ORDINAL_REPEAT step=5"); |
| 877 | |
| 878 | s_begin(); |
| 879 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 880 | mk(&m, 2, 0, 0, ""); m.subject[0] ^= 0xFF; s_add(SP, &m); |
| 881 | s_emit(dir, "bad-subject-change.pss", "refuse PST_SUBJECT_MISMATCH step=2"); |
| 882 | |
| 883 | s_begin(); |
| 884 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 885 | s_msg(SP, 2, 0, 0, ""); |
| 886 | s_msg(PI, 1, 0, 0, "Write a long poem about snow."); /* same task and key, other payload */ |
| 887 | s_emit(dir, "bad-idempotence-key-other-digest.pss", "refuse PST_IDEMPOTENCE_CONFLICT step=3"); |
| 888 | |
| 889 | s_begin(); |
| 890 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 891 | mk(&m, 1, 1, 0, "Write a short poem about rain."); fill(m.idem, 16, 0x1D); s_add(PI, &m); /* task 1, key of task 0 */ |
| 892 | s_emit(dir, "bad-idempotence-key-other-task.pss", "refuse PST_IDEMPOTENCE_CONFLICT step=2"); |
| 893 | |
| 894 | s_begin(); s_open(0); |
| 895 | s_msg(PI, 7, 0, 1, RESULT32); |
| 896 | s_emit(dir, "bad-completed-by-pi.pss", "refuse PST_WRONG_SIDE step=4"); |
| 897 | |
| 898 | s_begin(); s_open(0); |
| 899 | s_msg(PI, 6, 0, 1, RESULT32); |
| 900 | s_emit(dir, "bad-approval-by-pi.pss", "refuse PST_WRONG_SIDE step=4"); |
| 901 | |
| 902 | s_begin(); |
| 903 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 904 | s_msg(SP, 2, 0, 0, ""); |
| 905 | s_msg(PI, 14, 1, 0, ""); |
| 906 | s_emit(dir, "bad-resume-unknown-task.pss", "refuse PST_UNKNOWN_TASK step=3"); |
| 907 | |
| 908 | s_begin(); |
| 909 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 910 | s_msg(SP, 2, 0, 0, ""); |
| 911 | mk(&m, 4, 0, 1, ""); m.cause[5] ^= 0x01; s_add(SP, &m); |
| 912 | s_emit(dir, "bad-context-change.pss", "refuse PST_CONTEXT_MISMATCH step=3"); |
| 913 | |
| 914 | s_begin(); s_open(0); |
| 915 | s_msg(SP, 7, 0, 2, RESULT32); s_msg(SP, 5, 0, 3, "one more line"); |
| 916 | s_emit(dir, "bad-chunk-after-final.pss", "refuse PST_BAD_ORDER step=5"); |
| 917 | |
| 918 | s_begin(); s_open(0); |
| 919 | s_msg(SP, 6, 0, 2, RESULT32); |
| 920 | s_emit(dir, "bad-approval-rule-not-yet-defined.pss", "refuse PST_BAD_ORDER step=4"); |
| 921 | |
| 922 | s_begin(); |
| 923 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 924 | mk(&m, 2, 0, 0, ""); s_add(SP, &m); |
| 925 | sess[sess_n - (PST_HDR - O_MAGIC)] = 'X'; /* break the magic of the accepted message */ |
| 926 | s_emit(dir, "bad-message-in-session.pss", "refuse PST_BAD_MAGIC step=2"); |
| 927 | |
| 928 | s_begin(); |
| 929 | for (unsigned tn = 0; tn <= MAX_TASKS; tn++) s_msg(PI, 1, tn, 0, "Count to three."); |
| 930 | s_emit(dir, "bad-session-full.pss", "refuse PST_SESSION_FULL step=17"); |
| 931 | |
| 932 | s_begin(); |
| 933 | s_msg(PI, 1, 0, 0, "Write a short poem about rain."); |
| 934 | sess[sess_n++] = 3; /* a side byte that is neither Pi nor Spark */ |
| 935 | sess[sess_n++] = 0; sess[sess_n++] = 0; |
| 936 | s_emit(dir, "bad-recording.pss", "refuse PST_BAD_RECORDING step=2"); |
| 937 | |
| 938 | /* tool-call tasks (spec section 11): one canonical INTERPLANE request in three chunks */ |
| 939 | { |
| 940 | static uint8_t buf[IPR_LEN + 8]; |
| 941 | uint8_t dg[32]; |
| 942 | |
| 943 | s_begin(); |
| 944 | s_tool_open(0, ipr_digest); s_tool_chunks(0, (const uint8_t *)IPR_REQ, IPR_LEN); |
| 945 | s_msg(SP, 2, 0, 0, ""); s_msg(SP, 4, 0, 1, ""); s_msg(SP, 7, 0, 2, RESULT32); |
| 946 | s_emit(dir, "ok-tool-call.pss", "ok steps=7 tasks=1"); |
| 947 | |
| 948 | s_begin(); |
| 949 | s_tool_open(0, ipr_digest); s_tool_chunk(0, 1, (const uint8_t *)IPR_REQ, PST_MAX_PAY); |
| 950 | s_msg(SP, 8, 0, 0, "tool request incomplete"); /* the Spark may end it unread */ |
| 951 | s_emit(dir, "ok-tool-call-failed-while-collecting.pss", "ok steps=3 tasks=1"); |
| 952 | |
| 953 | memcpy(dg, ipr_digest, 32); dg[0] ^= 0x01; |
| 954 | s_begin(); |
| 955 | s_tool_open(0, dg); s_tool_chunks(0, (const uint8_t *)IPR_REQ, IPR_LEN); |
| 956 | s_emit(dir, "bad-tool-digest-mismatch.pss", "refuse PST_TOOL_DIGEST_MISMATCH step=4"); |
| 957 | |
| 958 | memcpy(buf, IPR_REQ, IPR_LEN); buf[1000] ^= 0x20; /* one byte in the second chunk */ |
| 959 | s_begin(); |
| 960 | s_tool_open(0, ipr_digest); s_tool_chunks(0, buf, IPR_LEN); |
| 961 | s_emit(dir, "bad-tool-altered-byte.pss", "refuse PST_TOOL_DIGEST_MISMATCH step=4"); |
| 962 | |
| 963 | memcpy(buf, IPR_REQ, IPR_LEN); buf[IPR_LEN] = ' '; /* one byte past the canonical end */ |
| 964 | s_begin(); |
| 965 | s_tool_open(0, ipr_digest); |
| 966 | s_tool_chunk(0, 1, buf, PST_MAX_PAY); s_tool_chunk(0, 2, buf + PST_MAX_PAY, PST_MAX_PAY); |
| 967 | s_tool_chunk(0, 3, buf + 2 * PST_MAX_PAY, IPR_LEN - 2 * PST_MAX_PAY + 1); |
| 968 | s_emit(dir, "bad-tool-extra-bytes.pss", "refuse PST_TOOL_CHUNK_SIZE step=4"); |
| 969 | |
| 970 | s_begin(); |
| 971 | s_tool_open(0, ipr_digest); s_tool_chunk(0, 1, (const uint8_t *)IPR_REQ, PST_MAX_PAY - 1); |
| 972 | s_emit(dir, "bad-tool-short-chunk.pss", "refuse PST_TOOL_CHUNK_SIZE step=2"); |
| 973 | |
| 974 | s_begin(); |
| 975 | s_tool_open(0, ipr_digest); s_tool_chunks(0, (const uint8_t *)IPR_REQ, IPR_LEN); |
| 976 | s_tool_chunk(0, 4, (const uint8_t *)" ", 1); /* nothing may follow the last chunk */ |
| 977 | s_emit(dir, "bad-tool-chunk-after-complete.pss", "refuse PST_BAD_ORDER step=5"); |
| 978 | |
| 979 | s_begin(); |
| 980 | s_tool_open(0, ipr_digest); |
| 981 | s_tool_chunk(0, 1, (const uint8_t *)IPR_REQ, PST_MAX_PAY); |
| 982 | s_tool_chunk(0, 2, (const uint8_t *)IPR_REQ + PST_MAX_PAY, PST_MAX_PAY); |
| 983 | s_msg(SP, 2, 0, 0, ""); /* accepted with the last 143 bytes missing */ |
| 984 | s_emit(dir, "bad-tool-truncated.pss", "refuse PST_TOOL_INCOMPLETE step=4"); |
| 985 | |
| 986 | s_begin(); |
| 987 | s_msg(PI, 1, 0, 0, "Search my receipts for the garden hose."); |
| 988 | s_tool_chunk(0, 1, (const uint8_t *)IPR_REQ, PST_MAX_PAY); |
| 989 | s_emit(dir, "bad-tool-payload-in-chat-task.pss", "refuse PST_NOT_TOOL_TASK step=2"); |
| 990 | } |
| 991 | |
| 992 | if (fclose(gen_expected) != 0) gen_fail = 1; |
| 993 | return gen_fail ? 2 : 0; |
| 994 | } |
| 995 | |
| 996 | static int make_vectors(const char *dir) { |
| 997 | char path[4096]; |
| 998 | pst_msg m; |
| 999 | if (ipr_init()) return 2; |
| 1000 | snprintf(path, sizeof path, "%s/expected.txt", dir); |
| 1001 | if (!(gen_expected = create_file(path, "w"))) { fprintf(stderr, "cannot write %s\n", path); return 2; } |
| 1002 | fprintf(gen_expected, |
| 1003 | "# PST1 single-message vectors. Spec: ../SPEC.md (wire version 1), sections 4 and 7.\n" |
| 1004 | "# Line: <file> <verdict line>, verdict line as printed by tools/pst_verify.c:\n" |
| 1005 | "# ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64 parent=H32\n" |
| 1006 | "# task=H32 idem=H32 ordinal=N effect=E len=L digest=H64 (one line)\n" |
| 1007 | "# refuse <code from SPEC.md section 7>\n" |
| 1008 | "# Generated by tools/pst_verify.c --make-vectors; do not edit by hand.\n"); |
| 1009 | |
| 1010 | /* accepted */ |
| 1011 | base(&m, 1, "Summarise the last three receipts in plain words."); |
| 1012 | emit_ok(dir, "ok-request.pst", &m); |
| 1013 | base(&m, 2, ""); |
| 1014 | emit_ok(dir, "ok-accepted-empty.pst", &m); |
| 1015 | base(&m, 5, ""); |
| 1016 | memset(gen_payload, 'x', PST_MAX_PAY); |
| 1017 | m.payload_len = PST_MAX_PAY; m.ordinal = 7; m.effect = 0; |
| 1018 | fill(m.parent, 16, 0x9E); |
| 1019 | sha_once(gen_payload, PST_MAX_PAY, m.digest); |
| 1020 | emit_ok(dir, "ok-text-chunk-1024.pst", &m); |
| 1021 | base(&m, 14, ""); |
| 1022 | m.effect = 2; m.ordinal = 0xFFFFFFFFu; |
| 1023 | emit_ok(dir, "ok-resume-request-max-ordinal.pst", &m); |
| 1024 | |
| 1025 | /* refused: one rule each, in check order */ |
| 1026 | base(&m, 1, "hello"); |
| 1027 | emit_bad(dir, "bad-length-short-header.pst", &m, -1, 0, -(long)(5 + 1), "PST_BAD_LENGTH"); |
| 1028 | emit_bad(dir, "bad-length-empty-file.pst", &m, -1, 0, -(long)(5 + PST_HDR), "PST_BAD_LENGTH"); |
| 1029 | base(&m, 5, ""); |
| 1030 | memset(gen_payload, 'y', PST_MAX_PAY); |
| 1031 | m.payload_len = PST_MAX_PAY; |
| 1032 | sha_once(gen_payload, PST_MAX_PAY, m.digest); |
| 1033 | emit_bad(dir, "bad-length-over-1024.pst", &m, -1, 0, 1, "PST_BAD_LENGTH"); |
| 1034 | base(&m, 1, "hello"); |
| 1035 | emit_bad(dir, "bad-magic.pst", &m, O_MAGIC + 3, '2', 0, "PST_BAD_MAGIC"); |
| 1036 | emit_bad(dir, "bad-version-unknown.pst", &m, O_VERSION, 2, 0, "PST_BAD_VERSION"); |
| 1037 | emit_bad(dir, "bad-version-downgrade.pst", &m, O_VERSION, 0, 0, "PST_DOWNGRADE"); |
| 1038 | emit_bad(dir, "bad-type-zero.pst", &m, O_TYPE, 0, 0, "PST_BAD_TYPE"); |
| 1039 | emit_bad(dir, "bad-type-unknown.pst", &m, O_TYPE, 0xFF, 0, "PST_BAD_TYPE"); |
| 1040 | emit_bad(dir, "bad-reserved0.pst", &m, O_RES0 + 1, 1, 0, "PST_RESERVED_NONZERO"); |
| 1041 | emit_bad(dir, "bad-reserved1.pst", &m, O_RES1, 0x80, 0, "PST_RESERVED_NONZERO"); |
| 1042 | emit_bad(dir, "bad-length-mismatch.pst", &m, -1, 0, -2, "PST_BAD_LENGTH"); |
| 1043 | emit_bad(dir, "bad-payload-len-over-800.pst", &m, O_PLEN, 0x04, 0, "PST_BAD_LENGTH"); |
| 1044 | emit_bad(dir, "bad-trailing-byte.pst", &m, -1, 0, 1, "PST_TRAILING_BYTES"); |
| 1045 | emit_bad(dir, "bad-effect-class.pst", &m, O_EFFECT, 3, 0, "PST_BAD_EFFECT_CLASS"); |
| 1046 | memset(m.cause, 0, 32); |
| 1047 | emit_bad(dir, "bad-no-cause.pst", &m, -1, 0, 0, "PST_NO_CAUSE"); |
| 1048 | base(&m, 1, "hello"); |
| 1049 | memset(m.task, 0, 16); |
| 1050 | emit_bad(dir, "bad-no-task-id.pst", &m, -1, 0, 0, "PST_NO_TASK"); |
| 1051 | base(&m, 1, "hello"); |
| 1052 | memset(m.idem, 0, 16); |
| 1053 | emit_bad(dir, "bad-no-idempotence-key.pst", &m, -1, 0, 0, "PST_NO_TASK"); |
| 1054 | base(&m, 1, "hello"); |
| 1055 | memset(m.objective, 0, 32); |
| 1056 | emit_bad(dir, "bad-request-no-objective.pst", &m, -1, 0, 0, "PST_NO_OBJECTIVE"); |
| 1057 | base(&m, 1, "hello"); |
| 1058 | m.digest[31] ^= 1; |
| 1059 | emit_bad(dir, "bad-digest.pst", &m, -1, 0, 0, "PST_DIGEST_MISMATCH"); |
| 1060 | |
| 1061 | /* tool-call messages (spec section 11): the head, one chunk, and the head refusals */ |
| 1062 | emit_raw(dir, "interplane-tool-request.json", (const uint8_t *)IPR_REQ, IPR_LEN); |
| 1063 | base(&m, T_TOOL_REQ, ""); |
| 1064 | set_tool_head(&m, ipr_digest, (uint32_t)IPR_LEN, 0); |
| 1065 | emit_ok(dir, "ok-tool-call-request.pst", &m); |
| 1066 | base(&m, T_TOOL_CHUNK, ""); |
| 1067 | set_payload(&m, (const uint8_t *)IPR_REQ, PST_MAX_PAY); |
| 1068 | emit_ok(dir, "ok-tool-call-chunk.pst", &m); |
| 1069 | base(&m, T_TOOL_REQ, ""); |
| 1070 | set_tool_head(&m, ipr_digest, (uint32_t)IPR_LEN, 4); /* a fifth field, e.g. a chunk count */ |
| 1071 | emit_bad(dir, "bad-tool-head-extra-field.pst", &m, -1, 0, 0, "PST_BAD_TOOL_HEAD"); |
| 1072 | base(&m, T_TOOL_REQ, ""); |
| 1073 | set_payload(&m, ipr_digest, 32); /* the digest without the length */ |
| 1074 | emit_bad(dir, "bad-tool-head-short.pst", &m, -1, 0, 0, "PST_BAD_TOOL_HEAD"); |
| 1075 | base(&m, T_TOOL_REQ, ""); |
| 1076 | set_tool_head(&m, ipr_digest, 0, 0); |
| 1077 | emit_bad(dir, "bad-tool-head-zero-length.pst", &m, -1, 0, 0, "PST_BAD_TOOL_HEAD"); |
| 1078 | base(&m, T_TOOL_REQ, ""); |
| 1079 | set_tool_head(&m, ipr_digest, PST_TOOL_MAX + 1, 0); |
| 1080 | emit_bad(dir, "bad-tool-head-over-limit.pst", &m, -1, 0, 0, "PST_BAD_TOOL_HEAD"); |
| 1081 | base(&m, T_TOOL_REQ, ""); |
| 1082 | set_tool_head(&m, ipr_digest, (uint32_t)IPR_LEN, 0); |
| 1083 | memset(m.objective, 0, 32); |
| 1084 | emit_bad(dir, "bad-tool-request-no-objective.pst", &m, -1, 0, 0, "PST_NO_OBJECTIVE"); |
| 1085 | |
| 1086 | if (fclose(gen_expected) != 0) gen_fail = 1; |
| 1087 | return gen_fail ? 2 : make_sessions(dir); |
| 1088 | } |
| 1089 | |
| 1090 | int main(int argc, char **argv) { |
| 1091 | char out[1024]; |
| 1092 | int rt; |
| 1093 | if (argc == 2 && argv[1][0] != '-') { |
| 1094 | int code = check_file(argv[1], out, sizeof out, &rt); |
| 1095 | if (code < 0) return 2; |
| 1096 | puts(out); |
| 1097 | return code == PST_OK && rt ? 0 : 1; |
| 1098 | } |
| 1099 | if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return corpus(argv[2]); |
| 1100 | if (argc == 3 && strcmp(argv[1], "--make-vectors") == 0) return make_vectors(argv[2]); |
| 1101 | if (argc == 3 && strcmp(argv[1], "--session") == 0) { |
| 1102 | session_verdict v; |
| 1103 | size_t n; |
| 1104 | if (slurp(argv[2], &n)) { fprintf(stderr, "cannot read %s\n", argv[2]); return 2; } |
| 1105 | session_check(file_buf, n, &v); |
| 1106 | session_line(&v, out, sizeof out); |
| 1107 | puts(out); |
| 1108 | return v.code == PST_OK ? 0 : 1; |
| 1109 | } |
| 1110 | if (argc >= 5 && strcmp(argv[1], "--session-fuzz") == 0) |
| 1111 | return session_fuzz(strtoull(argv[2], NULL, 10), strtoul(argv[3], NULL, 10), argc - 4, argv + 4); |
| 1112 | fprintf(stderr, "usage: pst_verify FILE | --session FILE | --corpus DIR | --make-vectors DIR\n" |
| 1113 | " | --session-fuzz SEED COUNT SESSION_FILE...\n"); |
| 1114 | return 2; |
| 1115 | } |