nx_substrate_tier_audit_test.nx source
↩ module page · 205 lines · 6530 B
1// nx_substrate_tier_audit_test.nx -- smoke for tier-cardinal audit.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_substrate_tier_audit.nx"
6
7func main() -> nx_int {
8 let rep: *TierAuditReport = (sys_mmap(16 * NX_SIZEOF_NX_INT)) as *TierAuditReport
9
10 // === Test 1: clean file -> CLEAN verdict ===
11 // Source: just a comment + one line using nx_int + tier import.
12 // "import \"nx_tier.nx\"\nlet x: nx_int = 5\n"
13 let buf_clean: *u8 = sys_mmap(64)
14 buf_clean[0] = 105 // i
15 buf_clean[1] = 109 // m
16 buf_clean[2] = 112 // p
17 buf_clean[3] = 111 // o
18 buf_clean[4] = 114 // r
19 buf_clean[5] = 116 // t
20 buf_clean[6] = 32 //
21 buf_clean[7] = 34 // "
22 buf_clean[8] = 110 // n
23 buf_clean[9] = 120 // x
24 buf_clean[10] = 95 // _
25 buf_clean[11] = 116 // t
26 buf_clean[12] = 105 // i
27 buf_clean[13] = 101 // e
28 buf_clean[14] = 114 // r
29 buf_clean[15] = 46 // .
30 buf_clean[16] = 110 // n
31 buf_clean[17] = 120 // x
32 buf_clean[18] = 34 // "
33 buf_clean[19] = 10 // \n
34 buf_clean[20] = 108 // l
35 buf_clean[21] = 101 // e
36 buf_clean[22] = 116 // t
37 buf_clean[23] = 32
38 buf_clean[24] = 120 // x
39 buf_clean[25] = 58 // :
40 buf_clean[26] = 32
41 buf_clean[27] = 110 // n
42 buf_clean[28] = 120 // x
43 buf_clean[29] = 95 // _
44 buf_clean[30] = 105 // i
45 buf_clean[31] = 110 // n
46 buf_clean[32] = 116 // t
47 buf_clean[33] = 32
48 buf_clean[34] = 61 // =
49 buf_clean[35] = 32
50 buf_clean[36] = 53 // 5
51 buf_clean[37] = 10
52 nx_substrate_tier_audit(buf_clean, 38, rep)
53 if rep.verdict != NX_TIERAUDIT_CLEAN { return 1 }
54 if rep.bare_i64_count != 0 { return 2 }
55 if rep.missing_tier_present != 0 { return 3 }
56
57 // === Test 2: bare i64 in arithmetic context (no `*` prefix) -> violation ===
58 // "let y: i64 = 7\n"
59 let buf_i64: *u8 = sys_mmap(64)
60 buf_i64[0] = 108
61 buf_i64[1] = 101
62 buf_i64[2] = 116
63 buf_i64[3] = 32
64 buf_i64[4] = 121 // y
65 buf_i64[5] = 58
66 buf_i64[6] = 32
67 buf_i64[7] = 105 // i
68 buf_i64[8] = 54 // 6
69 buf_i64[9] = 52 // 4
70 buf_i64[10] = 32
71 buf_i64[11] = 61
72 buf_i64[12] = 32
73 buf_i64[13] = 55 // 7
74 buf_i64[14] = 10
75 nx_substrate_tier_audit(buf_i64, 15, rep)
76 if rep.bare_i64_count < 1 { return 10 }
77 if rep.verdict == NX_TIERAUDIT_CLEAN { return 11 }
78 if rep.first_bare_i64_line != 1 { return 12 }
79
80 // === Test 3: *i64 pointer (NOT a violation; storage shape) ===
81 // "let p: *i64 = 0 as *i64\n"
82 let buf_ptr: *u8 = sys_mmap(64)
83 buf_ptr[0] = 108
84 buf_ptr[1] = 101
85 buf_ptr[2] = 116
86 buf_ptr[3] = 32
87 buf_ptr[4] = 112 // p
88 buf_ptr[5] = 58
89 buf_ptr[6] = 32
90 buf_ptr[7] = 42 // *
91 buf_ptr[8] = 105 // i
92 buf_ptr[9] = 54 // 6
93 buf_ptr[10] = 52 // 4
94 buf_ptr[11] = 32
95 buf_ptr[12] = 61
96 buf_ptr[13] = 32
97 buf_ptr[14] = 48
98 buf_ptr[15] = 32
99 buf_ptr[16] = 97 // a
100 buf_ptr[17] = 115 // s
101 buf_ptr[18] = 32
102 buf_ptr[19] = 42 // *
103 buf_ptr[20] = 105 // i
104 buf_ptr[21] = 54 // 6
105 buf_ptr[22] = 52 // 4
106 buf_ptr[23] = 10
107 nx_substrate_tier_audit(buf_ptr, 24, rep)
108 // Both occurrences are preceded by '*' -> not counted.
109 if rep.bare_i64_count != 0 { return 20 }
110
111 // === Test 4: `* 8` near sys_mmap -> hardcoded-bytes violation ===
112 // "sys_mmap(n * 8)\n"
113 let buf_mul8: *u8 = sys_mmap(64)
114 buf_mul8[0] = 115 // s
115 buf_mul8[1] = 121 // y
116 buf_mul8[2] = 115 // s
117 buf_mul8[3] = 95 // _
118 buf_mul8[4] = 109 // m
119 buf_mul8[5] = 109 // m
120 buf_mul8[6] = 97 // a
121 buf_mul8[7] = 112 // p
122 buf_mul8[8] = 40 // (
123 buf_mul8[9] = 110 // n
124 buf_mul8[10] = 32
125 buf_mul8[11] = 42 // *
126 buf_mul8[12] = 32
127 buf_mul8[13] = 56 // 8
128 buf_mul8[14] = 41 // )
129 buf_mul8[15] = 10
130 nx_substrate_tier_audit(buf_mul8, 16, rep)
131 if rep.hardcoded_bytes_count < 1 { return 30 }
132 if rep.first_hardcoded_line != 1 { return 31 }
133
134 // === Test 5: bare numeric literal in sys_mmap -> bare-mmap violation ===
135 // "sys_mmap(96)\n"
136 let buf_mmap_lit: *u8 = sys_mmap(64)
137 buf_mmap_lit[0] = 115
138 buf_mmap_lit[1] = 121
139 buf_mmap_lit[2] = 115
140 buf_mmap_lit[3] = 95
141 buf_mmap_lit[4] = 109
142 buf_mmap_lit[5] = 109
143 buf_mmap_lit[6] = 97
144 buf_mmap_lit[7] = 112
145 buf_mmap_lit[8] = 40
146 buf_mmap_lit[9] = 57 // 9
147 buf_mmap_lit[10] = 54 // 6
148 buf_mmap_lit[11] = 41 // )
149 buf_mmap_lit[12] = 10
150 nx_substrate_tier_audit(buf_mmap_lit, 13, rep)
151 if rep.bare_mmap_literal_count < 1 { return 40 }
152
153 // === Test 6: file uses nx_int but no import "nx_tier.nx" ===
154 // "let x: nx_int = 5\n" (no import line)
155 let buf_no_import: *u8 = sys_mmap(64)
156 buf_no_import[0] = 108
157 buf_no_import[1] = 101
158 buf_no_import[2] = 116
159 buf_no_import[3] = 32
160 buf_no_import[4] = 120 // x
161 buf_no_import[5] = 58
162 buf_no_import[6] = 32
163 buf_no_import[7] = 110 // n
164 buf_no_import[8] = 120 // x
165 buf_no_import[9] = 95 // _
166 buf_no_import[10] = 105 // i
167 buf_no_import[11] = 110 // n
168 buf_no_import[12] = 116 // t
169 buf_no_import[13] = 32
170 buf_no_import[14] = 61
171 buf_no_import[15] = 32
172 buf_no_import[16] = 53
173 buf_no_import[17] = 10
174 nx_substrate_tier_audit(buf_no_import, 18, rep)
175 if rep.missing_tier_present != 1 { return 50 }
176 if rep.verdict == NX_TIERAUDIT_CLEAN { return 51 }
177
178 // === Test 7: comment with i64 doesn't trigger ===
179 // "// i64 stays bare here\n"
180 let buf_comment: *u8 = sys_mmap(64)
181 buf_comment[0] = 47 // /
182 buf_comment[1] = 47 // /
183 buf_comment[2] = 32
184 buf_comment[3] = 105
185 buf_comment[4] = 54
186 buf_comment[5] = 52
187 buf_comment[6] = 32
188 buf_comment[7] = 115 // s
189 buf_comment[8] = 116
190 buf_comment[9] = 97
191 buf_comment[10] = 121
192 buf_comment[11] = 115
193 buf_comment[12] = 10
194 nx_substrate_tier_audit(buf_comment, 13, rep)
195 if rep.bare_i64_count != 0 { return 60 }
196
197 // === Test 8: sealed-enum validity ===
198 if nx_substrate_tier_audit_verdict_is_valid(NX_TIERAUDIT_CLEAN) != 1 { return 70 }
199 if nx_substrate_tier_audit_verdict_is_valid(NX_TIERAUDIT_NEEDS_REFACTOR) != 1 { return 71 }
200 if nx_substrate_tier_audit_verdict_is_valid(99) != 0 { return 72 }
201 if nx_substrate_tier_audit_axis_is_valid(NX_TIERAUDIT_AXIS_HARDCODED_BYTES) != 1 { return 73 }
202 if nx_substrate_tier_audit_axis_is_valid(99) != 0 { return 74 }
203
204 return 0
205}