omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / core / sovereign-core / om / runtime / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/sovereign-core/om/runtime/test.om |
| 3 | note Comprehensive behavioral test suite with negative/refusal controls for the |
| 4 | note pure-Omega aien-runtime, inference-abi, capability membrane, and execution journal. |
| 5 | note Compose with: |
| 6 | note si/directives/om2.om core/sovereign-core/om/runtime/inference_abi.om om/tls/sha2.om |
| 7 | note core/sovereign-core/om/runtime/membrane.om core/sovereign-core/om/runtime/test.om |
| 8 | |
| 9 | text t_pass "PASS " |
| 10 | text t_fail "FAIL " |
| 11 | text t_nl "\n" |
| 12 | text t_all_ok "SOVEREIGN-CORE RUNTIME TEST SUITE PASS\n" |
| 13 | text t_all_bad "SOVEREIGN-CORE RUNTIME TEST SUITE FAIL\n" |
| 14 | |
| 15 | text t_n1 "abi_frame_roundtrip_and_negative" |
| 16 | text t_n2 "abi_sampling_and_stop_token_guard" |
| 17 | text t_n3 "world_store_fork_and_no_escalation" |
| 18 | text t_n4 "controller_idempotency_and_negative" |
| 19 | text t_n5 "membrane_authorize_and_containment_freeze" |
| 20 | text t_n6 "journal_sha256_chain_and_negative_tamper" |
| 21 | |
| 22 | global t_fails ulong 1 |
| 23 | |
| 24 | fn t_w s view uchar * to void |
| 25 | n = len s |
| 26 | off = ulong 0 |
| 27 | while lt off n bound 256 |
| 28 | k = write 1 s off sub n off |
| 29 | if le k 0 |
| 30 | break |
| 31 | end |
| 32 | off = add off ulong k |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | fn t_report name view uchar * ok bool to void |
| 37 | if ok |
| 38 | t_w t_pass |
| 39 | else |
| 40 | t_w t_fail |
| 41 | put t_fails 0 add get t_fails 0 1 |
| 42 | end |
| 43 | t_w name |
| 44 | t_w t_nl |
| 45 | end |
| 46 | |
| 47 | note Test 1: InferenceReqFrame & DecodeOutFrame wire roundtrip + negative magic/ver/len/bounds. |
| 48 | fn test_abi_frames to bool |
| 49 | req = new InferenceReqFrame 42 7 128 256 2 3 |
| 50 | wire = new uchar 48 0 |
| 51 | if ne abi_encode_req req wire ABI_OK |
| 52 | ret false |
| 53 | end |
| 54 | dec = new InferenceReqFrame 0 0 0 0 0 0 |
| 55 | if ne abi_decode_req wire dec ABI_OK |
| 56 | ret false |
| 57 | end |
| 58 | if or ne get dec req_id 42 or ne get dec model_handle 7 ne get dec prompt_tokens 128 |
| 59 | ret false |
| 60 | end |
| 61 | if or ne get dec max_tokens 256 or ne get dec priority 2 ne get dec flags 3 |
| 62 | ret false |
| 63 | end |
| 64 | note NEGATIVE: truncated buffer, bad magic, bad version, max_tokens=0 |
| 65 | if ne abi_decode_req part wire 0 30 dec ABI_ERR_TRUNCATED |
| 66 | ret false |
| 67 | end |
| 68 | put wire 0 xor get wire 0 uchar 1 |
| 69 | if ne abi_decode_req wire dec ABI_ERR_BAD_MAGIC |
| 70 | ret false |
| 71 | end |
| 72 | put wire 0 xor get wire 0 uchar 1 |
| 73 | put wire 4 uchar 99 |
| 74 | if ne abi_decode_req wire dec ABI_ERR_BAD_VERSION |
| 75 | ret false |
| 76 | end |
| 77 | req_bad = new InferenceReqFrame 1 1 10 0 1 0 |
| 78 | if ne abi_encode_req req_bad wire ABI_ERR_BAD_LIMITS |
| 79 | ret false |
| 80 | end |
| 81 | note DecodeOutFrame roundtrip + bad finish_reason negative |
| 82 | out_f = new DecodeOutFrame 42 151645 FINISH_STOP_TOKEN 64 |
| 83 | owire = new uchar 32 0 |
| 84 | if ne abi_encode_out out_f owire ABI_OK |
| 85 | ret false |
| 86 | end |
| 87 | odec = new DecodeOutFrame 0 0 0 0 |
| 88 | if ne abi_decode_out owire odec ABI_OK |
| 89 | ret false |
| 90 | end |
| 91 | if ne get odec req_id 42 |
| 92 | ret false |
| 93 | end |
| 94 | if ne get odec token_id 151645 |
| 95 | ret false |
| 96 | end |
| 97 | if ne get odec finish_reason FINISH_STOP_TOKEN |
| 98 | ret false |
| 99 | end |
| 100 | if ne get odec total_tokens 64 |
| 101 | ret false |
| 102 | end |
| 103 | out_bad = new DecodeOutFrame 42 1 99 1 |
| 104 | if ne abi_encode_out out_bad owire ABI_ERR_BAD_LIMITS |
| 105 | ret false |
| 106 | end |
| 107 | ret true |
| 108 | end |
| 109 | |
| 110 | note Test 2: SamplingParams validation (incl. NaN/range) and StopTokenGuard state machine. |
| 111 | fn test_sampling_and_stop to bool |
| 112 | sp = new SamplingParams 0.7f32 0.95f32 3 40 2 uint 2 uint 151645 uint 0 uint 0 |
| 113 | if ne abi_validate_sampling sp ABI_OK |
| 114 | ret false |
| 115 | end |
| 116 | note NEGATIVE: NaN temperature, temp > 2.0, top_p == 0.0, top_p > 1.0, max_tokens == 0 |
| 117 | nan_f = bitcast f32 uint 0x7fc00000 |
| 118 | sp_nan = new SamplingParams nan_f 0.9f32 16 40 0 uint 0 uint 0 uint 0 uint 0 |
| 119 | if ne abi_validate_sampling sp_nan ABI_ERR_BAD_TEMP |
| 120 | ret false |
| 121 | end |
| 122 | sp_hi = new SamplingParams 2.5f32 0.9f32 16 40 0 uint 0 uint 0 uint 0 uint 0 |
| 123 | if ne abi_validate_sampling sp_hi ABI_ERR_BAD_TEMP |
| 124 | ret false |
| 125 | end |
| 126 | sp_tp0 = new SamplingParams 0.7f32 0.0f32 16 40 0 uint 0 uint 0 uint 0 uint 0 |
| 127 | if ne abi_validate_sampling sp_tp0 ABI_ERR_BAD_TOP_P |
| 128 | ret false |
| 129 | end |
| 130 | sp_tp2 = new SamplingParams 0.7f32 1.1f32 16 40 0 uint 0 uint 0 uint 0 uint 0 |
| 131 | if ne abi_validate_sampling sp_tp2 ABI_ERR_BAD_TOP_P |
| 132 | ret false |
| 133 | end |
| 134 | note StopGuard: hits stop token 151645 on step 2, then refuses step 3 |
| 135 | g = new StopGuard 0 0 0 0 0 uint 0 uint 0 uint 0 uint 0 |
| 136 | if ne abi_stop_guard_init g sp ABI_OK |
| 137 | ret false |
| 138 | end |
| 139 | if ne abi_stop_guard_step g uint 100 ABI_OK |
| 140 | ret false |
| 141 | end |
| 142 | if ne get g finished 0 |
| 143 | ret false |
| 144 | end |
| 145 | if ne abi_stop_guard_step g uint 151645 ABI_OK |
| 146 | ret false |
| 147 | end |
| 148 | if or ne get g finished 1 ne get g reason FINISH_STOP_TOKEN |
| 149 | ret false |
| 150 | end |
| 151 | if ne abi_stop_guard_step g uint 101 ABI_ERR_ALREADY_FINISHED |
| 152 | ret false |
| 153 | end |
| 154 | note StopGuard: length finish after 3 non-stop tokens |
| 155 | if ne abi_stop_guard_init g sp ABI_OK |
| 156 | ret false |
| 157 | end |
| 158 | if ne abi_stop_guard_step g uint 10 ABI_OK |
| 159 | ret false |
| 160 | end |
| 161 | if ne abi_stop_guard_step g uint 11 ABI_OK |
| 162 | ret false |
| 163 | end |
| 164 | if ne abi_stop_guard_step g uint 12 ABI_OK |
| 165 | ret false |
| 166 | end |
| 167 | if or ne get g finished 1 ne get g reason FINISH_LENGTH |
| 168 | ret false |
| 169 | end |
| 170 | ret true |
| 171 | end |
| 172 | |
| 173 | note Test 3: Persistent WorldStore fork & strict capability non-escalation. |
| 174 | fn test_world_fork_and_escalation to bool |
| 175 | rt_init |
| 176 | pcaps = or RT_CAP_INFER or RT_CAP_FORK_WORLD RT_CAP_READ_FS |
| 177 | root = rt_world_create_root 1001 pcaps 500 |
| 178 | if eq root 0 |
| 179 | ret false |
| 180 | end |
| 181 | note Attenuated child fork succeeds |
| 182 | child = rt_world_fork root RT_CAP_INFER 501 |
| 183 | if or eq child 0 ne rt_world_parent child root |
| 184 | ret false |
| 185 | end |
| 186 | if ne rt_world_caps child RT_CAP_INFER |
| 187 | ret false |
| 188 | end |
| 189 | note NEGATIVE: child without RT_CAP_FORK_WORLD cannot fork again |
| 190 | if ne rt_world_fork child RT_CAP_INFER 502 0 |
| 191 | ret false |
| 192 | end |
| 193 | note NEGATIVE: requesting RT_CAP_EXEC_CMD not held by parent is refused (anti-escalation) |
| 194 | if ne rt_world_fork root or RT_CAP_INFER RT_CAP_EXEC_CMD 503 0 |
| 195 | ret false |
| 196 | end |
| 197 | note NEGATIVE: unknown parent refused |
| 198 | if ne rt_world_fork 999 RT_CAP_INFER 504 0 |
| 199 | ret false |
| 200 | end |
| 201 | if ne rt_world_count 2 |
| 202 | ret false |
| 203 | end |
| 204 | ret true |
| 205 | end |
| 206 | |
| 207 | note Test 4: RuntimeController idempotency & protocol version validation. |
| 208 | fn test_controller_idempotency to bool |
| 209 | rt_init |
| 210 | if ne rt_ctrl_begin_op 1 777 RT_OK |
| 211 | ret false |
| 212 | end |
| 213 | if not rt_ctrl_is_processed 777 |
| 214 | ret false |
| 215 | end |
| 216 | note NEGATIVE: replay of operation_id 777 refused |
| 217 | if ne rt_ctrl_begin_op 1 777 RT_ERR_DUPLICATE_OP |
| 218 | ret false |
| 219 | end |
| 220 | note NEGATIVE: wrong protocol version or op_id 0 refused |
| 221 | if ne rt_ctrl_begin_op 2 778 RT_ERR_BAD_PROTO |
| 222 | ret false |
| 223 | end |
| 224 | if ne rt_ctrl_begin_op 1 0 RT_ERR_BAD_PROTO |
| 225 | ret false |
| 226 | end |
| 227 | ret true |
| 228 | end |
| 229 | |
| 230 | note Test 5: Capability membrane authorization, denial, and containment freeze. |
| 231 | fn test_membrane_and_freeze to bool |
| 232 | rt_init |
| 233 | w1 = rt_world_create_root 10 or RT_CAP_INFER RT_CAP_ADMIN 100 |
| 234 | w2 = rt_world_create_root 20 RT_CAP_INFER 101 |
| 235 | note Authorized infer succeeds |
| 236 | if ne rt_authorize_effect 1 1001 w1 RT_EFF_INFER RT_OK |
| 237 | ret false |
| 238 | end |
| 239 | note NEGATIVE: unauthorized exec_cmd denied |
| 240 | if ne rt_authorize_effect 1 1002 w2 RT_EFF_EXEC_CMD RT_ERR_UNAUTHORIZED |
| 241 | ret false |
| 242 | end |
| 243 | note NEGATIVE: unknown world denied |
| 244 | if ne rt_authorize_effect 1 1003 999 RT_EFF_INFER RT_ERR_NOT_FOUND |
| 245 | ret false |
| 246 | end |
| 247 | note Enable emergency containment freeze -> non-admin infer denied, admin still allowed |
| 248 | rt_set_freeze 1 |
| 249 | if ne rt_authorize_effect 1 1004 w2 RT_EFF_INFER RT_ERR_CONTAINED |
| 250 | ret false |
| 251 | end |
| 252 | if ne rt_authorize_effect 1 1005 w1 RT_EFF_ADMIN RT_OK |
| 253 | ret false |
| 254 | end |
| 255 | if ne rt_journal_count 5 |
| 256 | ret false |
| 257 | end |
| 258 | if ne rt_journal_verify_chain RT_OK |
| 259 | ret false |
| 260 | end |
| 261 | ret true |
| 262 | end |
| 263 | |
| 264 | note Test 6 (NEGATIVE): SHA-256 execution journal chain verification catches status & prev_hash tampering. |
| 265 | fn test_journal_tamper_negative to bool |
| 266 | rt_init |
| 267 | w = rt_world_create_root 1 RT_CAP_INFER 1 |
| 268 | for i ulong 4 |
| 269 | if ne rt_authorize_effect 1 add ulong 500 i w RT_EFF_INFER RT_OK |
| 270 | ret false |
| 271 | end |
| 272 | end |
| 273 | if ne rt_journal_verify_chain RT_OK |
| 274 | ret false |
| 275 | end |
| 276 | note Tamper with record 1 status -> must fail with RT_ERR_JRN_HASH |
| 277 | rt_jrn_tamper_status 1 RT_ERR_UNAUTHORIZED |
| 278 | if ne rt_journal_verify_chain RT_ERR_JRN_HASH |
| 279 | ret false |
| 280 | end |
| 281 | note Restore status, then tamper with record 2 prev_hash -> must fail with RT_ERR_JRN_PREV |
| 282 | rt_jrn_tamper_status 1 RT_OK |
| 283 | if ne rt_journal_verify_chain RT_OK |
| 284 | ret false |
| 285 | end |
| 286 | rt_jrn_tamper_prev 2 5 uchar 0x40 |
| 287 | if ne rt_journal_verify_chain RT_ERR_JRN_PREV |
| 288 | ret false |
| 289 | end |
| 290 | ret true |
| 291 | end |
| 292 | |
| 293 | fn main to int |
| 294 | put t_fails 0 0 |
| 295 | t_report t_n1 test_abi_frames |
| 296 | t_report t_n2 test_sampling_and_stop |
| 297 | t_report t_n3 test_world_fork_and_escalation |
| 298 | t_report t_n4 test_controller_idempotency |
| 299 | t_report t_n5 test_membrane_and_freeze |
| 300 | t_report t_n6 test_journal_tamper_negative |
| 301 | if eq get t_fails 0 0 |
| 302 | t_w t_all_ok |
| 303 | ret 0 |
| 304 | end |
| 305 | t_w t_all_bad |
| 306 | ret 1 |
| 307 | end |