nx_immune_test.nx source
↩ module page · 93 lines · 3953 B
1// nx_immune_test.nx -- smoke for nx_immune.
2
3import "nx_syscalls.nx"
4import "nx_evict_journal.nx"
5import "nx_pamp.nx"
6import "nx_antibody.nx"
7import "nx_immune.nx"
8
9func main() -> i64 {
10 // 1: status enum sealed
11 if NX_COL_N_STATUSES != 4 { return 1 }
12 if nx_col_status_is_valid(NX_COL_HEALTHY) != 1 { return 2 }
13 if nx_col_status_is_valid(NX_COL_COMPROMISED) != 1 { return 3 }
14 if nx_col_status_is_valid(-1) != 0 { return 4 }
15 if nx_col_status_is_valid(4) != 0 { return 5 }
16
17 // 2: construction
18 let j: *NxEvictJournal = nx_evict_journal_new(16)
19 let i: *NxImmune = nx_immune_new(8, j)
20 if i.cell_capacity != 8 { return 6 }
21 if i.n_cells != 0 { return 7 }
22 if i.global_status != NX_COL_HEALTHY { return 8 }
23
24 // 3: register 3 cells, each with its own antibody array
25 let ab1: *NxAntibodyArray = nx_antibody_array_new(8)
26 let ab2: *NxAntibodyArray = nx_antibody_array_new(8)
27 let ab3: *NxAntibodyArray = nx_antibody_array_new(8)
28 if nx_immune_register_cell(i, 100, ab1) != 0 { return 9 }
29 if nx_immune_register_cell(i, 200, ab2) != 1 { return 10 }
30 if nx_immune_register_cell(i, 300, ab3) != 2 { return 11 }
31 if i.n_cells != 3 { return 12 }
32
33 // 4: duplicate registration refused
34 if nx_immune_register_cell(i, 100, ab1) != -1 { return 13 }
35
36 // 5: set cell status -- HEALTHY -> ALERTED
37 nx_immune_set_cell_status(i, 100, NX_COL_ALERTED, 1000)
38 if nx_immune_alert_count(i, 100) != 1 { return 14 }
39
40 // 6: bad status rejected
41 if nx_immune_set_cell_status(i, 100, 99, 1000) != NX_IM_ERR_BAD_STATUS { return 15 }
42
43 // 7: unknown cell rejected
44 if nx_immune_set_cell_status(i, 9999, NX_COL_ALERTED, 1000) != NX_IM_ERR_BAD_CELL { return 16 }
45
46 // 8: global status = max(per-cell) = ALERTED
47 if nx_immune_global_status(i) != NX_COL_ALERTED { return 17 }
48
49 // 9: cell 200 -> RESPONDING; global escalates
50 nx_immune_set_cell_status(i, 200, NX_COL_RESPONDING, 2000)
51 if nx_immune_global_status(i) != NX_COL_RESPONDING { return 18 }
52
53 // 10: cell 300 -> COMPROMISED; global escalates again
54 nx_immune_set_cell_status(i, 300, NX_COL_COMPROMISED, 3000)
55 if nx_immune_global_status(i) != NX_COL_COMPROMISED { return 19 }
56
57 // 11: count cells by status
58 if nx_immune_count_cells_in_status(i, NX_COL_ALERTED) != 1 { return 20 }
59 if nx_immune_count_cells_in_status(i, NX_COL_RESPONDING) != 1 { return 21 }
60 if nx_immune_count_cells_in_status(i, NX_COL_COMPROMISED) != 1 { return 22 }
61 if nx_immune_count_cells_in_status(i, NX_COL_HEALTHY) != 0 { return 23 }
62
63 // 12: should_quarantine_host -- all 3 cells are non-healthy
64 if nx_immune_should_quarantine_host(i) != 1 { return 24 }
65
66 // 13: clonal expansion -- cell 100 has an antibody, broadcast to others
67 let src_idx: nx_int = nx_antibody_generate(ab1,
68 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0xfeed, 870, 4000)
69 if src_idx < 0 { return 25 }
70 let distributed: nx_int = nx_immune_clonal_expand(i, 100,
71 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0xfeed, 870, 4100)
72 // Should distribute to cells 200 and 300 (the two non-source cells)
73 if distributed != 2 { return 26 }
74 if ab2.count != 1 { return 27 }
75 if ab3.count != 1 { return 28 }
76
77 // 14: re-running clonal_expand for same signature does NOT
78 // re-distribute (other cells already have it)
79 let redist: nx_int = nx_immune_clonal_expand(i, 100,
80 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0xfeed, 870, 5000)
81 if redist != 0 { return 29 }
82
83 // 15: fresh immune with all healthy cells -- no quarantine
84 let i2: *NxImmune = nx_immune_new(4, j)
85 let ab_h1: *NxAntibodyArray = nx_antibody_array_new(4)
86 let ab_h2: *NxAntibodyArray = nx_antibody_array_new(4)
87 nx_immune_register_cell(i2, 1, ab_h1)
88 nx_immune_register_cell(i2, 2, ab_h2)
89 if nx_immune_should_quarantine_host(i2) != 0 { return 30 }
90 if nx_immune_global_status(i2) != NX_COL_HEALTHY { return 31 }
91
92 return 0
93}