-- archive/tar tests against Python's tarfile and GNU tar (tools/fixtures/tar.py). local T = require("@tests/T") local F = require("@rosetta") local TarF = require("@rosetta/archive/tar") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/tar") local t = T.suite("tar") t:eq(TarF.name, "tar", "name") local function check(a: any, want: any, label: string) local list = a:list() t:eq(#list, #want, label .. ": entry count") 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 " .. w.name) t:eq(e.type, w.type, label .. ": " .. w.name .. " type") t:eq(e.size, w.size, label .. ": " .. w.name .. " size") t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime") t:eq(e.mode, w.mode, label .. ": " .. w.name .. " mode") t:eq(e.link, w.link, label .. ": " .. w.name .. " link") t:eq(e.dir, w.type == "5", label .. ": " .. w.name .. " dir") if w.crc then local b, err = a:read(w.name) t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err)) t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data") end end end for _, c in Fx.tars do local a, err = TarF.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(a, c.name .. ": " .. tostring(err)) check(a, c.entries, c.name) t:eq(F.detect(c.data), "tar", c.name .. ": F.detect") t:note("%-18s %2d entries", c.name, #c.entries) end -- byte-identical to Python's tarfile (which pads to 10240-byte records) for _, w in Fx.writes do local list = {} for _, e in w.entries do table.insert(list, { name = e.name, data = if e.data then buffer.fromstring(e.data) else nil, dir = e.dir, type = e.type, link = e.link, mode = e.mode or (if e.dir then 0x1ED else 0x1A4), mtime = 1700000000, uname = "user", gname = "group", }) end local a = TarF.new() for _, e in list do a:add(e.name, e.data or "", { dir = e.dir, mtime = e.mtime, mode = e.mode }) local ent = a._byName[e.name] ent.type, ent.link, ent.uname, ent.gname = e.type, e.link, e.uname, e.gname end local out, err = a:write({ format = w.format }) t:check(out, w.format .. ": write: " .. tostring(err)) local s = buffer.tostring(out :: buffer) local ref = w.data t:check(#s <= #ref and string.sub(ref, 1, #s) == s and not string.find(string.sub(ref, #s + 1), "[^%z]"), w.format .. ": bytes identical to tarfile") local back = TarF.decode(Bytes.reader(out :: buffer)) t:eq(#back:list(), #list, w.format .. ": round trip count") for _, e in w.entries do if e.data then t:eq(buffer.tostring((back:read(e.name))), e.data, w.format .. ": round trip " .. e.name) end end end do local a = TarF.new() a:add("a.txt", "alpha", { mtime = 5 }) a:add("b/", "", { dir = true }) a:add("b/c.bin", buffer.fromstring("\0\1\2")) a:add("a.txt", "replaced") t:eq(#a:list(), 3, "add replaces by name") local buf = F.save(a) t:check(buf, "F.save(tar archive) uses the source format") local back = F.load(buf) t:eq(buffer.tostring(back:read("a.txt")), "replaced", "read back") t:check(back:list()[2].dir, "directory entry") local _, e = back:read("missing") t:check(e and e.code == Err.ARG, "missing entry") local g = F.load(Fx.gzips[1].data) local tb = F.save(g, nil, { format = "tar" }) local ta = F.load(tb) t:eq(ta:list()[1].name, "story.txt", "gzip -> tar entry name") t:eq(ta:list()[1].size, Fx.gzips[1].size, "gzip -> tar entry size") end for _, w in Fx.wrapped do local a, err = F.load(w.data, { name = w.name }) t:check(a, w.name .. ": " .. tostring(err)) t:eq(a.format, "tar", w.name .. ": unwrapped to the tar") t:eq(a.meta.compression, w.outer, w.name .. ": meta.compression") check(a, Fx.innerEntries, w.name) local outer = F.load(w.data, { name = w.name, unwrap = false }) t:eq(outer.format, w.outer, w.name .. ": unwrap = false keeps the outer archive") t:eq(outer:list()[1].name, "a.tar", w.name .. ": inner entry named after the file") end do local good = Fx.tars[1].data local b = buffer.fromstring(good) buffer.writeu8(b, 10, buffer.readu8(b, 10) + 1) local _, e1 = TarF.decode(Bytes.reader(b)) t:check(e1 and e1.code == Err.CORRUPT, "first header checksum: " .. tostring(e1)) local _, e2 = TarF.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 700)))) t:check(e2 and e2.code == Err.TRUNCATED, "truncated data: " .. tostring(e2)) local _, e3 = TarF.encode({ kind = "image" }) t:check(e3 and e3.code == Err.ARG, "encode a non-archive") local u = TarF.new() u:add(string.rep("y", 120), "x") local _, e4 = u:write({ format = "ustar" }) t:check(e4 and e4.code == Err.ARG, "ustar cannot hold a 120-byte name without a slash") t:eq(TarF.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 1024)))), 0, "zeros are not a tar") end local samples = {} for _, c in Fx.tars do if #c.data <= 30000 then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) local a = TarF.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 = 30, keepHeader = 0 }) t:done()