nx_mvault_fetch_gate.nx source
↩ module page · 151 lines · 8346 B
1// nx_mvault_fetch_gate.nx -- proves the album-ingest parsing rules OFFLINE.
2//
3// The parsing rules are where downloader bugs actually live, and every one of
4// them fails SILENTLY -- you get fewer files, not an error. So these teeth are
5// the ways a naive scraper quietly loses data:
6// T5 a relative shape it does not understand must REFUSE, not guess a url
7// T8 a too-small buffer must REFUSE, not return half an album as complete
8// T9 <article> must not match tag "a" (the classic prefix collision)
9// T11 a mirror domain must resolve to its family's adapter, not miss
10// license_tier: ORIGINAL
11import "nx_mvault_fetch.nx"
12import "nx_gate.nx"
13import "nx_gate_verdict.nx" // D001: canonical verdict emission + actlog frame
14
15const FG_PFX: *u8 = "knowledge/mvfetch-test-\x00"
16
17func fg_eq(a: *u8, b: *u8) -> i64 {
18 var i: i64 = 0
19 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
20 if b[i] != (0 as u8) { return 0 }
21 return 1
22}
23func fg_lines(b: *u8, n: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { if b[i]==(10 as u8) { c=c+1 } i=i+1 } return c }
24func fg_has(b: *u8, n: i64, s: *u8) -> i64 {
25 let sl: i64 = mvf_strlen(s)
26 if sl == 0 { return 1 }
27 var i: i64 = 0
28 while i + sl <= n {
29 var m: i64 = 1
30 var j: i64 = 0
31 while j < sl { if b[i+j]!=s[j] { m=0; j=sl } else { j=j+1 } }
32 if m == 1 { return 1 }
33 i = i + 1
34 }
35 return 0
36}
37
38func main() -> i64 {
39 gw("=== nx_mvault_fetch_gate: album ingest parsing, proven without a network ===\n" as *u8)
40 var pass: i64=0; var tot: i64=0
41
42 let buf: *u8 = sys_mmap(65536)
43 let alb: *u8 = "https://bunkr.si/a/aXbYcZ" as *u8
44
45 let h: *u8 = sys_mmap(512)
46 mvf_host_of(alb, h)
47 tot=tot+1; if fg_eq(h, "bunkr.si" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
48 gw("T1 host_of extracts the host\n" as *u8)
49
50 let og: *u8 = sys_mmap(512)
51 mvf_origin_of(alb, og)
52 tot=tot+1; if fg_eq(og, "https://bunkr.si" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
53 gw("T2 origin_of keeps scheme+host and drops the path\n" as *u8)
54
55 let r: *u8 = sys_mmap(MVF_URL_CAP)
56 mvf_resolve(alb, "https://cdn.example/x.mp4" as *u8, r, MVF_URL_CAP)
57 tot=tot+1; if fg_eq(r, "https://cdn.example/x.mp4" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
58 gw("T3 an absolute url passes through untouched\n" as *u8)
59
60 mvf_resolve(alb, "/f/item1" as *u8, r, MVF_URL_CAP)
61 tot=tot+1; if fg_eq(r, "https://bunkr.si/f/item1" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
62 gw("T4 a root-relative href resolves against the album origin\n" as *u8)
63
64 let odd: i64 = mvf_resolve(alb, "../up/one" as *u8, r, MVF_URL_CAP)
65 tot=tot+1; if odd==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
66 gw("T5 TOOTH an un-understood relative shape REFUSES rather than guessing a url\n" as *u8)
67
68 // an album page shaped like the real thing: item links, plus noise that a
69 // sloppy scanner would swallow (an <article>, an off-prefix link, a script url)
70 let html: *u8 = sys_mmap(8192)
71 var ho: i64 = 0
72 ho = mvf_cat(html, ho, "<html><body><article class=wrap>\n" as *u8)
73 ho = mvf_cat(html, ho, "<a href=\"/f/one.mp4\">one</a>\n" as *u8)
74 ho = mvf_cat(html, ho, "<a class=x href='/f/two.jpg'>two</a>\n" as *u8)
75 ho = mvf_cat(html, ho, "<a href=\"/about\">about us</a>\n" as *u8)
76 ho = mvf_cat(html, ho, "<a href=\"/f/three.webm\">three</a>\n" as *u8)
77 ho = mvf_cat(html, ho, "</article></body></html>\n" as *u8)
78
79 let n1: i64 = mvf_extract(html, ho, "a" as *u8, "href" as *u8, "/f/" as *u8, buf, 65536)
80 tot=tot+1; if fg_lines(buf, n1)==3 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
81 gw("T6 extract pulls exactly the three /f/ item links (got " as *u8); gn(fg_lines(buf,n1)); gw(")\n" as *u8)
82
83 tot=tot+1; if fg_has(buf, n1, "/about" as *u8)==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
84 gw("T7 NEG-CONTROL the off-prefix /about link is excluded\n" as *u8)
85
86 tot=tot+1; if fg_has(buf, n1, "/f/two.jpg" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
87 gw("T8 SINGLE-quoted attribute values parse too\n" as *u8)
88
89 let tight: i64 = mvf_extract(html, ho, "a" as *u8, "href" as *u8, "/f/" as *u8, buf, 8)
90 tot=tot+1; if tight==(0-1) { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
91 gw("T9 TOOTH a too-small buffer REFUSES (-1) -- never a half album reported whole\n" as *u8)
92
93 // <article> must NOT be matched by tag "a": the tag name has to end at a
94 // separator. A scanner without this check silently harvests junk.
95 let only: *u8 = sys_mmap(1024)
96 let oo: i64 = mvf_cat(only, 0, "<article href=\"/f/nope\"></article>" as *u8)
97 let n2: i64 = mvf_extract(only, oo, "a" as *u8, "href" as *u8, "/f/" as *u8, buf, 65536)
98 tot=tot+1; if n2==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
99 gw("T10 TOOTH <article> is NOT matched by tag 'a' (prefix collision)\n" as *u8)
100
101 // adapters: one row covers the mirror family
102 mvf_adapter_put_pfx(FG_PFX, "bunkr.si" as *u8, "a" as *u8, "href" as *u8, "/f/" as *u8, "source" as *u8, "src" as *u8, "nishi" as *u8)
103 let po: *i64 = sys_mmap(16) as *i64
104 let lo: *i64 = sys_mmap(16) as *i64
105 let hit: i64 = mvf_adapter_find_pfx(FG_PFX, "img.cdn.bunkr.si" as *u8, po, lo)
106 tot=tot+1; if hit==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
107 gw("T11 TOOTH a mirror host img.cdn.bunkr.si resolves to the bunkr.si adapter\n" as *u8)
108
109 let miss: i64 = mvf_adapter_find_pfx(FG_PFX, "unknownhost.example" as *u8, po, lo)
110 tot=tot+1; if miss==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
111 gw("T12 an unknown host has NO adapter -- refused, never guessed\n" as *u8)
112
113 // ---- the destination filename is chosen by a REMOTE SERVER ----
114 // Untrusted input at a boundary (rule 12): a crafted name must never be able
115 // to write outside the destination directory.
116 let bn: *u8 = sys_mmap(1024)
117 let b1: i64 = mvf_basename("https://cdn.example/a/b/clip.mp4" as *u8, bn, 1024)
118 tot=tot+1; if b1 > 0 { if fg_eq(bn, "clip.mp4" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
119 gw("T13 basename takes the last path segment\n" as *u8)
120
121 let b2: i64 = mvf_basename("https://cdn.example/a/pic.jpg?sig=abc123" as *u8, bn, 1024)
122 tot=tot+1; if b2 > 0 { if fg_eq(bn, "pic.jpg" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
123 gw("T14 a signed-url ?query is stripped from the filename\n" as *u8)
124
125 let b3: i64 = mvf_basename("https://cdn.example/a/.." as *u8, bn, 1024)
126 tot=tot+1; if b3 == 0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
127 gw("T15 TOOTH a '..' segment is REFUSED, never written\n" as *u8)
128
129 let b4: i64 = mvf_basename("https://cdn.example/" as *u8, bn, 1024)
130 tot=tot+1; if b4 == 0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
131 gw("T16 TOOTH an EMPTY basename is refused rather than creating a nameless file\n" as *u8)
132
133 // adapter fields must read back byte-exact, or the extraction rules silently
134 // become the wrong rules
135 let fv: *u8 = sys_mmap(256)
136 // re-find rather than reuse po/lo -- T12's MISS leaves them stale, and a test
137 // that reads stale state proves nothing about the thing it names
138 let fpo: *i64 = sys_mmap(16) as *i64
139 let flo: *i64 = sys_mmap(16) as *i64
140 mvf_adapter_find_pfx(FG_PFX, "bunkr.si" as *u8, fpo, flo)
141 mvf_field(fpo[0] as *u8, flo[0], "link_pfx" as *u8, fv, 256)
142 tot=tot+1; if fg_eq(fv, "/f/" as *u8)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
143 gw("T17 adapter fields read back byte-exact from the stored record\n" as *u8)
144
145 let ctr: *i64 = gv_ctr()
146 ctr[0] = pass
147 ctr[1] = tot
148 let rc: i64 = gv_verdict("ALBUM-PARSING" as *u8, ctr, "album parsing is data-driven, prefix-safe, and refuses partials" as *u8)
149 sys_exit(rc)
150 return rc
151}