ws-arch2: archive/uu (uuencode / xxencode / base64 blocks, read + write), core/Crc16 (ccitt / arc), Archive.files helper, shared fixture helpers (_arch2.py / _arch2_ours.luau) and tests/archive/_entries.luau
core/Crc16 is new (CRC-16/XMODEM for BinHex / MacBinary, CRC-16/ARC for LHA / Zoo). containers/Archive gains Archive.files(value, name?) (additive).
This commit is contained in:
1 parent
51117f9732
commit
8cdb65367a
10 files changed
+679
No files matched your search
@@ -0,0 +1,78 @@
|
||||
-- shared by the archive tests: check(t, archive, want, label) compares an Archive's entries with the reference listing
|
||||
-- want = { { name, size?, crc?, mode?, mtime?, dir?, link? } } (only the fields present are compared; every wanted
|
||||
-- entry must exist; crc is the CRC-32 of the data read back), and the files written by _arch2_ours.luau.
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local E = {}
|
||||
|
||||
function E.check(t: any, a: any, want: { any }, label: string, exact: boolean?)
|
||||
local list = a:list()
|
||||
if exact ~= false then
|
||||
t:eq(#list, #want, label .. ": entry count")
|
||||
end
|
||||
local by = {}
|
||||
for _, e in list do
|
||||
by[e.name] = e
|
||||
end
|
||||
for _, w in want do
|
||||
local e = by[w.name]
|
||||
t:check(e, label .. ": has " .. tostring(w.name))
|
||||
if e then
|
||||
for _, k in { "size", "mode", "mtime", "dir", "link" } do
|
||||
if w[k] ~= nil then
|
||||
t:eq(e[k], w[k], label .. ": " .. w.name .. " " .. k)
|
||||
end
|
||||
end
|
||||
if w.crc then
|
||||
local b, err = a:read(w.name)
|
||||
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
|
||||
if b then
|
||||
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the standard file set of tools/fixtures/_arch2_ours.luau / _arch2.py
|
||||
local function content(n: number, seed: number): buffer
|
||||
local b = buffer.create(n)
|
||||
local s = seed
|
||||
for i = 0, n - 1 do
|
||||
s = (s * 75 + 74) % 65537
|
||||
buffer.writeu8(b, i, if (i // 64) % 3 == 0 then (i // 7) % 256 else s % 256)
|
||||
end
|
||||
return b
|
||||
end
|
||||
E.content = content
|
||||
E.FILES = {
|
||||
{ name = "hello.txt", data = buffer.fromstring("hello, archive\n") },
|
||||
{ name = "data.bin", data = content(5000, 7) },
|
||||
{ name = "empty.txt", data = buffer.create(0) },
|
||||
}
|
||||
|
||||
-- roundTrip(t, mod, opts, label): add the standard files to mod.new(), write with opts, decode, compare
|
||||
function E.roundTrip(t: any, mod: any, opts: any?, label: string): buffer?
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local a = mod.new()
|
||||
for _, f in E.FILES do
|
||||
a:add(f.name, f.data, { mtime = 1700000000, mode = 0x1A4 })
|
||||
end
|
||||
local out, err = a:write(opts)
|
||||
t:check(out, label .. ": write: " .. tostring(err))
|
||||
if not out then
|
||||
return nil
|
||||
end
|
||||
local back, berr = mod.decode(Bytes.reader(out), {})
|
||||
t:check(back, label .. ": decode: " .. tostring(berr))
|
||||
if back then
|
||||
local want = {}
|
||||
for _, f in E.FILES do
|
||||
table.insert(want, { name = f.name, size = buffer.len(f.data), crc = Crc32.of(f.data) })
|
||||
end
|
||||
E.check(t, back, want, label, false)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return E
|
||||
@@ -0,0 +1,70 @@
|
||||
-- archive/uu tests: blocks by GNU uuencode (classic / -m) and uuenview (-u / -x) decode to the right names, sizes,
|
||||
-- modes and data; several blocks in a mail; CRLF lines; our uu / xx / base64 output decodes with uudecode / uudeview
|
||||
-- (fixture step) and is byte-identical to GNU uuencode for one file; round trips; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/uu.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Uu = require("@formats/archive/uu")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/uu")
|
||||
|
||||
local t = T.suite("uu")
|
||||
t:eq(Uu.name, "uu", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Uu.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
if a then
|
||||
E.check(t, a, c.entries, c.name)
|
||||
end
|
||||
t:eq(F.detect(c.data), "uu", c.name .. ": detect")
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
|
||||
-- byte-identical to GNU uuencode (mode 644) for each single file
|
||||
for _, g in Fx.gnu do
|
||||
local data = buffer.create(0)
|
||||
for _, f in E.FILES do
|
||||
if f.name == g.name then
|
||||
data = f.data
|
||||
end
|
||||
end
|
||||
local a = Uu.new()
|
||||
a:add(g.name, data, { mode = 0x1A4 })
|
||||
t:eq(buffer.tostring(a:write() :: buffer), g.uu, g.name .. ": same bytes as uuencode")
|
||||
t:eq(buffer.tostring(a:write({ variant = "base64" }) :: buffer), g.base64, g.name .. ": same bytes as uuencode -m")
|
||||
end
|
||||
for _, o in Fx.ours do
|
||||
t:check(o.ok, "our " .. o.variant .. " output decodes with the reference tool")
|
||||
end
|
||||
for _, v in { "uu", "xx", "base64" } do
|
||||
local out = E.roundTrip(t, Uu, { variant = v }, "round trip " .. v)
|
||||
if out then
|
||||
local back = Uu.decode(Bytes.reader(out)) :: any
|
||||
t:eq(back:list()[1].variant, v, v .. ": variant detected")
|
||||
end
|
||||
end
|
||||
-- a single block is a stream: the api unwraps what is inside (here: a uuencoded uuencoded file)
|
||||
local inner = Uu.encode("abc", { name = "x.uu" }) :: buffer
|
||||
local outer = Uu.encode(inner, { name = "x.uu" }) :: buffer
|
||||
local v = F.load(outer) :: any
|
||||
t:check(v and v.format == "uu" and v.meta.compression == "uu" and buffer.tostring((v:read("x.uu"))) == "abc", "api unwraps a single block")
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Uu.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("no blocks here\n"), Err.CORRUPT, "no begin line")
|
||||
t:eq(code("begin 644 x\n#86)C\n"), Err.TRUNCATED, "no end line")
|
||||
t:eq(code("begin 644 x\n#8~~~\n`\nend\n"), Err.CORRUPT, "bad characters")
|
||||
t:eq(select(2, Uu.encode({})) and (select(2, Uu.encode({})) :: any).code, Err.ARG, "encode a non-archive")
|
||||
t:eq((select(2, Uu.encode("x", { variant = "zz" :: any })) :: any).code, Err.ARG, "bad variant")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Uu.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
Reference in new issue
Block a user