Skip to content

Commit

Permalink
Add some random data fuzzing to the tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpakin committed Sep 29, 2018
1 parent 9a37edc commit 32ed55c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 Calvin Rose
Copyright (c) 2015-2018 Calvin Rose and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
19 changes: 17 additions & 2 deletions spec/binser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ describe("binser", function()
"Got nil resource name",
"Expected table metatable",
"Expected more bytes of string",
"No resources found for name"
"No resources found for name",
"Cannot deserialize class",
"Expected number"
}
local function fuzzcase(str)
local ok, err = pcall(binser.d, str)
Expand All @@ -425,7 +427,8 @@ describe("binser", function()
return
end
end
error(("Bad error: %s (%q)"):format(err, str))
error(("Bad error: %s (str='%s')"):format(err, str:gsub('.',
function(x) return '\\' .. string.byte(x) end)))
end

it("Can handle all 0 and 1 byte strings for deserialization", function()
Expand All @@ -451,4 +454,16 @@ describe("binser", function()
fuzzcase("\207\140\149\188\132\1\210\19\227\172")
end)

it("Can fail gracefully on random test data", function()
math.randomseed(123456789)
local unpack = unpack or table.unpack
for _ = 1, 40000 do
local bytes = {}
for i = 1, math.random(1, 10) do
bytes[i] = math.random(0, 255)
end
fuzzcase(string.char(unpack(bytes)))
end
end)

end)

0 comments on commit 32ed55c

Please sign in to comment.