77 lines
3.4 KiB
Luau
77 lines
3.4 KiB
Luau
-- containers/Chunks tests: RIFF (padding, an odd chunk without its pad byte, open-ended data), IFF big-endian, PNG
|
|
-- (size first + CRC trailer, truncation), CAF 64-bit sizes, resize hooks, find, and a no-throw pass over garbage.
|
|
local T = require("@tests/T")
|
|
local Chunks = require("@rosetta/containers/Chunks")
|
|
|
|
local t = T.suite("chunks")
|
|
|
|
local function le32(n: number): string
|
|
return string.char(n % 256, (n // 256) % 256, (n // 65536) % 256, (n // 16777216) % 256)
|
|
end
|
|
local function be32(n: number): string
|
|
return string.char((n // 16777216) % 256, (n // 65536) % 256, (n // 256) % 256, n % 256)
|
|
end
|
|
|
|
-- RIFF: an odd chunk with its pad byte, then one without
|
|
do
|
|
local s = "abcd" .. le32(3) .. "xyz\0" .. "efgh" .. le32(1) .. "Q" .. "ijkl" .. le32(2) .. "12"
|
|
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.RIFF)
|
|
t:eq(#list, 3, "riff: three chunks")
|
|
t:check(list[1].id == "abcd" and list[1].offset == 8 and list[1].size == 3, "riff: first")
|
|
t:check(list[2].id == "efgh" and list[2].size == 1, "riff: an odd chunk")
|
|
t:check(list[3].id == "ijkl" and list[3].offset == 29 and list[3].avail == 2, "riff: missing pad byte detected")
|
|
t:eq((Chunks.find(list, "efgh") :: any).start, 12, "find")
|
|
-- open-ended data with size 0, and a declared size past the end
|
|
local o = "fmt " .. le32(2) .. "ab" .. "data" .. le32(0) .. "123456"
|
|
local l2 = Chunks.walk(buffer.fromstring(o), 0, #o, { pad = 2, openEnded = { data = true } })
|
|
t:check(l2[2].streamed and l2[2].size == 6, "open-ended data")
|
|
local tr = "data" .. le32(100) .. "abc"
|
|
local l3 = Chunks.walk(buffer.fromstring(tr), 0, #tr, Chunks.RIFF)
|
|
t:check(l3[1].truncated and l3[1].avail == 3 and l3[1].size == 100, "truncated chunk keeps what is there")
|
|
local l4 = Chunks.walk(buffer.fromstring(o), 0, #o, { pad = 2, resize = function(id: string, n: number)
|
|
return if id == "fmt " then 1 else nil
|
|
end })
|
|
t:eq(l4[1].size, 1, "resize hook")
|
|
end
|
|
|
|
-- IFF (big-endian sizes)
|
|
do
|
|
local s = "COMM" .. be32(2) .. "ab" .. "SSND" .. be32(3) .. "xyz\0"
|
|
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.IFF)
|
|
t:check(#list == 2 and list[2].id == "SSND" and list[2].size == 3, "iff")
|
|
end
|
|
|
|
-- PNG: size, type, data, CRC
|
|
do
|
|
local s = be32(2) .. "IHDR" .. "ab" .. "CRC!" .. be32(0) .. "IEND" .. "CRC?"
|
|
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.PNG)
|
|
t:check(#list == 2 and list[1].id == "IHDR" and list[1].offset == 8 and list[2].id == "IEND" and list[2].start == 14, "png")
|
|
local cut = string.sub(s, 1, 12)
|
|
local l2 = Chunks.walk(buffer.fromstring(cut), 0, #cut, Chunks.PNG)
|
|
t:check(l2[1].truncated, "png: a missing CRC is a truncation")
|
|
end
|
|
|
|
-- CAF: 64-bit big-endian sizes
|
|
do
|
|
local s = "desc" .. be32(0) .. be32(4) .. "abcd" .. "data" .. be32(0) .. be32(2) .. "xy"
|
|
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.CAF)
|
|
t:check(#list == 2 and list[2].offset == 28 and list[2].size == 2, "caf")
|
|
end
|
|
|
|
-- an offset range inside a bigger buffer
|
|
do
|
|
local s = "junk" .. "abcd" .. le32(1) .. "x\0" .. "tail"
|
|
local list = Chunks.walk(buffer.fromstring(s), 4, 14, Chunks.RIFF)
|
|
t:check(#list == 1 and list[1].offset == 12, "a sub-range")
|
|
end
|
|
|
|
local samples = { "abcd" .. le32(3) .. "xyz\0efgh" .. le32(1) .. "Q", be32(2) .. "IHDRabCRC!" }
|
|
t:fuzz("walk", function(b: buffer)
|
|
local n = 0
|
|
for _, layout in { Chunks.RIFF, Chunks.IFF, Chunks.PNG, Chunks.CAF } do
|
|
n += #Chunks.walk(b, 0, buffer.len(b), layout)
|
|
end
|
|
return n
|
|
end, samples)
|
|
t:done()
|