nx_xenocell_test.nx source
↩ module page · 100 lines · 3783 B
1// nx_xenocell_test.nx -- smoke for nx_xenocell.
2
3import "nx_syscalls.nx"
4import "nx_attention_class.nx"
5import "nx_evict_journal.nx"
6import "nx_xenocell.nx"
7
8func main() -> i64 {
9 // 1: intrusion state sealed enum
10 if NX_INTR_N_STATES != 5 { return 1 }
11 if nx_intr_is_valid(NX_INTR_DORMANT) != 1 { return 2 }
12 if nx_intr_is_valid(NX_INTR_COMPROMISING) != 1 { return 3 }
13 if nx_intr_is_valid(-1) != 0 { return 4 }
14 if nx_intr_is_valid(5) != 0 { return 5 }
15
16 // 2: observation kind sealed enum
17 if NX_OBS_N_KINDS != 7 { return 6 }
18 if nx_obs_is_valid(NX_OBS_RAM_READ) != 1 { return 7 }
19 if nx_obs_is_valid(NX_OBS_BOOT_INTERPOSE) != 1 { return 8 }
20 if nx_obs_is_valid(7) != 0 { return 9 }
21
22 // 3: construct (Intel ME wrapped)
23 let name: *u8 = (sys_mmap(16)) as *u8
24 name[0] = 73 as u8 // 'I'
25 name[1] = 77 as u8 // 'M'
26 name[2] = 69 as u8 // 'E'
27 name[3] = 0 as u8
28 // id=1, name=name, attention=IDLE_OPPORTUNISTIC, obs_capacity=8
29 let x: *NxXenocell = nx_xenocell_new(1, name,
30 NX_AC_IDLE_OPPORTUNISTIC, 8)
31 if x.id != 1 { return 10 }
32 if x.intrusion_state != NX_INTR_DORMANT { return 11 }
33 if x.obs_count != 0 { return 12 }
34
35 // 4: initial state is not hostile-now
36 if nx_xenocell_is_hostile_now(x) != 0 { return 13 }
37
38 // 5: update observed
39 nx_xenocell_update(x, 50000000, 0, 1000, 100)
40 if x.observed_ram_bytes != 50000000 { return 14 }
41 if x.last_observe_us != 100 { return 15 }
42
43 // 6: record an observation (ME caught reading RAM)
44 let sig: *u8 = (sys_mmap(96)) as *u8 // pretend 96-byte ML-DSA sig
45 sig[0] = 1 as u8
46 let rc1: nx_int = nx_xenocell_record(x, 200,
47 NX_OBS_RAM_READ, 0x12345678abcdef00, sig, 96,
48 NX_INTR_PROBING)
49 if rc1 != NX_XENO_OK { return 16 }
50 if x.obs_count != 1 { return 17 }
51 if x.intrusion_state != NX_INTR_PROBING { return 18 }
52 if nx_xenocell_is_hostile_now(x) != 1 { return 19 }
53
54 // 7: bad kind rejected
55 let rc_bad: nx_int = nx_xenocell_record(x, 300,
56 99, 0, sig, 96, NX_INTR_PROBING)
57 if rc_bad != NX_XENO_ERR_BAD_KIND { return 20 }
58
59 // 8: bad state rejected
60 let rc_bad2: nx_int = nx_xenocell_record(x, 300,
61 NX_OBS_RAM_READ, 0, sig, 96, 99)
62 if rc_bad2 != NX_XENO_ERR_BAD_STATE { return 21 }
63
64 // 9: null sig rejected
65 let null_sig: *u8 = (0 as i64) as *u8
66 let rc_null: nx_int = nx_xenocell_record(x, 300,
67 NX_OBS_RAM_READ, 0, null_sig, 96, NX_INTR_PROBING)
68 if rc_null != NX_XENO_ERR_BAD_SIG { return 22 }
69
70 // 10: zero-length sig rejected
71 let rc_zero: nx_int = nx_xenocell_record(x, 300,
72 NX_OBS_RAM_READ, 0, sig, 0, NX_INTR_PROBING)
73 if rc_zero != NX_XENO_ERR_BAD_SIG { return 23 }
74
75 // 11: record more observations
76 nx_xenocell_record(x, 400, NX_OBS_NET_EGRESS, 0xaaa, sig, 96, NX_INTR_EXFILTRATING)
77 nx_xenocell_record(x, 500, NX_OBS_RAM_READ, 0xbbb, sig, 96, NX_INTR_EXFILTRATING)
78 nx_xenocell_record(x, 600, NX_OBS_NET_EGRESS, 0xccc, sig, 96, NX_INTR_EXFILTRATING)
79 if x.obs_count != 4 { return 24 }
80
81 // 12: tally by kind
82 if nx_xenocell_count_by_kind(x, NX_OBS_RAM_READ) != 2 { return 25 }
83 if nx_xenocell_count_by_kind(x, NX_OBS_NET_EGRESS) != 2 { return 26 }
84 if nx_xenocell_count_by_kind(x, NX_OBS_FIRMWARE_WRITE) != 0 { return 27 }
85
86 // 13: evidence count = obs_count
87 if nx_xenocell_evidence_count(x) != 4 { return 28 }
88
89 // 14: ring wraps cleanly after capacity hits
90 var k: nx_int = 0
91 while k < 10 {
92 nx_xenocell_record(x, 1000 + k, NX_OBS_DMA_PEEK, 0, sig, 96,
93 NX_INTR_INFILTRATING)
94 k = k + 1
95 }
96 if x.observations_head >= x.observations_capacity { return 29 }
97 if x.obs_count != 14 { return 30 } // 4 prior + 10 here
98
99 return 0
100}