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_core.c

1051 lines · 65202 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_core.c -- lane D unit tests: every table update, sequence
19 * anomaly, table-full behaviour, telemetry loss, overflow, incidents, ops;
20 * v1.2: containment kinds 90-93 (no detectors, no shadow change) and argus_core_incident.
21 * Linked against tests/stub_lanes_d.c (lane B/E stand-ins).
22 */
23#include "test_common_d.h"
24#include <stdlib.h>
25
26static _Alignas(16) uint8_t mem[1 << 17];
27
28static ArgusCore *fresh(void)
29{
30 ArgusCore *c = NULL;
31 stub_detect_mode = 1;
32 if (argus_core_init(&c, mem, sizeof mem) != ARGUS_OK) { fprintf(stderr, "init failed\n"); exit(2); }
33 return c;
34}
35
36static int ing(ArgusCore *c, const ArgusEvent *e, ArgusFinding *f, size_t *n)
37{
38 return argus_core_ingest(c, e, f, 16, n);
39}
40
41static void t_init(void)
42{
43 ArgusCore *c = NULL;
44 CHECK(argus_core_footprint() > 0 && argus_core_footprint() <= sizeof mem);
45 CHECK(argus_core_init(NULL, mem, sizeof mem) == ARGUS_ERR_ARG);
46 CHECK(argus_core_init(&c, NULL, sizeof mem) == ARGUS_ERR_ARG);
47 CHECK(argus_core_init(&c, mem, argus_core_footprint() - 1) == ARGUS_ERR_ARG);
48 CHECK(argus_core_init(&c, mem + 1, sizeof mem - 1) == ARGUS_ERR_ARG);
49 CHECK(argus_core_init(&c, mem, argus_core_footprint()) == ARGUS_OK);
50 CHECK(argus_core_view(c) != NULL && argus_core_ops() != NULL);
51 ArgusCoreHealth h;
52 argus_core_health(c, &h);
53 CHECK(h.events_received == 0 && h.incidents_open == 0);
54 size_t n;
55 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_USED, 1);
56 CHECK(argus_core_ingest(NULL, &e, NULL, 0, &n) == ARGUS_ERR_ARG);
57 CHECK(argus_core_ingest(c, NULL, NULL, 0, &n) == ARGUS_ERR_ARG);
58 CHECK(argus_core_ingest(c, &e, NULL, 4, &n) == ARGUS_ERR_ARG);
59 CHECK(argus_core_ingest(c, &e, NULL, 0, NULL) == ARGUS_ERR_ARG);
60 stub_detect_mode = 0; /* stub raises FORGED for cap 0: no room -> OVERFLOW, n 0 */
61 CHECK(argus_core_ingest(c, &e, NULL, 0, &n) == ARGUS_ERR_OVERFLOW && n == 0);
62 stub_detect_mode = 1; e.sequence = 2;
63 CHECK(argus_core_ingest(c, &e, NULL, 0, &n) == ARGUS_OK && n == 0);
64}
65
66static void t_malformed(void)
67{
68 ArgusCore *c = fresh();
69 ArgusFinding f[16]; size_t n = 99;
70 uint8_t d0[32], d1[32];
71 argus_core_state_digest(c, d0);
72 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1);
73 e.cap_id = 3; e.version = 2;
74 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1);
75 CHECK(f[0].code == ARGUS_F_MALFORMED_EVENT && f[0].severity == ARGUS_SEV_MEDIUM && f[0].sequence == 1);
76 CHECK(f[0].detector == ARGUS_F_MALFORMED_EVENT && f[0].cap_id == 3 && f[0].confidence == ARGUS_CONF_DETERMINISTIC);
77 uint8_t ed[32]; argus_event_digest(&e, ed);
78 CHECK(memcmp(f[0].event_digest, ed, 32) == 0);
79 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 3; e.outcome = 9; /* v1.1: flag 0x80 is stream 0x20, valid */
80 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1);
81 e = ev_make(99, 1);
82 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1);
83 /* core guards (header validate rules): cap_id >= ARGUS_CAP_MAX, sequence UINT64_MAX */
84 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = ARGUS_CAP_MAX; e.principal = 1234;
85 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
86 e = ev_make(ARGUS_EV_CAPABILITY_USED, 1); e.cap_id = 300;
87 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
88 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, UINT64_MAX);
89 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].sequence == UINT64_MAX);
90 /* cap 0 of output: still MALFORMED, finding counted */
91 e = ev_make(99, 1);
92 CHECK(argus_core_ingest(c, &e, NULL, 0, &n) == ARGUS_ERR_MALFORMED && n == 0);
93 ArgusCoreHealth h; argus_core_health(c, &h);
94 CHECK(h.events_received == 7 && h.events_rejected == 7 && h.findings_emitted == 7);
95 CHECK(h.incidents_open == 0 && h.tables_full == 0 && h.events_not_applied == 0);
96 uint8_t z[32] = {0};
97 CHECK(memcmp(h.chain, z, 32) == 0);
98 argus_core_state_digest(c, d1);
99 CHECK(memcmp(d0, d1, 32) == 0);
100 /* rejected events do not consume the producer sequence */
101 e = ev_make(ARGUS_EV_CAPABILITY_USED, 1);
102 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
103}
104
105static void t_caps(void)
106{
107 ArgusCore *c = fresh();
108 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
109 ArgusFinding f[16]; size_t n; ArgusCapShadow s;
110 CHECK(o->cap(v, 7, &s) == ARGUS_ERR_STATE);
111 CHECK(o->cap(v, 999, &s) == ARGUS_ERR_STATE);
112 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1);
113 e.cap_id = 7; e.cap_generation = 4; e.principal = 42; e.object_id = 0x13; e.resource = 0xABCDEF0123ull;
114 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
115 CHECK(o->cap(v, 7, &s) == ARGUS_OK);
116 CHECK(s.cap_id == 7 && s.generation == 4 && s.state == ARGUS_SHADOW_LIVE && s.subject == 42);
117 CHECK(s.rights == 0x13 && s.resource == 0xABCDEF0123ull && s.granted_sequence == 1 && s.revoked_sequence == 0);
118 /* stale grant: AUTHORITY_REPLAY, not applied */
119 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 2); e.cap_id = 7; e.cap_generation = 3; e.principal = 9;
120 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY);
121 CHECK(f[0].severity == ARGUS_SEV_HIGH && f[0].prior_sequence == 1 && f[0].sequence == 2 && f[0].principal == 9);
122 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.generation == 4 && s.subject == 42);
123 /* equal-generation grant on a LIVE slot: replay too */
124 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 3); e.cap_id = 7; e.cap_generation = 4; e.principal = 9;
125 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY);
126 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.subject == 42 && s.granted_sequence == 1);
127 /* denied grant ignored */
128 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 4); e.cap_id = 8; e.outcome = ARGUS_OUTCOME_DENIED;
129 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->cap(v, 8, &s) == ARGUS_ERR_STATE);
130 /* v1.1: ARGUS_CAP_NONE = none: no update, no error; cap_id 0 is the office slot and applies */
131 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 5); e.cap_id = ARGUS_CAP_NONE; e.cap_generation = 1;
132 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 0, &s) == ARGUS_ERR_STATE);
133 CHECK(o->cap(v, ARGUS_CAP_NONE, &s) == ARGUS_ERR_STATE);
134 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 6); e.cap_id = 0; e.cap_generation = 1; e.principal = 3; e.object_id = 0x7;
135 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 0, &s) == ARGUS_OK);
136 CHECK(s.cap_id == 0 && s.state == ARGUS_SHADOW_LIVE && s.subject == 3 && s.generation == 1 && s.rights == 0x7);
137 /* revoke above the live generation: replay, not applied */
138 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 21); e.cap_id = 7; e.cap_generation = 5;
139 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 1);
140 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.generation == 4);
141 /* revoke at the live generation applies */
142 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 25); e.cap_id = 7; e.cap_generation = 4;
143 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
144 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED && s.revoked_sequence == 25 && s.subject == 42);
145 /* double revoke: replay (prior = the revocation) */
146 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 26); e.cap_id = 7; e.cap_generation = 4;
147 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 25);
148 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.revoked_sequence == 25);
149 /* grant replay at the revoked generation does not revive */
150 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 27); e.cap_id = 7; e.cap_generation = 4; e.principal = 42;
151 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 25);
152 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED);
153 /* regrant at higher generation revives */
154 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 28); e.cap_id = 7; e.cap_generation = 5; e.principal = 43;
155 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
156 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.generation == 5 && s.revoked_sequence == 0);
157 /* revoking an older generation: replay, not applied */
158 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 30); e.cap_id = 7; e.cap_generation = 4;
159 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY);
160 CHECK(o->cap(v, 7, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.generation == 5);
161 /* revoke of an unseen slot: replay, creates no entry (even at UINT64_MAX) */
162 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 31); e.cap_id = 200; e.cap_generation = UINT64_MAX;
163 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 0);
164 CHECK(o->cap(v, 200, &s) == ARGUS_ERR_STATE);
165 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 32); e.cap_id = 200; e.cap_generation = 1;
166 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 200, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE);
167 /* denied/error revoke of an unseen slot is not a replay (never applied anyway) */
168 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 33); e.cap_id = 201; e.outcome = ARGUS_OUTCOME_DENIED;
169 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
170 /* highest slot 255 works; 256 is malformed, never table-full */
171 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 34); e.cap_id = 255; e.cap_generation = 1;
172 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->cap(v, 255, &s) == ARGUS_OK);
173 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 35); e.cap_id = 256; e.cap_generation = 1; e.principal = 5;
174 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
175 ArgusCoreHealth h; argus_core_health(c, &h);
176 CHECK(h.tables_full == 0 && h.events_not_applied == 7 && h.events_rejected == 1);
177}
178
179static void t_leases(void)
180{
181 ArgusCore *c = fresh();
182 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
183 ArgusFinding f[16]; size_t n; ArgusLeaseShadow s;
184 ArgusEvent e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 1);
185 e.object_id = 77; e.principal = 3; e.resource = 0x55;
186 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
187 CHECK(o->lease(v, 77, &s) == ARGUS_OK && s.lease_id == 77 && s.subject == 3 && s.scope == 0x55 && s.state == ARGUS_SHADOW_LIVE && s.sequence == 1);
188 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_USED, 2); e.object_id = 77; e.principal = 4;
189 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->lease(v, 77, &s) == ARGUS_OK && s.subject == 3);
190 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_REVOKED, 3); e.object_id = 77;
191 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->lease(v, 77, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED && s.sequence == 3);
192 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 4); e.object_id = 77; e.principal = 9;
193 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->lease(v, 77, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED && s.subject == 3);
194 /* revoke of an unknown lease: no entry */
195 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_REVOKED, 5); e.object_id = 78;
196 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->lease(v, 78, &s) == ARGUS_ERR_STATE);
197 ArgusCoreHealth h0; argus_core_health(c, &h0);
198 CHECK(h0.events_not_applied == 2);
199 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 6); e.object_id = 0;
200 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->lease(v, 0, &s) == ARGUS_ERR_STATE);
201 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 7); e.object_id = 79; e.outcome = ARGUS_OUTCOME_ERROR;
202 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->lease(v, 79, &s) == ARGUS_ERR_STATE);
203 /* re-CREATE on a LIVE lease: not applied (no hijack of subject/scope) */
204 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 60); e.object_id = 80; e.principal = 3; e.resource = 1;
205 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
206 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, 61); e.object_id = 80; e.principal = 9; e.resource = UINT64_MAX;
207 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
208 CHECK(o->lease(v, 80, &s) == ARGUS_OK && s.subject == 3 && s.scope == 1 && s.sequence == 60);
209 argus_core_health(c, &h0);
210 CHECK(h0.events_not_applied == 3);
211 /* fill to the table size then overflow */
212 uint64_t seq = 62;
213 for (uint32_t id = 100; id < 100 + ARGUS_CORE_LEASES - 2; id++) {
214 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, seq++); e.object_id = id;
215 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
216 }
217 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, seq++); e.object_id = 5000;
218 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
219 CHECK(o->lease(v, 5000, &s) == ARGUS_ERR_STATE);
220 /* ruling (a): later overflows of the same table: ERR_FULL, no finding, counted */
221 { ArgusCoreHealth ha, hb; argus_core_health(c, &ha);
222 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_CREATED, seq++); e.object_id = 5001;
223 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 0 && o->lease(v, 5001, &s) == ARGUS_ERR_STATE);
224 argus_core_health(c, &hb);
225 CHECK(hb.tables_full == ha.tables_full + 1 && hb.events_not_applied == ha.events_not_applied + 1); }
226 /* existing lease still updatable when full */
227 e = ev_make(ARGUS_EV_CREDENTIAL_LEASE_REVOKED, seq++); e.object_id = 100;
228 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
229 ArgusCoreHealth h; argus_core_health(c, &h);
230 uint8_t z[32] = {0};
231 CHECK(memcmp(h.chain, z, 32) != 0);
232 CHECK(h.tables_full == 2);
233}
234
235static void t_artifacts(void)
236{
237 ArgusCore *c = fresh();
238 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
239 ArgusFinding f[16]; size_t n; ArgusArtifactShadow s;
240 ArgusEvent e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, 1); dg(e.evidence_digest, 1);
241 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
242 CHECK(o->artifact(v, e.evidence_digest, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.sequence == 1);
243 e = ev_make(ARGUS_EV_ARTIFACT_REJECTED, 2); dg(e.evidence_digest, 2); e.outcome = ARGUS_OUTCOME_DENIED;
244 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REJECTED);
245 e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, 3); dg(e.evidence_digest, 2);
246 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REJECTED);
247 e = ev_make(ARGUS_EV_ARTIFACT_REJECTED, 4); dg(e.evidence_digest, 1);
248 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REJECTED && s.sequence == 4);
249 e = ev_make(ARGUS_EV_ARTIFACT_REJECTED, 5); dg(e.evidence_digest, 3); e.outcome = ARGUS_OUTCOME_ERROR;
250 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_ERR_STATE);
251 e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, 1); mid(e.machine_id, 9); dg(e.evidence_digest, 4); e.code = -3; /* OK but nonzero code: not admitted */
252 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_ERR_STATE);
253 e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, 6); /* zero digest = none */
254 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->artifact(v, e.evidence_digest, &s) == ARGUS_ERR_STATE);
255 uint64_t seq = 7;
256 for (uint32_t i = 10; i < 10 + ARGUS_CORE_ARTIFACTS - 2; i++) {
257 e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, seq++); dg(e.evidence_digest, i);
258 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
259 }
260 e = ev_make(ARGUS_EV_ARTIFACT_ADMITTED, seq++); dg(e.evidence_digest, 9999);
261 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS);
262 e = ev_make(ARGUS_EV_ARTIFACT_REJECTED, seq++); dg(e.evidence_digest, 9998); /* same table, second overflow: quiet */
263 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 0);
264 { ArgusCoreHealth h; argus_core_health(c, &h); CHECK(h.tables_full == 2 && h.events_not_applied == 2); }
265}
266
267static void t_machines(void)
268{
269 ArgusCore *c = fresh();
270 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
271 ArgusFinding f[16]; size_t n; ArgusMachineShadow s; ArgusCoreHealth h;
272 ArgusEvent e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 1);
273 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
274 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_OBSERVED && s.joined_sequence == 1 && s.changed_sequence == 1);
275 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 2); e.object_id = ARGUS_TRUST_QUARANTINED; e.resource = ARGUS_TRUST_TRUSTED;
276 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED && s.changed_sequence == 2);
277 e = ev_make(ARGUS_EV_MACHINE_JOINED, 3); /* rejoin: no laundering */
278 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED && s.joined_sequence == 1);
279 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 4); e.object_id = 77; /* invalid trust ignored */
280 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED);
281 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 40); e.object_id = 0; e.resource = ARGUS_TRUST_TRUSTED; /* 0 ignored */
282 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED);
283 argus_core_health(c, &h);
284 CHECK(h.events_not_applied == 0);
285 /* self-upgrade QUARANTINED -> TRUSTED: ignored, counted */
286 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 41); e.object_id = ARGUS_TRUST_TRUSTED;
287 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED && s.changed_sequence == 2);
288 /* rank is not enum order: QUARANTINED(4) -> REATTESTATION_REQUIRED(6) is UP the rank: ignored */
289 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 42); e.object_id = ARGUS_TRUST_REATTESTATION_REQUIRED;
290 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED);
291 /* QUARANTINED -> UNTRUSTED is down: applied */
292 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 43); e.object_id = ARGUS_TRUST_UNTRUSTED;
293 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_UNTRUSTED && s.changed_sequence == 43);
294 argus_core_health(c, &h);
295 CHECK(h.events_not_applied == 2);
296 /* trust change for an unknown machine: no entry, counted */
297 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 1); mid(e.machine_id, 2); e.object_id = ARGUS_TRUST_TRUSTED;
298 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->machine(v, e.machine_id, &s) == ARGUS_ERR_STATE);
299 argus_core_health(c, &h);
300 CHECK(h.events_not_applied == 3);
301 /* ... so the real JOIN afterwards works normally (not bricked, not pre-seeded) */
302 e = ev_make(ARGUS_EV_MACHINE_JOINED, 2); mid(e.machine_id, 2);
303 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_OBSERVED && s.joined_sequence == 2);
304 /* OBSERVED -> REATTESTATION_REQUIRED -> RESTRICTED: both down */
305 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 3); mid(e.machine_id, 2); e.object_id = ARGUS_TRUST_REATTESTATION_REQUIRED;
306 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_REATTESTATION_REQUIRED);
307 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 4); mid(e.machine_id, 2); e.object_id = ARGUS_TRUST_RESTRICTED;
308 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_RESTRICTED);
309 /* remove machine 1: tombstone keeps trust, joined_sequence 0 */
310 e = ev_make(ARGUS_EV_MACHINE_REMOVED, 50); mid(e.machine_id, 1);
311 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
312 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_UNTRUSTED && s.joined_sequence == 0 && s.changed_sequence == 50);
313 /* downward trust change on a tombstone still applies; upward does not */
314 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 51); mid(e.machine_id, 1); e.object_id = ARGUS_TRUST_OBSERVED;
315 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_UNTRUSTED);
316 /* rejoin of the tombstone: quarantine/untrusted survives, joined again */
317 e = ev_make(ARGUS_EV_MACHINE_JOINED, 52); mid(e.machine_id, 1); e.object_id = ARGUS_TRUST_TRUSTED;
318 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_UNTRUSTED && s.joined_sequence == 52);
319 /* remove of an unknown machine: nothing */
320 e = ev_make(ARGUS_EV_MACHINE_REMOVED, 53); mid(e.machine_id, 77);
321 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_ERR_STATE);
322 /* machine 2 unaffected */
323 mid(e.machine_id, 2);
324 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_RESTRICTED);
325 /* JOINED trust = weaker of OBSERVED and the claim */
326 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 3); e.object_id = ARGUS_TRUST_RESTRICTED;
327 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_RESTRICTED && s.joined_sequence == 1);
328 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 4); e.object_id = 9;
329 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_OBSERVED);
330 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 5); e.object_id = ARGUS_TRUST_TRUSTED;
331 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_OBSERVED);
332 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 6); e.object_id = ARGUS_TRUST_REATTESTATION_REQUIRED;
333 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_REATTESTATION_REQUIRED);
334 /* a JOINED of a known live machine may lower trust, never raise it */
335 e = ev_make(ARGUS_EV_MACHINE_JOINED, 2); mid(e.machine_id, 4); e.object_id = ARGUS_TRUST_QUARANTINED;
336 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_QUARANTINED && s.joined_sequence == 1 && s.changed_sequence == 2);
337 /* table stays sorted by machine_id; fill to capacity then one more is FULL */
338 uint32_t have = 6; /* machines 1..6 */
339 for (uint32_t m = 1000 + ARGUS_CORE_MACHINES; m > 1000 + have; m--) { /* descending ids: insert at front */
340 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, m); /* own producer stream */
341 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
342 }
343 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 500);
344 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS);
345 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_ERR_STATE);
346 e = ev_make(ARGUS_EV_MACHINE_JOINED, 2); mid(e.machine_id, 500); /* second overflow: quiet, counted */
347 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 0 && o->machine(v, e.machine_id, &s) == ARGUS_ERR_STATE);
348 for (uint32_t m = 1; m <= 6; m++) {
349 mid(e.machine_id, m);
350 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_OK);
351 }
352 mid(e.machine_id, 1000 + ARGUS_CORE_MACHINES);
353 CHECK(o->machine(v, e.machine_id, &s) == ARGUS_OK && s.trust == ARGUS_TRUST_OBSERVED);
354 argus_core_health(c, &h);
355 CHECK(h.tables_full == 2);
356}
357
358static void t_providers_world_policy(void)
359{
360 ArgusCore *c = fresh();
361 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
362 ArgusFinding f[16]; size_t n; ArgusProviderShadow p; ArgusWorldShadow w; uint8_t d[32], z[32] = {0};
363 ArgusEvent e = ev_make(ARGUS_EV_PROVIDER_DISCOVERED, 1); dg(e.evidence_digest, 7);
364 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->provider(v, e.evidence_digest, &p) == ARGUS_OK && p.state == ARGUS_SHADOW_LIVE && p.sequence == 1);
365 e = ev_make(ARGUS_EV_PROVIDER_QUARANTINED, 2); dg(e.evidence_digest, 7);
366 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->provider(v, e.evidence_digest, &p) == ARGUS_OK && p.state == ARGUS_SHADOW_REVOKED && p.sequence == 2);
367 e = ev_make(ARGUS_EV_PROVIDER_DISCOVERED, 3); dg(e.evidence_digest, 7);
368 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->provider(v, e.evidence_digest, &p) == ARGUS_OK && p.state == ARGUS_SHADOW_REVOKED);
369 e = ev_make(ARGUS_EV_PROVIDER_QUARANTINED, 4); dg(e.evidence_digest, 8);
370 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->provider(v, e.evidence_digest, &p) == ARGUS_OK && p.state == ARGUS_SHADOW_REVOKED);
371 uint64_t seq = 5;
372 for (uint32_t i = 20; i < 20 + ARGUS_CORE_PROVIDERS - 2; i++) {
373 e = ev_make(ARGUS_EV_PROVIDER_DISCOVERED, seq++); dg(e.evidence_digest, i);
374 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
375 }
376 e = ev_make(ARGUS_EV_PROVIDER_DISCOVERED, seq++); dg(e.evidence_digest, 999);
377 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1);
378 CHECK(f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
379 e = ev_make(ARGUS_EV_PROVIDER_QUARANTINED, seq++); dg(e.evidence_digest, 998); /* second overflow: quiet */
380 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 0);
381
382 CHECK(o->world(v, 0, &w) == ARGUS_ERR_STATE);
383 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 0x100000003ull; dg(e.evidence_digest, 31);
384 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.generation == 0x100000003ull && memcmp(w.digest, e.evidence_digest, 32) == 0);
385 uint64_t wseq = e.sequence;
386 ArgusCoreHealth h0, h1; argus_core_health(c, &h0);
387 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 2; dg(e.evidence_digest, 32); /* rollback */
388 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.generation == 0x100000003ull && w.sequence == wseq);
389 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = UINT64_MAX; dg(e.evidence_digest, 33); /* skip */
390 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.generation == 0x100000003ull);
391 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 0x100000003ull; dg(e.evidence_digest, 34); /* conflict */
392 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.digest[0] == 31 && w.sequence == wseq);
393 argus_core_health(c, &h1);
394 CHECK(h1.events_not_applied == h0.events_not_applied + 3);
395 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 0x100000003ull; dg(e.evidence_digest, 31); /* idempotent */
396 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.sequence == wseq);
397 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 0; dg(e.evidence_digest, 35); /* unknown */
398 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.sequence == wseq);
399 argus_core_health(c, &h1);
400 CHECK(h1.events_not_applied == h0.events_not_applied + 3);
401 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.world_generation = 0x100000004ull; dg(e.evidence_digest, 36); /* +1 */
402 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 0, &w) == ARGUS_OK && w.generation == 0x100000004ull && w.sequence == e.sequence);
403
404 CHECK(o->policy_digest(v, d) == ARGUS_OK && memcmp(d, z, 32) == 0);
405 e = ev_make(ARGUS_EV_POLICY_CHANGED, seq++); dg(e.evidence_digest, 41);
406 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->policy_digest(v, d) == ARGUS_OK && memcmp(d, e.evidence_digest, 32) == 0);
407 e = ev_make(ARGUS_EV_RUNTIME_BUILD_CHANGED, seq++); dg(e.evidence_digest, 42);
408 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->runtime_digest(v, d) == ARGUS_OK && memcmp(d, e.evidence_digest, 32) == 0);
409 e = ev_make(ARGUS_EV_RUNTIME_BUILD_CHANGED, seq++); dg(e.evidence_digest, 43); e.outcome = ARGUS_OUTCOME_DENIED;
410 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->runtime_digest(v, d) == ARGUS_OK && d[0] == 42);
411}
412
413static void t_sequence(void)
414{
415 ArgusCore *c = fresh();
416 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
417 ArgusFinding f[16]; size_t n; ArgusCapShadow s;
418 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_USED, 5);
419 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
420 e = ev_make(ARGUS_EV_CAPABILITY_USED, 5); e.principal = 11; /* repeat */
421 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1);
422 CHECK(f[0].code == ARGUS_F_SEQUENCE_ANOMALY && f[0].severity == ARGUS_SEV_HIGH && f[0].prior_sequence == 5 && f[0].sequence == 5 && f[0].principal == 11);
423 uint8_t ed[32]; argus_event_digest(&e, ed);
424 CHECK(memcmp(f[0].event_digest, ed, 32) == 0);
425 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 3); e.cap_id = 9; e.cap_generation = 1; /* decrease: detected, NOT applied */
426 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY && f[0].prior_sequence == 5);
427 CHECK(o->cap(v, 9, &s) == ARGUS_ERR_STATE);
428 e = ev_make(ARGUS_EV_CAPABILITY_USED, 4); /* high-water mark stays 5 */
429 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].prior_sequence == 5);
430 e = ev_make(ARGUS_EV_CAPABILITY_USED, 6);
431 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
432 /* consumer stream on the same machine is separate */
433 e = ev_make(ARGUS_EV_TELEMETRY_DROPPED, 1); e.flags = ARGUS_FLAG_CONSUMER; e.object_id = ARGUS_CLASS_AUDIT; e.resource = 3;
434 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
435 /* another machine is separate */
436 e = ev_make(ARGUS_EV_CAPABILITY_USED, 1); mid(e.machine_id, 2);
437 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
438 ArgusCoreHealth h; argus_core_health(c, &h);
439 CHECK(h.findings_emitted == 3 && h.incidents_open == 2); /* (11, SEQ) and (0, SEQ) */
440 CHECK(h.events_not_applied == 3 && h.producers_untracked == 0);
441 /* lane E still runs on an anomalous event (stub mode 0: forged use) */
442 stub_detect_mode = 0;
443 e = ev_make(ARGUS_EV_CAPABILITY_USED, 6); e.cap_id = 77;
444 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 2 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY && f[1].code == ARGUS_F_FORGED_CAPABILITY);
445 stub_detect_mode = 1;
446 /* sequence UINT64_MAX is malformed and does not move the high-water mark */
447 e = ev_make(ARGUS_EV_CAPABILITY_USED, UINT64_MAX);
448 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED);
449 e = ev_make(ARGUS_EV_CAPABILITY_USED, 7);
450 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
451}
452
453static void t_producers_full(void)
454{
455 ArgusCore *c = fresh();
456 ArgusFinding f[16]; size_t n;
457 for (uint32_t m = 0; m < ARGUS_CORE_PRODUCERS; m++) {
458 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 1); mid(e.machine_id, 1000 + m);
459 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
460 }
461 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 1); mid(e.machine_id, 5000);
462 /* first untracked event: one CRITICAL loss + FULL; later ones only counted */
463 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
464 e.sequence = 2;
465 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
466 mid(e.machine_id, 5001);
467 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
468 e.kind = ARGUS_EV_CAPABILITY_GRANTED; e.cap_id = 300; e.sequence = 3; /* out-of-range cap: malformed, not FULL */
469 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
470 /* untracked streams are still applied */
471 e.cap_id = 30; e.cap_generation = 1; e.sequence = 4;
472 CHECK(ing(c, &e, f, &n) == ARGUS_OK && argus_core_ops()->cap(argus_core_view(c), 30, &(ArgusCapShadow){0}) == ARGUS_OK);
473 ArgusCoreHealth h; argus_core_health(c, &h);
474 CHECK(h.producers_untracked == 4 && h.tables_full == 1);
475 /* tracked streams keep working */
476 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 1); mid(e.machine_id, 1000);
477 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
478}
479
480static void t_telemetry(void)
481{
482 ArgusCore *c = fresh();
483 ArgusFinding f[16]; size_t n;
484 ArgusEvent e = ev_make(ARGUS_EV_TELEMETRY_DROPPED, 1); e.flags = ARGUS_FLAG_CONSUMER; e.class_ = ARGUS_CLASS_CRITICAL;
485 e.object_id = ARGUS_CLASS_CRITICAL; e.resource = 4;
486 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
487 e.sequence = 2; e.object_id = ARGUS_CLASS_SECURITY;
488 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].severity == ARGUS_SEV_HIGH);
489 e.sequence = 3; e.object_id = ARGUS_CLASS_AUDIT;
490 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
491 e.sequence = 4; e.object_id = ARGUS_CLASS_INFORMATIONAL;
492 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
493 ArgusCoreHealth h; argus_core_health(c, &h);
494 CHECK(h.findings_emitted == 2 && h.incidents_open == 1);
495}
496
497static void t_prestate_and_detectors(void)
498{
499 ArgusCore *c = fresh();
500 ArgusFinding f[16]; size_t n;
501 stub_detect_mode = 3;
502 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 12; e.cap_generation = 1;
503 CHECK(ing(c, &e, f, &n) == ARGUS_OK && stub_probe_rc == ARGUS_ERR_STATE); /* detector saw pre-grant state */
504 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 2); e.cap_id = 12; e.cap_generation = 1;
505 CHECK(ing(c, &e, f, &n) == ARGUS_OK && stub_probe_rc == ARGUS_OK && stub_probe_cap.state == ARGUS_SHADOW_LIVE);
506 stub_detect_mode = 0;
507 e = ev_make(ARGUS_EV_CAPABILITY_USED, 3); e.cap_id = 12; e.cap_generation = 1; e.principal = 50;
508 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_REVOKED_CAPABILITY_USED && f[0].prior_sequence == 2);
509 uint8_t ed[32]; argus_event_digest(&e, ed);
510 CHECK(memcmp(f[0].event_digest, ed, 32) == 0); /* core stamps lane E findings */
511 e.sequence = 4; e.cap_id = 13;
512 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_FORGED_CAPABILITY);
513 ArgusCoreHealth h; argus_core_health(c, &h);
514 CHECK(h.findings_emitted == 2 && h.incidents_open == 2);
515 e.sequence = 5; e.cap_id = 12; /* same (principal, code): no new incident */
516 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1);
517 argus_core_health(c, &h);
518 CHECK(h.incidents_open == 2);
519}
520
521static void t_overflow(void)
522{
523 ArgusCore *c = fresh();
524 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
525 ArgusFinding f[16]; size_t n; ArgusCapShadow s;
526 stub_detect_mode = 2;
527 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 20; e.cap_generation = 1;
528 CHECK(argus_core_ingest(c, &e, f, 4, &n) == ARGUS_ERR_OVERFLOW && n == 4);
529 CHECK(o->cap(v, 20, &s) == ARGUS_OK); /* state applied despite the small buffer */
530 ArgusCoreHealth h; argus_core_health(c, &h);
531 CHECK(h.findings_emitted == ARGUS_CORE_MAX_FINDINGS - 2);
532 /* replay verdict + table full + canned + telemetry drop: core findings survive in scratch */
533 ArgusFinding big[ARGUS_CORE_MAX_FINDINGS];
534 stub_detect_mode = 1;
535 for (uint32_t i = 0; i < ARGUS_CORE_PROVIDERS; i++) {
536 e = ev_make(ARGUS_EV_PROVIDER_DISCOVERED, 10 + i); dg(e.evidence_digest, 100 + i);
537 CHECK(argus_core_ingest(c, &e, big, ARGUS_CORE_MAX_FINDINGS, &n) == ARGUS_OK);
538 }
539 stub_detect_mode = 2;
540 e = ev_make(ARGUS_EV_PROVIDER_QUARANTINED, 100); dg(e.evidence_digest, 999);
541 CHECK(argus_core_ingest(c, &e, big, ARGUS_CORE_MAX_FINDINGS, &n) == ARGUS_ERR_FULL && n == ARGUS_CORE_MAX_FINDINGS - 1);
542 CHECK(big[n - 1].code == ARGUS_F_TELEMETRY_LOSS);
543 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 101); e.cap_id = 20; e.cap_generation = 1; /* replay of the grant */
544 CHECK(argus_core_ingest(c, &e, big, ARGUS_CORE_MAX_FINDINGS, &n) == ARGUS_OK && n == ARGUS_CORE_MAX_FINDINGS - 2);
545 CHECK(big[0].code == ARGUS_F_AUTHORITY_REPLAY && big[1].code == ARGUS_F_FORGED_CAPABILITY);
546 e = ev_make(ARGUS_EV_TELEMETRY_DROPPED, 1); e.flags = ARGUS_FLAG_CONSUMER; e.object_id = ARGUS_CLASS_CRITICAL; e.resource = 1;
547 mid(e.machine_id, 1);
548 e.sequence = 1;
549 CHECK(argus_core_ingest(c, &e, big, ARGUS_CORE_MAX_FINDINGS, &n) == ARGUS_OK && n == ARGUS_CORE_MAX_FINDINGS - 1);
550 e.sequence = 1; /* anomaly + canned + drop */
551 CHECK(argus_core_ingest(c, &e, big, ARGUS_CORE_MAX_FINDINGS, &n) == ARGUS_OK && n == ARGUS_CORE_MAX_FINDINGS - 1);
552 CHECK(big[0].code == ARGUS_F_SEQUENCE_ANOMALY && big[n - 1].code == ARGUS_F_TELEMETRY_LOSS);
553 stub_detect_mode = 1;
554}
555
556static void t_digest(void)
557{
558 ArgusCore *c = fresh();
559 ArgusFinding f[16]; size_t n;
560 uint8_t d0[32], d1[32], d2[32];
561 argus_core_state_digest(c, d0);
562 argus_core_state_digest(c, d1);
563 CHECK(memcmp(d0, d1, 32) == 0);
564 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 1; e.cap_generation = 1;
565 ing(c, &e, f, &n);
566 argus_core_state_digest(c, d1);
567 CHECK(memcmp(d0, d1, 32) != 0);
568 e = ev_make(ARGUS_EV_CAPABILITY_USED, 2); e.cap_id = 1; e.cap_generation = 1; /* observation only: producer hwm moves */
569 ing(c, &e, f, &n);
570 argus_core_state_digest(c, d2);
571 CHECK(memcmp(d1, d2, 32) != 0);
572 /* same state reached through a different path gives the same digest */
573 static _Alignas(16) uint8_t m2[1 << 17];
574 ArgusCore *c2 = NULL;
575 argus_core_init(&c2, m2, sizeof m2);
576 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 1; e.cap_generation = 1;
577 ing(c2, &e, f, &n);
578 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 2); e.outcome = ARGUS_OUTCOME_DENIED;
579 ing(c2, &e, f, &n);
580 argus_core_state_digest(c2, d0);
581 CHECK(memcmp(d0, d2, 32) == 0);
582}
583
584
585static void t_digest_domain_and_order(void)
586{
587 ArgusFinding f[16]; size_t n;
588 static _Alignas(16) uint8_t m1[1 << 17], m2[1 << 17];
589 ArgusCore *a = NULL, *b = NULL;
590 stub_detect_mode = 1;
591 argus_core_init(&a, m1, sizeof m1);
592 argus_core_init(&b, m2, sizeof m2);
593 /* empty core: state digest != zero chain, != chain of anything */
594 uint8_t da[32], db[32], z[32] = {0};
595 argus_core_state_digest(a, da);
596 CHECK(memcmp(da, z, 32) != 0);
597 /* same machine SET joined in a different order (each on its own stream) -> same digest */
598 for (uint32_t i = 0; i < 5; i++) {
599 ArgusEvent e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 10 + i);
600 argus_core_ingest(a, &e, f, 16, &n);
601 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 14 - i);
602 argus_core_ingest(b, &e, f, 16, &n);
603 }
604 argus_core_state_digest(a, da);
605 argus_core_state_digest(b, db);
606 CHECK(memcmp(da, db, 32) == 0);
607 ArgusCoreHealth ha, hb; argus_core_health(a, &ha); argus_core_health(b, &hb);
608 CHECK(memcmp(ha.chain, hb.chain, 32) != 0); /* the event chain is order-sensitive */
609 CHECK(memcmp(da, ha.chain, 32) != 0);
610 /* a different trust on one machine changes the digest */
611 ArgusEvent e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 2); mid(e.machine_id, 12); e.object_id = ARGUS_TRUST_QUARANTINED;
612 argus_core_ingest(b, &e, f, 16, &n);
613 argus_core_state_digest(b, db);
614 CHECK(memcmp(da, db, 32) != 0);
615 /* tombstone differs from live */
616 argus_core_init(&b, m2, sizeof m2);
617 for (uint32_t i = 0; i < 5; i++) {
618 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 10 + i);
619 argus_core_ingest(b, &e, f, 16, &n);
620 }
621 e = ev_make(ARGUS_EV_MACHINE_REMOVED, 2); mid(e.machine_id, 12);
622 argus_core_ingest(b, &e, f, 16, &n);
623 argus_core_state_digest(b, db);
624 CHECK(memcmp(da, db, 32) != 0);
625}
626
627static void t_incidents(void)
628{
629 ArgusCore *c = fresh();
630 ArgusFinding f[16]; size_t n; ArgusCoreHealth h;
631 stub_detect_mode = 0; /* forged use -> CRITICAL per principal */
632 for (uint32_t p = 0; p < ARGUS_CORE_INCIDENTS + 5; p++) {
633 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_USED, 1 + p); e.cap_id = 1; e.principal = 1000 + p;
634 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1);
635 }
636 argus_core_health(c, &h);
637 CHECK(h.incidents_open == ARGUS_CORE_INCIDENTS && h.incidents_untracked == 5);
638 CHECK(h.findings_emitted == ARGUS_CORE_INCIDENTS + 5);
639 /* MEDIUM findings never become incidents */
640 ArgusEvent e = ev_make(99, 1);
641 ing(c, &e, f, &n);
642 argus_core_health(c, &h);
643 CHECK(h.incidents_untracked == 5);
644 stub_detect_mode = 1;
645}
646
647/* Mirrors of lane G's hostile scenarios for the rules the core owns (stub detectors). */
648static void t_hostile_rules(void)
649{
650 ArgusFinding f[16]; size_t n; ArgusCapShadow s; ArgusCoreHealth h;
651 const ArgusStateOps *o = argus_core_ops();
652 /* G-3: exact byte replay after revoke: anomaly only (not also 13), stays REVOKED */
653 ArgusCore *c = fresh();
654 ArgusEvent g = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); g.cap_id = 5; g.cap_generation = 1; g.principal = 7;
655 ing(c, &g, f, &n);
656 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 2); e.cap_id = 5; e.cap_generation = 1;
657 ing(c, &e, f, &n);
658 CHECK(ing(c, &g, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
659 CHECK(o->cap(argus_core_view(c), 5, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED);
660 /* same replay with a fresh sequence: 13, stays REVOKED; a later use is revoked-use */
661 g.sequence = 3;
662 CHECK(ing(c, &g, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 2);
663 stub_detect_mode = 0;
664 e = ev_make(ARGUS_EV_CAPABILITY_USED, 4); e.cap_id = 5; e.cap_generation = 1; e.principal = 7;
665 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_REVOKED_CAPABILITY_USED);
666 stub_detect_mode = 1;
667 argus_core_health(c, &h);
668 CHECK(h.events_not_applied == 2 && h.incidents_open == 3);
669 /* G-4: forged REVOKED at UINT64_MAX on an unseen slot poisons nothing */
670 c = fresh();
671 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 1); e.cap_id = 5; e.cap_generation = UINT64_MAX;
672 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY);
673 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 2); e.cap_id = 5; e.cap_generation = 1; e.principal = 7;
674 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
675 stub_detect_mode = 0;
676 e = ev_make(ARGUS_EV_CAPABILITY_USED, 3); e.cap_id = 5; e.cap_generation = 1; e.principal = 7;
677 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
678 stub_detect_mode = 1;
679 /* G-4: forged REVOKED at UINT64_MAX on a LIVE slot is not applied either */
680 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 4); e.cap_id = 5; e.cap_generation = UINT64_MAX;
681 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].prior_sequence == 2);
682 CHECK(o->cap(argus_core_view(c), 5, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.generation == 1);
683 /* G-15: a skipping commit is not adopted; the legitimate successor is */
684 c = fresh();
685 ArgusWorldShadow w;
686 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 1); e.world_generation = 1; dg(e.evidence_digest, 1);
687 ing(c, &e, f, &n);
688 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 2); e.world_generation = UINT64_MAX; dg(e.evidence_digest, 99);
689 ing(c, &e, f, &n);
690 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 3); e.world_generation = 2; dg(e.evidence_digest, 2);
691 ing(c, &e, f, &n);
692 CHECK(o->world(argus_core_view(c), 0, &w) == ARGUS_OK && w.generation == 2 && w.sequence == 3);
693 /* G-8: quarantine survives remove + rejoin */
694 c = fresh();
695 ArgusMachineShadow ms;
696 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 3); ing(c, &e, f, &n);
697 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 2); mid(e.machine_id, 3); e.object_id = ARGUS_TRUST_QUARANTINED; ing(c, &e, f, &n);
698 e = ev_make(ARGUS_EV_MACHINE_REMOVED, 3); mid(e.machine_id, 3); ing(c, &e, f, &n);
699 e = ev_make(ARGUS_EV_MACHINE_JOINED, 4); mid(e.machine_id, 3); ing(c, &e, f, &n);
700 CHECK(o->machine(argus_core_view(c), e.machine_id, &ms) == ARGUS_OK && ms.trust == ARGUS_TRUST_QUARANTINED && ms.joined_sequence == 4);
701 /* G-8: 17th machine is still tracked (table 64) */
702 c = fresh();
703 for (uint32_t m = 1; m <= 17; m++) {
704 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 100 + m);
705 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
706 }
707 e = ev_make(ARGUS_EV_MACHINE_TRUST_CHANGED, 2); mid(e.machine_id, 117); e.object_id = ARGUS_TRUST_QUARANTINED;
708 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->machine(argus_core_view(c), e.machine_id, &ms) == ARGUS_OK && ms.trust == ARGUS_TRUST_QUARANTINED);
709 /* G-21: 40 producers are all tracked; a replay on the 41st stream is flagged */
710 c = fresh();
711 for (uint32_t m = 0; m < 40; m++) {
712 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 1 + m); mid(e.machine_id, 1000 + m); ing(c, &e, f, &n);
713 }
714 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 7); mid(e.machine_id, 5000);
715 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
716 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
717 argus_core_health(c, &h);
718 CHECK(h.producers_untracked == 0);
719}
720
721/* ---- v1.1: streams, cap 0 / CAP_NONE, USE_SUMMARY, per-store Worlds ---- */
722static ArgusEvent ev_stream(uint16_t kind, uint64_t seq, uint32_t machine, uint16_t stream)
723{
724 ArgusEvent e = ev_make(kind, seq);
725 mid(e.machine_id, machine);
726 e.flags = (uint16_t)(ARGUS_FLAG_SYNTHETIC | (stream << ARGUS_FLAG_STREAM_SHIFT));
727 return e;
728}
729
730static void t_v11_streams(void)
731{
732 ArgusCore *c = fresh();
733 ArgusFinding f[16]; size_t n;
734 /* three streams on one machine keep independent high-water marks */
735 for (uint16_t s = 0; s < 3; s++) {
736 ArgusEvent e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 10, 1, s);
737 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
738 }
739 ArgusEvent e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 5, 1, 1); /* below stream 1's mark */
740 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY && f[0].prior_sequence == 10);
741 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 11, 1, 1);
742 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
743 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 1, 7); /* new stream starts anywhere */
744 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
745 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 1, (uint16_t)(ARGUS_STREAM_MAX - 1u));
746 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
747 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
748 /* the CONSUMER bit stays part of the key: stream 0 consumer vs producer */
749 e = ev_stream(ARGUS_EV_TELEMETRY_DROPPED, 1, 1, 0); e.flags = ARGUS_FLAG_CONSUMER;
750 e.object_id = ARGUS_CLASS_AUDIT; e.resource = 1;
751 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
752 ArgusCoreHealth h; argus_core_health(c, &h);
753 CHECK(h.producers_untracked == 0);
754
755 /* the producer table holds ARGUS_CORE_PRODUCERS streams in total, across machines x streams */
756 c = fresh();
757 for (uint32_t k = 0; k < ARGUS_CORE_PRODUCERS; k++) {
758 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 100 + k / 16u, (uint16_t)(k % 16u));
759 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
760 }
761 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 100, 16); /* 257th stream */
762 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
763 e.sequence = 2;
764 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0); /* later untracked: counted only */
765 argus_core_health(c, &h);
766 CHECK(h.producers_untracked == 2 && h.tables_full == 1);
767 e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 100, 15); /* tracked stream: replay flagged */
768 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
769
770 /* the state digest depends on the stream id */
771 uint8_t d1[32], d2[32];
772 c = fresh(); e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 1, 2); ing(c, &e, f, &n); argus_core_state_digest(c, d1);
773 c = fresh(); e = ev_stream(ARGUS_EV_CAPABILITY_DENIED, 1, 1, 3); ing(c, &e, f, &n); argus_core_state_digest(c, d2);
774 CHECK(memcmp(d1, d2, 32) != 0);
775}
776
777static void t_v11_caps_summary(void)
778{
779 ArgusCore *c = fresh();
780 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
781 ArgusFinding f[16]; size_t n; ArgusCapShadow s;
782 /* cap 0 lifecycle: grant, replay, revoke, regrant above */
783 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1); e.cap_id = 0; e.cap_generation = 1; e.principal = 4;
784 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 0, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE);
785 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 2); e.cap_id = 0; e.cap_generation = 1; e.principal = 9;
786 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_AUTHORITY_REPLAY && f[0].cap_id == 0);
787 CHECK(o->cap(v, 0, &s) == ARGUS_OK && s.subject == 4);
788 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 3); e.cap_id = 0; e.cap_generation = 1;
789 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 0, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_REVOKED);
790 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 4); e.cap_id = 0; e.cap_generation = 2; e.principal = 4;
791 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->cap(v, 0, &s) == ARGUS_OK && s.state == ARGUS_SHADOW_LIVE && s.generation == 2);
792 /* CAP_NONE on REVOKED: no-op, never a replay finding */
793 e = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 5); e.cap_id = ARGUS_CAP_NONE;
794 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
795 /* cap ids in (ARGUS_CAP_MAX, CAP_NONE) stay malformed */
796 e = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 6); e.cap_id = ARGUS_CAP_NONE - 1u;
797 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
798
799 /* USE_SUMMARY applies nothing, is sequence-checked, detected and chained */
800 uint8_t d0[32], d1[32]; ArgusCoreHealth h0, h1;
801 argus_core_state_digest(c, d0); argus_core_health(c, &h0);
802 e = ev_make(ARGUS_EV_CAPABILITY_USE_SUMMARY, 7); e.class_ = ARGUS_CLASS_AUDIT;
803 e.cap_id = 0; e.cap_generation = 2; e.world_generation = 2; e.resource = 4096; e.principal = 4;
804 stub_detect_mode = 3; /* detector ran: probes cap 0 */
805 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && stub_probe_rc == ARGUS_OK && stub_probe_cap.generation == 2);
806 stub_detect_mode = 1;
807 argus_core_state_digest(c, d1); argus_core_health(c, &h1);
808 CHECK(memcmp(h0.chain, h1.chain, 32) != 0 && h1.events_not_applied == h0.events_not_applied);
809 CHECK(o->cap(v, 0, &s) == ARGUS_OK && s.generation == 2 && s.state == ARGUS_SHADOW_LIVE && s.granted_sequence == 4);
810 /* state digest changes only through the producer high-water mark (tag 10), not a table */
811 c = fresh();
812 e = ev_make(ARGUS_EV_CAPABILITY_DENIED, 7); ing(c, &e, f, &n);
813 uint8_t d2[32]; argus_core_state_digest(c, d2);
814 ArgusCore *c2 = fresh(); /* same buffer: c is gone */
815 e = ev_make(ARGUS_EV_CAPABILITY_USE_SUMMARY, 7); e.class_ = ARGUS_CLASS_AUDIT; e.cap_id = 3; e.cap_generation = 1; e.world_generation = 1; e.resource = 1;
816 ing(c2, &e, f, &n);
817 uint8_t d3[32]; argus_core_state_digest(c2, d3);
818 CHECK(memcmp(d2, d3, 32) == 0); /* summary == any non-applying event */
819 e = ev_make(ARGUS_EV_CAPABILITY_USE_SUMMARY, 7); e.class_ = ARGUS_CLASS_AUDIT;
820 CHECK(ing(c2, &e, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY); /* replay */
821 e.outcome = ARGUS_OUTCOME_DENIED; e.sequence = 8;
822 CHECK(ing(c2, &e, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
823}
824
825static void t_v11_worlds(void)
826{
827 ArgusCore *c = fresh();
828 const ArgusStateOps *o = argus_core_ops(); const ArgusStateView *v = argus_core_view(c);
829 ArgusFinding f[16]; size_t n; ArgusWorldShadow w;
830 uint64_t seq = 1;
831 /* two stores interleaved, each +1 on its own lineage */
832 for (uint64_t g = 1; g <= 5; g++) {
833 for (uint32_t st = 7; st <= 8; st++) {
834 ArgusEvent e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = st;
835 e.world_generation = st == 7 ? g : 100 + g; dg(e.evidence_digest, st * 1000 + (uint32_t)g);
836 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
837 }
838 }
839 CHECK(o->world(v, 7, &w) == ARGUS_OK && w.store_id == 7 && w.generation == 5 && w.digest[0] == (uint8_t)(7005 & 0xFF));
840 CHECK(o->world(v, 8, &w) == ARGUS_OK && w.store_id == 8 && w.generation == 105);
841 CHECK(o->world(v, 0, &w) == ARGUS_ERR_STATE);
842 /* a skip on store 7 is not adopted and does not touch store 8 */
843 ArgusEvent e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = 7; e.world_generation = 9; dg(e.evidence_digest, 1);
844 ArgusCoreHealth h0, h1; argus_core_health(c, &h0);
845 CHECK(ing(c, &e, f, &n) == ARGUS_OK);
846 argus_core_health(c, &h1);
847 CHECK(h1.events_not_applied == h0.events_not_applied + 1);
848 CHECK(o->world(v, 7, &w) == ARGUS_OK && w.generation == 5);
849 CHECK(o->world(v, 8, &w) == ARGUS_OK && w.generation == 105);
850 /* 8 stores fit; the 9th is table-full: dropped, ERR_FULL + CRITICAL loss, counted */
851 for (uint32_t st = 20; st < 26; st++) {
852 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = st; e.world_generation = 1; dg(e.evidence_digest, st);
853 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
854 }
855 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = 99; e.world_generation = 1; dg(e.evidence_digest, 99);
856 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
857 CHECK(o->world(v, 99, &w) == ARGUS_ERR_STATE);
858 argus_core_health(c, &h1);
859 CHECK(h1.tables_full == 1);
860 /* ruling (a): a 10th store is quiet (ERR_FULL, no finding), counted in both counters */
861 { ArgusCoreHealth h2;
862 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = 98; e.world_generation = 1; dg(e.evidence_digest, 98);
863 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 0 && o->world(v, 98, &w) == ARGUS_ERR_STATE);
864 argus_core_health(c, &h2);
865 CHECK(h2.tables_full == 2 && h2.events_not_applied == h1.events_not_applied + 1); }
866 /* a different table still reports its own first overflow (per-table, not global) */
867 for (uint32_t m = 0; m < ARGUS_CORE_MACHINES; m++) {
868 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 3000 + m); e.flags = 0;
869 ing(c, &e, f, &n);
870 }
871 e = ev_make(ARGUS_EV_MACHINE_JOINED, 1); mid(e.machine_id, 2999);
872 CHECK(ing(c, &e, f, &n) == ARGUS_ERR_FULL && n == 1 && f[0].code == ARGUS_F_TELEMETRY_LOSS && f[0].severity == ARGUS_SEV_CRITICAL);
873 /* known stores still advance with the table full */
874 e = ev_make(ARGUS_EV_WORLD_COMMITTED, seq++); e.object_id = 20; e.world_generation = 2; dg(e.evidence_digest, 2020);
875 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0 && o->world(v, 20, &w) == ARGUS_OK && w.generation == 2);
876 /* world_generation 0 on a new store: no entry */
877 c = fresh(); v = argus_core_view(c);
878 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 1); e.object_id = 3; e.world_generation = 0;
879 CHECK(ing(c, &e, f, &n) == ARGUS_OK && o->world(v, 3, &w) == ARGUS_ERR_STATE);
880 /* the state digest includes the store id and does not depend on store arrival order */
881 uint8_t d1[32], d2[32], d3[32];
882 c = fresh();
883 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 1); e.object_id = 1; e.world_generation = 1; dg(e.evidence_digest, 5); ing(c, &e, f, &n);
884 argus_core_state_digest(c, d1);
885 c = fresh();
886 e = ev_make(ARGUS_EV_WORLD_COMMITTED, 1); e.object_id = 2; e.world_generation = 1; dg(e.evidence_digest, 5); ing(c, &e, f, &n);
887 argus_core_state_digest(c, d2);
888 CHECK(memcmp(d1, d2, 32) != 0);
889 c = fresh();
890 e = ev_stream(ARGUS_EV_WORLD_COMMITTED, 5, 1, 1); e.object_id = 1; e.world_generation = 1; dg(e.evidence_digest, 5); ing(c, &e, f, &n);
891 e = ev_stream(ARGUS_EV_WORLD_COMMITTED, 6, 1, 2); e.object_id = 2; e.world_generation = 1; dg(e.evidence_digest, 6); ing(c, &e, f, &n);
892 argus_core_state_digest(c, d1);
893 c = fresh();
894 e = ev_stream(ARGUS_EV_WORLD_COMMITTED, 6, 1, 2); e.object_id = 2; e.world_generation = 1; dg(e.evidence_digest, 6); ing(c, &e, f, &n);
895 e = ev_stream(ARGUS_EV_WORLD_COMMITTED, 5, 1, 1); e.object_id = 1; e.world_generation = 1; dg(e.evidence_digest, 5); ing(c, &e, f, &n);
896 argus_core_state_digest(c, d3);
897 CHECK(memcmp(d1, d3, 32) == 0);
898}
899
900
901/* ---- v1.2 (ARGUS-1): containment kinds 90-93 and argus_core_incident ------- */
902
903static ArgusEvent ev_contain(uint16_t kind, uint64_t seq, uint8_t status, uint8_t outcome)
904{
905 ArgusEvent e = ev_make(kind, seq);
906 e.class_ = ARGUS_CLASS_CRITICAL;
907 e.effect_class = ARGUS_EFFECT_NONE;
908 e.outcome = outcome;
909 e.flags = kind == ARGUS_EV_CONTAINMENT_PROPOSED ? ARGUS_FLAG_CONSUMER : (uint16_t)(1u << ARGUS_FLAG_STREAM_SHIFT);
910 e.principal = 7; e.cap_id = 5; e.cap_generation = 2;
911 e.object_id = kind == ARGUS_EV_AUTHORITY_ESCALATED ? 0u
912 : ARGUS_CONTAIN_PACK(ARGUS_CONTAIN_REVOKE_CAPABILITY, status, ARGUS_F_STALE_GENERATION);
913 e.world_generation = kind == ARGUS_EV_AUTHORITY_ESCALATED ? 0u : 1u;
914 e.resource = kind == ARGUS_EV_CONTAINMENT_EXECUTED ? 1u : 3u;
915 dg(e.evidence_digest, 900);
916 return e;
917}
918
919static void t_v12_containment_kinds(void)
920{
921 ArgusCore *c = fresh();
922 ArgusFinding f[16]; size_t n; ArgusCoreHealth h0, h1;
923 ArgusEvent g = ev_make(ARGUS_EV_CAPABILITY_GRANTED, 1);
924 g.principal = 7; g.cap_id = 5; g.cap_generation = 2; g.object_id = 1;
925 CHECK(ing(c, &g, f, &n) == ARGUS_OK && n == 0);
926 ArgusCapShadow s0, s1;
927 argus_core_ops()->cap(argus_core_view(c), 5, &s0);
928
929 /* Every detector mode: 90-93 never reach argus_detect_run and raise nothing. Mode 2
930 * would fill every free slot with a finding if the detectors were called. */
931 const uint16_t kinds[4] = { ARGUS_EV_CONTAINMENT_PROPOSED, ARGUS_EV_CONTAINMENT_DECIDED,
932 ARGUS_EV_CONTAINMENT_EXECUTED, ARGUS_EV_AUTHORITY_ESCALATED };
933 const uint8_t status[4] = { ARGUS_CSTATUS_PROPOSED, ARGUS_CSTATUS_GRANT, ARGUS_CSTATUS_DONE, 0 };
934 uint64_t seq = 1;
935 for (int mode = 0; mode <= 3; mode++) {
936 for (int k = 0; k < 4; k++) {
937 ArgusEvent e = ev_contain(kinds[k], seq++, status[k], ARGUS_OUTCOME_OK);
938 stub_detect_mode = mode;
939 uint64_t calls = stub_detect_calls;
940 uint8_t chain0[32];
941 argus_core_health(c, &h0); memcpy(chain0, h0.chain, 32);
942 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 0);
943 CHECK(stub_detect_calls == calls);
944 argus_core_health(c, &h1);
945 CHECK(h1.events_received == h0.events_received + 1 && h1.findings_emitted == h0.findings_emitted);
946 CHECK(h1.events_not_applied == h0.events_not_applied && h1.events_rejected == h0.events_rejected);
947 CHECK(memcmp(chain0, h1.chain, 32) != 0); /* chained */
948 argus_core_ops()->cap(argus_core_view(c), 5, &s1);
949 CHECK(memcmp(&s0, &s1, sizeof s0) == 0); /* no shadow change */
950 }
951 }
952 /* Non-OK outcomes (DENY / ESCALATE / FAILED) change nothing either. */
953 stub_detect_mode = 2;
954 ArgusEvent d = ev_contain(ARGUS_EV_CONTAINMENT_DECIDED, seq, ARGUS_CSTATUS_DENY, ARGUS_OUTCOME_DENIED);
955 CHECK(ing(c, &d, f, &n) == ARGUS_OK && n == 0);
956 d = ev_contain(ARGUS_EV_CONTAINMENT_EXECUTED, seq + 1, ARGUS_CSTATUS_FAILED, ARGUS_OUTCOME_ERROR);
957 CHECK(ing(c, &d, f, &n) == ARGUS_OK && n == 0);
958 argus_core_ops()->cap(argus_core_view(c), 5, &s1);
959 CHECK(memcmp(&s0, &s1, sizeof s0) == 0);
960
961 /* The sequence check still applies to their streams (core step 1, unchanged): a
962 * replayed 91 is a core SEQUENCE_ANOMALY; argus_contain adds code 17 on its side. */
963 stub_detect_mode = 1;
964 ArgusEvent r = ev_contain(ARGUS_EV_CONTAINMENT_DECIDED, 2, ARGUS_CSTATUS_GRANT, ARGUS_OUTCOME_OK);
965 CHECK(ing(c, &r, f, &n) == ARGUS_OK && n == 1 && f[0].code == ARGUS_F_SEQUENCE_ANOMALY);
966
967 /* A malformed 90-93 is still MALFORMED: code 16, not chained. */
968 ArgusEvent m = ev_contain(ARGUS_EV_CONTAINMENT_EXECUTED, 100, ARGUS_CSTATUS_DONE, ARGUS_OUTCOME_OK);
969 m.version = 0;
970 argus_core_health(c, &h0);
971 CHECK(ing(c, &m, f, &n) == ARGUS_ERR_MALFORMED && n == 1 && f[0].code == ARGUS_F_MALFORMED_EVENT);
972 argus_core_health(c, &h1);
973 CHECK(memcmp(h0.chain, h1.chain, 32) == 0 && h1.events_rejected == h0.events_rejected + 1);
974
975
976 /* The observer's kind 4 (bridged revoke) applies exactly as in ARGUS-0. */
977 ArgusEvent rv = ev_make(ARGUS_EV_CAPABILITY_REVOKED, 2);
978 rv.principal = 7; rv.cap_id = 5; rv.cap_generation = 2;
979 CHECK(ing(c, &rv, f, &n) == ARGUS_OK && n == 0);
980 argus_core_ops()->cap(argus_core_view(c), 5, &s1);
981 CHECK(s1.state == ARGUS_SHADOW_REVOKED && s1.revoked_sequence == 2);
982}
983
984static void t_v12_incident(void)
985{
986 ArgusCore *c = fresh();
987 ArgusFinding f[16]; size_t n; uint64_t first = 12345;
988 CHECK(argus_core_incident(NULL, 1, 1, &first) == ARGUS_ERR_ARG);
989 CHECK(argus_core_incident(c, 1, 1, NULL) == ARGUS_ERR_ARG);
990 CHECK(argus_core_incident(c, 1, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_ERR_STATE && first == 12345);
991
992 /* HIGH/CRITICAL findings open an incident at the first sequence; later ones keep it. */
993 stub_detect_mode = 0; /* forged use -> CRITICAL FORGED for principal */
994 for (uint64_t s = 1; s <= 3; s++) {
995 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_USED, 10 + s); e.cap_id = 1; e.principal = 77;
996 CHECK(ing(c, &e, f, &n) == ARGUS_OK && n == 1);
997 }
998 CHECK(argus_core_incident(c, 77, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_OK && first == 11);
999 CHECK(argus_core_incident(c, 77, ARGUS_F_STALE_GENERATION, &first) == ARGUS_ERR_STATE);
1000 CHECK(argus_core_incident(c, 78, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_ERR_STATE);
1001
1002 /* MEDIUM (code 16) never opens an incident. */
1003 ArgusEvent m = ev_make(99, 1); m.principal = 55;
1004 CHECK(ing(c, &m, f, &n) == ARGUS_ERR_MALFORMED && n == 1);
1005 CHECK(argus_core_incident(c, 55, ARGUS_F_MALFORMED_EVENT, &first) == ARGUS_ERR_STATE);
1006
1007 /* Table full: untracked pairs are not found (ERR_STATE), tracked ones still are. */
1008 for (uint32_t p = 0; p < ARGUS_CORE_INCIDENTS + 3; p++) {
1009 ArgusEvent e = ev_make(ARGUS_EV_CAPABILITY_USED, 100 + p); e.cap_id = 1; e.principal = 2000 + p;
1010 ing(c, &e, f, &n);
1011 }
1012 ArgusCoreHealth h; argus_core_health(c, &h);
1013 CHECK(h.incidents_open == ARGUS_CORE_INCIDENTS && h.incidents_untracked > 0);
1014 CHECK(argus_core_incident(c, 77, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_OK && first == 11);
1015 CHECK(argus_core_incident(c, 2000, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_OK && first == 100);
1016 CHECK(argus_core_incident(c, 2000 + ARGUS_CORE_INCIDENTS + 2, ARGUS_F_FORGED_CAPABILITY, &first) == ARGUS_ERR_STATE);
1017
1018 /* Replayable: a byte copy of the core answers identically (no pointers, no hidden state). */
1019 static _Alignas(16) uint8_t copy[1 << 17];
1020 memcpy(copy, mem, argus_core_footprint());
1021 uint64_t f2 = 0;
1022 CHECK(argus_core_incident((const ArgusCore *)(const void *)copy, 77, ARGUS_F_FORGED_CAPABILITY, &f2) == ARGUS_OK && f2 == 11);
1023 stub_detect_mode = 1;
1024}
1025
1026int main(void)
1027{
1028 t_init();
1029 t_malformed();
1030 t_caps();
1031 t_leases();
1032 t_artifacts();
1033 t_machines();
1034 t_providers_world_policy();
1035 t_sequence();
1036 t_producers_full();
1037 t_telemetry();
1038 t_prestate_and_detectors();
1039 t_overflow();
1040 t_digest();
1041 t_digest_domain_and_order();
1042 t_incidents();
1043 t_hostile_rules();
1044 t_v11_streams();
1045 t_v11_caps_summary();
1046 t_v11_worlds();
1047 t_v12_containment_kinds();
1048 t_v12_incident();
1049 printf("test_argus_core: %d passed, %d failed (footprint %zu bytes)\n", t_pass, t_fail, argus_core_footprint());
1050 return t_fail ? 1 : 0;
1051}