-- archive/unitypackage tests: packages built with Python's tarfile against their asset tables (paths, guids, asset -- and meta MD5s, folders); our writer's output read by Python's tarfile; api detection; errors; fuzz. -- Fixtures: tools/fixtures/unitypackage.py. local T = require("@tests/T") local F = require("@rosetta") local UnityPackage = require("@rosetta/archive/unitypackage") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Md5 = require("@rosetta/core/Md5") local Fx = require("@tests/gen/unitypackage") local t = T.suite("unitypackage") local function decode(s: string | buffer, opts: any?): (any, any) local b = if type(s) == "string" then buffer.fromstring(s) else s return UnityPackage.decode(Bytes.reader(b), opts) end t:eq(UnityPackage.name, "unitypackage", "name") t:eq(UnityPackage.category, "archive", "category") t:check(UnityPackage.caps.decode and UnityPackage.caps.encode, "caps") local function checkAssets(a: any, assets: { any }, label: string) local entries = {} for _, e in a:list() do entries[e.name] = e end local n = 0 for _, want in assets do local e = entries[want.path] t:check(e, label .. ": " .. want.path .. " listed") if e then n += 1 t:eq(e.dir, want.dir, label .. ": " .. want.path .. " dir") t:eq(e.guid, want.guid, label .. ": " .. want.path .. " guid") if not want.dir then local b = a:read(want.path) t:eq(Md5.of(b), want.md5, label .. ": " .. want.path .. " data") end local m = a:read(want.path .. ".meta") t:check(m and Md5.of(m) == want.metaMd5, label .. ": " .. want.path .. ".meta") end end t:eq(#a:list(), n * 2, label .. ": an entry and a .meta per asset, nothing else") end local samples = {} for _, c in Fx.cases do local a, err = decode(c.data) t:check(a, c.name .. ": decode: " .. tostring(err)) if a then checkAssets(a, c.assets, c.name) t:eq(a.meta.source.assets, #c.assets, c.name .. ": asset count") local e for _, x in a:list() do if x.name == "Assets/Textures/brick.png" then e = x end end local pv: any = e and (e :: any).preview and (e :: any).preview() t:check(pv and buffer.readstring(pv, 8, 7) == "preview", c.name .. ": preview.png") -- round trip through our writer local out, werr = UnityPackage.encode(a) t:check(out, c.name .. ": encode: " .. tostring(werr)) local back = out and decode(out) t:check(back, c.name .. ": re-decode") if back then checkAssets(back, c.assets, c.name .. " round trip") end end t:eq(UnityPackage.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff") t:eq(F.detect(c.data, { name = "pack.unitypackage" }), "unitypackage", c.name .. ": F.detect with the extension") table.insert(samples, c.data) end -- Python's tarfile reads ours for _, o in Fx.ours do t:check(o.ok, o.name .. ": Python reads it: " .. tostring(o.error)) local want: any = nil for _, c in Fx.cases do if c.name .. ".unitypackage" == o.name then want = c.assets end end if want then local got = {} for _, x in o.assets do got[x.path] = x end for _, w in want do local g = got[w.path] t:check(g and g.guid == w.guid and g.md5 == w.md5 and g.metaMd5 == w.metaMd5 and g.dir == w.dir, o.name .. ": " .. w.path) end t:eq(#o.assets, #want, o.name .. ": asset count") elseif o.name == "scratch.unitypackage" then local got = {} for _, x in o.assets do got[x.path] = x end t:check(got["Assets/Models"] and got["Assets/Models"].dir, "scratch: folder") t:check(got["Assets/Models/cube.obj"] and not got["Assets/Models/cube.obj"].dir, "scratch: file") t:eq(got["Assets/readme.txt"] and got["Assets/readme.txt"].guid, "0123456789abcdef0123456789abcdef", "scratch: guid from the .meta") t:eq(#o.assets, 3, "scratch: assets") end end -- from any archive (a tar) through F.save do local tar = F.format("tar").new() tar:add("Assets/a.txt", "a") tar:add("Assets/b/", "", { dir = true }) local out = assert(F.save(tar, nil, { format = "unitypackage" })) local back = assert(decode(out)) local names = {} for _, e in back:list() do names[e.name] = e.dir end t:check(names["Assets/a.txt"] == false and names["Assets/a.txt.meta"] == false and names["Assets/b"] == true, "tar -> unitypackage") local meta = buffer.tostring(back:read("Assets/b.meta")) t:check(string.find(meta, "folderAsset: yes", 1, true) ~= nil, "folder meta") end do local _, e = decode("\31\139\8\0\0\0\0\0\0\255garbage") t:check(e ~= nil, "bad deflate data") local _, e2 = decode(Fx.cases[1].data, { maxEntries = 2 }) t:check(e2 and e2.code == Err.LIMIT, "entry limit: " .. tostring(e2)) local _, e3 = UnityPackage.encode({} :: any) t:check(e3 and e3.code == Err.ARG, "encode arg") t:eq(UnityPackage.sniff(Bytes.reader(buffer.fromstring("hello"))), 0, "sniff text") end t:fuzz("decode", function(b: buffer) return decode(b) end, samples) t:done()