-- archive/iso tests (and containers/Iso9660) against xorriso, genisoimage and pycdlib (tools/fixtures/iso.py). local T = require("@tests/T") local F = require("@rosetta") local Iso = require("@rosetta/archive/iso") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/iso") local t = T.suite("iso") t:eq(Iso.name, "iso", "name") local function dataOf(c: any): string return if c.same then Fx.images[c.same].data else c.data end for _, c in Fx.images do local a, err = Iso.decode(Bytes.reader(buffer.fromstring(dataOf(c))), { prefer = c.tree :: any }) t:check(a, c.name .. ": " .. tostring(err)) t:eq(a.meta.iso.tree, c.tree, c.name .. ": name set") t:eq(F.detect(dataOf(c)), "iso", c.name .. ": F.detect") local by = {} for _, e in a:list() do by[e.name] = e end t:eq(#a:list(), #c.entries, c.name .. ": entry count") for _, w in c.entries do local e = by[w.name] t:check(e, c.name .. ": has " .. w.name) t:eq(e.dir, w.dir, c.name .. ": " .. w.name .. " dir") if w.mode then t:eq(e.mode, w.mode, c.name .. ": " .. w.name .. " mode") end if w.link then t:eq(e.type, "2", c.name .. ": " .. w.name .. " symlink") t:eq(e.link, w.link, c.name .. ": " .. w.name .. " target") end if w.mtime and not w.dir then t:eq(e.mtime, w.mtime, c.name .. ": " .. w.name .. " mtime") end if w.crc then t:eq(e.size, w.size, c.name .. ": " .. w.name .. " size") local b, rerr = a:read(w.name) t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr)) t:eq(Crc32.of(b), w.crc, c.name .. ": " .. w.name .. " data") end end t:note("%-24s %-9s %3d entries", c.name, c.tree, #c.entries) end -- the default name set: Rock Ridge over Joliet; Joliet over plain do local jr = Iso.decode(Bytes.reader(buffer.fromstring(Fx.images[5].data))) t:eq(jr.meta.iso.tree, "rockridge", "-J -R: Rock Ridge by default") t:eq(jr.meta.iso.joliet, 3, "-J -R: Joliet level 3 seen") t:check(jr.meta.iso.rockRidge, "-J -R: rockRidge") local j = Iso.decode(Bytes.reader(buffer.fromstring(Fx.images[2].data))) t:eq(j.meta.iso.tree, "joliet", "-J: Joliet by default") local p = F.load(Fx.images[1].data) t:eq(p.format, "iso", "F.load") t:eq(p.meta.iso.tree, "iso9660", "plain") t:check(p.meta.iso.blockSize == 2048 and p.meta.iso.blocks > 0, "volume facts") local tar = F.load(assert(F.save(jr, nil, { format = "tar" }))) t:eq(buffer.tostring(tar:read("hello.txt")), "hello, iso\n", "iso -> tar") end do local a = Iso.decode(Bytes.reader(buffer.fromstring(Fx.boot.data))) local bt = a.meta.boot t:check(bt, "boot catalog") t:eq(bt.catalog, Fx.boot.catalog, "boot: catalog LBA") t:eq(bt.lba, Fx.boot.lba, "boot: image LBA") t:eq(bt.sectors, Fx.boot.sectors, "boot: load size") t:eq(bt.bootable, Fx.boot.bootable, "boot: bootable") t:eq(bt.media, 0, "boot: no emulation") t:check(bt.valid, "boot: validation entry checksum") end do local a = Iso.decode(Bytes.reader(buffer.fromstring(Fx.multi.data))) local n = 0 for _, e in a:list() do if e.name == Fx.multi.name then n += 1 t:eq(e.size, Fx.multi.size, "multi-extent: size") end end t:eq(n, 1, "multi-extent: one entry") t:eq(Crc32.of(a:read(Fx.multi.name)), Fx.multi.crc, "multi-extent: data") local il = Iso.decode(Bytes.reader(buffer.fromstring(Fx.interleaved))) local _, e = il:read("HELLO.TXT") t:check(e and e.code == Err.UNSUPPORTED, "interleaved: " .. tostring(e)) end do local good = Fx.images[1].data local _, e1 = Iso.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 20000)))) t:check(e1 and e1.code == Err.TRUNCATED, "short: " .. tostring(e1)) local b = buffer.fromstring(good) buffer.writeu8(b, 16 * 2048 + 1, 0x58) local _, e2 = Iso.decode(Bytes.reader(b)) t:check(e2 and e2.code == Err.CORRUPT, "bad descriptor: " .. tostring(e2)) local cut = Iso.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 20 * 2048)))) t:check(cut == nil or cut, "a cut image: no throw") t:eq(Iso.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 40000)))), 0, "zeros are not an iso") end -- fuzz: the volume descriptors and directories (the first 64 KB of a few images: their data lies further out) local samples = {} for i, c in Fx.images do if c.data and (i == 1 or i == 3 or i == 5) then table.insert(samples, string.sub(c.data, 1, 26 * 2048)) end end t:fuzz("decode", function(b: buffer) local a = Iso.decode(Bytes.reader(b)) if a then for _, e in a:list() do a:read(e.name) end end return a end, samples, { mutations = 40, keepHeader = 16 * 2048 }) t:done()