nx_ice_agent_gate.nx source
↩ module page · 94 lines · 5062 B
1// nx_ice_agent_gate.nx -- proves the sovereign ICE agent orchestration: candidate pairing (component-matched),
2// candidate-pair priority ordering (highest first), priority-ordered check scheduling, and nomination of the
3// best VALID pair as the selected path -- plus the controlling/controlled role affecting the (G,D) priority.
4// license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_ice_agent.nx"
7
8func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func gn(v: i64) -> i64 {
10 let b: *u8=sys_mmap(28); var m: i64=v; if m<0 {sys_write(1,"-" as *u8,1); m=0-m}
11 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 {t[0]=48 as u8;k=1} while m>0 {t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
12 var i: i64=0; while i<k {b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0
13}
14func gck(pass: i64, name: *u8, fails: *i64) -> i64 {
15 if pass==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8); fails[0]=fails[0]+1 }
16 gw(name); gw("\n" as *u8); return 0
17}
18
19func main() -> i64 {
20 let fails: *i64 = sys_mmap(16) as *i64
21 fails[0]=0
22 gw("=== nx_ice_agent_gate -- ICE agent pairing / ordering / nomination ===\n" as *u8)
23
24 // controlling agent: 2 local (comp 1), 2 remote (comp 1) + 1 remote (comp 2, should NOT pair -- no comp-2 local)
25 let a: *i64 = ag_new(1)
26 ag_add_local(a, 0x7F000001, 5001, 100, 1) // local0 prio 100
27 ag_add_local(a, 0x7F000001, 5002, 90, 1) // local1 prio 90
28 ag_add_remote(a, 0xC0A80001, 6001, 200, 1) // remote0 prio 200
29 ag_add_remote(a, 0xC0A80001, 6002, 80, 1) // remote1 prio 80
30 ag_add_remote(a, 0xC0A80001, 6003, 500, 2) // remote2 comp 2 (unpaired)
31 let np: i64 = ag_form_pairs(a)
32
33 // T1: exactly 4 component-matched pairs (2 local x 2 remote of component 1); the comp-2 remote is unpaired
34 var t1: i64=1
35 if np != 4 { t1=0 }
36 gw(" [measure] pairs formed=" as *u8); gn(np); gw(" (expect 4; comp-2 remote unpaired)\n" as *u8)
37 gck(t1, "T1 pairing: component-matched only -> exactly 4 pairs" as *u8, fails)
38
39 // T2: ordered highest candidate-pair priority first. Expected order: (l0,r0),(l1,r0),(l0,r1),(l1,r1).
40 var t2: i64=1
41 if ag_pair_local(a,0)!=0 { t2=0 }
42 if ag_pair_remote(a,0)!=0 { t2=0 } // top pair = local0(100) x remote0(200)
43 if ag_pair_local(a,1)!=1 { t2=0 }
44 if ag_pair_remote(a,1)!=0 { t2=0 } // 2nd = local1(90) x remote0(200)
45 gck(t2, "T2 pairs ordered by candidate-pair priority: top=(l0,r0), 2nd=(l1,r0)" as *u8, fails)
46
47 // T3: checks are scheduled highest-priority first, then exhausted
48 var t3: i64=1
49 if ag_next_check(a)!=0 { t3=0 }
50 if ag_next_check(a)!=1 { t3=0 }
51 if ag_next_check(a)!=2 { t3=0 }
52 if ag_next_check(a)!=3 { t3=0 }
53 if ag_next_check(a)!=(0-1) { t3=0 } // none left
54 gck(t3, "T3 check schedule yields pairs in priority order then -1" as *u8, fails)
55
56 // T4: nomination picks the highest-priority SUCCEEDED pair (top pair fails, 2nd succeeds -> nominate 2nd)
57 var t4: i64=1
58 ag_result(a, 0, 0) // top pair FAILED
59 ag_result(a, 1, 1) // 2nd pair SUCCEEDED
60 ag_result(a, 2, 1) // a lower pair also succeeded
61 let nom: i64 = ag_nominate(a)
62 if nom != 1 { t4=0 } // the BEST succeeded pair, not just any
63 if a[AG_NOMINATED] != 1 { t4=0 }
64 if ag_pair_state(a, 1) != AG_ST_SELECTED { t4=0 }
65 gw(" [measure] nominated pair=" as *u8); gn(nom); gw(" (l" as *u8); gn(ag_pair_local(a,nom)); gw(",r" as *u8); gn(ag_pair_remote(a,nom)); gw(")\n" as *u8)
66 gck(t4, "T4 nomination selects the highest-priority VALID pair (top failed -> 2nd, over a lower success)" as *u8, fails)
67
68 // T5: no valid pair -> no nomination
69 var t5: i64=1
70 let b: *i64 = ag_new(1)
71 ag_add_local(b, 1, 1, 50, 1)
72 ag_add_remote(b, 2, 2, 60, 1)
73 ag_form_pairs(b)
74 ag_result(b, 0, 0) // the only pair failed
75 if ag_nominate(b) != (0-1) { t5=0 }
76 if b[AG_NOMINATED] != (0-1) { t5=0 }
77 gck(t5, "T5 no VALID pair -> nomination returns -1 (no path selected)" as *u8, fails)
78
79 // T6: role affects (G,D) -- controlling uses local as G, controlled uses remote as G (RFC 8445 6.1.2.3)
80 var t6: i64=1
81 let c: *i64 = ag_new(0) // CONTROLLED
82 ag_add_local(c, 1, 1, 100, 1)
83 ag_add_remote(c, 2, 2, 200, 1)
84 ag_form_pairs(c)
85 if ag_pair_G(a, 0) != ag_local_prio(a, ag_pair_local(a,0)) { t6=0 } // controlling: G = local prio
86 if ag_pair_G(c, 0) != ag_remote_prio(c, ag_pair_remote(c,0)) { t6=0 } // controlled: G = remote prio
87 gck(t6, "T6 controlling/controlled role swaps the (G,D) priority assignment" as *u8, fails)
88
89 gw(" fails=" as *u8); gn(fails[0]); gw("\n" as *u8)
90 if fails[0]==0 { gw("VERDICT: verdict=GREEN (sovereign ICE agent pairs, orders, schedules, and nominates correctly)\n" as *u8); sys_exit(0) }
91 gw("VERDICT: verdict=RED\n" as *u8)
92 sys_exit(1)
93 return 1
94}