code wiki / _hdl_build / nx_adnet_creative_gate.nx

nx_adnet_creative_gate.nx source

↩ module page · 103 lines · 5345 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" 13 14func tc_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i } 15func tc_puts(s: *u8) -> i64 { sys_write(1, s, tc_slen(s)); return 0 } 16func tc_pn(v: i64) -> i64 { 17 let b: *u8 = sys_mmap(32) 18 if v == 0 { b[0] = 48 as u8; sys_write(1, b, 1); return 0 } 19 var n: i64 = 0 20 var x: i64 = v 21 while x > 0 { b[n] = ((x - (x / 10) * 10) + 48) as u8; n = n + 1; x = x / 10 } 22 let r: *u8 = sys_mmap(32) 23 var i: i64 = 0 24 while i < n { r[i] = b[n - 1 - i]; i = i + 1 } 25 sys_write(1, r, n) 26 return 0 27} 28func tc_eq(a: *u8, b: *u8) -> i64 { 29 var i: i64 = 0 30 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 31 if b[i] != (0 as u8) { return 0 } 32 return 1 33} 34func tc_check(name: *u8, cond: i64) -> i64 { 35 if cond == 1 { tc_puts(" PASS " as *u8) } else { tc_puts(" FAIL " as *u8) } 36 tc_puts(name); tc_puts("\n" as *u8) 37 return cond 38} 39 40// build a minimal PNG header with the given dimensions into b, total length n (zero-filled body) 41func tc_mkpng(b: *u8, n: i64, w: i64, h: i64) -> i64 { 42 var i: i64 = 0 43 while i < n { b[i] = 0 as u8; i = i + 1 } 44 b[0] = 137 as u8; b[1] = 80 as u8; b[2] = 78 as u8; b[3] = 71 as u8 45 b[4] = 13 as u8; b[5] = 10 as u8; b[6] = 26 as u8; b[7] = 10 as u8 46 b[12] = 73 as u8; b[13] = 72 as u8; b[14] = 68 as u8; b[15] = 82 as u8 47 b[16] = ((w / 16777216) & 255) as u8; b[17] = ((w / 65536) & 255) as u8 48 b[18] = ((w / 256) & 255) as u8; b[19] = (w & 255) as u8 49 b[20] = ((h / 16777216) & 255) as u8; b[21] = ((h / 65536) & 255) as u8 50 b[22] = ((h / 256) & 255) as u8; b[23] = (h & 255) as u8 51 return n 52} 53 54func main() -> i64 { 55 tc_puts("=== nx_adnet_creative_gate ===\n" as *u8) 56 var pass: i64 = 0 57 var total: i64 = 0 58 59 let good: *u8 = sys_mmap(4096) 60 tc_mkpng(good, 4096, 728, 90) 61 let u1: *u8 = sys_mmap(128) 62 let u2: *u8 = sys_mmap(128) 63 64 // ---- acceptance + header parsing ---- 65 pass = pass + tc_check("valid 728x90 png -> ACR_OK" as *u8, acr_validate(good, 4096) == ACR_OK); total = total + 1 66 pass = pass + tc_check("width read from IHDR" as *u8, acr_width(good, 4096) == 728); total = total + 1 67 pass = pass + tc_check("height read from IHDR" as *u8, acr_height(good, 4096) == 90); total = total + 1 68 pass = pass + tc_check("url emitted under /synth/" as *u8, acr_url(good, 4096, u1, 128) > 0); total = total + 1 69 70 // ---- content addressing: deterministic AND sensitive ---- 71 acr_url(good, 4096, u2, 128) 72 pass = pass + tc_check("same bytes -> SAME url (re-upload idempotent)" as *u8, tc_eq(u1, u2)); total = total + 1 73 good[2048] = 7 as u8 74 acr_url(good, 4096, u2, 128) 75 pass = pass + tc_check("one byte changed -> DIFFERENT url (cache-bust)" as *u8, tc_eq(u1, u2) == 0); total = total + 1 76 77 // ---- the refusals ---- 78 let wrong: *u8 = sys_mmap(4096) 79 tc_mkpng(wrong, 4096, 300, 250) 80 pass = pass + tc_check("wrong dimensions -> ACR_BAD_DIMS" as *u8, acr_validate(wrong, 4096) == ACR_BAD_DIMS); total = total + 1 81 pass = pass + tc_check("wrong dimensions -> no url minted" as *u8, acr_url(wrong, 4096, u2, 128) == 0); total = total + 1 82 83 let heavy: *u8 = sys_mmap(200000) 84 tc_mkpng(heavy, 200000, 728, 90) 85 pass = pass + tc_check("196KB-class creative -> ACR_TOO_HEAVY" as *u8, acr_validate(heavy, 200000) == ACR_TOO_HEAVY); total = total + 1 86 pass = pass + tc_check("oversize -> no url minted" as *u8, acr_url(heavy, 200000, u2, 128) == 0); total = total + 1 87 88 let notpng: *u8 = sys_mmap(4096) 89 var k: i64 = 0 90 while k < 4096 { notpng[k] = 65 as u8; k = k + 1 } 91 pass = pass + tc_check("non-png -> ACR_NOT_PNG" as *u8, acr_validate(notpng, 4096) == ACR_NOT_PNG); total = total + 1 92 pass = pass + tc_check("truncated input -> refused, no overread" as *u8, acr_validate(good, 8) != ACR_OK); total = total + 1 93 94 // ---- reasons are named, never a bare boolean ---- 95 let rb: *u8 = sys_mmap(128) 96 acr_reason(ACR_TOO_HEAVY, rb) 97 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 98 99 tc_puts("pass=" as *u8); tc_pn(pass); tc_puts(" fail=" as *u8); tc_pn(total - pass); tc_puts("\n" as *u8) 100 if pass == total { tc_puts("=== ADNET-CREATIVE-GATE verdict=GREEN ===\n" as *u8); return 0 } 101 tc_puts("=== ADNET-CREATIVE-GATE verdict=RED ===\n" as *u8) 102 return 1 103}