Files
rosetta/tests/archive/uu.luau
T

67 lines
2.5 KiB
Luau

-- archive/uu tests against GNU uuencode, uuenview and uudeview (tools/fixtures/uu.py).
local T = require("@tests/T")
local F = require("@rosetta")
local Uu = require("@rosetta/archive/uu")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/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")
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()