interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / protocol / interplane / core / test.om

346 lines · 11052 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/interplane/core/test.om: comprehensive positive & negative control tests
3note for Interplane core envelope parser, trust-boundary policy evaluator, conformance
4note lifecycle state machine, and hash-chained provenance ledger.
5note Compose with:
6note si/directives/om2.om om/tls/sha2.om protocol/interplane/core/envelope.om
7note protocol/interplane/core/policy.om protocol/interplane/core/test.om
8
9text t_pass "PASS "
10text t_fail "FAIL "
11text t_nl "\n"
12
13text t_c1 "version_and_identifier_positive_and_negative"
14text t_c2 "untrusted_envelope_parser_valid_and_digest_determinism"
15text t_c3 "untrusted_envelope_parser_rejects_dup_unknown_missing_badchar"
16text t_c4 "inv1_parsing_never_executes_and_lifecycle_machine"
17text t_c5 "inv2_transport_never_grants_mtls_and_loopback_denied"
18text t_c6 "inv3_valid_envelope_is_not_authorization"
19text t_c7 "inv4_signature_proves_origin_not_authority_and_revoked"
20text t_c8 "inv5_runtime_policy_decides_no_request_upgrade"
21text t_c9 "provenance_ledger_chain_and_tamper_detection"
22text t_done "PASS interplane-core all 9 test suites passed\n"
23
24text t_v_ok1 "1.0"
25text t_v_ok2 "1.12"
26text t_v_bad_maj "2.0"
27text t_v_bad_lz "1.01"
28text t_v_bad_fmt "1.0.0"
29
30text t_id_ok "agent.spark-01_a"
31text t_id_trav "foo..bar"
32text t_id_lead ".hidden"
33text t_id_upper "BadUpper"
34text t_id_space "bad id"
35
36text t_env_ok "version=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ntransport=mtls\nclaimed_policy=1\n"
37text t_env_ok_alt "version=1.0\nenvelope_id=env-002\norigin_id=node.spark\naction=read.state\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ntransport=mtls\nclaimed_policy=1\n"
38text t_env_dup "version=1.0\nversion=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
39text t_env_unk "version=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\nevil_key=1\n"
40text t_env_miss "version=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\n"
41text t_env_badhex "version=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\npayload_sha256=ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ\n"
42text t_env_badtr "version=1.0\nenvelope_id=env-001\norigin_id=node.spark\naction=read.state\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ntransport=godmode\n"
43text t_env_escal "version=1.0\nenvelope_id=env-003\norigin_id=node.spark\naction=policy.set\npayload_sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ntransport=loopback\nclaimed_policy=3\n"
44
45fn report_case name view uchar * ok bool to int
46if ok
47w1 = ip_write_all int 1 t_pass
48w2 = ip_write_all int 1 name
49w3 = ip_write_all int 1 t_nl
50if and w1 and w2 w3
51ret int 0
52end
53ret int 1
54end
55w4 = ip_write_all int 1 t_fail
56w5 = ip_write_all int 1 name
57w6 = ip_write_all int 1 t_nl
58if and w4 and w5 w6
59ret int 1
60end
61ret int 1
62end
63
64fn test_ver_ident to bool
65if ne ip_validate_version t_v_ok1 IP_OK
66ret false
67end
68if ne ip_validate_version t_v_ok2 IP_OK
69ret false
70end
71if ne ip_validate_version t_v_bad_maj IP_ERR_BAD_VERSION
72ret false
73end
74if ne ip_validate_version t_v_bad_lz IP_ERR_BAD_VERSION
75ret false
76end
77if ne ip_validate_version t_v_bad_fmt IP_ERR_BAD_VERSION
78ret false
79end
80if ne ip_valid_ident t_id_ok IP_OK
81ret false
82end
83if ne ip_valid_ident t_id_trav IP_ERR_BAD_IDENT
84ret false
85end
86if ne ip_valid_ident t_id_lead IP_ERR_BAD_IDENT
87ret false
88end
89if ne ip_valid_ident t_id_upper IP_ERR_BAD_IDENT
90ret false
91end
92if ne ip_valid_ident t_id_space IP_ERR_BAD_IDENT
93ret false
94end
95ret true
96end
97
98fn test_parse_valid to bool
99m1 = new ulong 4 0
100m2 = new ulong 4 0
101d1 = new uchar 32 0
102d2 = new uchar 32 0
103if ne ip_parse_envelope t_env_ok m1 d1 IP_OK
104ret false
105end
106if ne get m1 1 TRANS_MTLS
107ret false
108end
109if ne get m1 2 POL_READ_ONLY
110ret false
111end
112if ne get m1 3 LC_PARSED
113ret false
114end
115note Determinism check
116if ne ip_parse_envelope t_env_ok m2 d2 IP_OK
117ret false
118end
119if not ip_digest_eq d1 d2
120ret false
121end
122note Different envelope_id produces different digest
123if ne ip_parse_envelope t_env_ok_alt m2 d2 IP_OK
124ret false
125end
126if ip_digest_eq d1 d2
127ret false
128end
129ret true
130end
131
132fn test_parse_negative to bool
133m = new ulong 4 0
134d = new uchar 32 0
135if ne ip_parse_envelope part t_env_ok 0 0 m d IP_ERR_BAD_LEN
136ret false
137end
138if ne ip_parse_envelope t_env_dup m d IP_ERR_DUP_KEY
139ret false
140end
141if ne ip_parse_envelope t_env_unk m d IP_ERR_UNKNOWN_KEY
142ret false
143end
144if ne ip_parse_envelope t_env_miss m d IP_ERR_MISSING_FIELD
145ret false
146end
147if ne ip_parse_envelope t_env_badhex m d IP_ERR_BAD_HEX
148ret false
149end
150if ne ip_parse_envelope t_env_badtr m d IP_ERR_BAD_TRANSPORT
151ret false
152end
153note Control byte injection (tab 0x09) must be rejected
154bad = new uchar 16 0
155put bad 0 uchar 118
156put bad 1 uchar 61
157put bad 2 uchar 9
158if ne ip_parse_envelope part bad 0 3 m d IP_ERR_BAD_CHAR
159ret false
160end
161ret true
162end
163
164fn test_inv1_parse_never_exec to bool
165m = new ulong 4 0
166d = new uchar 32 0
167if ne ip_parse_envelope t_env_ok m d IP_OK
168ret false
169end
170lc = get m 3
171if ne lc LC_PARSED
172ret false
173end
174if ip_can_execute lc
175ret false
176end
177note Jumping directly from PARSED -> EXECUTED or PARSED -> POLICY_AUTHORIZED must fail
178if ne ip_lc_transition LC_PARSED LC_EXECUTED POL_ERR_UNAUTH_TRANSITION
179ret false
180end
181if ne ip_lc_transition LC_PARSED LC_POLICY_AUTHORIZED POL_ERR_UNAUTH_TRANSITION
182ret false
183end
184note Valid full sequence: RECEIVED -> PARSED -> ORIGIN_CHECKED -> POLICY_AUTHORIZED -> EXECUTED
185if ne ip_lc_transition LC_RECEIVED LC_PARSED POL_OK
186ret false
187end
188if ne ip_lc_transition LC_PARSED LC_ORIGIN_CHECKED POL_OK
189ret false
190end
191if ne ip_lc_transition LC_ORIGIN_CHECKED LC_POLICY_AUTHORIZED POL_OK
192ret false
193end
194if not ip_can_execute LC_POLICY_AUTHORIZED
195ret false
196end
197if ne ip_lc_transition LC_POLICY_AUTHORIZED LC_EXECUTED POL_OK
198ret false
199end
200note Terminal states cannot transition
201if ne ip_lc_transition LC_EXECUTED LC_PARSED POL_ERR_UNAUTH_TRANSITION
202ret false
203end
204if ne ip_lc_transition LC_DENIED LC_POLICY_AUTHORIZED POL_ERR_UNAUTH_TRANSITION
205ret false
206end
207ret true
208end
209
210fn test_inv2_transport_never_grants to bool
211note Even LOOPBACK and MTLS with valid signature MUST be denied under POL_DENY_ALL
212if ne ip_eval_policy POL_DENY_ALL OP_READ ORIG_SIG_VALID TRANS_LOOPBACK OP_READ POL_DENY_ALL POL_ERR_POLICY_DENIED
213ret false
214end
215if ne ip_eval_policy POL_DENY_ALL OP_READ ORIG_SIG_VALID TRANS_MTLS OP_READ POL_DENY_ALL POL_ERR_POLICY_DENIED
216ret false
217end
218note Even LOOPBACK cannot grant OP_EXEC when runtime only allows OP_READ
219if ne ip_eval_policy POL_STANDARD OP_READ ORIG_SIG_VALID TRANS_LOOPBACK OP_EXEC POL_READ_ONLY POL_ERR_OP_NOT_ALLOWED
220ret false
221end
222note Conversely, UNTRUSTED transport succeeds when runtime policy + origin + op allow it
223if ne ip_eval_policy POL_READ_ONLY OP_READ ORIG_SIG_VALID TRANS_UNTRUSTED OP_READ POL_READ_ONLY POL_OK
224ret false
225end
226ret true
227end
228
229fn test_inv3_valid_env_not_auth to bool
230m = new ulong 4 0
231d = new uchar 32 0
232if ne ip_parse_envelope t_env_ok m d IP_OK
233ret false
234end
235note Valid envelope is in LC_PARSED, not authorized; calling authorize_step before origin check fails
236if ne ip_authorize_step get m 3 POL_READ_ONLY OP_READ ORIG_SIG_VALID OP_READ POL_READ_ONLY POL_ERR_UNAUTH_TRANSITION
237ret false
238end
239if ip_can_execute LC_ORIGIN_CHECKED
240ret false
241end
242note Only after origin check AND policy eval passes is authorization granted
243if ne ip_authorize_step LC_ORIGIN_CHECKED POL_READ_ONLY OP_READ ORIG_SIG_VALID OP_READ POL_READ_ONLY POL_OK
244ret false
245end
246ret true
247end
248
249fn test_inv4_sig_not_authority to bool
250note Valid signature on ORIG_SIG_VALID does NOT grant OP_ADMIN when runtime is POL_STANDARD
251if ne ip_eval_policy POL_STANDARD OP_ALL_MASK ORIG_SIG_VALID TRANS_MTLS OP_ADMIN POL_STANDARD POL_ERR_POLICY_DENIED
252ret false
253end
254note Valid signature does NOT grant OP_WRITE when allowed_ops only has OP_READ
255if ne ip_eval_policy POL_ELEVATED OP_READ ORIG_SIG_VALID TRANS_MTLS OP_WRITE POL_READ_ONLY POL_ERR_OP_NOT_ALLOWED
256ret false
257end
258note Unverified origin and revoked origin are both refused
259if ne ip_eval_policy POL_ELEVATED OP_ALL_MASK ORIG_UNVERIFIED TRANS_LOOPBACK OP_READ POL_READ_ONLY POL_ERR_ORIGIN_UNVERIFIED
260ret false
261end
262if ne ip_eval_policy POL_ELEVATED OP_ALL_MASK ORIG_REVOKED TRANS_LOOPBACK OP_READ POL_READ_ONLY POL_ERR_ORIGIN_REVOKED
263ret false
264end
265ret true
266end
267
268fn test_inv5_no_policy_upgrade to bool
269m = new ulong 4 0
270d = new uchar 32 0
271if ne ip_parse_envelope t_env_escal m d IP_OK
272ret false
273end
274note Envelope claims policy=3 (POL_ELEVATED) over loopback, but runtime policy is POL_STANDARD (2)
275if ne ip_eval_policy POL_STANDARD OP_ALL_MASK ORIG_SIG_VALID get m 1 OP_READ get m 2 POL_ERR_ESCALATION
276ret false
277end
278note Requesting OP_POLICY_MUTATE under POL_STANDARD must be refused
279if ne ip_eval_policy POL_STANDARD OP_ALL_MASK ORIG_SIG_VALID TRANS_LOOPBACK OP_POLICY_MUTATE POL_STANDARD POL_ERR_POLICY_DENIED
280ret false
281end
282note Requesting OP_POLICY_MUTATE under POL_ELEVATED succeeds only if OP_POLICY_MUTATE is in allowed_ops
283if ne ip_eval_policy POL_ELEVATED OP_READ ORIG_SIG_VALID TRANS_LOOPBACK OP_POLICY_MUTATE POL_ELEVATED POL_ERR_OP_NOT_ALLOWED
284ret false
285end
286if ne ip_eval_policy POL_ELEVATED OP_POLICY_MUTATE ORIG_SIG_VALID TRANS_MTLS OP_POLICY_MUTATE POL_ELEVATED POL_OK
287ret false
288end
289ret true
290end
291
292fn test_ledger_chain to bool
293gen = new uchar 32 0
294e1 = new uchar 32 0
295e2 = new uchar 32 0
296l1 = new uchar 32 0
297l2 = new uchar 32 0
298m = new ulong 4 0
299if ne ip_parse_envelope t_env_ok m e1 IP_OK
300ret false
301end
302if ne ip_parse_envelope t_env_ok_alt m e2 IP_OK
303ret false
304end
305ip_ledger_step gen ulong 1 e1 POL_OK l1
306ip_ledger_step l1 ulong 2 e2 POL_ERR_ESCALATION l2
307if ne ip_ledger_verify gen ulong 1 e1 POL_OK l1 POL_OK
308ret false
309end
310if ne ip_ledger_verify l1 ulong 2 e2 POL_ERR_ESCALATION l2 POL_OK
311ret false
312end
313note Negative controls: wrong verdict, wrong seq, or flipped digest byte detected as tamper
314if ne ip_ledger_verify l1 ulong 2 e2 POL_OK l2 POL_ERR_LEDGER_TAMPER
315ret false
316end
317if ne ip_ledger_verify l1 ulong 3 e2 POL_ERR_ESCALATION l2 POL_ERR_LEDGER_TAMPER
318ret false
319end
320put l2 0 xor get l2 0 uchar 1
321if ne ip_ledger_verify l1 ulong 2 e2 POL_ERR_ESCALATION l2 POL_ERR_LEDGER_TAMPER
322ret false
323end
324ret true
325end
326
327fn main to int
328fail = int 0
329fail = add fail report_case t_c1 test_ver_ident
330fail = add fail report_case t_c2 test_parse_valid
331fail = add fail report_case t_c3 test_parse_negative
332fail = add fail report_case t_c4 test_inv1_parse_never_exec
333fail = add fail report_case t_c5 test_inv2_transport_never_grants
334fail = add fail report_case t_c6 test_inv3_valid_env_not_auth
335fail = add fail report_case t_c7 test_inv4_sig_not_authority
336fail = add fail report_case t_c8 test_inv5_no_policy_upgrade
337fail = add fail report_case t_c9 test_ledger_chain
338if ne fail int 0
339ret int 1
340end
341ok = ip_write_all int 1 t_done
342if not ok
343ret int 1
344end
345ret int 0
346end