-- archive/mpq tests against StormLib (tools/fixtures/mpq.py). local T = require("@tests/T") local F = require("@rosetta") local Mpq = require("@rosetta/archive/mpq") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/mpq") local t = T.suite("mpq") t:eq(Mpq.name, "mpq", "name") -- the classic test vector: the hash table key t:eq(Mpq.hash("(hash table)", 3), 0xC3AF3770, "hash of (hash table)") t:eq(Mpq.hash("(block table)", 3), 0xEC83B3A3, "hash of (block table)") for _, c in Fx.archives do local a, err = Mpq.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(a, c.name .. ": " .. tostring(err)) t:eq(F.detect(c.data), "mpq", c.name .. ": F.detect") local by = {} for _, e in a:list() do by[e.name] = e end local ok = 0 for _, w in c.files do if c.unnamed then continue end local e = by[w.name] t:check(e, c.name .. ": has " .. w.name) if w.size > 0 then local b, rerr = a:read(w.name) if w.huffman then t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": " .. w.name .. ": Huffman is unsupported: " .. tostring(rerr)) else t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr)) t:eq(e.size, w.size, c.name .. ": size " .. w.name) t:eq(Crc32.of(b), w.crc, c.name .. ": data " .. w.name) ok += 1 end end end if c.unnamed then -- no listfile: unnamed entries; unencrypted ones read by block index, names through opts.names local n = 0 for _, e in a:list() do if string.match(e.name, "^File%d+%.xxx$") then n += 1 local b, rerr = a:read(e.name) if (e :: any).encrypted then t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": encrypted without a name") else t:check(b, c.name .. ": read " .. e.name .. ": " .. tostring(rerr)) end end end t:check(n > 0, c.name .. ": entries named by block index") local names = {} for _, w in c.files do table.insert(names, w.name) end local b = Mpq.decode(Bytes.reader(buffer.fromstring(c.data)), { names = names }) :: any for _, w in c.files do if w.size > 0 then t:eq(Crc32.of((b:read(w.name))), w.crc, c.name .. ": opts.names " .. w.name) end end end t:note("%-9s v%d, %2d entries, %2d files checked", c.name, a.meta.source.version, #a:list(), ok) end do local c = Fx.archives[1] local _, e1 = Mpq.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 200)))) t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "truncated: " .. tostring(e1)) local _, e2 = Mpq.decode(Bytes.reader(buffer.fromstring(string.rep("x", 64)))) t:check(e2 and e2.code == Err.CORRUPT, "no header") local a = Mpq.decode(Bytes.reader(buffer.fromstring(c.data))) :: any local _, e3 = a:write() t:check(e3 and e3.code == Err.UNSUPPORTED, "read-only") end local samples = {} for _, c in Fx.archives do if c.name == "v1" or c.name == "nolist" or c.name == "v4" then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) local a = Mpq.decode(Bytes.reader(b)) if a then for _, e in a:list() do a:read(e.name) end end return a end, samples, { mutations = 60 }) do local c = Fx.archives[1] local rand = T.rng(5) local threw = 0 for _ = 1, 80 do local b = buffer.fromstring(c.data) for _ = 1, 1 + rand(8) do buffer.writeu8(b, 32 + rand(#c.data // 2), rand(256)) end local ok = pcall(function() local a = Mpq.decode(Bytes.reader(b)) if a then for _, e in a:list() do a:read(e.name) end end end) if not ok then threw += 1 end end t:eq(threw, 0, "mutated file data never throws") end t:done()