ws-game: archive/vpk (Valve VPK v1 / v2 read incl. multi-part via opts.resolve / opts.files, preload, CRC checks; single-file v1 / v2 write with MD5 section)
This commit is contained in:
1 parent
5ec3121f65
commit
34d0600109
6 files changed
+611
No files matched your search
@@ -0,0 +1,132 @@
|
||||
-- archive/vpk tests: version 1 and 2 directory files written by Python's vpk package list the entries it reads back
|
||||
-- (names, sizes, CRC-32s); a multi-part archive (part files through opts.files and opts.resolve, preload bytes, an
|
||||
-- embedded entry); our writer's version 1 / 2 output read back by the vpk package (MD5 verify for version 2) and by
|
||||
-- us; CRC checks; errors; the fuzz pass. Fixtures: tools/fixtures/vpk.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Vpk = require("@formats/archive/vpk")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/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
|
||||
|
||||
-- 1. Python's vpk package
|
||||
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
|
||||
|
||||
-- 2. multi-part
|
||||
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")
|
||||
-- multi-part -> single file
|
||||
local one = F.load(F.save(b) :: buffer)
|
||||
check(one, m.entries, "multi -> one directory file")
|
||||
end
|
||||
|
||||
-- 3. our writer, read back by the vpk package and by us
|
||||
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
|
||||
|
||||
-- 4. errors
|
||||
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()
|
||||
Reference in new issue
Block a user