code wiki / _hdl_build / nx_adnet_creative_gate.nx

nx_adnet_creative_gate.nx source

↩ module page · 110 lines · 5758 B

1// nx_adnet_creative_gate.nx -- GATE for advertiser creative intake (nx_adnet_creative). 2// Fixtures are synthesised IN this gate rather than read from disk, so it is an independent 3// re-derivation: the gate builds a PNG header itself and asserts what the lib concludes about it. 4// The load-bearing teeth are the two that protect a paying client and the host page: 5// * CONTENT-ADDRESSING IS DETERMINISTIC AND SENSITIVE -- same bytes -> same url (idempotent re-upload), 6// one byte different -> different url (replacing artwork mints a new url, so the creative can be 7// served cacheable without ever going stale). 8// * THE WEIGHT CEILING ACTUALLY REFUSES -- the four live house banners are 196,733 bytes each; if this 9// tooth ever passes an oversize creative the platform silently re-admits that class. 10// expect_exit: 0 license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "_hdl_build/nx_adnet_creative.nx" 13import "nx_gate_verdict.nx" 14 15func tc_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i } 16func tc_puts(s: *u8) -> i64 { sys_write(1, s, tc_slen(s)); return 0 } 17func tc_pn(v: i64) -> i64 { 18 let b: *u8 = sys_mmap(32) 19 if v == 0 { b[0] = 48 as u8; sys_write(1, b, 1); return 0 } 20 var n: i64 = 0 21 var x: i64 = v 22 while x > 0 { b[n] = ((x - (x / 10) * 10) + 48) as u8; n = n + 1; x = x / 10 } 23 let r: *u8 = sys_mmap(32) 24 var i: i64 = 0 25 while i < n { r[i] = b[n - 1 - i]; i = i + 1 } 26 sys_write(1, r, n) 27 return 0 28} 29func tc_eq(a: *u8, b: *u8) -> i64 { 30 var i: i64 = 0 31 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 32 if b[i] != (0 as u8) { return 0 } 33 return 1 34} 35func tc_check(name: *u8, cond: i64) -> i64 { 36 if cond == 1 { tc_puts(" PASS " as *u8) } else { tc_puts(" FAIL " as *u8) } 37 tc_puts(name); tc_puts("\n" as *u8) 38 return cond 39} 40 41// build a minimal PNG header with the given dimensions into b, total length n (zero-filled body) 42func tc_mkpng(b: *u8, n: i64, w: i64, h: i64) -> i64 { 43 var i: i64 = 0 44 while i < n { b[i] = 0 as u8; i = i + 1 } 45 b[0] = 137 as u8; b[1] = 80 as u8; b[2] = 78 as u8; b[3] = 71 as u8 46 b[4] = 13 as u8; b[5] = 10 as u8; b[6] = 26 as u8; b[7] = 10 as u8 47 b[12] = 73 as u8; b[13] = 72 as u8; b[14] = 68 as u8; b[15] = 82 as u8 48 b[16] = ((w / 16777216) & 255) as u8; b[17] = ((w / 65536) & 255) as u8 49 b[18] = ((w / 256) & 255) as u8; b[19] = (w & 255) as u8 50 b[20] = ((h / 16777216) & 255) as u8; b[21] = ((h / 65536) & 255) as u8 51 b[22] = ((h / 256) & 255) as u8; b[23] = (h & 255) as u8 52 return n 53} 54 55func main() -> i64 { 56 tc_puts("=== nx_adnet_creative_gate ===\n" as *u8) 57 var pass: i64 = 0 58 var total: i64 = 0 59 60 let good: *u8 = sys_mmap(4096) 61 tc_mkpng(good, 4096, 728, 90) 62 let u1: *u8 = sys_mmap(128) 63 let u2: *u8 = sys_mmap(128) 64 65 // ---- acceptance + header parsing ---- 66 pass = pass + tc_check("valid 728x90 png -> ACR_OK" as *u8, acr_validate(good, 4096) == ACR_OK); total = total + 1 67 pass = pass + tc_check("width read from IHDR" as *u8, acr_width(good, 4096) == 728); total = total + 1 68 pass = pass + tc_check("height read from IHDR" as *u8, acr_height(good, 4096) == 90); total = total + 1 69 pass = pass + tc_check("url emitted under /synth/" as *u8, acr_url(good, 4096, u1, 128) > 0); total = total + 1 70 71 // ---- content addressing: deterministic AND sensitive ---- 72 acr_url(good, 4096, u2, 128) 73 pass = pass + tc_check("same bytes -> SAME url (re-upload idempotent)" as *u8, tc_eq(u1, u2)); total = total + 1 74 good[2048] = 7 as u8 75 acr_url(good, 4096, u2, 128) 76 pass = pass + tc_check("one byte changed -> DIFFERENT url (cache-bust)" as *u8, tc_eq(u1, u2) == 0); total = total + 1 77 78 // ---- the refusals ---- 79 let wrong: *u8 = sys_mmap(4096) 80 tc_mkpng(wrong, 4096, 300, 250) 81 pass = pass + tc_check("wrong dimensions -> ACR_BAD_DIMS" as *u8, acr_validate(wrong, 4096) == ACR_BAD_DIMS); total = total + 1 82 pass = pass + tc_check("wrong dimensions -> no url minted" as *u8, acr_url(wrong, 4096, u2, 128) == 0); total = total + 1 83 84 let heavy: *u8 = sys_mmap(200000) 85 tc_mkpng(heavy, 200000, 728, 90) 86 pass = pass + tc_check("196KB-class creative -> ACR_TOO_HEAVY" as *u8, acr_validate(heavy, 200000) == ACR_TOO_HEAVY); total = total + 1 87 pass = pass + tc_check("oversize -> no url minted" as *u8, acr_url(heavy, 200000, u2, 128) == 0); total = total + 1 88 89 let notpng: *u8 = sys_mmap(4096) 90 var k: i64 = 0 91 while k < 4096 { notpng[k] = 65 as u8; k = k + 1 } 92 pass = pass + tc_check("non-png -> ACR_NOT_PNG" as *u8, acr_validate(notpng, 4096) == ACR_NOT_PNG); total = total + 1 93 pass = pass + tc_check("truncated input -> refused, no overread" as *u8, acr_validate(good, 8) != ACR_OK); total = total + 1 94 95 // ---- reasons are named, never a bare boolean ---- 96 let rb: *u8 = sys_mmap(128) 97 acr_reason(ACR_TOO_HEAVY, rb) 98 pass = pass + tc_check("refusal names a remediable reason" as *u8, tc_eq(rb, "refused=over-64KB-recompress-the-png" as *u8)); total = total + 1 99 100 tc_puts("pass=" as *u8); tc_pn(pass); tc_puts(" fail=" as *u8); tc_pn(total - pass); tc_puts("\n" as *u8) 101 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 102 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 103 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 104 let ctr__dry: *i64 = gv_ctr() 105 ctr__dry[0] = pass 106 ctr__dry[1] = total 107 let rc__dry: i64 = gv_verdict("ADNET-CREATIVE-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 108 sys_exit(rc__dry) 109 return rc__dry 110}