Skip to content

Instantly share code, notes, and snippets.

@mpeterv
Created September 29, 2018 10:31
Show Gist options
  • Save mpeterv/ecead7fcf9897aca3a027fbab7b50915 to your computer and use it in GitHub Desktop.
Save mpeterv/ecead7fcf9897aca3a027fbab7b50915 to your computer and use it in GitHub Desktop.
binser random bad data deserialization testing
local binser = require "binser"
local unpack = table.unpack or unpack
local error_patterns = {
"Expected more bytes of input",
"Could not deserialize type byte",
"Expected more bytes of input",
"Got nil resource name",
"Expected table metatable",
"No resources found for name",
"Expected more bytes of string",
"Could not deserialize type byte",
"Cannot deserialize class",
"Can't have nil table keys",
"Expected number"
}
local function test(bytes)
local ok, err = xpcall(function()
binser.d(string.char(unpack(bytes)))
end, debug.traceback)
if ok then
return
end
for _, error_pattern in ipairs(error_patterns) do
if err:find(error_pattern) then
return
end
end
print("Bad error: " .. err)
print("Bytes: " .. ("\\%d"):rep(#bytes):format(unpack(bytes)))
end
local function randtest()
local bytes = {}
for i = 1, math.random(1, 10) do
bytes[i] = math.random(0, 255)
end
test(bytes)
end
local seed = tonumber(arg[1]) or os.time()
print("Seed", seed)
math.randomseed(seed)
for _ = 1, 100000 do
randtest()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment