code wiki / _hdl_build / nx_timefmt_gate.nx
nx_timefmt_gate.nx source
↩ module page · 67 lines · 3592 B
1// nx_timefmt_gate.nx -- GATE for nx_timefmt (RFC3339 + X.509 UTCTime/GeneralizedTime). Flagged by the
2// HONESTY SYSTEM 2026-07-16: reach=482 (TLS cert validity, HTTP Date, logs) with ZERO direct validation.
3// KAT-able by construction: epoch 0, the billennium (1e9 = 2001-09-09T01:46:40Z), leap rules incl the
4// 1900/2000 century traps, civil-date round-trips, and X.509 parse KATs. license_tier: ORIGINAL expect_exit:0
5import "nx_syscalls.nx"
6import "nx_timefmt.nx"
7
8func tw2(s: *u8) -> i64 { var n:i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func tn2(v: i64) -> i64 { let b:*u8=sys_mmap(24); var m:i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t:*u8=sys_mmap(24); 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} var j:i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 }
10func teq(a: *u8, b: *u8, n: i64) -> i64 { var i:i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
11
12func main() -> i64 {
13 var pass: i64 = 0
14 let buf: *u8 = sys_mmap(64)
15
16 // T1: KAT epoch 0 -> 1970-01-01T00:00:00Z
17 format_rfc3339(0, buf)
18 if teq(buf, "1970-01-01T00:00:00Z" as *u8, 20) == 1 { pass = pass + 1; tw2("T1 epoch0 PASS\n" as *u8) } else { tw2("T1 FAIL: " as *u8); sys_write(1, buf, 20); tw2("\n" as *u8) }
19
20 // T2: KAT the billennium -> 2001-09-09T01:46:40Z
21 format_rfc3339(1000000000, buf)
22 if teq(buf, "2001-09-09T01:46:40Z" as *u8, 20) == 1 { pass = pass + 1; tw2("T2 billennium PASS\n" as *u8) } else { tw2("T2 FAIL: " as *u8); sys_write(1, buf, 20); tw2("\n" as *u8) }
23
24 // T3: leap rules incl century traps (2000 leap, 1900 not, 2024 leap, 2026 not)
25 var ok3: i64 = 1
26 if is_leap(2000) != 1 { ok3 = 0 }
27 if is_leap(1900) != 0 { ok3 = 0 }
28 if is_leap(2024) != 1 { ok3 = 0 }
29 if is_leap(2026) != 0 { ok3 = 0 }
30 if ok3 == 1 { pass = pass + 1; tw2("T3 leap PASS\n" as *u8) } else { tw2("T3 FAIL leap rules\n" as *u8) }
31
32 // T4: civil round-trip across eras (1970-01-01, 2000-02-29, 2026-07-16, 2050-12-31)
33 var ok4: i64 = 1
34 let yy: *i64 = sys_mmap(24) as *i64
35 let mm: *i64 = sys_mmap(24) as *i64
36 let dd: *i64 = sys_mmap(24) as *i64
37 civil_from_days(days_from_civil(1970, 1, 1), yy, mm, dd)
38 if yy[0] != 1970 { ok4 = 0 }
39 if mm[0] != 1 { ok4 = 0 }
40 if dd[0] != 1 { ok4 = 0 }
41 civil_from_days(days_from_civil(2000, 2, 29), yy, mm, dd)
42 if yy[0] != 2000 { ok4 = 0 }
43 if mm[0] != 2 { ok4 = 0 }
44 if dd[0] != 29 { ok4 = 0 }
45 civil_from_days(days_from_civil(2026, 7, 16), yy, mm, dd)
46 if yy[0] != 2026 { ok4 = 0 }
47 if mm[0] != 7 { ok4 = 0 }
48 if dd[0] != 16 { ok4 = 0 }
49 civil_from_days(days_from_civil(2050, 12, 31), yy, mm, dd)
50 if yy[0] != 2050 { ok4 = 0 }
51 if mm[0] != 12 { ok4 = 0 }
52 if dd[0] != 31 { ok4 = 0 }
53 if ok4 == 1 { pass = pass + 1; tw2("T4 civil-roundtrip PASS\n" as *u8) } else { tw2("T4 FAIL civil roundtrip\n" as *u8) }
54
55 // T5: X.509 UTCTime KAT: "700101000000Z" -> 0
56 let u: i64 = parse_utctime("700101000000Z" as *u8)
57 if u == 0 { pass = pass + 1; tw2("T5 utctime PASS\n" as *u8) } else { tw2("T5 FAIL got=" as *u8); tn2(u); tw2("\n" as *u8) }
58
59 // T6: X.509 GeneralizedTime KAT: "20010909014640Z" -> 1000000000
60 let gt: i64 = parse_gentime("20010909014640Z" as *u8)
61 if gt == 1000000000 { pass = pass + 1; tw2("T6 gentime PASS\n" as *u8) } else { tw2("T6 FAIL got=" as *u8); tn2(gt); tw2("\n" as *u8) }
62
63 tw2("NX-TIMEFMT GATE " as *u8); tn2(pass); tw2("/6" as *u8)
64 if pass == 6 { tw2(" GREEN\n" as *u8); return 0 }
65 tw2(" RED\n" as *u8)
66 return 1
67}