-- codecs/Xml tests: random documents (namespaces, escapes, CDATA, comments, PIs, DOCTYPE entities, UTF-16, Latin-1) -- against Python's ElementTree trees; encode -> decode round trips; well-formedness verdicts against expat -- (hand-written cases and random mutations); events, helpers, options, error codes; the fuzz pass. -- Fixtures: tools/fixtures/xml.py. local T = require("@tests/T") local Xml = require("@formats/codecs/Xml") local Types = require("@formats/Types") local Err = require("@formats/core/Err") local Docs = require("@tests/gen/xml_docs") local Wf = require("@tests/gen/xml_wf") local t = T.suite("xml") -- ElementTree's view of an element (tools/fixtures/xml.py canon): Clark names, xmlns attributes dropped, text / tail local function s(x: string?): string x = x or "" return #(x :: string) .. ":" .. (x :: string) end local function canon(e: any, tail: string?): string local out = { "(" .. s(Xml.clark(e)) } local keys = {} local vals = {} for _, k in Types.keys(e.attrs) do if k ~= "xmlns" and string.sub(k, 1, 6) ~= "xmlns:" then local ck = Xml.clark(e, k) table.insert(keys, ck) vals[ck] = e.attrs[k] end end table.sort(keys) for _, k in keys do table.insert(out, "|" .. s(k) .. "=" .. s(vals[k])) end -- text: strings before the first element child; each child's tail: strings after it local text = {} local i = 1 local ch = e.children while i <= #ch and (type(ch[i]) == "string" or ch[i].name == "#cdata") do table.insert(text, Xml.text(ch[i])) i += 1 end table.insert(out, "|t" .. s(table.concat(text))) while i <= #ch do local c = ch[i] i += 1 local tl = {} while i <= #ch and (type(ch[i]) == "string" or ch[i].name == "#cdata") do table.insert(tl, Xml.text(ch[i])) i += 1 end table.insert(out, canon(c, table.concat(tl))) end return table.concat(out) .. ")t" .. s(tail) end -- 1. documents vs ElementTree, and round trips for i, d in Docs do local doc, err = Xml.decode(d.data, { whitespace = true }) t:check(doc, "doc " .. i .. ": " .. tostring(err)) t:eq(canon((doc :: any).root), d.canon, "doc " .. i .. ": tree") -- keep everything, encode, decode again: the same tree local full = Xml.decode(d.data, { whitespace = true, comments = true, cdata = true, pi = true }) :: any local out, eerr = Xml.encode(full) t:check(out, "doc " .. i .. ": encodes (" .. tostring(eerr) .. ")") local again, aerr = Xml.decode(out :: string, { whitespace = true }) t:check(again, "doc " .. i .. ": re-decodes (" .. tostring(aerr) .. ")") t:eq(canon((again :: any).root), d.canon, "doc " .. i .. ": round trip") local pretty = Xml.encode(full, { indent = 2 }) :: string t:check(Xml.decode(pretty), "doc " .. i .. ": pretty output parses") -- the namespace option agrees with the clark helper local nsdoc = Xml.decode(d.data, { namespaces = true }) :: any local root = nsdoc.root local want = string.match(Xml.clark(root), "^{([^}]*)}") t:eq(root.ns, want, "doc " .. i .. ": node.ns") end -- 2. well-formedness against expat local agree, total, accepted, rejected = 0, 0, 0, 0 for i, c in Wf do local doc = Xml.decode(c.data) local ok = doc ~= nil total += 1 if ok == c.ok then agree += 1 elseif i <= 60 then t:check(false, "wf case " .. i .. " " .. string.format("%q", c.data) .. ": expat says " .. tostring(c.ok)) end if c.ok then accepted += 1 else rejected += 1 end end t:note("well-formedness: %d of %d verdicts agree with expat (%d well-formed, %d not)", agree, total, accepted, rejected) t:check(agree >= total * 0.99, "well-formedness agreement >= 99%") -- 3. API details do local src = '\n]>\nt]]>u\n' local doc = Xml.decode(src, { comments = true, cdata = true, pi = true, namespaces = true }) :: any t:eq(doc.declaration.version, "1.0", "declaration") t:eq(doc.doctype.name, "r", "doctype name") t:eq(doc.doctype.entities.e, "E&", "doctype entities (char refs expanded once)") t:eq(doc.children[1].name, "#comment", "prolog comment kept") local r = doc.root t:eq(table.concat(Types.keys(r.attrs), ","), "xmlns,xmlns:p,z,a", "attribute order") t:eq(r.ns, "urn:d", "default namespace") local x = Xml.child(r, "x") :: any t:eq(x.name, "p:x", "child by local name") t:eq(x.ns, "urn:p", "prefixed namespace") t:eq(Xml.clark(x, "p:k"), "{urn:p}k", "attribute clark") t:eq(x.children[2].name, "#cdata", "cdata node") t:eq(Xml.text(x), "tu", "text with cdata") t:eq(r.children[2].name, "#pi", "pi node") t:eq(r.children[2].target .. "|" .. r.children[2].text, "go|now", "pi target / data") t:eq(#Xml.children(r), 2, "element children") t:eq(#Xml.descendants(doc), 3, "descendants") t:eq(x.parent, r, "parent link") t:eq(Xml.decode("&e;") == nil, true, "undefined entity") local d2 = Xml.decode(']>&e;') :: any t:eq(d2.root.attrs.a, "E&", "entity in attribute") t:eq(Xml.text(d2.root), "E&", "entity in text") -- whitespace handling local w = Xml.decode("\n x \n \n") :: any t:eq(#w.root.children, 2, "whitespace-only text dropped") t:eq(Xml.text(w.root.children[1]), " x ", "other text kept as is") t:eq(w.root.children[2].children[1], " ", "xml:space preserve") t:eq(#(Xml.decode("\n \n", { whitespace = true }) :: any).root.children, 3, "opts.whitespace") -- attribute normalization and line endings local n = Xml.decode("l1\r\nl2\rl3") :: any t:eq(n.root.attrs.b, "x y z\n", "attribute value normalization") t:eq(Xml.text(n.root), "l1\nl2\nl3", "line endings") -- builders and encode local e = Xml.element("svg", { width = "10", height = "5" }, { Xml.element("rect", { x = "1" }), "aa<b & "c"', "encode element") local d3 = Xml.document(Xml.element("r", nil, { Xml.element("a"), Xml.element("b", nil, { "t" }) })) t:eq(Xml.encode(d3, { indent = 2 }), '\n\n \n t\n\n', "pretty document") t:eq(Xml.encode(Xml.element("a"), { selfClose = false }), "", "selfClose false") local _, be = Xml.encode(Xml.element("1bad")) t:eq(be and be.code, Err.ARG, "bad name on encode") local cyc = Xml.element("a") table.insert(cyc.children, cyc) local _, ce = Xml.encode(cyc) t:eq(ce and ce.code, Err.ARG, "cycle on encode") t:eq(Xml.encode({ name = "#cdata", attrs = Types.object(), children = {}, text = "a]]>b" } :: any), "b]]>", "cdata split") -- events local evs = {} for ev, a in Xml.events("t") do table.insert(evs, ev .. (if type(a) == "string" then ":" .. a else "")) end t:eq(table.concat(evs, " "), "start:a start:b text:t end:b comment:c end:a", "events") local last for ev, a in Xml.events("") do last = { ev, a } end t:eq(last[1], "error", "events error") t:eq(last[2].code, Err.CORRUPT, "events error code") -- UTF-16 without BOM and encodings local u16 = buffer.create(8) for k, ch in { 60, 0, 97, 0, 47, 0, 62, 0 } do buffer.writeu8(u16, k - 1, ch) end t:eq((Xml.decode(u16) :: any).root.name, "a", "UTF-16LE without BOM") local _, ue = Xml.decode("") t:eq(ue and ue.code, Err.UNSUPPORTED, "unsupported encoding") end -- 4. errors and limits local function code(src: string, opts: any?): string? local _, e = Xml.decode(src, opts) return e and e.code end t:eq(code(""), Err.TRUNCATED, "unclosed") t:eq(code(""), Err.CORRUPT, "mismatch") t:eq(code(""), Err.CORRUPT, "duplicate attribute") t:eq(code(string.rep("", 60) .. string.rep("", 60), { maxDepth = 50 }), Err.LIMIT, "maxDepth") t:eq(code(']>&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;', { maxExpansion = 100000 }), Err.LIMIT, "billion laughs") t:eq(code(']>&a;'), Err.CORRUPT, "recursive entity") t:eq(code(']>&x;'), Err.UNSUPPORTED, "external entity") t:eq(code("", { namespaces = true }), Err.CORRUPT, "undeclared prefix") local _, ae = Xml.decode(5 :: any) t:eq(ae and ae.code, Err.ARG, "bad argument") -- 5. fuzz local samples = {} for i = 1, 40 do table.insert(samples, Docs[i].data) end t:fuzz("decode", function(b: buffer) return Xml.decode(b, { comments = true, namespaces = true }) end, samples) t:fuzz("encode(decode)", function(b: buffer) local d = Xml.decode(b, { comments = true, cdata = true, pi = true }) return d and Xml.encode(d, { indent = 1 }) end, samples, { garbage = 20 }) t:done()