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 / protocol / open-humanity / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/open-humanity/test.om: comprehensive positive and negative control tests for |
| 3 | note protocol/open-humanity/lib.om. |
| 4 | note Compose with: si/directives/om2.om protocol/open-humanity/lib.om protocol/open-humanity/test.om |
| 5 | |
| 6 | text oht_pass "PASS " |
| 7 | text oht_fail "FAIL " |
| 8 | text oht_nl "\n" |
| 9 | text oht_sum_ok "OPEN_HUMANITY_TEST PASS passed=" |
| 10 | text oht_sum_fl "OPEN_HUMANITY_TEST FAIL passed=" |
| 11 | text oht_fld " failed=" |
| 12 | |
| 13 | text oht_n1 "wire_roundtrip_and_tamper_refusal" |
| 14 | text oht_n2 "opt_in_distress_routing_and_resolution" |
| 15 | text oht_n3 "opt_out_refusal_outbound_and_inbound" |
| 16 | text oht_n4 "ttl_expiry_and_invalid_ttl_refusal" |
| 17 | text oht_n5 "privacy_firewall_secrets_refusal" |
| 18 | text oht_n6 "privacy_firewall_paths_and_obfuscation" |
| 19 | text oht_n7 "privacy_path_scrubber_sanitizes_safely" |
| 20 | text oht_n8 "authority_boundary_priv_esc_refusal" |
| 21 | text oht_n9 "replay_self_resolve_and_queue_bound" |
| 22 | |
| 23 | text oht_clean_q "E0425 cannot find value x in crate::module at line 42" |
| 24 | text oht_clean_a "Add use crate::module::x before invoking the helper function" |
| 25 | text oht_aws "leak AKIA1234567890ABCDEF in build log" |
| 26 | text oht_gh "token ghp_0123456789abcdefghijklmnopqrstuvwxyz found" |
| 27 | text oht_ant "key sk-ant-abcdefghijklmnopqrstuvwx leaked" |
| 28 | text oht_oai "key sk-proj-0123456789abcdefghijklm leaked" |
| 29 | text oht_pem "-----BEGIN RSA PRIVATE KEY----- body" |
| 30 | text oht_gen "config api_key = \"supersecretvalue123456\" here" |
| 31 | text oht_upath "error at /Users/alice/private/main.rs:10" |
| 32 | text oht_hpath "error at /home/bob/work/main.rs:10" |
| 33 | text oht_wpath "error at C:\\Users\\eve\\proj\\main.rs" |
| 34 | text oht_trav "include ../../etc/shadow failed" |
| 35 | text oht_pct "obfuscated %41KIA1234567890ABCDEF secret" |
| 36 | text oht_scrub_want "error at ~/private/main.rs:10" |
| 37 | text oht_esc_sudo "try running sudo rm -rf /tmp/cache" |
| 38 | text oht_esc_id "export OPEN_HUMANITY_IDENTITY from host" |
| 39 | text oht_esc_exec "exec: /bin/sh to inspect" |
| 40 | |
| 41 | fn oht_rec name view uchar * ok bool tally edit ulong 2 to void |
| 42 | w = false |
| 43 | if ok |
| 44 | put tally 0 add get tally 0 1 |
| 45 | w = oh_write_all 1 oht_pass |
| 46 | else |
| 47 | put tally 1 add get tally 1 1 |
| 48 | w = oh_write_all 1 oht_fail |
| 49 | end |
| 50 | w = and w oh_write_all 1 name |
| 51 | w = and w oh_write_all 1 oht_nl |
| 52 | if w |
| 53 | ret |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | fn oht_bytes_eq a view uchar * b view uchar * to bool |
| 58 | if ne len a len b |
| 59 | ret false |
| 60 | end |
| 61 | n = len a |
| 62 | i = ulong 0 |
| 63 | while lt i n bound 1024 |
| 64 | if ne get a i get b i |
| 65 | ret false |
| 66 | end |
| 67 | i = add i 1 |
| 68 | end |
| 69 | ret true |
| 70 | end |
| 71 | |
| 72 | fn test_wire_roundtrip to bool |
| 73 | env = new OhEnv OH_VERSION 777 1 OH_CAT_RUST 1000 60000 0 0 0 |
| 74 | wire = new uchar 560 0 |
| 75 | wn = oh_serialize_envelope env oht_clean_q wire |
| 76 | if eq wn 0 |
| 77 | ret false |
| 78 | end |
| 79 | if ne wn add OH_HDR_LEN len oht_clean_q |
| 80 | ret false |
| 81 | end |
| 82 | parsed = new OhEnv 0 0 0 0 0 0 0 0 0 |
| 83 | pbuf = new uchar 512 0 |
| 84 | rc = oh_parse_envelope part wire 0 wn parsed pbuf |
| 85 | if ne rc OH_OK |
| 86 | ret false |
| 87 | end |
| 88 | if ne get parsed peer_id 777 |
| 89 | ret false |
| 90 | end |
| 91 | if ne get parsed consent 1 |
| 92 | ret false |
| 93 | end |
| 94 | if ne get parsed category OH_CAT_RUST |
| 95 | ret false |
| 96 | end |
| 97 | if ne get parsed ttl_ms 60000 |
| 98 | ret false |
| 99 | end |
| 100 | if not oht_bytes_eq part pbuf 0 get parsed payload_len oht_clean_q |
| 101 | ret false |
| 102 | end |
| 103 | note Negative control: flip 1 byte in payload -> BAD_DIGEST |
| 104 | put wire OH_HDR_LEN xor get wire OH_HDR_LEN 1 |
| 105 | rc_tamper = oh_parse_envelope part wire 0 wn parsed pbuf |
| 106 | if ne rc_tamper OH_ERR_BAD_DIGEST |
| 107 | ret false |
| 108 | end |
| 109 | note Negative control: bad version -> BAD_VERSION |
| 110 | put wire 2 uchar 9 |
| 111 | rc_ver = oh_parse_envelope part wire 0 wn parsed pbuf |
| 112 | if ne rc_ver OH_ERR_BAD_VERSION |
| 113 | ret false |
| 114 | end |
| 115 | note Negative control: bad magic -> BAD_MAGIC |
| 116 | put wire 0 uchar 0 |
| 117 | rc_mag = oh_parse_envelope part wire 0 wn parsed pbuf |
| 118 | ret eq rc_mag OH_ERR_BAD_MAGIC |
| 119 | end |
| 120 | |
| 121 | fn test_optin_routing_resolution to bool |
| 122 | pol = new OhPolicy 0 0 1 0 0 |
| 123 | oh_reset_state pol |
| 124 | put pol opt_in ulong 1 |
| 125 | put pol now_ms ulong 5000 |
| 126 | env = new OhEnv OH_VERSION 101 1 OH_CAT_RUST 4000 60000 0 0 0 |
| 127 | req = new ulong 1 0 |
| 128 | rc1 = oh_submit_distress pol env oht_clean_q req |
| 129 | if ne rc1 OH_OK |
| 130 | ret false |
| 131 | end |
| 132 | rid = get req 0 |
| 133 | if ne rid 1 |
| 134 | ret false |
| 135 | end |
| 136 | if ne oh_queue_pending_count 1 |
| 137 | ret false |
| 138 | end |
| 139 | helper = new OhEnv OH_VERSION 202 1 OH_CAT_RUST 5000 60000 0 0 0 |
| 140 | rc2 = oh_resolve_distress pol rid helper oht_clean_a |
| 141 | if ne rc2 OH_OK |
| 142 | ret false |
| 143 | end |
| 144 | if ne oh_queue_pending_count 0 |
| 145 | ret false |
| 146 | end |
| 147 | note Audit trail must record ENQUEUED then RESOLVED |
| 148 | if ne oh_audit_count 2 |
| 149 | ret false |
| 150 | end |
| 151 | if ne get oh_aud_kind 0 OH_AUD_ENQUEUED |
| 152 | ret false |
| 153 | end |
| 154 | if ne get oh_aud_kind 1 OH_AUD_RESOLVED |
| 155 | ret false |
| 156 | end |
| 157 | ret eq get oh_aud_peer 1 202 |
| 158 | end |
| 159 | |
| 160 | fn test_opt_out_refusals to bool |
| 161 | pol = new OhPolicy 0 0 1 0 0 |
| 162 | oh_reset_state pol |
| 163 | put pol now_ms ulong 5000 |
| 164 | env = new OhEnv OH_VERSION 101 1 OH_CAT_RUST 4500 60000 0 0 0 |
| 165 | req = new ulong 1 0 |
| 166 | note Node opt-in is 0 by default -> MUST refuse outbound distress |
| 167 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_NODE_OPT_OUT |
| 168 | ret false |
| 169 | end |
| 170 | note Enable node opt-in, but envelope consent_flag=0 -> MUST refuse |
| 171 | put pol opt_in ulong 1 |
| 172 | put env consent ulong 0 |
| 173 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_NO_CONSENT |
| 174 | ret false |
| 175 | end |
| 176 | note Submit valid request with consent=1 |
| 177 | put env consent ulong 1 |
| 178 | if ne oh_submit_distress pol env oht_clean_q req OH_OK |
| 179 | ret false |
| 180 | end |
| 181 | note Now opt out node -> inbound resolution MUST be refused |
| 182 | put pol opt_in ulong 0 |
| 183 | helper = new OhEnv OH_VERSION 202 1 OH_CAT_RUST 5000 60000 0 0 0 |
| 184 | ret eq oh_resolve_distress pol get req 0 helper oht_clean_a OH_ERR_NODE_OPT_OUT |
| 185 | end |
| 186 | |
| 187 | fn test_ttl_refusals to bool |
| 188 | pol = new OhPolicy 0 0 1 0 0 |
| 189 | oh_reset_state pol |
| 190 | put pol opt_in ulong 1 |
| 191 | put pol now_ms ulong 10000 |
| 192 | req = new ulong 1 0 |
| 193 | note Zero TTL refused |
| 194 | e0 = new OhEnv OH_VERSION 101 1 OH_CAT_SIMD 10000 0 0 0 0 |
| 195 | if ne oh_submit_distress pol e0 oht_clean_q req OH_ERR_TTL_INVALID |
| 196 | ret false |
| 197 | end |
| 198 | note Oversized TTL (> 7 days) refused |
| 199 | ebig = new OhEnv OH_VERSION 101 1 OH_CAT_SIMD 10000 add OH_MAX_TTL_MS 1 0 0 0 |
| 200 | if ne oh_submit_distress pol ebig oht_clean_q req OH_ERR_TTL_INVALID |
| 201 | ret false |
| 202 | end |
| 203 | note Already expired envelope refused |
| 204 | eexp = new OhEnv OH_VERSION 101 1 OH_CAT_SIMD 1000 5000 0 0 0 |
| 205 | if ne oh_submit_distress pol eexp oht_clean_q req OH_ERR_TTL_EXPIRED |
| 206 | ret false |
| 207 | end |
| 208 | note Enqueue valid 2000ms request at now=10000, advance clock to 12000 -> resolution refused |
| 209 | eok = new OhEnv OH_VERSION 101 1 OH_CAT_SIMD 10000 2000 0 0 0 |
| 210 | if ne oh_submit_distress pol eok oht_clean_q req OH_OK |
| 211 | ret false |
| 212 | end |
| 213 | put pol now_ms ulong 12000 |
| 214 | helper = new OhEnv OH_VERSION 202 1 OH_CAT_SIMD 12000 2000 0 0 0 |
| 215 | if ne oh_resolve_distress pol get req 0 helper oht_clean_a OH_ERR_TTL_EXPIRED |
| 216 | ret false |
| 217 | end |
| 218 | ret eq oh_queue_pending_count 0 |
| 219 | end |
| 220 | |
| 221 | fn test_privacy_secrets to bool |
| 222 | if ne oh_privacy_check oht_aws OH_ERR_PRIV_AWS_KEY |
| 223 | ret false |
| 224 | end |
| 225 | if ne oh_privacy_check oht_gh OH_ERR_PRIV_GITHUB_TOKEN |
| 226 | ret false |
| 227 | end |
| 228 | if ne oh_privacy_check oht_ant OH_ERR_PRIV_ANTHROPIC_KEY |
| 229 | ret false |
| 230 | end |
| 231 | if ne oh_privacy_check oht_oai OH_ERR_PRIV_OPENAI_KEY |
| 232 | ret false |
| 233 | end |
| 234 | if ne oh_privacy_check oht_pem OH_ERR_PRIV_PEM_KEY |
| 235 | ret false |
| 236 | end |
| 237 | if ne oh_privacy_check oht_gen OH_ERR_PRIV_GENERIC_SECRET |
| 238 | ret false |
| 239 | end |
| 240 | note Answer firewall: secret inside peer resolution MUST also be refused |
| 241 | pol = new OhPolicy 0 0 1 0 0 |
| 242 | oh_reset_state pol |
| 243 | put pol opt_in ulong 1 |
| 244 | put pol now_ms ulong 1000 |
| 245 | env = new OhEnv OH_VERSION 101 1 OH_CAT_SEC 1000 60000 0 0 0 |
| 246 | req = new ulong 1 0 |
| 247 | if ne oh_submit_distress pol env oht_clean_q req OH_OK |
| 248 | ret false |
| 249 | end |
| 250 | helper = new OhEnv OH_VERSION 202 1 OH_CAT_SEC 1000 60000 0 0 0 |
| 251 | ret eq oh_resolve_distress pol get req 0 helper oht_aws OH_ERR_PRIV_AWS_KEY |
| 252 | end |
| 253 | |
| 254 | fn test_privacy_paths_obf to bool |
| 255 | if ne oh_privacy_check oht_upath OH_ERR_PRIV_USER_PATH |
| 256 | ret false |
| 257 | end |
| 258 | if ne oh_privacy_check oht_hpath OH_ERR_PRIV_USER_PATH |
| 259 | ret false |
| 260 | end |
| 261 | if ne oh_privacy_check oht_wpath OH_ERR_PRIV_USER_PATH |
| 262 | ret false |
| 263 | end |
| 264 | if ne oh_privacy_check oht_trav OH_ERR_PRIV_TRAVERSAL |
| 265 | ret false |
| 266 | end |
| 267 | note Percent-encoded AWS key (%41KIA...) MUST be caught after obfuscation normalization |
| 268 | if ne oh_privacy_check oht_pct OH_ERR_PRIV_AWS_KEY |
| 269 | ret false |
| 270 | end |
| 271 | note Zero-width UTF-8 U+200B (E2 80 8B) inserted between A and KIA MUST be caught |
| 272 | zw = new uchar 24 0 |
| 273 | put zw 0 uchar 65 |
| 274 | put zw 1 uchar 0xe2 |
| 275 | put zw 2 uchar 0x80 |
| 276 | put zw 3 uchar 0x8b |
| 277 | put zw 4 uchar 75 |
| 278 | put zw 5 uchar 73 |
| 279 | put zw 6 uchar 65 |
| 280 | i = ulong 0 |
| 281 | while lt i 16 bound 16 |
| 282 | put zw add 7 i uchar add 65 rem i 10 |
| 283 | i = add i 1 |
| 284 | end |
| 285 | ret eq oh_privacy_check part zw 0 23 OH_ERR_PRIV_AWS_KEY |
| 286 | end |
| 287 | |
| 288 | fn test_path_scrubber to bool |
| 289 | dst = new uchar 128 0 |
| 290 | n = oh_scrub_paths oht_upath dst |
| 291 | if not oht_bytes_eq part dst 0 n oht_scrub_want |
| 292 | ret false |
| 293 | end |
| 294 | ret eq oh_privacy_check part dst 0 n OH_OK |
| 295 | end |
| 296 | |
| 297 | fn test_authority_boundary to bool |
| 298 | pol = new OhPolicy 0 0 1 0 0 |
| 299 | oh_reset_state pol |
| 300 | put pol opt_in ulong 1 |
| 301 | put pol now_ms ulong 2000 |
| 302 | req = new ulong 1 0 |
| 303 | env = new OhEnv OH_VERSION 101 1 OH_CAT_PROTO 2000 60000 OH_CAP_LOCAL_EXEC 0 0 |
| 304 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_AUTH_EXEC |
| 305 | ret false |
| 306 | end |
| 307 | put env cap_flags OH_CAP_SECRET_ACCESS |
| 308 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_AUTH_SECRET |
| 309 | ret false |
| 310 | end |
| 311 | put env cap_flags OH_CAP_IDENTITY_MUTATE |
| 312 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_AUTH_IDENTITY |
| 313 | ret false |
| 314 | end |
| 315 | put env cap_flags OH_CAP_ADMIN_RIGHTS |
| 316 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_AUTH_ADMIN |
| 317 | ret false |
| 318 | end |
| 319 | put env cap_flags ulong 0x10 |
| 320 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_AUTH_UNKNOWN |
| 321 | ret false |
| 322 | end |
| 323 | note Zero cap_flags with smuggled escalation directive in payload MUST still be refused |
| 324 | put env cap_flags OH_CAP_NONE |
| 325 | if ne oh_submit_distress pol env oht_esc_sudo req OH_ERR_AUTH_PAYLOAD_ESC |
| 326 | ret false |
| 327 | end |
| 328 | if ne oh_submit_distress pol env oht_esc_id req OH_ERR_AUTH_PAYLOAD_ESC |
| 329 | ret false |
| 330 | end |
| 331 | if ne oh_submit_distress pol env oht_esc_exec req OH_ERR_AUTH_PAYLOAD_ESC |
| 332 | ret false |
| 333 | end |
| 334 | note Resolution with privilege flag MUST also be refused |
| 335 | if ne oh_submit_distress pol env oht_clean_q req OH_OK |
| 336 | ret false |
| 337 | end |
| 338 | helper = new OhEnv OH_VERSION 202 1 OH_CAT_PROTO 2000 60000 OH_CAP_LOCAL_EXEC 0 0 |
| 339 | if ne oh_resolve_distress pol get req 0 helper oht_clean_a OH_ERR_AUTH_EXEC |
| 340 | ret false |
| 341 | end |
| 342 | put helper cap_flags OH_CAP_NONE |
| 343 | ret eq oh_resolve_distress pol get req 0 helper oht_esc_sudo OH_ERR_AUTH_PAYLOAD_ESC |
| 344 | end |
| 345 | |
| 346 | fn test_replay_self_and_bound to bool |
| 347 | pol = new OhPolicy 0 0 1 0 0 |
| 348 | oh_reset_state pol |
| 349 | put pol opt_in ulong 1 |
| 350 | put pol now_ms ulong 10000 |
| 351 | req = new ulong 1 0 |
| 352 | env = new OhEnv OH_VERSION 500 1 OH_CAT_AGENT 10000 60000 0 0 0 |
| 353 | if ne oh_submit_distress pol env oht_clean_q req OH_OK |
| 354 | ret false |
| 355 | end |
| 356 | first_rid = get req 0 |
| 357 | note Duplicate distress (same peer + digest) -> REPLAY |
| 358 | if ne oh_submit_distress pol env oht_clean_q req OH_ERR_REPLAY |
| 359 | ret false |
| 360 | end |
| 361 | note Self-resolution by same peer_id -> SELF_RESOLVE |
| 362 | selfh = new OhEnv OH_VERSION 500 1 OH_CAT_AGENT 10000 60000 0 0 0 |
| 363 | if ne oh_resolve_distress pol first_rid selfh oht_clean_a OH_ERR_SELF_RESOLVE |
| 364 | ret false |
| 365 | end |
| 366 | note Fill remaining 31 queue slots with distinct peers -> 32nd succeeds, 33rd MUST hit QUEUE_FULL |
| 367 | i = ulong 1 |
| 368 | while lt i OH_QUEUE_CAP bound 32 |
| 369 | put env peer_id add 500 i |
| 370 | if ne oh_submit_distress pol env oht_clean_q req OH_OK |
| 371 | ret false |
| 372 | end |
| 373 | i = add i 1 |
| 374 | end |
| 375 | if ne oh_queue_pending_count OH_QUEUE_CAP |
| 376 | ret false |
| 377 | end |
| 378 | put env peer_id ulong 9999 |
| 379 | ret eq oh_submit_distress pol env oht_clean_q req OH_ERR_QUEUE_FULL |
| 380 | end |
| 381 | |
| 382 | fn main to int |
| 383 | tally = new ulong 2 0 |
| 384 | oht_rec oht_n1 test_wire_roundtrip tally |
| 385 | oht_rec oht_n2 test_optin_routing_resolution tally |
| 386 | oht_rec oht_n3 test_opt_out_refusals tally |
| 387 | oht_rec oht_n4 test_ttl_refusals tally |
| 388 | oht_rec oht_n5 test_privacy_secrets tally |
| 389 | oht_rec oht_n6 test_privacy_paths_obf tally |
| 390 | oht_rec oht_n7 test_path_scrubber tally |
| 391 | oht_rec oht_n8 test_authority_boundary tally |
| 392 | oht_rec oht_n9 test_replay_self_and_bound tally |
| 393 | passed = get tally 0 |
| 394 | failed = get tally 1 |
| 395 | ok = false |
| 396 | if eq failed 0 |
| 397 | ok = oh_write_all 1 oht_sum_ok |
| 398 | else |
| 399 | ok = oh_write_all 1 oht_sum_fl |
| 400 | end |
| 401 | oh_write_u64 1 passed |
| 402 | ok = and ok oh_write_all 1 oht_fld |
| 403 | oh_write_u64 1 failed |
| 404 | ok = and ok oh_write_all 1 oht_nl |
| 405 | if and ok eq failed 0 |
| 406 | ret 0 |
| 407 | end |
| 408 | ret 1 |
| 409 | end |