113 lines
3.3 KiB
Luau
113 lines
3.3 KiB
Luau
--!strict
|
|
-- tests/T: the test helper; a failed check throws, so tools/test.sh sees a non-zero exit.
|
|
local T = {}
|
|
|
|
T.runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
|
|
|
|
function T.rng(seed: number): (n: number) -> number
|
|
local s = seed % 2147483648
|
|
return function(n: number): number
|
|
s = (s * 1103515245 + 12345) % 2147483648
|
|
return s % n
|
|
end
|
|
end
|
|
|
|
function T.bytes(t: { number }): buffer
|
|
local b = buffer.create(#t)
|
|
for i, v in t do
|
|
buffer.writeu8(b, i - 1, v)
|
|
end
|
|
return b
|
|
end
|
|
|
|
function T.hex(b: buffer, max: number?): string
|
|
local n = math.min(buffer.len(b), max or 64)
|
|
local out = table.create(n)
|
|
for i = 0, n - 1 do
|
|
out[i + 1] = string.format("%02x", buffer.readu8(b, i))
|
|
end
|
|
return table.concat(out, " ") .. (if buffer.len(b) > n then " ..." else "")
|
|
end
|
|
|
|
function T.clock(): number
|
|
return os.clock()
|
|
end
|
|
|
|
local Suite = {}
|
|
Suite.__index = Suite
|
|
|
|
export type Suite = typeof(setmetatable({} :: { name: string, passed: number, t0: number }, Suite))
|
|
|
|
function T.suite(name: string): Suite
|
|
return setmetatable({ name = name, passed = 0, t0 = os.clock() }, Suite)
|
|
end
|
|
|
|
function Suite.check(self: Suite, cond: any, label: string)
|
|
if not cond then
|
|
error(string.format("FAILED [%s, %s]: %s", self.name, T.runtime, label), 2)
|
|
end
|
|
self.passed += 1
|
|
end
|
|
|
|
function Suite.eq(self: Suite, got: any, want: any, label: string)
|
|
if got ~= want then
|
|
error(string.format("FAILED [%s, %s]: %s: got %s, want %s", self.name, T.runtime, label, tostring(got), tostring(want)), 2)
|
|
end
|
|
self.passed += 1
|
|
end
|
|
|
|
function Suite.note(_self: Suite, fmt: string, ...: any)
|
|
print(" " .. string.format(fmt, ...))
|
|
end
|
|
|
|
-- the no-throw pass: truncations, opts.mutations (default 8) mutated copies, opts.garbage (default 200) random buffers.
|
|
function Suite.fuzz(self: Suite, label: string, decode: (buffer) -> any, samples: { any }, opts: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? }?): (number, number)
|
|
local o: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? } = opts or {}
|
|
local rand = T.rng(o.seed or 1234)
|
|
local tries, threw = 0, 0
|
|
local function try(b: buffer, what: string)
|
|
tries += 1
|
|
local ok, err = pcall(decode, b)
|
|
if not ok then
|
|
threw += 1
|
|
print(string.format(" THREW on %s (%s): %s", what, label, tostring(err)))
|
|
end
|
|
end
|
|
local keep = o.keepHeader or 0
|
|
for _, s in samples do
|
|
local str = if type(s) == "string" then s else buffer.tostring(s)
|
|
local n = #str
|
|
for _, cut in { 1, 2, 4, 8, 16, 33, n // 3, n // 2, n - 13, n - 4, n - 1 } do
|
|
if cut > 0 and cut < n then
|
|
try(buffer.fromstring(string.sub(str, 1, cut)), "truncation")
|
|
end
|
|
end
|
|
if n > keep then
|
|
for _ = 1, o.mutations or 8 do
|
|
local b = buffer.fromstring(str)
|
|
for _ = 1, 1 + rand(4) do
|
|
buffer.writeu8(b, keep + rand(n - keep), rand(256))
|
|
end
|
|
try(b, "mutation")
|
|
end
|
|
end
|
|
end
|
|
for _ = 1, o.garbage or 200 do
|
|
local n = rand(300)
|
|
local b = buffer.create(n)
|
|
for k = 0, n - 1 do
|
|
buffer.writeu8(b, k, rand(256))
|
|
end
|
|
try(b, "garbage")
|
|
end
|
|
self:check(threw == 0, label .. ": bad input never throws (" .. threw .. " of " .. tries .. " threw)")
|
|
self:note("%s: %d bad inputs, none threw", label, tries)
|
|
return tries, threw
|
|
end
|
|
|
|
function Suite.done(self: Suite)
|
|
print(string.format("%s: %d checks passed (%s, %.2f s)", self.name, self.passed, T.runtime, os.clock() - self.t0))
|
|
end
|
|
|
|
return T
|