-- archive/lha tests (and codecs/compress/Lzh) against lhasa and LHa for UNIX (tools/fixtures/lha.py). local T = require("@tests/T") local F = require("@rosetta") local Lha = require("@rosetta/archive/lha") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local E = require("@tests/archive/_entries") local Fx = require("@tests/gen/lha") local t = T.suite("lha") t:eq(Lha.name, "lha", "name") local samples = {} local methods = {} for _, c in Fx.cases do local a, err = Lha.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, false) for _, l in c.links do local e = a._byName[l.name] t:check(e and e.link == l.link, c.name .. ": symlink " .. l.name) end for _, e in a:list() do methods[e.method] = (methods[e.method] or 0) + 1 end end if string.sub(c.data, 1, 2) ~= "MZ" then t:eq(F.detect(c.data), "lha", c.name .. ": detect") end if #c.data < 6000 then table.insert(samples, c.data) end end local seen = {} for m in methods do table.insert(seen, m) end table.sort(seen) t:note("methods: %s", table.concat(seen, " ")) for _, m in { "lh0", "lh1", "lh5", "lh6", "lh7", "lzs", "lz5", "lz4", "lhd" } do t:check(methods[m], "covers " .. m) end t:check(Fx.ours.ok, "lhasa extracts our archive: " .. Fx.ours.listing) E.roundTrip(t, Lha, nil, "round trip") local a = Lha.new() a:add("dir/sub/file.txt", "nested", { mode = 0x1A4, mtime = 1700000000 }) a:add("dir/", "", { dir = true, mode = 0x1ED }) local back = Lha.decode(Bytes.reader(a:write() :: buffer)) :: any t:check(back._byName["dir/sub/file.txt"] and back._byName["dir/sub/file.txt"].mode == 0x81A4 and back._byName["dir/"], "directories and modes round trip") -- errors: a flipped data byte in an lh5 entry fails its CRC (or decodes to a bad code), a cut archive is truncated local function first(pattern: string): any for _, c in Fx.cases do if string.find(c.name, pattern) then return c end end return nil end local c5 = first("lha114i %-h2 %-5") local bad = buffer.fromstring(c5.data) local ar = Lha.decode(Bytes.reader(bad)) :: any local ent = ar._byName["data.bin"] buffer.writeu8(bad, buffer.len(bad) // 2, bit32.bxor(buffer.readu8(bad, buffer.len(bad) // 2), 0x40)) -- inside dir/rand.bin local mut = Lha.decode(Bytes.reader(bad)) :: any local _, rerr = mut:read("dir/rand.bin") t:check(ent and rerr and (rerr.code == Err.CHECKSUM or rerr.code == Err.CORRUPT), "corrupt data: " .. tostring(rerr)) local _, terr = Lha.decode(Bytes.reader(buffer.fromstring(string.sub(c5.data, 1, 200)))) t:eq(terr and terr.code, Err.TRUNCATED, "cut archive") t:eq((select(2, Lha.decode(Bytes.reader(buffer.fromstring(string.rep("x", 100))))) :: any).code, Err.CORRUPT, "no header") t:fuzz("decode", function(b: buffer) local r = Lha.decode(Bytes.reader(b)) if r then for _, e in r:list() do r:read(e.name) end end return r end, samples, { mutations = 4, garbage = 50 }) t:done()