Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/argus/tests/test_argus_contain.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / argus / tests / test_argus_contain.c` 362 lines ยท 23146 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 #include "../argus_abi.h" 18 #include "../argus_core.h" 19 #include "../argus_contain.h" 20 #include "stub_state.h" 21 22 #include 23 #include 24 #include 25 26 static int checks, failures; 27 #define CHECK(x) do { checks++; if(!(x)) { failures++; fprintf(stderr,"FAIL %s:%d: %s\n",__FILE__,__LINE__,#x); } } while(0) 28 29 static ArgusEvent event(uint16_t kind, uint64_t seq) 30 { 31 ArgusEvent e={0}; e.version=ARGUS_ABI_VERSION; e.kind=kind; e.sequence=seq; 32 e.class_=argus_event_min_class(kind); e.outcome=ARGUS_OUTCOME_OK; 33 e.cap_id=ARGUS_CAP_NONE; e.principal=7; e.machine_id[0]=0xA5; return e; 34 } 35 36 static void corpus_storm_test(void) 37 { 38 const size_t uses=100000, cap=1u+ARGUS_CAP_MAX+uses; 39 ArgusEvent *stream=calloc(cap,sizeof *stream); 40 void *cmem=calloc(1,argus_core_footprint()), *smem=calloc(1,argus_contain_footprint()); 41 ArgusCore *core=NULL; ArgusContain *state=NULL; 42 CHECK(stream&&cmem&&smem); 43 size_t n=argus_corpus_containment_storm(stream,cap,uses,41); 44 CHECK(n==cap); 45 CHECK(argus_core_init(&core,cmem,argus_core_footprint())==ARGUS_OK); 46 CHECK(argus_contain_init(&state,smem,argus_contain_footprint())==ARGUS_OK); 47 size_t requested=0; uint32_t per_principal[ARGUS_CAP_MAX+1]={0}; 48 uint32_t per_window[32]={0}; 49 for(size_t i=0;i=1000u?rq[j].principal-1000u:ARGUS_CAP_MAX; 67 CHECK(key<=ARGUS_CAP_MAX); if(key<=ARGUS_CAP_MAX) per_principal[key]++; 68 } 69 } 70 ArgusContainHealth h; argus_contain_health(state,&h); 71 CHECK(requested==h.requested && requested>0 && h.pending_full>0); 72 for(size_t w=0;w<32;w++) CHECK(per_window[w]<=ARGUS_CONTAIN_WINDOW_MAX); 73 for(size_t k=0;k<=ARGUS_CAP_MAX;k++) CHECK(per_principal[k]<=2); 74 free(stream); free(smem); free(cmem); 75 } 76 77 static void corpus_cap0_test(void) 78 { 79 ArgusEvent stream[102]; size_t n=argus_corpus_cap0_framing(stream,102,100,7); 80 CHECK(n==102); 81 void *cmem=calloc(1,argus_core_footprint()), *smem=calloc(1,argus_contain_footprint()); 82 ArgusCore *core=NULL; ArgusContain *state=NULL; 83 CHECK(cmem&&smem&&argus_core_init(&core,cmem,argus_core_footprint())==ARGUS_OK); 84 CHECK(argus_contain_init(&state,smem,argus_contain_footprint())==ARGUS_OK); 85 for(size_t i=0;i0); 96 free(smem); free(cmem); 97 } 98 99 static void corpus_saturation_test(void) 100 { 101 const uint16_t kinds[]={ARGUS_EV_MACHINE_JOINED,ARGUS_EV_CREDENTIAL_LEASE_CREATED, 102 ARGUS_EV_PROVIDER_DISCOVERED,ARGUS_EV_ARTIFACT_ADMITTED}; 103 const size_t slots[]={ARGUS_CORE_MACHINES,ARGUS_CORE_LEASES,ARGUS_CORE_PROVIDERS,ARGUS_CORE_ARTIFACTS}; 104 const uint8_t expected[]={ARGUS_CONTAIN_REQUIRE_REATTESTATION,ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE, 105 ARGUS_CONTAIN_QUARANTINE_PROVIDER,ARGUS_CONTAIN_REJECT_ARTIFACT}; 106 for(size_t t=0;t<4;t++) { 107 size_t cap=slots[t]+5; ArgusEvent *stream=calloc(cap,sizeof *stream); 108 CHECK(stream!=NULL); size_t n=argus_corpus_table_saturation(stream,cap,kinds[t],slots[t],4,91+t); 109 CHECK(n==cap - (kinds[t] == ARGUS_EV_MACHINE_JOINED ? 1u : 0u)); 110 void *cmem=calloc(1,argus_core_footprint()), *smem=calloc(1,argus_contain_footprint()); 111 ArgusCore *core=NULL; ArgusContain *state=NULL; 112 CHECK(cmem&&smem&&argus_core_init(&core,cmem,argus_core_footprint())==ARGUS_OK); 113 CHECK(argus_contain_init(&state,smem,argus_contain_footprint())==ARGUS_OK); 114 size_t requested=0; 115 for(size_t i=0;i 91 GRANT -> observer kind 4 -> 92 DONE confirms only the observed action. */ 181 size_t nfind=0; 182 CHECK(argus_contain_observe(state,core,&prop[0],scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 183 ArgusEvent decision=event(ARGUS_EV_CONTAINMENT_DECIDED,3); 184 decision.class_=ARGUS_CLASS_CRITICAL; decision.world_generation=req[0].request_id; 185 decision.object_id=ARGUS_CONTAIN_PACK(req[0].containment,ARGUS_CSTATUS_GRANT,req[0].finding_code); 186 decision.cap_id=req[0].target.cap_id; decision.cap_generation=req[0].target.generation; 187 memcpy(decision.machine_id,req[0].machine_id,ARGUS_MACHINE_ID_LEN); 188 argus_contain_request_digest(&req[0],decision.evidence_digest); 189 CHECK(argus_contain_observe(state,core,&decision,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK && nfind==0); 190 ArgusEvent revoke=event(ARGUS_EV_CAPABILITY_REVOKED,4); revoke.class_=ARGUS_CLASS_CRITICAL; 191 revoke.cap_id=req[0].target.cap_id; revoke.cap_generation=req[0].target.generation; 192 CHECK(argus_contain_observe(state,core,&revoke,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 193 ArgusEvent done=event(ARGUS_EV_CONTAINMENT_EXECUTED,5); done.class_=ARGUS_CLASS_CRITICAL; 194 done.world_generation=req[0].request_id; done.object_id=ARGUS_CONTAIN_PACK(req[0].containment,ARGUS_CSTATUS_DONE,req[0].finding_code); 195 done.resource=1; done.cap_id=req[0].target.cap_id; done.cap_generation=req[0].target.generation; 196 memcpy(done.machine_id,req[0].machine_id,ARGUS_MACHINE_ID_LEN); argus_contain_request_digest(&req[0],done.evidence_digest); 197 CHECK(argus_contain_observe(state,core,&done,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK && nfind==0); 198 argus_contain_health(state,&h); CHECK(h.requested==1 && h.granted==1 && h.confirmed==1 && h.failed==0); 199 200 /* A closed confirmed key cannot be replayed; an unknown decision raises code 17. */ 201 np=0; CHECK(argus_contain_propose(state,core,&f,1,&trigger,req,4,prop,4,&np)==ARGUS_OK && np==0); 202 argus_contain_health(state,&h); CHECK(h.suppressed_cooldown==1); 203 204 /* Only the first overflow per shadow table creates its synthetic ask. */ 205 void *sat_mem=calloc(1,argus_contain_footprint()); ArgusContain *sat_state=NULL; 206 CHECK(sat_mem && argus_contain_init(&sat_state,sat_mem,argus_contain_footprint())==ARGUS_OK); 207 const uint16_t full_kinds[4]={ARGUS_EV_MACHINE_JOINED,ARGUS_EV_CREDENTIAL_LEASE_CREATED, 208 ARGUS_EV_PROVIDER_DISCOVERED,ARGUS_EV_ARTIFACT_ADMITTED}; 209 const uint8_t sat_types[4]={ARGUS_CONTAIN_REQUIRE_REATTESTATION,ARGUS_CONTAIN_REVOKE_CREDENTIAL_LEASE, 210 ARGUS_CONTAIN_QUARANTINE_PROVIDER,ARGUS_CONTAIN_REJECT_ARTIFACT}; 211 ArgusFinding sat_f={0}; sat_f.code=ARGUS_F_TELEMETRY_LOSS; sat_f.severity=ARGUS_SEV_CRITICAL; 212 sat_f.confidence=ARGUS_CONF_DETERMINISTIC; sat_f.principal=7; sat_f.machine_id[0]=0xA5; 213 for(size_t i=0;i<4;i++) { 214 trigger=event(full_kinds[i],11000+i); trigger.object_id=(uint32_t)(80+i); trigger.evidence_digest[0]=(uint8_t)(0xC0+i); 215 sat_f.sequence=trigger.sequence; 216 np=0; CHECK(argus_contain_propose(sat_state,core,&sat_f,1,&trigger,req,4,prop,4,&np)==ARGUS_OK && np==1); 217 CHECK(req[0].containment==sat_types[i] && (req[0].flags&ARGUS_CREQ_SATURATION)!=0); 218 if(i==1) CHECK(req[0].target_object==trigger.object_id); 219 if(i==2 || i==3) CHECK(req[0].target_digest[0]==trigger.evidence_digest[0]); 220 } 221 trigger=event(ARGUS_EV_MACHINE_JOINED,12000); sat_f.sequence=trigger.sequence; np=0; 222 CHECK(argus_contain_propose(sat_state,core,&sat_f,1,&trigger,req,4,prop,4,&np)==ARGUS_OK && np==0); 223 argus_contain_health(sat_state,&h); CHECK(h.requested==4 && h.suppressed_dedup==1); 224 free(sat_mem); 225 ArgusEvent foreign=event(ARGUS_EV_CONTAINMENT_DECIDED,6); foreign.class_=ARGUS_CLASS_CRITICAL; 226 foreign.world_generation=999; foreign.object_id=ARGUS_CONTAIN_PACK(ARGUS_CONTAIN_REVOKE_CAPABILITY,ARGUS_CSTATUS_GRANT,ARGUS_F_STALE_GENERATION); 227 CHECK(argus_contain_observe(state,core,&foreign,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 228 CHECK(nfind==1 && scratch[0].code==ARGUS_F_CONTAINMENT_DECISION_UNMATCHED); 229 argus_contain_health(state,&h); CHECK(h.decisions_unmatched==1); 230 ArgusEvent foreign_proposal=event(ARGUS_EV_CONTAINMENT_PROPOSED,7); foreign_proposal.class_=ARGUS_CLASS_CRITICAL; 231 foreign_proposal.flags=ARGUS_FLAG_CONSUMER; foreign_proposal.world_generation=777; 232 foreign_proposal.object_id=ARGUS_CONTAIN_PACK(ARGUS_CONTAIN_REVOKE_CAPABILITY,ARGUS_CSTATUS_PROPOSED,ARGUS_F_STALE_GENERATION); 233 foreign_proposal.cap_id=5; foreign_proposal.cap_generation=9; foreign_proposal.resource=2; 234 CHECK(argus_contain_observe(state,core,&foreign_proposal,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 235 CHECK(nfind==1 && scratch[0].code==ARGUS_F_CONTAINMENT_DECISION_UNMATCHED); 236 argus_contain_health(state,&h); CHECK(h.decisions_unmatched==2); 237 238 /* A stale protected target is suppressed, even when the finding asks for revoke. */ 239 f.cap_id=0; f.sequence=8; np=99; 240 CHECK(argus_contain_propose(state,core,&f,1,&trigger,req,4,prop,4,&np)==ARGUS_OK && np==0); 241 argus_contain_health(state,&h); CHECK(h.suppressed_protected==1); 242 243 /* Timeout origin is the last transition; no code 20 at exactly the limit, 244 * then one code 20 after the limit. A separate request exercises expiry. */ 245 f.cap_id=5; f.sequence=10; f.code=ARGUS_F_FORGED_CAPABILITY; 246 f.containment=ARGUS_CONTAIN_REVOKE_CAPABILITY; f.severity=ARGUS_SEV_CRITICAL; 247 trigger.sequence=10; np=0; CHECK(argus_contain_propose(state,core,&f,1,&trigger,req,4,prop,4,&np)==ARGUS_OK && np==1); 248 CHECK(argus_contain_observe(state,core,&prop[0],scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 249 decision=event(ARGUS_EV_CONTAINMENT_DECIDED,11); decision.class_=ARGUS_CLASS_CRITICAL; 250 decision.world_generation=req[0].request_id; decision.object_id=ARGUS_CONTAIN_PACK(req[0].containment,ARGUS_CSTATUS_ESCALATE,req[0].finding_code); 251 decision.cap_id=req[0].target.cap_id; decision.cap_generation=req[0].target.generation; 252 memcpy(decision.machine_id,req[0].machine_id,ARGUS_MACHINE_ID_LEN); argus_contain_request_digest(&req[0],decision.evidence_digest); 253 CHECK(argus_contain_observe(state,core,&decision,scratch,ARGUS_CORE_MAX_FINDINGS,&nfind)==ARGUS_OK); 254 for(uint64_t i=0;i