nx_friend_video_caller_test.nx source
↩ module page · 93 lines · 3462 B
1// nx_friend_video_caller_test.nx -- KAT for nx_friend envelopes +
2// JSON-type extractor. Live-network behaviour (dance against the
3// production nx_signaling daemon) is covered by the deploy bench
4// at bench/ops/nx_friend_video_caller_deploy.sh -- this file
5// verifies the bits-up offline-checkable invariants.
6//
7// Covers:
8// T1 hello envelope length > 0 and parses to kind=1
9// T2 offer envelope length > 0 and parses to kind=2
10// T3 answer envelope length > 0 and parses to kind=3
11// T4 ice envelope length > 0 and parses to kind=4
12// T5 envelope-type extractor returns 0 on malformed payload
13// T6 envelope-type extractor tolerates whitespace around the colon
14// T7 sealed-enum verdict-range check
15// T8 IPv4 sockaddr_in builder writes the correct byte layout
16//
17// expect_exit: 0
18// license_tier: ORIGINAL
19
20import "nx_syscalls.nx"
21import "nx_friend_video_caller.nx"
22
23func _t_strlen(s: *u8) -> i64 {
24 var n: i64 = 0
25 while s[n] != 0 as u8 { n = n + 1 }
26 return n
27}
28
29func main() -> i64 {
30 // ---- T1: hello ----
31 let h: *u8 = nx_friend_hello_bytes()
32 let hn: i64 = nx_friend_hello_len()
33 if hn <= 0 { return 1 }
34 if _t_strlen(h) != hn { return 2 }
35 if nx_friend_envelope_type(h, hn) != 1 { return 3 }
36
37 // ---- T2: offer ----
38 let o: *u8 = nx_friend_offer_bytes()
39 let on: i64 = nx_friend_offer_len()
40 if on <= 0 { return 4 }
41 if _t_strlen(o) != on { return 5 }
42 if nx_friend_envelope_type(o, on) != 2 { return 6 }
43
44 // ---- T3: answer ----
45 let a: *u8 = nx_friend_answer_bytes()
46 let an: i64 = nx_friend_answer_len()
47 if an <= 0 { return 7 }
48 if _t_strlen(a) != an { return 8 }
49 if nx_friend_envelope_type(a, an) != 3 { return 9 }
50
51 // ---- T4: ice ----
52 let ic: *u8 = nx_friend_ice_bytes()
53 let icn: i64 = nx_friend_ice_len()
54 if icn <= 0 { return 10 }
55 if _t_strlen(ic) != icn { return 11 }
56 if nx_friend_envelope_type(ic, icn) != 4 { return 12 }
57
58 // ---- T5: malformed payload returns 0 ----
59 let bad: *u8 = "{\"random\":true}" as *u8
60 if nx_friend_envelope_type(bad, _t_strlen(bad)) != 0 { return 13 }
61 let bad2: *u8 = "no json here just bytes" as *u8
62 if nx_friend_envelope_type(bad2, _t_strlen(bad2)) != 0 { return 14 }
63
64 // ---- T6: tolerates whitespace around colon ----
65 let ws: *u8 = "{\"type\" : \"hello\"}" as *u8
66 if nx_friend_envelope_type(ws, _t_strlen(ws)) != 1 { return 15 }
67
68 // ---- T7: sealed-enum verdict-range ----
69 if nx_friend_verdict_is_valid(NX_FRIEND_OK) != 1 { return 16 }
70 if nx_friend_verdict_is_valid(NX_FRIEND_TIMEOUT) != 1 { return 17 }
71 if nx_friend_verdict_is_valid(0) != 0 { return 18 }
72 if nx_friend_verdict_is_valid(NX_FRIEND_VERDICT_N) != 0 { return 19 }
73 if nx_friend_verdict_is_valid(99) != 0 { return 20 }
74
75 // ---- T8: sockaddr_in byte layout ----
76 let sa: *u8 = sys_mmap(16)
77 _friend_build_sockaddr(sa, 192, 168, 8, 227, 8445)
78 if sa[0] != 2 as u8 { return 21 } // AF_INET LE
79 if sa[1] != 0 as u8 { return 22 }
80 // port 8445 = 0x20FD; BE bytes 0x20 0xFD
81 if sa[2] != 0x20 as u8 { return 23 }
82 if sa[3] != 0xFD as u8 { return 24 }
83 if sa[4] != 192 as u8 { return 25 }
84 if sa[5] != 168 as u8 { return 26 }
85 if sa[6] != 8 as u8 { return 27 }
86 if sa[7] != 227 as u8 { return 28 }
87
88 // "PASS\n"
89 let ok: *u8 = sys_mmap(8)
90 ok[0]=80; ok[1]=65; ok[2]=83; ok[3]=83; ok[4]=10
91 sys_write(1, ok, 5)
92 return 0
93}