interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / argus / tests / test_argus_event.c

891 lines · 48273 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * test_argus_event.c -- ARGUS lane B: event ABI tests.
19 * layout (static), round trip for every kind, known-answer digests,
20 * malformed-field rejection, forward compatibility, secret-negative scan.
21 * Run from native/argus (the secret scan reads . and tests/).
22 */
23#include "argus_abi.h"
24#include "sha256.h"
25#include "../capability/aienos_capability.h"
26
27#include <dirent.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <stddef.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <strings.h>
35#include <sys/stat.h>
36#include <unistd.h>
37
38/* ---- ABI layout: the header's documented offsets are the contract ---- */
39_Static_assert(sizeof(ArgusEvent) == ARGUS_EVENT_SIZE, "event is 128 bytes");
40_Static_assert(offsetof(ArgusEvent, version) == 0, "off");
41_Static_assert(offsetof(ArgusEvent, class_) == 1, "off");
42_Static_assert(offsetof(ArgusEvent, kind) == 2, "off");
43_Static_assert(offsetof(ArgusEvent, effect_class) == 4, "off");
44_Static_assert(offsetof(ArgusEvent, outcome) == 5, "off");
45_Static_assert(offsetof(ArgusEvent, flags) == 6, "off");
46_Static_assert(offsetof(ArgusEvent, sequence) == 8, "off");
47_Static_assert(offsetof(ArgusEvent, tick) == 16, "off");
48_Static_assert(offsetof(ArgusEvent, principal) == 24, "off");
49_Static_assert(offsetof(ArgusEvent, code) == 28, "off");
50_Static_assert(offsetof(ArgusEvent, cap_id) == 32, "off");
51_Static_assert(offsetof(ArgusEvent, object_id) == 36, "off");
52_Static_assert(offsetof(ArgusEvent, cap_generation) == 40, "off");
53_Static_assert(offsetof(ArgusEvent, world_generation) == 48, "off");
54_Static_assert(offsetof(ArgusEvent, resource) == 56, "off");
55_Static_assert(offsetof(ArgusEvent, machine_id) == 64, "off");
56_Static_assert(offsetof(ArgusEvent, evidence_digest) == 96, "off");
57
58/* ArgusCapRef must stay layout-identical to the authority's AienosCapRef. */
59_Static_assert(sizeof(ArgusCapRef) == sizeof(AienosCapRef), "capref size");
60_Static_assert(offsetof(ArgusCapRef, cap_id) == offsetof(AienosCapRef, cap_id), "capref cap_id");
61_Static_assert(offsetof(ArgusCapRef, generation) == offsetof(AienosCapRef, generation), "capref generation");
62_Static_assert(sizeof(((ArgusCapRef *)0)->cap_id) == sizeof(((AienosCapRef *)0)->cap_id), "capref cap_id size");
63_Static_assert(sizeof(((ArgusCapRef *)0)->generation) == sizeof(((AienosCapRef *)0)->generation), "capref gen size");
64
65/* Secret-negative (layout half): no event field is 32 bytes except the two digest slots. */
66#define NOT32(f) _Static_assert(sizeof(((ArgusEvent *)0)->f) != 32, "32-byte field: " #f)
67NOT32(version); NOT32(class_); NOT32(kind); NOT32(effect_class); NOT32(outcome); NOT32(flags);
68NOT32(sequence); NOT32(tick); NOT32(principal); NOT32(code); NOT32(cap_id); NOT32(object_id);
69NOT32(cap_generation); NOT32(world_generation); NOT32(resource);
70_Static_assert(sizeof(((ArgusEvent *)0)->machine_id) == 32, "machine_id");
71_Static_assert(sizeof(((ArgusEvent *)0)->evidence_digest) == 32, "evidence_digest");
72/* 17 fields; if a field is added, the sum below stops matching and this file must be updated. */
73_Static_assert(1 + 1 + 2 + 1 + 1 + 2 + 8 + 8 + 4 + 4 + 4 + 4 + 8 + 8 + 8 + 32 + 32 == ARGUS_EVENT_SIZE,
74 "field list complete");
75
76static int failures;
77#define CHECK(cond) do { if (!(cond)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); } } while (0)
78
79static const uint16_t all_kinds[] = {
80 ARGUS_EV_CAPABILITY_GRANTED, ARGUS_EV_CAPABILITY_USED, ARGUS_EV_CAPABILITY_DENIED, ARGUS_EV_CAPABILITY_REVOKED,
81 ARGUS_EV_CREDENTIAL_LEASE_CREATED, ARGUS_EV_CREDENTIAL_LEASE_USED, ARGUS_EV_CREDENTIAL_LEASE_REVOKED,
82 ARGUS_EV_ARTIFACT_ADMITTED, ARGUS_EV_ARTIFACT_REJECTED, ARGUS_EV_ARTIFACT_ACTIVATED,
83 ARGUS_EV_MACHINE_JOINED, ARGUS_EV_MACHINE_TRUST_CHANGED, ARGUS_EV_MACHINE_REMOVED,
84 ARGUS_EV_PROVIDER_DISCOVERED, ARGUS_EV_PROVIDER_CHANGED, ARGUS_EV_PROVIDER_QUARANTINED, ARGUS_EV_PROVIDER_USED,
85 ARGUS_EV_EXTERNAL_EFFECT_REQUESTED, ARGUS_EV_EXTERNAL_EFFECT_DENIED, ARGUS_EV_EXTERNAL_EFFECT_COMMITTED,
86 ARGUS_EV_INTEGRITY_VIOLATION, ARGUS_EV_SIGNATURE_FAILURE, ARGUS_EV_STALE_GENERATION, ARGUS_EV_FORGED_CAPABILITY,
87 ARGUS_EV_WORLD_COMMITTED, ARGUS_EV_POLICY_CHANGED, ARGUS_EV_RUNTIME_BUILD_CHANGED,
88 ARGUS_EV_TELEMETRY_DROPPED, ARGUS_EV_CAPABILITY_USE_SUMMARY,
89 ARGUS_EV_CONTAINMENT_PROPOSED, ARGUS_EV_CONTAINMENT_DECIDED, ARGUS_EV_CONTAINMENT_EXECUTED, /* v1.2 */
90 ARGUS_EV_AUTHORITY_ESCALATED,
91};
92#define N_KINDS (sizeof all_kinds / sizeof all_kinds[0])
93
94static int is_known_kind(unsigned k)
95{
96 for (size_t i = 0; i < N_KINDS; i++) if (all_kinds[i] == k) return 1;
97 return 0;
98}
99
100/* Independent restatement of the per-kind class floor (argus_abi.h hostile-review rules). */
101static unsigned floor_of(unsigned k)
102{
103 static const uint16_t crit[] = { 60, 61, 62, 63, 80, 42, 31, 32, 71, 72, 4, 12, 21, 52, 90, 91, 92, 93 };
104 static const uint16_t sec[] = { 1, 3, 10, 20, 22, 30, 40, 41, 50, 51, 70 };
105 static const uint16_t aud[] = { 2, 11, 43, 81 };
106 for (size_t i = 0; i < sizeof crit / sizeof crit[0]; i++) if (crit[i] == k) return 1;
107 for (size_t i = 0; i < sizeof sec / sizeof sec[0]; i++) if (sec[i] == k) return 2;
108 for (size_t i = 0; i < sizeof aud / sizeof aud[0]; i++) if (aud[i] == k) return 3;
109 return 0;
110}
111
112/* v1.2 kinds 90-92, restated from ARGUS1_SPEC section 3 as a table: for each (kind, status)
113 * the bit set of allowed outcomes (bit 1 OK, bit 2 DENIED, bit 3 ERROR). object_id =
114 * type | status << 8 | finding_code << 16; world_generation = request_id. */
115static int containment_ok(unsigned kind, uint32_t object_id, unsigned outcome, unsigned flags, uint64_t request_id)
116{
117 static const uint8_t allowed[3][9] = {
118 /* status: 0 PROP 1 GRANT 2 DENY 3 ESC 4 DONE 5 PART 6 FAIL 7 UNAV 8 SCOPE */
119 /* 90 */ { 1u << 1, 0, 0, 0, 0, 0, 0, 0, 0 },
120 /* 91 */ { 0, 1u << 1, 1u << 2, 1u << 3, 0, 0, 0, 0, 0 },
121 /* 92 */ { 0, 0, 0, 0, 1u << 1, 1u << 1, (1u << 2) | (1u << 3), 1u << 3, 1u << 3 },
122 };
123 unsigned type = object_id & 0xFFu, status = (object_id >> 8) & 0xFFu;
124 if (type < 1 || type > 10 || request_id == 0 || status > 8 || outcome > 3) return 0;
125 if (!(allowed[kind - 90][status] & (1u << outcome))) return 0;
126 if (kind == 92 && status == 4 && type != 1 && !(flags & 1u)) return 0; /* I2: DONE on a SYNTH type needs SYNTHETIC */
127 return 1;
128}
129
130/* Reference oracle: the result argus_event_validate must give. */
131static int oracle(const ArgusEvent *e)
132{
133 if (e->version != 1) return ARGUS_ERR_VERSION;
134 if (e->class_ < 1 || e->class_ > 4) return ARGUS_ERR_MALFORMED;
135 if (!is_known_kind(e->kind)) return ARGUS_ERR_MALFORMED;
136 if (e->class_ > floor_of(e->kind)) return ARGUS_ERR_MALFORMED;
137 if (e->effect_class > 3) return ARGUS_ERR_MALFORMED;
138 if (e->outcome < 1 || e->outcome > 3) return ARGUS_ERR_MALFORMED;
139 if (((e->flags & 2u) != 0) != (e->kind == 80 || e->kind == 90)) return ARGUS_ERR_MALFORMED; /* v1.2: 90 too */
140 if (e->kind >= 90 && e->kind <= 92 && !containment_ok(e->kind, e->object_id, e->outcome, e->flags, e->world_generation))
141 return ARGUS_ERR_MALFORMED;
142 if (e->cap_id >= 256 && e->cap_id != 0xFFFFFFFFu) return ARGUS_ERR_MALFORMED; /* v1.1: CAP_NONE allowed */
143 if (e->kind == 81 && e->outcome != 1) return ARGUS_ERR_MALFORMED; /* summary: outcome OK only */
144 if (e->kind == 81 && e->world_generation > e->cap_generation) return ARGUS_ERR_MALFORMED; /* summary: min gen <= max gen (v1.1) */
145 if (e->sequence == 0 || e->sequence == UINT64_MAX) return ARGUS_ERR_MALFORMED;
146 if (e->kind >= 50 && e->kind <= 52 && e->effect_class != 3) return ARGUS_ERR_MALFORMED;
147 return ARGUS_OK;
148}
149
150static ArgusEvent fixed_event(void)
151{
152 ArgusEvent e;
153 memset(&e, 0, sizeof e);
154 e.version = ARGUS_ABI_VERSION;
155 e.class_ = ARGUS_CLASS_SECURITY;
156 e.kind = ARGUS_EV_CAPABILITY_USED;
157 e.effect_class = ARGUS_EFFECT_EXTERNAL;
158 e.outcome = ARGUS_OUTCOME_OK;
159 e.flags = ARGUS_FLAG_SYNTHETIC;
160 e.sequence = 0x0102030405060708ull;
161 e.tick = 0x1112131415161718ull;
162 e.principal = 0x21222324u;
163 e.code = -7;
164 e.cap_id = 0x31323334u;
165 e.object_id = 0x41424344u;
166 e.cap_generation = 0x5152535455565758ull;
167 e.world_generation = 0x6162636465666768ull;
168 e.resource = 0x8182838485868788ull;
169 for (unsigned i = 0; i < 32; i++) {
170 e.machine_id[i] = (uint8_t)(0xA0 + i);
171 e.evidence_digest[i] = (uint8_t)(0xC0 + i);
172 }
173 return e;
174}
175
176/* fixed_event is the known-answer sample (its bytes and digests are pinned; encode
177 * never validates). Its cap_id is out of range, so the validity tests start from
178 * this valid variant instead. */
179static ArgusEvent valid_event(void)
180{
181 ArgusEvent e = fixed_event();
182 e.cap_id = 0x34;
183 return e;
184}
185
186/* Make e valid for its kind: class at the floor, CONSUMER iff TELEMETRY_DROPPED, EXTERNAL for effects,
187 * outcome OK for CAPABILITY_USE_SUMMARY (MIN generation = MAX). Stream bits (2-15) are kept. */
188static void fit_kind(ArgusEvent *e)
189{
190 e->class_ = (uint8_t)floor_of(e->kind);
191 e->flags = (uint16_t)((e->flags & (uint16_t)~ARGUS_FLAG_CONSUMER) |
192 ((e->kind == ARGUS_EV_TELEMETRY_DROPPED || e->kind == ARGUS_EV_CONTAINMENT_PROPOSED) ? ARGUS_FLAG_CONSUMER : 0));
193 if (e->kind >= 50 && e->kind <= 52) e->effect_class = ARGUS_EFFECT_EXTERNAL;
194 if (e->kind == ARGUS_EV_CAPABILITY_USE_SUMMARY) { e->outcome = ARGUS_OUTCOME_OK; e->world_generation = e->cap_generation; }
195 /* v1.2 90-92: RevokeCapability, a status that matches the outcome, request_id != 0. */
196 if (e->kind >= ARGUS_EV_CONTAINMENT_PROPOSED && e->kind <= ARGUS_EV_CONTAINMENT_EXECUTED) {
197 if (e->outcome < ARGUS_OUTCOME_OK || e->outcome > ARGUS_OUTCOME_MAX) e->outcome = ARGUS_OUTCOME_OK;
198 unsigned st;
199 if (e->kind == ARGUS_EV_CONTAINMENT_PROPOSED) { e->outcome = ARGUS_OUTCOME_OK; st = ARGUS_CSTATUS_PROPOSED; }
200 else if (e->kind == ARGUS_EV_CONTAINMENT_DECIDED) st = ARGUS_CSTATUS_GRANT + (unsigned)(e->outcome - 1);
201 else st = e->outcome == ARGUS_OUTCOME_OK ? ARGUS_CSTATUS_DONE : ARGUS_CSTATUS_FAILED;
202 e->object_id = ARGUS_CONTAIN_PACK(ARGUS_CONTAIN_REVOKE_CAPABILITY, st, ARGUS_F_STALE_GENERATION);
203 if (e->world_generation == 0) e->world_generation = 1;
204 }
205}
206
207static ArgusFinding fixed_finding(void)
208{
209 ArgusFinding f;
210 memset(&f, 0, sizeof f);
211 f.code = ARGUS_F_REVOKED_CAPABILITY_USED;
212 f.severity = ARGUS_SEV_HIGH;
213 f.confidence = ARGUS_CONF_DETERMINISTIC;
214 f.sync_allowed = 1;
215 f.containment = ARGUS_CONTAIN_REVOKE_CAPABILITY;
216 f.detector = ARGUS_F_REVOKED_CAPABILITY_USED;
217 f.sequence = 42;
218 f.prior_sequence = 17;
219 f.principal = 7;
220 f.cap_id = 9;
221 f.cap_generation = 3;
222 for (unsigned i = 0; i < 32; i++) { f.machine_id[i] = (uint8_t)i; f.event_digest[i] = (uint8_t)(0xFF - i); }
223 return f;
224}
225
226static void hex(char *out, const uint8_t *b, size_t n)
227{
228 for (size_t i = 0; i < n; i++) sprintf(out + 2 * i, "%02x", b[i]);
229 out[2 * n] = 0;
230}
231
232static void check_digest(const char *name, const uint8_t d[32], const char *expect)
233{
234 char h[65];
235 hex(h, d, 32);
236 printf(" %-22s %s\n", name, h);
237 if (strcmp(h, expect) != 0) { failures++; fprintf(stderr, "FAIL %s: expected %s\n", name, expect); }
238}
239
240/* ---- known-answer vectors ---- */
241static void test_known_answers(void)
242{
243 uint8_t d[32], b[ARGUS_EVENT_SIZE];
244 char h[2 * ARGUS_EVENT_SIZE + 1];
245 printf("known-answer vectors:\n");
246 sha256_hash((const uint8_t *)"abc", 3, d);
247 check_digest("sha256(\"abc\")", d, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
248
249 ArgusEvent e = fixed_event();
250 CHECK(argus_event_encode(&e, b) == ARGUS_OK);
251 hex(h, b, sizeof b);
252 printf(" fixed event bytes %s\n", h);
253 /* Spot-check little-endian placement independently of the digest. */
254 CHECK(b[0] == 1 && b[1] == 2 && b[2] == 2 && b[3] == 0 && b[4] == 3 && b[5] == 1 && b[6] == 1 && b[7] == 0);
255 CHECK(b[8] == 0x08 && b[15] == 0x01 && b[16] == 0x18 && b[24] == 0x24 && b[27] == 0x21);
256 CHECK(b[28] == 0xF9 && b[29] == 0xFF && b[30] == 0xFF && b[31] == 0xFF); /* -7 */
257 CHECK(b[32] == 0x34 && b[36] == 0x44 && b[40] == 0x58 && b[47] == 0x51 && b[48] == 0x68 && b[55] == 0x61);
258 CHECK(b[56] == 0x88 && b[63] == 0x81 && b[64] == 0xA0 && b[95] == 0xBF && b[96] == 0xC0 && b[127] == 0xDF);
259
260 argus_event_digest(&e, d);
261 check_digest("event digest", d, "1be29450b792d28426444fceb5e44174e69bb631d5ef6063a870c21929574303");
262 uint8_t chain[32] = {0};
263 argus_chain_extend(chain, &e);
264 check_digest("chain(0, event)", chain, "09ea598e432197cc2d6ff341ec57ab403c06312bad2992480b72c6ce87a6d747");
265 ArgusEvent e2 = e;
266 e2.sequence++;
267 argus_chain_extend(chain, &e2);
268 check_digest("chain(chain, event+1)", chain, "21b19c9ba36f86074263f49952b64ee8dea9b5ff02ed215555cb1a03bfda78e0");
269 ArgusFinding f = fixed_finding();
270 argus_finding_digest(&f, d);
271 check_digest("finding digest", d, "b56e133cc6f6cfefe642ce0f1ec74e67c3fee57db2f114257ba04d3d3ee7680a");
272
273 /* Chain definition check: H(chain || bytes) computed by hand. */
274 uint8_t buf[32 + ARGUS_EVENT_SIZE], manual[32], c2[32] = {0};
275 memset(buf, 0, 32);
276 argus_event_encode(&e, buf + 32);
277 sha256_hash(buf, sizeof buf, manual);
278 argus_chain_extend(c2, &e);
279 CHECK(memcmp(manual, c2, 32) == 0);
280 /* encode/digest/chain never validate: a sequence-0 record still has canonical bytes. */
281 {
282 ArgusEvent z; memset(&z, 0, sizeof z);
283 uint8_t zb[ARGUS_EVENT_SIZE], zc[32] = {0}, zd[32];
284 CHECK(argus_event_validate(&z) != ARGUS_OK);
285 CHECK(argus_event_encode(&z, zb) == ARGUS_OK);
286 for (unsigned i = 0; i < ARGUS_EVENT_SIZE; i++) CHECK(zb[i] == 0);
287 argus_chain_extend(zc, &z);
288 argus_event_digest(&z, zd);
289 CHECK(memcmp(zc, zd, 32) != 0);
290 }
291 /* Event digest definition check: H(bytes). */
292 sha256_hash(buf + 32, ARGUS_EVENT_SIZE, manual);
293 argus_event_digest(&e, d);
294 CHECK(memcmp(manual, d, 32) == 0);
295 /* Every finding field affects its digest. */
296 uint8_t base[32];
297 argus_finding_digest(&f, base);
298 for (int field = 0; field < 13; field++) {
299 ArgusFinding g = f;
300 switch (field) {
301 case 0: g.code ^= 1; break; case 1: g.severity ^= 1; break;
302 case 2: g.confidence ^= 1; break; case 3: g.sync_allowed ^= 1; break;
303 case 4: g.containment ^= 1; break; case 5: g.detector ^= 0x100; break;
304 case 6: g.sequence ^= 1ull << 63; break; case 7: g.prior_sequence ^= 1; break;
305 case 8: g.principal ^= 1u << 31; break; case 9: g.cap_id ^= 1; break;
306 case 10: g.cap_generation ^= 1ull << 40; break;
307 case 11: g.machine_id[31] ^= 1; break; case 12: g.event_digest[0] ^= 0x80; break;
308 }
309 argus_finding_digest(&g, d);
310 CHECK(memcmp(d, base, 32) != 0);
311 }
312}
313
314/* ---- round trip, every kind, every class it allows ---- */
315static void test_round_trip(void)
316{
317 uint8_t b[ARGUS_EVENT_SIZE], b2[ARGUS_EVENT_SIZE];
318 size_t n = 0;
319 for (size_t i = 0; i < N_KINDS; i++) {
320 unsigned fl = floor_of(all_kinds[i]);
321 CHECK(fl >= 1 && fl <= 3);
322 CHECK(argus_event_min_class(all_kinds[i]) == fl);
323 for (unsigned cls = 1; cls <= fl; cls++) {
324 ArgusEvent e = valid_event(), d;
325 e.kind = all_kinds[i];
326 e.effect_class = (uint8_t)(i % 4);
327 e.outcome = (uint8_t)(1 + i % 3);
328 e.flags = (uint16_t)((i % 2) | ((i * 613u) << ARGUS_FLAG_STREAM_SHIFT)); /* v1.1 stream bits */
329 fit_kind(&e);
330 e.class_ = (uint8_t)cls;
331 e.sequence = 1 + i * 7 + cls;
332 e.code = (int32_t)(0 - (int32_t)i);
333 e.cap_id = i % 5 == 4 ? ARGUS_CAP_NONE : (uint32_t)(i * 9 % ARGUS_CAP_MAX);
334 CHECK(argus_event_validate(&e) == ARGUS_OK);
335 CHECK(argus_event_encode(&e, b) == ARGUS_OK);
336 memset(&d, 0x5A, sizeof d);
337 CHECK(argus_event_decode(b, &d) == ARGUS_OK);
338 CHECK(argus_event_encode(&d, b2) == ARGUS_OK);
339 CHECK(memcmp(b, b2, sizeof b) == 0);
340 CHECK(memcmp(&e, &d, sizeof e) == 0); /* no padding in ArgusEvent (asserted above) */
341 n++;
342 }
343 }
344 CHECK(N_KINDS == 33);
345 CHECK(n == 18 * 1 + 11 * 2 + 4 * 3);
346 printf("round trip: %zu kinds x every class at or above the kind's floor = %zu events OK\n", N_KINDS, n);
347}
348
349/* ---- malformed fields ---- */
350static int decode_mut(size_t off, uint8_t v)
351{
352 ArgusEvent e = valid_event(), out, sentinel;
353 uint8_t b[ARGUS_EVENT_SIZE];
354 argus_event_encode(&e, b);
355 b[off] = v;
356 memset(&out, 0x77, sizeof out);
357 sentinel = out;
358 int rc = argus_event_decode(b, &out);
359 if (rc != ARGUS_OK && memcmp(&out, &sentinel, sizeof out) != 0) { failures++; fprintf(stderr, "FAIL decode wrote output on error\n"); }
360 return rc;
361}
362
363/* validate rejects; encode never validates but its bytes must then fail decode the same way. */
364static void expect_struct_at(ArgusEvent e, int want, int line)
365{
366 uint8_t b[ARGUS_EVENT_SIZE];
367 ArgusEvent out;
368 int f0 = failures;
369 CHECK(argus_event_validate(&e) == want);
370 CHECK(argus_event_encode(&e, b) == ARGUS_OK);
371 CHECK(argus_event_decode(b, &out) == want);
372 if (failures != f0) fprintf(stderr, " (expect_struct called from line %d)\n", line);
373}
374#define expect_struct(e, want) expect_struct_at((e), (want), __LINE__)
375
376/* Byte-level check: e encoded, decode must agree with the oracle. */
377static void expect_oracle(ArgusEvent e)
378{
379 expect_struct(e, oracle(&e));
380}
381
382static void test_malformed(void)
383{
384 size_t n = 0;
385 CHECK(oracle(&(ArgusEvent){0}) == ARGUS_ERR_VERSION);
386 { ArgusEvent v = valid_event(); CHECK(oracle(&v) == ARGUS_OK); expect_struct(v, ARGUS_OK); }
387 /* version: anything but 1 -> VERSION (checked first) */
388 for (unsigned v = 0; v < 256; v++) {
389 if (v == 1) continue;
390 CHECK(decode_mut(0, (uint8_t)v) == ARGUS_ERR_VERSION);
391 ArgusEvent e = valid_event(); e.version = (uint8_t)v; expect_struct(e, ARGUS_ERR_VERSION);
392 n++;
393 }
394 /* class: only 1..4, and no weaker than the kind floor (CAPABILITY_USED: AUDIT) */
395 for (unsigned v = 0; v < 256; v++) {
396 int want = (v >= 1 && v <= 3) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
397 CHECK(decode_mut(1, (uint8_t)v) == want);
398 ArgusEvent e = valid_event(); e.class_ = (uint8_t)v; expect_struct(e, want);
399 n++;
400 }
401 /* class x kind: every class byte against every known kind (fitted otherwise valid) */
402 for (size_t i = 0; i < N_KINDS; i++) {
403 for (unsigned v = 0; v < 256; v++) {
404 ArgusEvent e = valid_event(); e.kind = all_kinds[i]; fit_kind(&e); e.class_ = (uint8_t)v;
405 int want = (v >= 1 && v <= floor_of(all_kinds[i])) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
406 CHECK(oracle(&e) == want);
407 expect_struct(e, want);
408 n++;
409 }
410 }
411 /* kind: every 16-bit value. min_class is 0 exactly for unknown kinds.
412 * (a) the valid base as is (AUDIT, no CONSUMER, EXTERNAL) -> oracle decides;
413 * (b) fitted to the kind -> OK iff the kind is known. */
414 size_t known = 0;
415 for (unsigned v = 0; v < 65536; v++) {
416 CHECK((argus_event_min_class((uint16_t)v) != 0) == is_known_kind(v));
417 CHECK(argus_event_min_class((uint16_t)v) == floor_of(v));
418 ArgusEvent e = valid_event(); e.kind = (uint16_t)v; expect_oracle(e);
419 ArgusEvent f = valid_event(); f.kind = (uint16_t)v; if (is_known_kind(v)) fit_kind(&f);
420 int want = is_known_kind(v) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
421 CHECK(oracle(&f) == want);
422 expect_struct(f, want);
423 uint8_t b[ARGUS_EVENT_SIZE]; ArgusEvent out;
424 argus_event_encode(&f, b);
425 b[2] = (uint8_t)v; b[3] = (uint8_t)(v >> 8);
426 CHECK(argus_event_decode(b, &out) == want);
427 known += is_known_kind(v);
428 n++;
429 }
430 CHECK(known == N_KINDS);
431 CHECK(decode_mut(3, 1) == ARGUS_ERR_MALFORMED); /* high byte of kind */
432 /* effect_class 0..3 (CAPABILITY_USED: any); EXTERNAL_EFFECT_*: only EXTERNAL */
433 for (unsigned v = 0; v < 256; v++) {
434 int want = v <= 3 ? ARGUS_OK : ARGUS_ERR_MALFORMED;
435 CHECK(decode_mut(4, (uint8_t)v) == want);
436 ArgusEvent e = valid_event(); e.effect_class = (uint8_t)v; expect_struct(e, want);
437 for (uint16_t k = 50; k <= 52; k++) {
438 ArgusEvent x = valid_event(); x.kind = k; fit_kind(&x); x.effect_class = (uint8_t)v;
439 expect_struct(x, v == ARGUS_EFFECT_EXTERNAL ? ARGUS_OK : ARGUS_ERR_MALFORMED);
440 n++;
441 }
442 n++;
443 }
444 /* outcome 1..3 */
445 for (unsigned v = 0; v < 256; v++) {
446 int want = (v >= 1 && v <= 3) ? ARGUS_OK : ARGUS_ERR_MALFORMED;
447 CHECK(decode_mut(5, (uint8_t)v) == want);
448 ArgusEvent e = valid_event(); e.outcome = (uint8_t)v; expect_struct(e, want);
449 n++;
450 }
451 /* flags: every 16-bit value, on a producer kind and on TELEMETRY_DROPPED.
452 * v1.1: bits 2-15 are the stream id, so no bit is reserved. Producer kind: OK iff
453 * CONSUMER is clear. TELEMETRY_DROPPED: OK iff CONSUMER is set. */
454 for (unsigned v = 0; v < 65536; v++) {
455 int want_p = (v & ARGUS_FLAG_CONSUMER) == 0 ? ARGUS_OK : ARGUS_ERR_MALFORMED;
456 int want_t = (v & ARGUS_FLAG_CONSUMER) != 0 ? ARGUS_OK : ARGUS_ERR_MALFORMED;
457 CHECK(ARGUS_STREAM_OF(v) == (v >> 2));
458 ArgusEvent e = valid_event(); e.flags = (uint16_t)v;
459 CHECK(oracle(&e) == want_p);
460 expect_struct(e, want_p);
461 ArgusEvent t = valid_event(); t.kind = ARGUS_EV_TELEMETRY_DROPPED; fit_kind(&t); t.flags = (uint16_t)v;
462 CHECK(oracle(&t) == want_t);
463 expect_struct(t, want_t);
464 n += 2;
465 }
466 CHECK(decode_mut(6, 0x04) == ARGUS_OK); /* stream 1 (v1.1; was reserved in v1) */
467 CHECK(decode_mut(6, 0x03) == ARGUS_ERR_MALFORMED); /* CONSUMER on a producer kind */
468 CHECK(decode_mut(7, 0x80) == ARGUS_OK); /* stream 0x2000 */
469 CHECK(decode_mut(6, 0xFD) == ARGUS_OK && decode_mut(7, 0xFF) == ARGUS_OK); /* stream 0x3FFF, the maximum */
470 /* cap_id: < ARGUS_CAP_MAX or == ARGUS_CAP_NONE (v1.1; 0 is the OFFICE slot, a real cap) */
471 {
472 static const uint32_t ok_ids[] = { 0, 1, 255, ARGUS_CAP_NONE };
473 static const uint32_t bad_ids[] = { 256, 257, 300, 0x10000, 0x7FFFFFFFu, 0xFFFFFFFEu };
474 for (size_t i = 0; i < 4; i++) { ArgusEvent e = valid_event(); e.cap_id = ok_ids[i]; expect_struct(e, ARGUS_OK); n++; }
475 for (size_t i = 0; i < 6; i++) { ArgusEvent e = valid_event(); e.cap_id = bad_ids[i]; expect_struct(e, ARGUS_ERR_MALFORMED); n++; }
476 /* byte 33 of cap_id 0x34: 0x0000VV34 is never CAP_NONE, so only VV == 0 is valid */
477 for (unsigned v = 0; v < 256; v++) {
478 CHECK(decode_mut(33, (uint8_t)v) == (v == 0 ? ARGUS_OK : ARGUS_ERR_MALFORMED));
479 n++;
480 }
481 }
482 /* sequence: 0 and UINT64_MAX rejected, UINT64_MAX-1 accepted */
483 {
484 ArgusEvent e = valid_event(); e.sequence = 0; expect_struct(e, ARGUS_ERR_MALFORMED);
485 e.sequence = UINT64_MAX; expect_struct(e, ARGUS_ERR_MALFORMED);
486 e.sequence = UINT64_MAX - 1; expect_struct(e, ARGUS_OK);
487 uint8_t b[ARGUS_EVENT_SIZE]; ArgusEvent out;
488 e.sequence = 1; argus_event_encode(&e, b); b[8] = 0;
489 CHECK(argus_event_decode(b, &out) == ARGUS_ERR_MALFORMED);
490 memset(b + 8, 0xFF, 8);
491 CHECK(argus_event_decode(b, &out) == ARGUS_ERR_MALFORMED);
492 n += 5;
493 }
494 /* v1.1 CAPABILITY_USE_SUMMARY: floor AUDIT, outcome OK only, any tick accepted,
495 * CONSUMER rejected, stream bits and CAP_NONE accepted. */
496 {
497 ArgusEvent s = valid_event(); s.kind = ARGUS_EV_CAPABILITY_USE_SUMMARY; fit_kind(&s);
498 s.tick = 0; s.resource = 4096; s.cap_generation = 9; s.world_generation = 7; s.object_id = 0; s.effect_class = ARGUS_EFFECT_NONE;
499 CHECK(argus_event_min_class(ARGUS_EV_CAPABILITY_USE_SUMMARY) == ARGUS_CLASS_AUDIT);
500 expect_struct(s, ARGUS_OK);
501 ArgusEvent t = s; t.tick = 12345; expect_struct(t, ARGUS_OK); /* producer rule says 0; not enforced */
502 t = s; t.outcome = ARGUS_OUTCOME_DENIED; expect_struct(t, ARGUS_ERR_MALFORMED);
503 t = s; t.outcome = ARGUS_OUTCOME_ERROR; expect_struct(t, ARGUS_ERR_MALFORMED);
504 t = s; t.class_ = ARGUS_CLASS_INFORMATIONAL; expect_struct(t, ARGUS_ERR_MALFORMED);
505 t = s; t.class_ = ARGUS_CLASS_CRITICAL; expect_struct(t, ARGUS_OK);
506 t = s; t.flags |= ARGUS_FLAG_CONSUMER; expect_struct(t, ARGUS_ERR_MALFORMED);
507 t = s; t.flags = (uint16_t)(ARGUS_FLAG_SYNTHETIC | (0x3FFFu << ARGUS_FLAG_STREAM_SHIFT)); expect_struct(t, ARGUS_OK);
508 CHECK(ARGUS_STREAM_OF(t.flags) == 0x3FFFu);
509 t = s; t.cap_id = ARGUS_CAP_NONE; expect_struct(t, ARGUS_OK);
510 t = s; t.cap_id = 0; expect_struct(t, ARGUS_OK);
511 /* header v1.1 (398cfb9): MIN generation in world_generation (64-bit), must be <= MAX
512 * (cap_generation); object_id is unused and deliberately not checked (forward-compat). */
513 t = s; t.world_generation = t.cap_generation; expect_struct(t, ARGUS_OK);
514 t = s; t.world_generation = 0; expect_struct(t, ARGUS_OK);
515 t = s; t.world_generation = t.cap_generation + 1; expect_struct(t, ARGUS_ERR_MALFORMED);
516 t = s; t.cap_generation = 0; t.world_generation = UINT64_MAX; expect_struct(t, ARGUS_ERR_MALFORMED);
517 t = s; t.cap_generation = UINT64_MAX; t.world_generation = (uint64_t)1 << 40; expect_struct(t, ARGUS_OK);
518 t = s; t.object_id = 0xFFFFFFFFu; expect_struct(t, ARGUS_OK);
519 t = s; t.object_id = 0; expect_struct(t, ARGUS_OK);
520 { ArgusEvent u = valid_event(); u.world_generation = u.cap_generation + 1; expect_struct(u, ARGUS_OK); } /* rule is summary-only */
521 for (unsigned v = 0; v < 256; v++) { /* outcome byte of an encoded summary */
522 uint8_t b[ARGUS_EVENT_SIZE]; ArgusEvent out;
523 argus_event_encode(&s, b); b[5] = (uint8_t)v;
524 CHECK(argus_event_decode(b, &out) == (v == ARGUS_OUTCOME_OK ? ARGUS_OK : ARGUS_ERR_MALFORMED));
525 n++;
526 }
527 n += 11;
528 }
529 /* The ring's own drop report must be a valid event (CRITICAL, CONSUMER, CAP_NONE). */
530 {
531 ArgusEvent t = { .version = 1, .class_ = ARGUS_CLASS_CRITICAL, .kind = ARGUS_EV_TELEMETRY_DROPPED,
532 .outcome = ARGUS_OUTCOME_ERROR, .flags = ARGUS_FLAG_CONSUMER, .sequence = 1,
533 .code = ARGUS_ERR_FULL, .object_id = ARGUS_CLASS_AUDIT, .resource = 3 };
534 expect_struct(t, ARGUS_OK);
535 t.class_ = ARGUS_CLASS_SECURITY; expect_struct(t, ARGUS_ERR_MALFORMED);
536 n += 2;
537 }
538 /* NULL arguments */
539 {
540 uint8_t b[ARGUS_EVENT_SIZE]; ArgusEvent e = valid_event();
541 CHECK(argus_event_validate(NULL) == ARGUS_ERR_ARG);
542 CHECK(argus_event_encode(NULL, b) == ARGUS_ERR_ARG);
543 CHECK(argus_event_encode(&e, NULL) == ARGUS_ERR_ARG);
544 CHECK(argus_event_decode(NULL, &e) == ARGUS_ERR_ARG);
545 CHECK(argus_event_decode(b, NULL) == ARGUS_ERR_ARG);
546 }
547 printf("malformed: %zu field values checked (version, class, class x kind floor, every kind, effect incl. "
548 "EXTERNAL_EFFECT_*, outcome, every flags x {producer, TELEMETRY_DROPPED}, cap_id bound, sequence 0/max)\n", n);
549}
550
551/* ---- forward compatibility ---- */
552static void test_forward_compat(void)
553{
554 ArgusEvent e = fixed_event(), out;
555 uint8_t b[ARGUS_EVENT_SIZE];
556 argus_event_encode(&e, b);
557 b[0] = 2;
558 CHECK(argus_event_decode(b, &out) == ARGUS_ERR_VERSION);
559 /* A v2 event with otherwise-unknown fields must still report VERSION, not MALFORMED. */
560 memset(b, 0xFF, sizeof b);
561 b[0] = 2;
562 CHECK(argus_event_decode(b, &out) == ARGUS_ERR_VERSION);
563 printf("forward compat: version 2 bytes rejected with ARGUS_ERR_VERSION\n");
564}
565
566/* ---- secret-negative: tree scan ----
567 * Every regular file under native/argus (sources, headers, README, makefiles,
568 * tests, docs), recursively, except the build output directory `out/`.
569 * The forbidden word is matched case-insensitively; the authority's length
570 * macro case-sensitively. Both patterns are assembled at run time so this
571 * file never contains them literally. */
572static int contains_ci(const char *hay, const char *needle)
573{
574 size_t n = strlen(needle);
575 for (; *hay; hay++) if (strncasecmp(hay, needle, n) == 0) return 1;
576 return 0;
577}
578
579static void scan_tree(const char *dir, const char *word, const char *cap_len, int *files, int *hits)
580{
581 DIR *d = opendir(dir);
582 if (!d) { failures++; fprintf(stderr, "FAIL cannot open %s (run from native/argus)\n", dir); return; }
583 struct dirent *de;
584 while ((de = readdir(d)) != NULL) {
585 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
586 if (strcmp(dir, ".") == 0 && strcmp(de->d_name, "out") == 0) continue; /* build output */
587 char path[1024];
588 snprintf(path, sizeof path, "%s/%s", dir, de->d_name);
589 /* Open first, then fstat the descriptor (no check-then-use race).
590 * O_NOFOLLOW: a symlink fails with ELOOP and is skipped, as before;
591 * O_NONBLOCK: a FIFO cannot hang the scan (it is skipped below). */
592 int fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
593 if (fd < 0) {
594 if (errno == ELOOP || errno == ENXIO) continue; /* symlink / socket: not scanned */
595 failures++; fprintf(stderr, "FAIL cannot stat %s\n", path); continue;
596 }
597 struct stat st;
598 if (fstat(fd, &st) != 0) { close(fd); failures++; fprintf(stderr, "FAIL cannot stat %s\n", path); continue; }
599 if (S_ISDIR(st.st_mode)) { close(fd); scan_tree(path, word, cap_len, files, hits); continue; }
600 if (!S_ISREG(st.st_mode)) { close(fd); continue; }
601 FILE *fp = fdopen(fd, "r");
602 if (!fp) { close(fd); failures++; fprintf(stderr, "FAIL cannot read %s\n", path); continue; }
603 (*files)++;
604 char line[4096];
605 int lineno = 0;
606 while (fgets(line, sizeof line, fp)) {
607 lineno++;
608 if (contains_ci(line, word) || strstr(line, cap_len)) {
609 (*hits)++;
610 fprintf(stderr, "FAIL %s:%d contains a forbidden word\n", path, lineno);
611 }
612 }
613 fclose(fp);
614 }
615 closedir(d);
616}
617
618static void test_secret_negative(void)
619{
620 char word[8], cap_len[32];
621 snprintf(word, sizeof word, "%s%s", "to", "ken");
622 snprintf(cap_len, sizeof cap_len, "%s%s%s", "AIENOS_CAP_", "TO", "KEN_LEN");
623 int files = 0, hits = 0;
624 scan_tree(".", word, cap_len, &files, &hits);
625 CHECK(files >= 10);
626 CHECK(hits == 0);
627 printf("secret-negative: %d files under native/argus scanned (recursive, out/ excluded), %d hits; "
628 "no 32-byte event field except machine_id/evidence_digest (static)\n", files, hits);
629}
630
631/* ---- v1.2 containment kinds 90-93 (ARGUS1_SPEC section 3) ---- */
632static ArgusEvent contain_event(uint16_t kind, unsigned type, unsigned status, unsigned outcome)
633{
634 ArgusEvent e = valid_event();
635 e.kind = kind;
636 e.class_ = ARGUS_CLASS_CRITICAL;
637 e.effect_class = ARGUS_EFFECT_NONE;
638 e.outcome = (uint8_t)outcome;
639 e.flags = (uint16_t)(kind == ARGUS_EV_CONTAINMENT_PROPOSED ? ARGUS_FLAG_CONSUMER : (5u << ARGUS_FLAG_STREAM_SHIFT));
640 e.object_id = ARGUS_CONTAIN_PACK(type, status, ARGUS_F_STALE_GENERATION);
641 e.world_generation = 7; /* request_id */
642 e.resource = 1;
643 return e;
644}
645
646static void test_containment_kinds(void)
647{
648 size_t n = 0, ok = 0;
649 static const uint16_t ck[] = { ARGUS_EV_CONTAINMENT_PROPOSED, ARGUS_EV_CONTAINMENT_DECIDED,
650 ARGUS_EV_CONTAINMENT_EXECUTED, ARGUS_EV_AUTHORITY_ESCALATED };
651 for (size_t i = 0; i < 4; i++) {
652 CHECK(argus_event_min_class(ck[i]) == ARGUS_CLASS_CRITICAL);
653 for (unsigned c = 2; c <= 4; c++) { /* weaker than CRITICAL: MALFORMED */
654 ArgusEvent e = contain_event(ck[i], 1, i == 0 ? 0 : i == 1 ? 1 : 4, 1); e.class_ = (uint8_t)c;
655 expect_struct(e, ARGUS_ERR_MALFORMED); n++;
656 }
657 }
658 /* Every type byte x status byte x outcome 0..4 x SYNTHETIC {0,1} x request_id {0,1}, kinds 90-93,
659 * checked against the independent table oracle and through encode/decode. */
660 for (size_t i = 0; i < 4; i++)
661 for (unsigned type = 0; type < 256; type++)
662 for (unsigned st = 0; st < 256; st++)
663 for (unsigned out = 0; out <= 4; out++)
664 for (unsigned syn = 0; syn < 2; syn++)
665 for (unsigned rid = 0; rid < 2; rid++) {
666 ArgusEvent e = contain_event(ck[i], type, st, out);
667 e.flags = (uint16_t)(e.flags | syn);
668 e.world_generation = rid;
669 int want = oracle(&e);
670 CHECK(argus_event_validate(&e) == want);
671 if ((type < 12 && st < 10) || (type % 61 == 0 && st % 61 == 0)) expect_struct(e, want);
672 ok += want == ARGUS_OK; n++;
673 }
674 /* Named cases, straight from the spec bullets. */
675 ArgusEvent e;
676 e = contain_event(90, 1, 0, 1); expect_struct(e, ARGUS_OK);
677 e.flags = 0; expect_struct(e, ARGUS_ERR_MALFORMED); /* 90 without CONSUMER */
678 e = contain_event(90, 1, 0, 2); expect_struct(e, ARGUS_ERR_MALFORMED); /* 90 outcome not OK */
679 e = contain_event(90, 1, 1, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* 90 status not PROPOSED */
680 e = contain_event(90, 0, 0, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* type 0 (NONE) */
681 e = contain_event(90, 11, 0, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* type > 10 */
682 e = contain_event(90, 10, 0, 1); expect_struct(e, ARGUS_OK);
683 e = contain_event(90, 1, 0, 1); e.world_generation = 0; expect_struct(e, ARGUS_ERR_MALFORMED); /* request_id 0 */
684 e = contain_event(91, 1, 1, 1); expect_struct(e, ARGUS_OK); /* GRANT OK */
685 e = contain_event(91, 1, 2, 2); expect_struct(e, ARGUS_OK); /* DENY DENIED */
686 e = contain_event(91, 1, 3, 3); expect_struct(e, ARGUS_OK); /* ESCALATE ERROR */
687 e = contain_event(91, 1, 1, 2); expect_struct(e, ARGUS_ERR_MALFORMED); /* GRANT with DENIED */
688 e = contain_event(91, 1, 3, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* ESCALATE with OK */
689 e = contain_event(91, 1, 4, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* 92 status on 91 */
690 e = contain_event(91, 1, 1, 1); e.flags |= ARGUS_FLAG_CONSUMER; expect_struct(e, ARGUS_ERR_MALFORMED);
691 e = contain_event(92, 1, 4, 1); expect_struct(e, ARGUS_OK); /* DONE RevokeCapability */
692 e = contain_event(92, 1, 5, 1); expect_struct(e, ARGUS_OK); /* PARTIAL */
693 e = contain_event(92, 1, 6, 2); expect_struct(e, ARGUS_OK); /* FAILED DENIED */
694 e = contain_event(92, 1, 6, 3); expect_struct(e, ARGUS_OK); /* FAILED ERROR */
695 e = contain_event(92, 1, 6, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* FAILED OK */
696 e = contain_event(92, 2, 7, 3); expect_struct(e, ARGUS_OK); /* UNAVAILABLE ERROR */
697 e = contain_event(92, 2, 7, 2); expect_struct(e, ARGUS_ERR_MALFORMED);
698 e = contain_event(92, 1, 8, 3); expect_struct(e, ARGUS_OK); /* FAILED_SCOPE ERROR */
699 e = contain_event(92, 1, 8, 1); expect_struct(e, ARGUS_ERR_MALFORMED);
700 e = contain_event(92, 1, 9, 3); expect_struct(e, ARGUS_ERR_MALFORMED); /* status > 8 */
701 e = contain_event(92, 1, 1, 1); expect_struct(e, ARGUS_ERR_MALFORMED); /* 91 status on 92 */
702 for (unsigned t = 2; t <= 10; t++) { /* I2 */
703 e = contain_event(92, t, 4, 1); expect_struct(e, ARGUS_ERR_MALFORMED);
704 e.flags |= ARGUS_FLAG_SYNTHETIC; expect_struct(e, ARGUS_OK);
705 e = contain_event(92, t, 5, 1); expect_struct(e, ARGUS_OK); /* PARTIAL: spec rule is DONE only */
706 }
707 e = contain_event(92, 1, 4, 1); e.flags |= ARGUS_FLAG_CONSUMER; expect_struct(e, ARGUS_ERR_MALFORMED);
708 e = contain_event(93, 0, 0, 1); e.object_id = 0xDEADBEEFu; e.world_generation = 0; expect_struct(e, ARGUS_OK);
709 e.flags |= ARGUS_FLAG_CONSUMER; expect_struct(e, ARGUS_ERR_MALFORMED);
710 e = contain_event(93, 0, 0, 2); e.cap_id = ARGUS_CAP_NONE; expect_struct(e, ARGUS_OK);
711 e = contain_event(92, 1, 4, 1); e.cap_id = 256; expect_struct(e, ARGUS_ERR_MALFORMED); /* general rules still apply */
712 e = contain_event(92, 1, 4, 1); e.sequence = 0; expect_struct(e, ARGUS_ERR_MALFORMED);
713 /* Every flags value on 90 (OK iff CONSUMER) and 91 (OK iff not CONSUMER). */
714 for (unsigned v = 0; v < 65536; v++) {
715 ArgusEvent p = contain_event(90, 1, 0, 1); p.flags = (uint16_t)v;
716 CHECK(argus_event_validate(&p) == ((v & ARGUS_FLAG_CONSUMER) ? ARGUS_OK : ARGUS_ERR_MALFORMED));
717 ArgusEvent d = contain_event(91, 1, 1, 1); d.flags = (uint16_t)v;
718 CHECK(argus_event_validate(&d) == ((v & ARGUS_FLAG_CONSUMER) ? ARGUS_ERR_MALFORMED : ARGUS_OK));
719 n += 2;
720 }
721 printf("containment kinds 90-93: %zu cases (every type x status byte x outcome x SYNTHETIC x request_id "
722 "vs table oracle, %zu valid; CRITICAL floor; CONSUMER iff 80/90; I2 DONE rule)\n", n, ok);
723}
724
725/* ---- v1.2 ContainmentRequest canonical encoding, 152 bytes (ARGUS1_SPEC section 2) ---- */
726static ArgusContainmentRequest fixed_request(void)
727{
728 ArgusContainmentRequest r;
729 memset(&r, 0, sizeof r);
730 r.incident_id = 0x0102030405060708ull;
731 r.containment = ARGUS_CONTAIN_REVOKE_CAPABILITY;
732 r.severity = ARGUS_SEV_HIGH;
733 r.finding_code = ARGUS_F_STALE_GENERATION;
734 r.principal = 0x21222324u;
735 r.target.cap_id = 0x31u;
736 r.target.generation = 0x4142434445464748ull;
737 for (unsigned i = 0; i < 32; i++) {
738 r.machine_id[i] = (uint8_t)(0xA0 + i);
739 r.finding_digest[i] = (uint8_t)(0xC0 + i);
740 r.target_digest[i] = (uint8_t)(0xE0 - i);
741 }
742 r.request_id = 0x5152535455565758ull;
743 r.finding_sequence = 0x6162636465666768ull;
744 r.target_object = 0x71727374u;
745 r.target_rights = 0x81828384u;
746 r.flags = ARGUS_CREQ_SYNTHETIC | ARGUS_CREQ_SATURATION;
747 r.version = ARGUS_CONTAIN_REQUEST_VERSION;
748 r.reserved = 0;
749 return r;
750}
751
752/* Known-answer vector for lanes C/G/X: fixed_request() encodes to exactly these 152 bytes
753 * (offsets 0,8,9,10,12,16,20,28,60,92,100,108,112,116,148,150,151; little-endian, packed),
754 * and its request digest is SHA-256 of them. */
755static const char REQ_KAT_HEX[] =
756 "0807060504030201" "01" "04" "0200" "24232221" "31000000" "4847464544434241"
757 "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
758 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
759 "5857565554535251" "6867666564636261" "74737271" "84838281"
760 "e0dfdedddcdbdad9d8d7d6d5d4d3d2d1d0cfcecdcccbcac9c8c7c6c5c4c3c2c1"
761 "0300" "02" "00";
762static const char REQ_KAT_DIGEST[] = "e7b9720638cb215683fe4a5944efb630fa96a023d1afb7dd575ceebbed1204a6";
763
764static int decode_req_mut(size_t off, uint8_t v, ArgusContainmentRequest *out)
765{
766 ArgusContainmentRequest r = fixed_request();
767 uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE];
768 argus_contain_request_encode(&r, b);
769 b[off] = v;
770 return argus_contain_request_decode(b, out);
771}
772
773static void test_request(void)
774{
775 size_t n = 0;
776 ArgusContainmentRequest r = fixed_request(), d;
777 uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE], b2[ARGUS_CONTAIN_REQUEST_SIZE], dg[32], man[32];
778 char h[2 * ARGUS_CONTAIN_REQUEST_SIZE + 1];
779 CHECK(sizeof REQ_KAT_HEX - 1 == 2 * ARGUS_CONTAIN_REQUEST_SIZE);
780 CHECK(argus_contain_request_encode(&r, b) == ARGUS_OK);
781 hex(h, b, sizeof b);
782 printf(" request bytes %s\n", h);
783 CHECK(strcmp(h, REQ_KAT_HEX) == 0);
784 /* Independent spot checks of every field's first/last byte at the spec offsets. */
785 CHECK(b[0] == 0x08 && b[7] == 0x01 && b[8] == 1 && b[9] == 4 && b[10] == 2 && b[11] == 0);
786 CHECK(b[12] == 0x24 && b[15] == 0x21 && b[16] == 0x31 && b[19] == 0 && b[20] == 0x48 && b[27] == 0x41);
787 CHECK(b[28] == 0xA0 && b[59] == 0xBF && b[60] == 0xC0 && b[91] == 0xDF);
788 CHECK(b[92] == 0x58 && b[99] == 0x51 && b[100] == 0x68 && b[107] == 0x61);
789 CHECK(b[108] == 0x74 && b[111] == 0x71 && b[112] == 0x84 && b[115] == 0x81);
790 CHECK(b[116] == 0xE0 && b[147] == 0xC1 && b[148] == 3 && b[149] == 0 && b[150] == 2 && b[151] == 0);
791 argus_contain_request_digest(&r, dg);
792 sha256_hash(b, sizeof b, man);
793 CHECK(memcmp(dg, man, 32) == 0); /* digest = SHA-256 of the 152 bytes */
794 check_digest("request digest", dg, REQ_KAT_DIGEST);
795 /* Round trip both ways. */
796 memset(&d, 0x5A, sizeof d);
797 CHECK(argus_contain_request_decode(b, &d) == ARGUS_OK);
798 CHECK(argus_contain_request_encode(&d, b2) == ARGUS_OK && memcmp(b, b2, sizeof b) == 0);
799 CHECK(d.incident_id == r.incident_id && d.containment == r.containment && d.severity == r.severity &&
800 d.finding_code == r.finding_code && d.principal == r.principal && d.target.cap_id == r.target.cap_id &&
801 d.target.generation == r.target.generation && d.request_id == r.request_id &&
802 d.finding_sequence == r.finding_sequence && d.target_object == r.target_object &&
803 d.target_rights == r.target_rights && d.flags == r.flags && d.version == r.version && d.reserved == 0 &&
804 memcmp(d.machine_id, r.machine_id, 32) == 0 && memcmp(d.finding_digest, r.finding_digest, 32) == 0 &&
805 memcmp(d.target_digest, r.target_digest, 32) == 0);
806 /* Every one of the 17 fields changes the digest. */
807 for (int field = 0; field < 17; field++) {
808 ArgusContainmentRequest g = r;
809 switch (field) {
810 case 0: g.incident_id ^= 1ull << 63; break; case 1: g.containment ^= 3; break;
811 case 2: g.severity ^= 1; break; case 3: g.finding_code ^= 0x100; break;
812 case 4: g.principal ^= 1; break; case 5: g.target.cap_id ^= 1; break;
813 case 6: g.target.generation ^= 1; break; case 7: g.machine_id[31] ^= 1; break;
814 case 8: g.finding_digest[0] ^= 1; break; case 9: g.request_id ^= 1; break;
815 case 10: g.finding_sequence ^= 1; break; case 11: g.target_object ^= 1; break;
816 case 12: g.target_rights ^= 1u << 31; break; case 13: g.target_digest[16] ^= 1; break;
817 case 14: g.flags ^= 1; break; case 15: g.version ^= 1; break;
818 case 16: g.reserved ^= 1; break;
819 }
820 argus_contain_request_digest(&g, man);
821 CHECK(memcmp(man, dg, 32) != 0);
822 n++;
823 }
824 /* Padding never reaches the bytes: same fields, different padding -> same bytes. */
825 {
826 ArgusContainmentRequest p; memset(&p, 0xEE, sizeof p);
827 ArgusContainmentRequest q = fixed_request();
828 p.incident_id = q.incident_id; p.containment = q.containment; p.severity = q.severity;
829 p.finding_code = q.finding_code; p.principal = q.principal; p.target.cap_id = q.target.cap_id;
830 p.target.generation = q.target.generation; memcpy(p.machine_id, q.machine_id, 32);
831 memcpy(p.finding_digest, q.finding_digest, 32); p.request_id = q.request_id;
832 p.finding_sequence = q.finding_sequence; p.target_object = q.target_object;
833 p.target_rights = q.target_rights; memcpy(p.target_digest, q.target_digest, 32);
834 p.flags = q.flags; p.version = q.version; p.reserved = q.reserved;
835 CHECK(argus_contain_request_encode(&p, b2) == ARGUS_OK && memcmp(b, b2, sizeof b) == 0);
836 }
837 /* Field validation, each byte value, output untouched on error. */
838 ArgusContainmentRequest sent; memset(&sent, 0xA5, sizeof sent);
839#define REQ_EXPECT(off, v, want) do { ArgusContainmentRequest o_ = sent; int rc_ = decode_req_mut((off), (uint8_t)(v), &o_); \
840 CHECK(rc_ == (want)); if (rc_ != ARGUS_OK) CHECK(memcmp(&o_, &sent, sizeof o_) == 0); n++; } while (0)
841 for (unsigned v = 0; v < 256; v++) {
842 REQ_EXPECT(150, v, v == 2 ? ARGUS_OK : ARGUS_ERR_VERSION); /* version */
843 REQ_EXPECT(8, v, (v >= 1 && v <= 10) ? ARGUS_OK : ARGUS_ERR_MALFORMED); /* containment */
844 REQ_EXPECT(9, v, (v >= 1 && v <= 5) ? ARGUS_OK : ARGUS_ERR_MALFORMED); /* severity */
845 REQ_EXPECT(151, v, v == 0 ? ARGUS_OK : ARGUS_ERR_MALFORMED); /* reserved */
846 REQ_EXPECT(148, v, v <= 3 ? ARGUS_OK : ARGUS_ERR_MALFORMED); /* flags low */
847 REQ_EXPECT(149, v, v == 0 ? ARGUS_OK : ARGUS_ERR_MALFORMED); /* flags high */
848 REQ_EXPECT(10, v, ARGUS_OK); /* finding_code: any */
849 REQ_EXPECT(16, v, ARGUS_OK); /* target cap: any */
850 REQ_EXPECT(112, v, ARGUS_OK); /* target_rights: any */
851 }
852#undef REQ_EXPECT
853 {
854 ArgusContainmentRequest z = fixed_request(), o = sent;
855 z.request_id = 0; argus_contain_request_encode(&z, b2);
856 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_ERR_MALFORMED && memcmp(&o, &sent, sizeof o) == 0);
857 z = fixed_request(); z.finding_sequence = 0; argus_contain_request_encode(&z, b2);
858 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_ERR_MALFORMED);
859 z = fixed_request(); z.request_id = 1; z.finding_sequence = 1; z.flags = 0; argus_contain_request_encode(&z, b2);
860 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_OK);
861 z = fixed_request(); z.incident_id = 0; z.target.cap_id = ARGUS_CAP_NONE; argus_contain_request_encode(&z, b2);
862 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_OK); /* 0 = untracked incident */
863 memset(b2, 0xFF, sizeof b2); b2[150] = 1; /* version first */
864 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_ERR_VERSION);
865 memset(b2, 0, sizeof b2);
866 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_ERR_VERSION);
867 b2[150] = 2;
868 CHECK(argus_contain_request_decode(b2, &o) == ARGUS_ERR_MALFORMED);
869 n += 7;
870 }
871 CHECK(argus_contain_request_encode(NULL, b) == ARGUS_ERR_ARG);
872 CHECK(argus_contain_request_encode(&r, NULL) == ARGUS_ERR_ARG);
873 CHECK(argus_contain_request_decode(NULL, &d) == ARGUS_ERR_ARG);
874 CHECK(argus_contain_request_decode(b, NULL) == ARGUS_ERR_ARG);
875 printf("containment request: 152-byte known answer + digest, round trip, 17 fields perturb the digest, "
876 "%zu field values checked (version, type, severity, reserved, flags, ids), output untouched on error\n", n);
877}
878
879int main(void)
880{
881 test_known_answers();
882 test_round_trip();
883 test_malformed();
884 test_forward_compat();
885 test_containment_kinds();
886 test_request();
887 test_secret_negative();
888 if (failures) { printf("test_argus_event: FAIL (%d)\n", failures); return 1; }
889 printf("test_argus_event: PASS\n");
890 return 0;
891}