295 lines
9.7 KiB
Luau
295 lines
9.7 KiB
Luau
-- containers/Blend tests: synthetic files in every header / pointer size / byte order (12-byte header with 4 and 8
|
|
-- byte pointers, little and big endian; the 5.0+ 17-byte header with large block heads), structs by name, arrays,
|
|
-- pointers, ListBase walks, addresses reused between IDs, gzip; the real files of tools/fixtures/blend.py (headers,
|
|
-- versions, zstd); errors; the fuzz pass.
|
|
local T = require("@tests/T")
|
|
local Blend = require("@rosetta/containers/Blend")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Cur = require("@tests/gen/blend_cur")
|
|
local Old = require("@tests/gen/blend_293")
|
|
|
|
local t = T.suite("containers/Blend")
|
|
|
|
-- a writer of either byte order
|
|
local function writer(little: boolean)
|
|
local parts: { string } = {}
|
|
local w = {}
|
|
local function bytes(v: number, n: number): string
|
|
local s = {}
|
|
for k = 0, n - 1 do
|
|
s[k + 1] = string.char(math.floor(v / 256 ^ k) % 256)
|
|
end
|
|
local str = table.concat(s)
|
|
return if little then str else string.reverse(str)
|
|
end
|
|
function w.u8(v: number)
|
|
table.insert(parts, string.char(v))
|
|
end
|
|
function w.i16(v: number)
|
|
table.insert(parts, bytes(v % 65536, 2))
|
|
end
|
|
function w.i32(v: number)
|
|
table.insert(parts, bytes(v % 4294967296, 4))
|
|
end
|
|
function w.u64(v: number)
|
|
table.insert(parts, bytes(v, 8))
|
|
end
|
|
function w.f32(v: number)
|
|
local b = buffer.create(4)
|
|
buffer.writef32(b, 0, v)
|
|
local s = buffer.tostring(b)
|
|
table.insert(parts, if little then s else string.reverse(s))
|
|
end
|
|
function w.f64(v: number)
|
|
local b = buffer.create(8)
|
|
buffer.writef64(b, 0, v)
|
|
local s = buffer.tostring(b)
|
|
table.insert(parts, if little then s else string.reverse(s))
|
|
end
|
|
function w.str(s: string)
|
|
table.insert(parts, s)
|
|
end
|
|
function w.ptr(v: number, ps: number)
|
|
if ps == 8 then
|
|
w.u64(v)
|
|
else
|
|
w.i32(v)
|
|
end
|
|
end
|
|
function w.done(): string
|
|
return table.concat(parts)
|
|
end
|
|
return w
|
|
end
|
|
|
|
-- SDNA: ListBase { void *first, *last }; Thing { Thing *next, *prev; int a; float b[2]; char name[8]; short s;
|
|
-- char pad[2]; void *data }; Holder { ListBase things; Thing *one; double d; ID-less }
|
|
local NAMES = { "*first", "*last", "*next", "*prev", "a", "b[2]", "name[8]", "s", "pad[2]", "*data", "things", "*one", "d" }
|
|
local TYPES = { "char", "short", "int", "float", "double", "void", "ListBase", "Thing", "Holder" }
|
|
|
|
local function sdna(little: boolean, ps: number): string
|
|
local w = writer(little)
|
|
local function pad()
|
|
local s = w.done()
|
|
return (4 - #s % 4) % 4
|
|
end
|
|
w.str("SDNA")
|
|
w.str("NAME")
|
|
w.i32(#NAMES)
|
|
for _, n in NAMES do
|
|
w.str(n .. "\0")
|
|
end
|
|
for _ = 1, pad() do
|
|
w.u8(0)
|
|
end
|
|
w.str("TYPE")
|
|
w.i32(#TYPES)
|
|
for _, n in TYPES do
|
|
w.str(n .. "\0")
|
|
end
|
|
for _ = 1, pad() do
|
|
w.u8(0)
|
|
end
|
|
w.str("TLEN")
|
|
local thing = ps * 2 + 4 + 8 + 8 + 2 + 2 + ps
|
|
for _, l in { 1, 2, 4, 4, 8, 0, ps * 2, thing, ps * 2 + ps + 8 } do
|
|
w.i16(l)
|
|
end
|
|
for _ = 1, pad() do
|
|
w.u8(0)
|
|
end
|
|
w.str("STRC")
|
|
w.i32(3)
|
|
local function struct(ty: number, fields: { { number } })
|
|
w.i16(ty)
|
|
w.i16(#fields)
|
|
for _, f in fields do
|
|
w.i16(f[1])
|
|
w.i16(f[2])
|
|
end
|
|
end
|
|
-- type / name indices are 0-based
|
|
struct(6, { { 5, 0 }, { 5, 1 } })
|
|
struct(7, { { 7, 2 }, { 7, 3 }, { 2, 4 }, { 3, 5 }, { 0, 6 }, { 1, 7 }, { 0, 8 }, { 5, 9 } })
|
|
struct(8, { { 6, 10 }, { 7, 11 }, { 4, 12 } })
|
|
return w.done()
|
|
end
|
|
|
|
local function file(little: boolean, ps: number, large: boolean, dupe: boolean?): string
|
|
local w = writer(little)
|
|
if large then
|
|
w.str("BLENDER17-01v0502")
|
|
else
|
|
w.str("BLENDER" .. (if ps == 8 then "-" else "_") .. (if little then "v" else "V") .. "279")
|
|
end
|
|
local function head(code: string, size: number, addr: number, sdnaIndex: number, count: number)
|
|
w.str(code .. string.rep("\0", 4 - #code))
|
|
if large then
|
|
w.i32(sdnaIndex)
|
|
w.u64(addr)
|
|
w.u64(size)
|
|
w.u64(count)
|
|
else
|
|
w.i32(size)
|
|
w.ptr(addr, ps)
|
|
w.i32(sdnaIndex)
|
|
w.i32(count)
|
|
end
|
|
end
|
|
local thingSize = ps * 2 + 24 + ps
|
|
head("HO", ps * 3 + 8, 0x1000, 2, 1)
|
|
w.ptr(0x2000, ps) -- things.first
|
|
w.ptr(0x3000, ps) -- things.last
|
|
w.ptr(0x3000, ps) -- one
|
|
w.f64(2.5)
|
|
local function thing(next: number, prev: number, a: number, b1: number, b2: number, name: string, s: number, data: number)
|
|
w.ptr(next, ps)
|
|
w.ptr(prev, ps)
|
|
w.i32(a)
|
|
w.f32(b1)
|
|
w.f32(b2)
|
|
w.str(name .. string.rep("\0", 8 - #name))
|
|
w.i16(s)
|
|
w.str("\0\0")
|
|
w.ptr(data, ps)
|
|
end
|
|
head("DATA", thingSize, 0x2000, 1, 1)
|
|
thing(0x3000, 0, -7, 1.5, -2.25, "first", -300, 0x4000)
|
|
head("DATA", thingSize, 0x3000, 1, 1)
|
|
thing(0, 0x2000, 123456, 0.5, 4, "second!", 1234, 0)
|
|
head("DATA", 6, 0x4000, 0, 1)
|
|
w.str("hello\0")
|
|
if dupe then
|
|
-- a second ID whose own data reuses the address 0x4000
|
|
head("H2", ps * 3 + 8, 0x5000, 2, 1)
|
|
w.ptr(0, ps)
|
|
w.ptr(0, ps)
|
|
w.ptr(0x4000, ps)
|
|
w.f64(-1)
|
|
head("DATA", thingSize, 0x4000, 1, 1)
|
|
thing(0, 0, 99, 0, 0, "dup", 0, 0)
|
|
end
|
|
local dna = sdna(little, ps)
|
|
head("DNA1", #dna, 0x9000, 0, 1)
|
|
w.str(dna)
|
|
head("ENDB", 0, 0, 0, 0)
|
|
return w.done()
|
|
end
|
|
|
|
for _, v in { { true, 8, false }, { true, 4, false }, { false, 8, false }, { false, 4, false }, { true, 8, true } } do
|
|
local little, ps, large = v[1], v[2], v[3]
|
|
local label = string.format("%s-endian, %d-byte pointers%s", if little then "little" else "big", ps, if large then ", 5.0 header" else "")
|
|
local f, err = Blend.open(buffer.fromstring(file(little, ps, large)))
|
|
t:check(f, label .. ": open: " .. tostring(err))
|
|
if not f then
|
|
continue
|
|
end
|
|
t:eq(f.pointerSize, ps, label .. ": pointer size")
|
|
t:eq(f.little, little, label .. ": byte order")
|
|
t:eq(f.version, if large then 502 else 279, label .. ": version")
|
|
t:eq(f.format, if large then 1 else 0, label .. ": format")
|
|
t:eq(#f.blocks, 5, label .. ": blocks (ENDB not listed)")
|
|
t:eq(f.dna.byName.Thing.size, ps * 3 + 24, label .. ": struct size from TLEN")
|
|
t:eq(f.dna.byName.Thing.byName.data.offset, ps * 2 + 24, label .. ": field offsets")
|
|
local ho = assert(Blend.struct(f, Blend.blocks(f, "HO")[1]))
|
|
t:eq(ho:num("d"), 2.5, label .. ": double")
|
|
local list = ho:list("things")
|
|
t:eq(#list, 2, label .. ": ListBase walk")
|
|
t:eq(list[1]:num("a"), -7, label .. ": int")
|
|
t:eq(list[2]:num("a"), 123456, label .. ": int 2")
|
|
t:eq(list[1]:num("s"), -300, label .. ": short")
|
|
t:eq(list[1]:str("name"), "first", label .. ": char array")
|
|
t:eq(list[2]:str("name"), "second!", label .. ": char array 2")
|
|
local b = list[1]:vec("b")
|
|
t:check(b and b[1] == 1.5 and b[2] == -2.25, label .. ": float array")
|
|
t:eq(list[1]:cstring("data"), "hello", label .. ": pointer to raw data")
|
|
local one = ho:deref("one")
|
|
t:check(one and one:num("a") == 123456, label .. ": deref by field type")
|
|
t:check(ho:ptr("one") ~= nil and list[2]:ptr("next") == nil, label .. ": null pointers are nil")
|
|
t:check(ho:get("missing") == nil and ho:has("d") and not ho:has("missing"), label .. ": unknown fields")
|
|
t:eq(ho:num("missing", "d"), 2.5, label .. ": the first existing of several names")
|
|
local fld = Blend.field(f, "Thing", "nope", "b")
|
|
t:check(fld and fld.count == 2 and fld.type == "float" and #fld.dims == 1, label .. ": Blend.field")
|
|
end
|
|
|
|
-- an address reused by another ID's data resolves inside the ID that points at it
|
|
do
|
|
local f = assert(Blend.open(buffer.fromstring(file(true, 8, false, true))))
|
|
local h1 = assert(Blend.struct(f, Blend.blocks(f, "HO")[1]))
|
|
local h2 = assert(Blend.struct(f, Blend.blocks(f, "H2")[1]))
|
|
t:eq(h1:list("things")[1]:cstring("data"), "hello", "reused address: the first ID's raw data")
|
|
local d = h2:deref("one")
|
|
t:check(d and d:num("a") == 99 and d:str("name") == "dup", "reused address: the second ID's own block")
|
|
end
|
|
|
|
-- gzip (Blender before 3.0)
|
|
do
|
|
local raw = file(true, 8, false)
|
|
local gz = assert(Deflate.gzip(raw))
|
|
local f, err = Blend.open(gz)
|
|
t:check(f and f.compression == "gzip" and #f.blocks == 5, "gzip: " .. tostring(err))
|
|
t:eq(Blend.sniff(gz), 0.05, "gzip sniffs weakly")
|
|
t:eq(Blend.sniff(buffer.fromstring(raw)), 1, "magic")
|
|
end
|
|
|
|
-- the real files: headers and compression
|
|
for _, set in { Cur, Old } do
|
|
for _, c in set do
|
|
local f, err = Blend.open(buffer.fromstring(c.data))
|
|
t:check(f, c.name .. ": " .. tostring(err))
|
|
if f then
|
|
local want = c.ref.version[1] * 100 + c.ref.version[2]
|
|
t:eq(f.version, want, c.name .. ": version")
|
|
t:eq(f.format, if want >= 500 then 1 else 0, c.name .. ": header format")
|
|
if string.find(c.name, "_z%.blend$") then
|
|
t:eq(f.compression, if want >= 300 then "zstd" else "gzip", c.name .. ": compression")
|
|
end
|
|
t:check(f.dna.byName.Object and f.dna.byName.Mesh and #Blend.blocks(f, "OB") > 0, c.name .. ": SDNA and objects")
|
|
end
|
|
end
|
|
end
|
|
|
|
-- errors
|
|
local function code(s: string): string?
|
|
local f, e = Blend.open(buffer.fromstring(s))
|
|
return if f then nil else e.code
|
|
end
|
|
local good = file(true, 8, false)
|
|
t:eq(code("BLEND"), Err.TRUNCATED, "short file")
|
|
t:eq(code("NOTBLEND-v279" .. string.rep("\0", 40)), Err.CORRUPT, "bad magic")
|
|
t:eq(code(string.sub(good, 1, #good - 30)), Err.TRUNCATED, "no ENDB")
|
|
t:eq(code("BLENDER17-02v0502" .. string.rep("\0", 40)), Err.UNSUPPORTED, "unknown header format version")
|
|
local noDna = string.gsub(good, "DNA1", "DNA2")
|
|
t:eq(code(noDna), Err.CORRUPT, "no DNA1")
|
|
local badDna = string.gsub(good, "STRC", "STRX")
|
|
t:eq(code(badDna), Err.CORRUPT, "bad SDNA")
|
|
local _, e = Blend.open(buffer.fromstring(good), 0, 999999)
|
|
t:eq(e and e.code, Err.ARG, "bad range")
|
|
|
|
local samples = { good, file(false, 4, false), file(true, 8, true, true) }
|
|
t:fuzz("open", function(b: buffer)
|
|
local f = Blend.open(b)
|
|
if f then
|
|
for _, blk in f.blocks do
|
|
local v = Blend.struct(f, blk)
|
|
if v then
|
|
for _, fld in v.struct.fields do
|
|
local x = v:get(fld.name)
|
|
if fld.pointer then
|
|
v:deref(fld.name)
|
|
v:array(fld.name)
|
|
v:pointers(fld.name)
|
|
v:cstring(fld.name)
|
|
elseif fld.type == "ListBase" then
|
|
v:list(fld.name)
|
|
end
|
|
local _ = x
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return f
|
|
end, samples, { mutations = 64 })
|
|
t:done()
|