-- archive/vpk tests against Python's vpk package (tools/fixtures/vpk.py). local T = require("@tests/T") local F = require("@rosetta") local Vpk = require("@rosetta/archive/vpk") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/vpk") local Src = require("@tests/gen/vpk_src") local t = T.suite("vpk") t:eq(Vpk.name, "vpk", "name") local function check(a: any, want: { any }, label: string) local by = {} for _, e in a:list() do by[e.name] = e end t:eq(#a:list(), #want, label .. ": entry count") for _, w in want do local e = by[w.name] t:check(e, label .. ": has " .. w.name) if e then t:eq(e.size, w.size, label .. ": size " .. w.name) local b, err = a:read(w.name) t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err)) t:eq(Crc32.of(b), w.crc, label .. ": data " .. w.name) end end end for _, c in Fx.cases do local a, err = Vpk.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(a, c.name .. ": " .. tostring(err)) check(a, c.entries, c.name) t:eq(a.meta.source.version, c.version, c.name .. ": version") t:eq(F.detect(c.data), "vpk", c.name .. ": F.detect") if c.version == 2 then t:check(a.meta.source.v2.fileMd5 and #a.meta.source.v2.fileMd5 == 16, c.name .. ": MD5 section") end end do local m = Fx.multi local a = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir)), { files = m.parts, name = "maps/pak01_dir.vpk" }) check(a, m.entries, "multi (opts.files)") t:eq(#a.meta.source.parts, 2, "two part files") local asked = {} local b = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir)), { resolve = function(name: string): string? table.insert(asked, name) return m.parts[name] end, }) check(b, m.entries, "multi (opts.resolve, default pak01 name)") t:check(table.find(asked, "pak01_001.vpk") ~= nil, "resolve gets the part file name") local c = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir))) local _, e = c:read("materials/a.vmt") t:check(e and e.code == Err.UNSUPPORTED, "missing part: " .. tostring(e)) t:eq(buffer.tostring((c:read("root.txt"))), "embedded", "embedded entry without the parts") local one = F.load(F.save(b) :: buffer) check(one, m.entries, "multi -> one directory file") end for k, v in Fx.ours do t:check(v.ok, k .. ": the vpk package verifies our MD5s") t:eq(#v.entries, #Fx.want, k .. ": the vpk package lists every entry") for i, w in Fx.want do local g = v.entries[i] t:check(g and g.name == w.name and g.size == w.size and g.crc == w.crc, k .. ": " .. w.name) end local a = Vpk.new(v.version) for _, f in Src do a:add(f.name, f.data) end check(Vpk.decode(Bytes.reader(a:write() :: buffer)), Fx.want, k .. ": round trip") end do local c = Fx.cases[2] local bad = buffer.fromstring(c.data) -- flip a byte in the last entry's data (before the 48-byte MD5 section) local at = #c.data - 49 buffer.writeu8(bad, at, bit32.bxor(buffer.readu8(bad, at), 0xFF)) local a = Vpk.decode(Bytes.reader(bad)) local failed = 0 for _, e in a:list() do local _, err = a:read(e.name) if err and err.code == Err.CHECKSUM then failed += 1 end end t:eq(failed, 1, "a damaged entry fails its CRC") local nv = Vpk.decode(Bytes.reader(bad), { verify = false }) local ok = true for _, e in nv:list() do ok = ok and nv:read(e.name) ~= nil end t:check(ok, "verify = false") local _, e1 = Vpk.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 40)))) t:check(e1 and e1.code == Err.TRUNCATED, "truncated tree: " .. tostring(e1)) local v3 = buffer.fromstring(c.data) buffer.writeu32(v3, 4, 3) local _, e2 = Vpk.decode(Bytes.reader(v3)) t:check(e2 and e2.code == Err.UNSUPPORTED, "version 3") local _, e3 = Vpk.new(3):write() t:check(e3 and e3.code == Err.ARG, "write version 3") end local samples = { Fx.multi.dir } for _, c in Fx.cases do table.insert(samples, c.data) end t:fuzz("decode", function(b: buffer) local a = Vpk.decode(Bytes.reader(b), { files = Fx.multi.parts }) if a then for _, e in a:list() do a:read(e.name) end end return a end, samples, { mutations = 60 }) t:done()