251 lines
11 KiB
Luau
251 lines
11 KiB
Luau
-- archive/zip + containers/Zip tests against Python's zipfile; SKINS_DIR adds the Webamp demo skins.
|
|
-- Fixtures: tools/fixtures/zip.py.
|
|
local T = require("@tests/T")
|
|
local Zip = require("@rosetta/containers/Zip")
|
|
local ZipF = require("@rosetta/archive/zip")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local F = require("@rosetta")
|
|
local Fx = require("@tests/gen/zip_cases")
|
|
|
|
local t = T.suite("zip")
|
|
|
|
local function dataOf(a: any): string
|
|
if a.data then
|
|
return a.data
|
|
end
|
|
return buffer.tostring(Deflate.zlib(buffer.fromstring(a.dataZ)) :: buffer)
|
|
end
|
|
|
|
local function verify(a: any, label: string): (any, number)
|
|
local z, err = Zip.open(dataOf(a))
|
|
t:check(z, label .. ": open: " .. tostring(err))
|
|
local list = z:list()
|
|
t:eq(#list, a.count, label .. ": entries")
|
|
local bytes = 0
|
|
for i, want in a.entries do
|
|
local e = list[i]
|
|
t:check(e.rawName == want[1] or e.name == string.gsub(want[1], "\\", "/"), label .. ": name " .. e.rawName)
|
|
t:check(e.size == want[2] and e.crc32 == want[3] and e.method == want[4], label .. ": " .. e.name .. " size / crc / method")
|
|
local data, rerr = z:read(e)
|
|
t:check(data, label .. ": read " .. e.name .. ": " .. tostring(rerr))
|
|
t:check(buffer.len(data :: buffer) == want[2] and Crc32.of(data :: buffer) == want[3], label .. ": " .. e.name .. " content")
|
|
bytes += want[2]
|
|
end
|
|
return z, bytes
|
|
end
|
|
|
|
local byName = {}
|
|
for _, a in Fx.archives do
|
|
byName[a.name] = a
|
|
a.zip = verify(a, a.name)
|
|
end
|
|
t:note("%d generated archives match Python zipfile", #Fx.archives)
|
|
local total, nent = 0, 0
|
|
for _, s in Fx.skins do
|
|
local _, bytes = verify(s, s.name)
|
|
total += bytes
|
|
nent += s.count
|
|
end
|
|
if #Fx.skins > 0 then
|
|
t:note("%d skins, %d entries, %.2f MB read and CRC-checked", #Fx.skins, nent, total / 1e6)
|
|
else
|
|
t:note("(no skins: set SKINS_DIR to the Webamp demo skins)")
|
|
end
|
|
|
|
do
|
|
local z = byName.basic.zip
|
|
t:eq(z:find("readme.txt").name, "readme.txt", "find exact")
|
|
t:eq(z:find("README.TXT").name, "readme.txt", "find case-insensitive")
|
|
t:eq(z:find("main.bmp").name, "Skin/Main.BMP", "find by basename skips __MACOSX")
|
|
t:eq(z:find("skin\\MAIN.bmp").name, "Skin/Main.BMP", "find with backslashes")
|
|
t:check(z:find("__MACOSX/Skin/._Main.BMP") ~= nil, "junk still found by its full path")
|
|
t:check(z:find("Skin/") == nil, "directories are not found")
|
|
t:check(z:find("nope.bmp") == nil, "missing")
|
|
t:eq(z:find("deep.txt").name, "Skin/sub/deep.txt", "basename in a deeper folder")
|
|
t:eq(z:readString("DEEP.TXT"), "deep", "readString by name")
|
|
t:eq(z:readString("Other/\u{fc}nicode.txt"), "h\u{e9}llo", "unicode basename")
|
|
local u = z:find("sk\u{ef}n/\u{fc}nicode.txt")
|
|
t:check(u and u.utf8 and z:readString(u) == "h\u{e9}llo", "unicode name")
|
|
local m = z:find("readme.txt").modified
|
|
t:check(m.year == 2020 and m.month == 5 and m.day == 17 and m.hour == 13 and m.min == 37 and m.sec == 42, "dos time")
|
|
t:eq(z:find("readme.txt").mtime, 1589722662, "mtime (unix seconds)")
|
|
local _, e = z:read("nope")
|
|
t:check(e and e.code == Err.ARG, "read a missing name")
|
|
t:eq(string.sub(byName.comment.zip.comment, 1, 14), "a comment with", "archive comment")
|
|
t:check(byName.zip64_by_hand.zip.zip64 and byName.many.zip.zip64, "zip64 records detected")
|
|
t:check(bit32.band(byName.descriptors.zip:list()[1].flags, 8) ~= 0, "data descriptor flag")
|
|
t:check(not byName.basic.zip.zip64, "basic is not zip64")
|
|
t:eq(byName.many.zip:readString("d/65999.txt"), "65999", "last of 66000 entries")
|
|
t:eq(#byName.empty.zip:list(), 0, "empty archive")
|
|
t:check(Zip.isZip(byName.basic.data) and Zip.isZip(byName.empty.data) and not Zip.isZip("GIF89a"), "isZip")
|
|
local s = byName.basic.data
|
|
local big = buffer.create(#s + 10)
|
|
buffer.writestring(big, 4, s)
|
|
local zs = Zip.open(Bytes.reader(big, 4, #s))
|
|
t:check(zs and zs:readString("readme.txt") ~= nil, "open a sub-range Reader")
|
|
end
|
|
|
|
do
|
|
local data = buffer.fromstring(byName.basic.data)
|
|
local z = Zip.open(byName.basic.data)
|
|
local st = z:find("stored.bin")
|
|
local p = st.offset + 30 + buffer.readu16(data, st.offset + 26) + buffer.readu16(data, st.offset + 28) + 10
|
|
buffer.writeu8(data, p, bit32.bxor(buffer.readu8(data, p), 0xFF))
|
|
local z2 = Zip.open(data)
|
|
local out, err = z2:read("stored.bin")
|
|
t:check(out == nil and err.code == Err.CHECKSUM, "stored CRC mismatch: " .. tostring(err))
|
|
t:check(z2:read("stored.bin", false) ~= nil, "verify = false")
|
|
local e = table.clone(z:find("readme.txt"))
|
|
e.flags = 1
|
|
local _, ee = z:read(e)
|
|
t:check(ee and ee.code == Err.UNSUPPORTED, "encrypted refused")
|
|
e.flags, e.method = 0, 12
|
|
local _, merr = z:read(e)
|
|
t:check(merr and merr.code == Err.UNSUPPORTED and string.find(merr.message, "method 12", 1, true), "bzip2 refused: " .. tostring(merr))
|
|
local _, oe = Zip.open(5 :: any)
|
|
t:check(oe and oe.code == Err.ARG, "a non-buffer argument")
|
|
end
|
|
|
|
do
|
|
local rand = T.rng(5)
|
|
local w = Zip.writer()
|
|
local files = {}
|
|
local text = string.rep("the quick brown fox jumps over the lazy dog ", 2000)
|
|
local noise = buffer.create(5000)
|
|
for i = 0, 4999 do
|
|
buffer.writeu8(noise, i, rand(2147483648) // 8388608)
|
|
end
|
|
local function put(name: string, data: any, opts: any?)
|
|
t:check(w:add(name, data, opts), "add " .. name)
|
|
table.insert(files, { name, if type(data) == "string" then data elseif data then buffer.tostring(data) else "" })
|
|
end
|
|
put("text.txt", text, { level = 9, mtime = 1589722662 })
|
|
put("stored.txt", text, { method = "store" })
|
|
put("noise.bin", noise) -- deflate is not smaller: stored
|
|
put("empty.txt", "")
|
|
put("dir/", nil)
|
|
put("dir/\u{fc}nicode.txt", "h\u{e9}llo", { comment = "a file comment" })
|
|
put("level0.txt", text, { level = 0 })
|
|
local out, err = w:finish({ comment = "archive comment" })
|
|
t:check(out, "finish: " .. tostring(err))
|
|
local z = Zip.open(out :: buffer)
|
|
t:check(z, "our archive opens")
|
|
local list = z:list()
|
|
t:eq(#list, #files, "entry count")
|
|
for i, f in files do
|
|
t:eq(list[i].name, f[1], "name " .. i)
|
|
t:eq(z:readString(list[i]), f[2], "content " .. f[1])
|
|
end
|
|
t:eq(list[1].method, 8, "deflated")
|
|
t:check(list[1].compressedSize < #text // 10, "compressed")
|
|
t:eq(list[2].method, 0, "stored")
|
|
t:eq(list[3].method, 0, "incompressible data is stored")
|
|
t:check(list[5].isDir, "directory entry")
|
|
t:check(list[6].utf8 and not list[1].utf8, "UTF-8 flag only for non-ASCII names")
|
|
t:eq(list[6].comment, "a file comment", "entry comment")
|
|
t:eq(list[1].mtime, 1589722662, "mtime round trip")
|
|
local md = list[1].modified
|
|
t:check(md.year == 2020 and md.month == 5 and md.day == 17 and md.hour == 13 and md.min == 37 and md.sec == 42, "dos time written")
|
|
t:check(list[2].modified.year == 1980 and list[2].modified.month == 1 and list[2].modified.day == 1, "default time 1980-01-01")
|
|
t:eq(z.comment, "archive comment", "archive comment written")
|
|
t:check(not z.zip64, "small archive is not ZIP64")
|
|
local w64 = Zip.writer()
|
|
w64:add("a.txt", text)
|
|
w64:add("b.txt", "bee", { method = "store" })
|
|
local z64 = Zip.open(w64:finish({ zip64 = true }) :: buffer)
|
|
t:check(z64 and z64.zip64 and z64:readString("a.txt") == text and z64:readString("b.txt") == "bee", "forced ZIP64 round trip")
|
|
-- over 65535 entries: the ZIP64 end record
|
|
local wm = Zip.writer()
|
|
for i = 0, 65999 do
|
|
wm:add(string.format("d/%05d.txt", i), tostring(i), { method = "store" })
|
|
end
|
|
local zm = Zip.open(wm:finish() :: buffer)
|
|
t:check(zm and zm.zip64 and #zm:list() == 66000 and zm:readString("d/65999.txt") == "65999", "66000 entries")
|
|
for _, s in { 315532800, 946684799, 1700000000, 4354819198 } do
|
|
local tm, dt = Zip.dosTime(s)
|
|
t:eq(Zip.unixTime(tm, dt), s - s % 2, "dos time of " .. s)
|
|
end
|
|
t:check(select(2, Zip.dosTime(0)) == 0x21, "before 1980 clamps")
|
|
t:check(not w:add("", "x"), "empty name")
|
|
t:check(not w:add("x", 5 :: any), "bad data")
|
|
t:check(not w:add("x", "y", { method = "lzma" } :: any), "bad method")
|
|
end
|
|
|
|
do
|
|
t:eq(ZipF.name, "zip", "name")
|
|
t:eq(ZipF.category, "archive", "category")
|
|
local a, err = ZipF.decode(Bytes.reader(buffer.fromstring(byName.basic.data)))
|
|
t:check(a and a.kind == "archive" and a.format == "zip", "decode: " .. tostring(err))
|
|
local list = a:list()
|
|
t:eq(#list, byName.basic.count, "list")
|
|
t:check(list[1].name == "readme.txt" and list[1].method == "deflate" and not list[1].dir and list[1].mtime == 1589722662, "entry fields")
|
|
t:check(list[4].dir, "dir entry")
|
|
local rd = a:read("readme.txt")
|
|
t:check(rd and Crc32.of(rd :: buffer) == byName.basic.entries[1][3], "read")
|
|
t:check(a:read("MAIN.bmp") ~= nil, "read by the lookup rules")
|
|
-- edit: replace, add, remove, then write; unchanged entries are copied raw
|
|
a:add("readme.txt", "replaced")
|
|
a:add("new/one.txt", string.rep("x", 1000), { level = 9 })
|
|
t:check(a:remove("Skin/stored.bin") and not a:remove("nope"), "remove")
|
|
local out = a:write() :: buffer
|
|
local b = ZipF.decode(Bytes.reader(out)) :: any
|
|
t:check(b, "rewritten archive opens")
|
|
t:eq(buffer.tostring(b:read("readme.txt")), "replaced", "replaced entry")
|
|
t:eq(buffer.tostring(b:read("new/one.txt")), string.rep("x", 1000), "added entry")
|
|
t:check(b:read("Skin/stored.bin") == nil, "removed entry")
|
|
t:check(b:read("Skin/Main.BMP") and Crc32.of(b:read("Skin/Main.BMP")) == byName.basic.entries[2][3], "copied entry")
|
|
local names = {}
|
|
for _, e in b:list() do
|
|
table.insert(names, e.name)
|
|
end
|
|
t:eq(table.concat(names, ","), "readme.txt,Skin/Main.BMP,Skin/,Skin/sub/deep.txt,empty.txt,__MACOSX/Skin/._Main.BMP,Sk\u{ef}n/\u{fc}nicode.txt,new/one.txt", "order kept")
|
|
local c = ZipF.create()
|
|
c:add("hello.txt", "hi")
|
|
local cz = ZipF.decode(Bytes.reader(ZipF.encode(c) :: buffer)) :: any
|
|
t:eq(buffer.tostring(cz:read("hello.txt")), "hi", "create + encode")
|
|
local tz = ZipF.decode(Bytes.reader(ZipF.encode({ ["b.txt"] = "bee", ["a.txt"] = buffer.fromstring("ay") }) :: buffer)) :: any
|
|
t:check(tz:list()[1].name == "a.txt" and buffer.tostring(tz:read("b.txt")) == "bee", "encode a { name = data } table")
|
|
local _, ee = ZipF.encode(5 :: any)
|
|
t:check(ee and ee.code == Err.ARG, "encode a number")
|
|
local cm = ZipF.decode(Bytes.reader(buffer.fromstring(byName.comment.data))) :: any
|
|
t:eq(string.sub(Zip.open(cm:write()).comment, 1, 14), "a comment with", "comment kept on write")
|
|
local la = F.archive.load(byName.basic.data)
|
|
t:check(la and la.kind == "archive" and #la:list() == byName.basic.count, "F.archive.load")
|
|
t:eq(F.detect(byName.basic.data), "zip", "F.detect")
|
|
local saved = F.save(la, nil, { format = "zip" })
|
|
t:check(saved and #Zip.open(saved):list() == byName.basic.count, "F.save zip")
|
|
local saved2 = F.save(la)
|
|
t:check(saved2 ~= nil, "F.save without a format uses the source format")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, a in Fx.archives do
|
|
if a.data then
|
|
table.insert(samples, a.data)
|
|
end
|
|
end
|
|
t:fuzz("open + read every entry", function(b: buffer)
|
|
local z = Zip.open(b)
|
|
if z then
|
|
for _, e in z:list() do
|
|
z:read(e)
|
|
end
|
|
end
|
|
return z
|
|
end, samples, { mutations = 40 })
|
|
t:fuzz("archive decode + write", function(b: buffer)
|
|
local a = ZipF.decode(Bytes.reader(b))
|
|
if a then
|
|
for _, e in a:list() do
|
|
a:read(e.name)
|
|
end
|
|
a:write()
|
|
end
|
|
return a
|
|
end, samples, { mutations = 10, garbage = 50 })
|
|
|
|
t:done()
|