115 lines
5.1 KiB
Luau
115 lines
5.1 KiB
Luau
-- containers/Riff tests: the writer (RIFF, RIFX, forced RF64 with ds64, LIST/INFO, odd pad bytes, parts) read back by
|
|
-- the reader; lenient reading (missing pad byte, truncated chunk, open-ended data of size 0 / 0xFFFFFFFF, the ds64
|
|
-- table, a sub-range Reader); errors; the fuzz pass. The WAVE form is checked against ffmpeg in tests/audio/wav.luau.
|
|
local T = require("@tests/T")
|
|
local Riff = require("@rosetta/containers/Riff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
|
|
local t = T.suite("riff")
|
|
|
|
local function u32(v: number): string
|
|
local b = buffer.create(4)
|
|
buffer.writeu32(b, 0, v)
|
|
return buffer.tostring(b)
|
|
end
|
|
|
|
-- writer -> reader
|
|
local samples = {}
|
|
for _, mode in { "riff", "rifx", "rf64" } do
|
|
local w = Riff.writer("TEST", { bigEndian = mode == "rifx", rf64 = mode == "rf64", sampleCount = 77 })
|
|
w:chunk("abcd", "odd")
|
|
w:chunk("efgh", buffer.fromstring("xxevenxx"), 2, 4)
|
|
w:list("INFO", { { "INAM", "name" }, { "IART", buffer.fromstring("raw") } })
|
|
w:chunk("data", { buffer.fromstring("12345"), buffer.fromstring("678") })
|
|
local file = w:finish() :: buffer
|
|
table.insert(samples, buffer.tostring(file))
|
|
local f, err = Riff.read(Bytes.reader(file))
|
|
t:check(f, mode .. ": read: " .. tostring(err))
|
|
t:eq(f.container, if mode == "rifx" then "RIFX" elseif mode == "rf64" then "RF64" else "RIFF", mode .. ": container")
|
|
t:eq(f.form, "TEST", mode .. ": form")
|
|
t:eq(f.size, buffer.len(file) - 8, mode .. ": RIFF size")
|
|
local ids = {}
|
|
for _, c in f.chunks do
|
|
table.insert(ids, c.id)
|
|
end
|
|
t:eq(table.concat(ids, ","), if mode == "rf64" then "ds64,abcd,efgh,LIST,data" else "abcd,efgh,LIST,data", mode .. ": chunks")
|
|
local a = Riff.find(f, "abcd") :: any
|
|
t:check(a.size == 3 and buffer.readstring(file, a.offset, 3) == "odd", mode .. ": odd chunk")
|
|
local e = Riff.find(f, "efgh") :: any
|
|
t:eq(buffer.readstring(file, e.offset, e.size), "even", mode .. ": range chunk")
|
|
local l = Riff.find(f, "LIST") :: any
|
|
t:eq(Riff.listType(f, l), "INFO", mode .. ": list type")
|
|
local subs = Riff.list(f, l)
|
|
t:check(#subs == 2 and subs[1].id == "INAM" and buffer.readstring(file, subs[1].offset, subs[1].size) == "name\0", mode .. ": INFO item")
|
|
t:check(subs[2].size == 3 and subs[2].avail == 3, mode .. ": raw item")
|
|
local d = Riff.find(f, "data") :: any
|
|
t:check(d.size == 8 and buffer.readstring(file, d.offset, 8) == "12345678", mode .. ": data parts")
|
|
t:eq(buffer.tostring(Riff.body(f, d):bytes(8)), "12345678", mode .. ": body reader")
|
|
if mode == "rf64" then
|
|
t:check(f.ds64 and f.ds64.dataSize == 8 and f.ds64.sampleCount == 77 and d.declared == 0xFFFFFFFF, "rf64: ds64")
|
|
end
|
|
-- parts: the same bytes
|
|
local parts = w:finish({ parts = true }) :: { buffer }
|
|
local j = Bytes.writer()
|
|
for _, p in parts do
|
|
j:bytes(p)
|
|
end
|
|
t:check(buffer.tostring(j:finish()) == buffer.tostring(file), mode .. ": parts join to the file")
|
|
end
|
|
t:check(not Riff.writer("WAVE", { bigEndian = true, rf64 = true }):finish(), "RIFX RF64 refused")
|
|
|
|
-- lenient reading
|
|
do
|
|
-- a missing pad byte after an odd chunk
|
|
local body = "WAVE" .. "abcd" .. u32(5) .. "12345" .. "next" .. u32(2) .. "ok"
|
|
local f = Riff.read(buffer.fromstring("RIFF" .. u32(#body) .. body)) :: any
|
|
t:check(#f.chunks == 2 and f.chunks[2].id == "next", "missing pad byte detected")
|
|
-- truncated
|
|
local tb = "WAVE" .. "data" .. u32(100) .. "short"
|
|
local ft = Riff.read(buffer.fromstring("RIFF" .. u32(#tb) .. tb)) :: any
|
|
t:check(ft.chunks[1].truncated and ft.chunks[1].avail == 5 and ft.chunks[1].size == 100, "truncated chunk")
|
|
-- open-ended
|
|
for _, sz in { 0, 0xFFFFFFFF } do
|
|
local ob = "WAVE" .. "data" .. u32(sz) .. "abcdefLIST"
|
|
local fo = Riff.read(buffer.fromstring("RIFF" .. u32(0) .. ob), { openEnded = { data = true } }) :: any
|
|
t:check(#fo.chunks == 1 and fo.chunks[1].streamed and fo.chunks[1].size == 10, "open-ended data " .. sz)
|
|
end
|
|
-- ds64 table
|
|
local ds = "ds64" .. u32(40) .. u32(0) .. u32(0) .. u32(4) .. u32(0) .. u32(0) .. u32(0) .. u32(1) .. "big " .. u32(3) .. u32(0)
|
|
local rb = "WAVE" .. ds .. "big " .. u32(0xFFFFFFFF) .. "xyz" .. "\0"
|
|
local fr = Riff.read(buffer.fromstring("RF64" .. u32(0xFFFFFFFF) .. rb)) :: any
|
|
t:check(fr.ds64 and fr.ds64.table["big "] == 3 and fr.chunks[2].size == 3, "ds64 table")
|
|
-- a sub-range reader
|
|
local s = samples[1]
|
|
local big = buffer.create(#s + 7)
|
|
buffer.writestring(big, 3, s)
|
|
local fs = Riff.read(Bytes.reader(big, 3, #s)) :: any
|
|
t:check(fs and fs.form == "TEST" and #fs.chunks == 4 and fs.base == 3, "sub-range reader")
|
|
t:eq(Riff.sniff(Bytes.reader(buffer.fromstring(s))), "TEST", "sniff")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local _, e1 = Riff.read(buffer.fromstring("RIFF"))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "too short")
|
|
local _, e2 = Riff.read(buffer.fromstring("OggS00000000"))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "not RIFF")
|
|
local _, e3 = Riff.read(42 :: any)
|
|
t:check(e3 and e3.code == Err.ARG, "not a buffer")
|
|
end
|
|
|
|
t:fuzz("read + list", function(b: buffer)
|
|
local f = Riff.read(Bytes.reader(b), { openEnded = { data = true } })
|
|
if f then
|
|
for _, c in f.chunks do
|
|
if c.id == "LIST" then
|
|
Riff.list(f, c)
|
|
end
|
|
end
|
|
end
|
|
return f
|
|
end, samples, { mutations = 30 })
|
|
|
|
t:done()
|