nx_crispr_test.nx source
↩ module page · 79 lines · 2955 B
1// nx_crispr_test.nx -- smoke for nx_crispr.
2
3import "nx_syscalls.nx"
4import "nx_pamp.nx"
5import "nx_crispr.nx"
6
7func main() -> i64 {
8 // 1: construction
9 let a: *NxCrisprArray = nx_crispr_new(8)
10 if a.capacity != 8 { return 1 }
11 if a.head != 0 { return 2 }
12 if a.count != 0 { return 3 }
13
14 let out_idx: *i64 = (sys_mmap(8)) as *i64
15
16 // 2: miss on empty
17 if nx_crispr_match(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH,
18 0x12345, out_idx) != NX_CRISPR_MISS { return 4 }
19 if out_idx[0] != -1 { return 5 }
20
21 // 3: remember a homoglyph signature
22 nx_crispr_remember(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH,
23 0x12345, 1000)
24 if a.count != 1 { return 6 }
25
26 // 4: now hits
27 if nx_crispr_match(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH,
28 0x12345, out_idx) != NX_CRISPR_HIT { return 7 }
29 if out_idx[0] != 0 { return 8 }
30
31 // 5: different hash misses
32 if nx_crispr_match(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH,
33 0x67890, out_idx) != NX_CRISPR_MISS { return 9 }
34
35 // 6: different kind same hash misses
36 if nx_crispr_match(a, NX_PAMP_ENTROPY_ANOMALY,
37 0x12345, out_idx) != NX_CRISPR_MISS { return 10 }
38
39 // 7: remember + touch updates hit_count
40 nx_crispr_remember(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH,
41 0x12345, 2000)
42 if a.count != 1 { return 11 } // not double-counted
43 // The remember-on-existing should have bumped hit_count via the
44 // internal touch path. Look it up.
45 nx_crispr_match(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0x12345, out_idx)
46 let idx: nx_size = out_idx[0] as nx_size
47 let s: *NxCrisprSpacer = (a.spacers as i64 + (idx as i64) * 40) as *NxCrisprSpacer
48 if s.hit_count != 2 { return 12 }
49 if s.last_seen_us != 2000 { return 13 }
50
51 // 8: explicit touch increments
52 nx_crispr_touch(a, idx, 3000)
53 if s.hit_count != 3 { return 14 }
54 if s.last_seen_us != 3000 { return 15 }
55
56 // 9: count_kind tallies across signatures
57 nx_crispr_remember(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0xaaa, 4000)
58 nx_crispr_remember(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0xbbb, 4000)
59 nx_crispr_remember(a, NX_PAMP_ENTROPY_ANOMALY, 0xccc, 4000)
60 if nx_crispr_count_kind(a, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH) != 3 { return 16 }
61 if nx_crispr_count_kind(a, NX_PAMP_ENTROPY_ANOMALY) != 1 { return 17 }
62 if nx_crispr_count_kind(a, NX_PAMP_KNOWN_BAD_EGRESS) != 0 { return 18 }
63
64 // 10: total hits = sum of hit_count
65 // first signature has 3, others have 1 each = 6 total
66 if nx_crispr_total_hits(a) != 6 { return 19 }
67
68 // 11: ring wraps cleanly at capacity
69 var i: nx_int = 0
70 while i < 12 {
71 nx_crispr_remember(a, NX_PAMP_KNOWN_BAD_EGRESS,
72 10000 + i, 5000)
73 i = i + 1
74 }
75 if a.count != 16 { return 20 } // 4 prior + 12 here
76 if a.head >= a.capacity { return 21 }
77
78 return 0
79}