ws-arch: merge ws/arch-zstd
This commit is contained in:
commit
9fadc7155f
9 files changed
+1591
No files matched your search
@@ -0,0 +1,90 @@
|
||||
-- archive/zst tests: the contract, sniff (zstd and skippable-frame magic), decode of zstd CLI output into a one-entry
|
||||
-- single-stream archive (entry name from opts.name, size, read), F.load detection by content, read-only errors, and
|
||||
-- the fuzz no-throw pass. Fixtures: tools/fixtures/zstd.py (shared with tests/codecs/zstd.luau).
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Zst = require("@formats/archive/zst")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/zstd")
|
||||
|
||||
local t = T.suite("zst")
|
||||
|
||||
-- contract
|
||||
t:eq(Zst.name, "zst", "name")
|
||||
t:eq(Zst.category, "archive", "category")
|
||||
t:check(Zst.caps.decode and not Zst.caps.encode, "caps")
|
||||
|
||||
local function case(name: string): any
|
||||
for _, c in Fx.cases do
|
||||
if c.name == name then
|
||||
return c
|
||||
end
|
||||
end
|
||||
error("no case " .. name)
|
||||
end
|
||||
|
||||
-- sniff
|
||||
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring(case("text/l3").data))), 1, "sniff zstd")
|
||||
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring(case("skipfirst/frames").data))), 0.5, "sniff skippable frame")
|
||||
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring("PK\3\4"))), 0, "sniff other")
|
||||
t:eq(Zst.sniff(Bytes.reader(buffer.create(0))), 0, "sniff empty")
|
||||
|
||||
-- decode: one entry, named after the file
|
||||
for _, name in { "text/l3", "runs/ultra22", "multi/frames", "empty/l3", "big/l19" } do
|
||||
local c = case(name)
|
||||
local a, err = Zst.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "dir/notes.txt.zst" })
|
||||
t:check(a, name .. ": " .. tostring(err))
|
||||
local list = a:list()
|
||||
t:eq(#list, 1, name .. ": one entry")
|
||||
t:eq(list[1].name, "notes.txt", name .. ": entry name")
|
||||
t:eq(list[1].size, #Fx.corpora[c.corpus], name .. ": size")
|
||||
t:eq(list[1].compressedSize, #c.data, name .. ": compressedSize")
|
||||
t:check(a.meta.stream == true and a.kind == "archive" and a.format == "zst", name .. ": archive fields")
|
||||
local data = a:read(list[1].name)
|
||||
t:check(data and buffer.tostring(data) == Fx.corpora[c.corpus], name .. ": read")
|
||||
end
|
||||
do
|
||||
local c = case("short/l3")
|
||||
local a = Zst.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
|
||||
t:eq(a:list()[1].name, "data", "no name -> data")
|
||||
local a2 = Zst.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "backup.tzst" }) :: any
|
||||
t:eq(a2:list()[1].name, "backup.tar", ".tzst -> .tar")
|
||||
local _, e = a:read("nope")
|
||||
t:check(e and e.code == Err.ARG, "missing entry")
|
||||
local _, we = a:write()
|
||||
t:check(we and we.code == Err.UNSUPPORTED, "read-only: " .. tostring(we))
|
||||
-- inside a bigger buffer (the reader's range)
|
||||
local big = buffer.create(#c.data + 10)
|
||||
buffer.writestring(big, 5, c.data)
|
||||
local a3 = Zst.decode(Bytes.reader(big, 5, #c.data)) :: any
|
||||
t:check(a3 and buffer.tostring(a3:read("data")) == Fx.corpora.short, "reader range")
|
||||
end
|
||||
|
||||
-- the api: detection by content, the name from opts
|
||||
do
|
||||
local c = case("text/l5")
|
||||
local a, err = F.load(c.data, { name = "x/readme.md.zst" })
|
||||
t:check(a and a.format == "zst", "F.load detects zst: " .. tostring(err))
|
||||
t:eq(a:list()[1].name, "readme.md", "F.load entry name")
|
||||
t:eq(F.detect(c.data), "zst", "F.detect")
|
||||
local _, e = F.load(string.sub(c.data, 1, 30), { format = "zst" })
|
||||
t:check(e and e.code == Err.TRUNCATED, "truncated through the api: " .. tostring(e))
|
||||
local _, se = F.save(a, nil, { format = "zst" })
|
||||
t:check(se and se.code == Err.UNSUPPORTED, "no encoder")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 11 == 0 and #c.data < 30000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Zst.decode(Bytes.reader(b), { maxBytes = 1e6 })
|
||||
end, samples, { mutations = 10, garbage = 500 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,115 @@
|
||||
-- codecs/Zstd tests: every fixture case (the zstd CLI at levels 1..19, --ultra -22, --fast, --long, small windows,
|
||||
-- with / without checksum and content size, multi-frame and skippable frames, empty input) decodes to its corpus;
|
||||
-- offsets / consumedEnd; XXH64 vectors; checksum, limit and error codes; truncated / corrupted / random input never
|
||||
-- throws. Fixtures: tools/fixtures/zstd.py.
|
||||
local T = require("@tests/T")
|
||||
local Zstd = require("@formats/codecs/Zstd")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/zstd")
|
||||
|
||||
local t = T.suite("zstd")
|
||||
|
||||
-- XXH64 reference values (xxhsum -H64)
|
||||
do
|
||||
local function hex(s: string): string
|
||||
local hi, lo = Zstd.xxh64(buffer.fromstring(s))
|
||||
return string.format("%08x%08x", hi, lo)
|
||||
end
|
||||
t:eq(hex(""), "ef46db3751d8e999", "xxh64 empty")
|
||||
t:eq(hex("a"), "d24ec4f1a98c6e5b", "xxh64 a")
|
||||
t:eq(hex("abc"), "44bc2cf5ad770999", "xxh64 abc")
|
||||
t:eq(hex("Nobody inspects the spammish repetition"), "fbcea83c8a378bf1", "xxh64 sentence")
|
||||
t:eq(hex(string.rep("0123456789", 10)), "f80e7b96315afffa", "xxh64 100 bytes")
|
||||
end
|
||||
|
||||
-- every case decodes to its corpus
|
||||
local nbytes = 0
|
||||
for _, c in Fx.cases do
|
||||
local want = Fx.corpora[c.corpus]
|
||||
local b = buffer.fromstring(c.data)
|
||||
local out, consumed = Zstd.decompress(b)
|
||||
t:check(out ~= nil, c.name .. ": " .. tostring(consumed))
|
||||
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
|
||||
t:eq(consumed, #c.data, c.name .. ": consumed")
|
||||
nbytes += #want
|
||||
end
|
||||
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
|
||||
|
||||
local function case(name: string): any
|
||||
for _, c in Fx.cases do
|
||||
if c.name == name then
|
||||
return c
|
||||
end
|
||||
end
|
||||
error("no case " .. name)
|
||||
end
|
||||
|
||||
-- at an offset inside a bigger buffer, and string input
|
||||
do
|
||||
local c = case("text/l3")
|
||||
local big = buffer.create(#c.data + 20)
|
||||
buffer.fill(big, 0, 0xAB)
|
||||
buffer.writestring(big, 7, c.data)
|
||||
local out, consumed = Zstd.decompress(big, 7, #c.data)
|
||||
t:check(out and buffer.tostring(out) == Fx.corpora.text, "at an offset")
|
||||
t:eq(consumed, 7 + #c.data, "consumed at an offset")
|
||||
t:check(buffer.tostring((Zstd.decompress(c.data))) == Fx.corpora.text, "string input")
|
||||
t:check(Zstd.isFrame(buffer.fromstring(c.data)), "isFrame")
|
||||
t:check(not Zstd.isFrame(buffer.fromstring("abcd")), "isFrame on other data")
|
||||
-- trailing garbage is an error, as zstd -d says
|
||||
local _, e = Zstd.decompress(c.data .. "junk")
|
||||
t:check(e and e.code == Err.CORRUPT, "trailing garbage: " .. tostring(e))
|
||||
end
|
||||
|
||||
-- checksum mismatch (the last 4 bytes), and verify = false skips it
|
||||
do
|
||||
local c = case("text/l9")
|
||||
local b = buffer.fromstring(c.data)
|
||||
local n = buffer.len(b)
|
||||
buffer.writeu8(b, n - 1, bit32.bxor(buffer.readu8(b, n - 1), 0x10))
|
||||
local out, e = Zstd.decompress(b)
|
||||
t:check(out == nil and Err.is(e) and e.code == Err.CHECKSUM, "checksum mismatch: " .. tostring(e))
|
||||
local out2 = Zstd.decompress(b, nil, nil, { verify = false })
|
||||
t:check(out2 and buffer.tostring(out2) == Fx.corpora.text, "verify = false")
|
||||
end
|
||||
|
||||
-- limits and arguments
|
||||
do
|
||||
local c = case("text/l3")
|
||||
local _, e = Zstd.decompress(c.data, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxBytes with a content size: " .. tostring(e))
|
||||
local s = case("text/stdin")
|
||||
local _, e2 = Zstd.decompress(s.data, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "maxBytes without a content size: " .. tostring(e2))
|
||||
local ok, r1, r2 = pcall(Zstd.decompress, 5 :: any)
|
||||
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
||||
local _, e3 = Zstd.decompress("")
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e3))
|
||||
local _, e4 = Zstd.decompress("\40\181\47\253\0")
|
||||
t:check(e4 and e4.code == Err.TRUNCATED, "header only: " .. tostring(e4))
|
||||
-- a dictionary ID: FHD with Dictionary_ID_flag = 1, single segment, then a 1-byte dictionary id and 1-byte size
|
||||
local _, e5 = Zstd.decompress("\40\181\47\253\33\7\0\1\0\0")
|
||||
t:check(e5 and e5.code == Err.UNSUPPORTED, "dictionary: " .. tostring(e5))
|
||||
-- reserved block type
|
||||
local _, e6 = Zstd.decompress("\40\181\47\253\32\0\7\0\0")
|
||||
t:check(e6 and e6.code == Err.CORRUPT, "reserved block type: " .. tostring(e6))
|
||||
end
|
||||
|
||||
-- truncation, corruption and garbage never throw
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 5 == 0 and #c.data > 8 and #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return Zstd.decompress(b)
|
||||
end, samples, { mutations = 12, garbage = 2000 })
|
||||
-- keep the frame header intact so the mutations reach the blocks
|
||||
t:fuzz("decompress (blocks)", function(b: buffer)
|
||||
return Zstd.decompress(b, nil, nil, { verify = false, maxBytes = 1e6 })
|
||||
end, samples, { mutations = 40, garbage = 0, keepHeader = 12, seed = 99 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user