Merge pull request #508 from status-im/fuzz

add fuzz test tool
This commit is contained in:
andri lim 2020-05-21 12:39:41 +07:00 committed by GitHub
commit 6aa962cd76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 16 deletions

15
stateless/test_fuzz.nim Normal file
View File

@ -0,0 +1,15 @@
import
testutils/fuzzing, eth/trie/db,
./tree_from_witness, ./witness_types
# please read instruction in status-im/nim-testutils/fuzzing/readme.md
# or status-im/nim-testutils/fuzzing/fuzzing_on_windows.md
# if you want to run fuzz test
test:
var db = newMemoryDB()
try:
var tb = initTreeBuilder(payload, db, {wfEIP170})
let root = tb.buildTree()
except ParsingError, ContractCodeError:
debugEcho "Error detected ", getCurrentExceptionMsg()

View File

@ -1,6 +1,6 @@
import
eth/common, eth/trie/db, json, os, unittest,
../stateless/[tree_from_witness],
eth/common, eth/trie/db, json, os, unittest2,
./tree_from_witness, parseopt,
./witness_types, stew/byteutils
type
@ -99,7 +99,7 @@ proc processNode(t: var Tester, x: JsonNode, storageMode: bool = false) =
proc parseRootHash(x: string): KeccakHash =
result.data = hexToByteArray[32](x)
proc parseTester(t: var Tester, n: JsonNode) =
proc parseTester(t: var Tester, n: JsonNode, testStatusIMPL: var TestStatus) =
t.error = n["error"].getBool()
t.rootHash = parseRootHash(n["rootHash"].getStr())
t.write(n["version"])
@ -112,13 +112,13 @@ proc parseTester(t: var Tester, n: JsonNode) =
except ParsingError:
check t.error == true
proc parseTester(filename: string): Tester =
proc parseTester(filename: string, testStatusIMPL: var TestStatus): Tester =
let n = parseFile(filename)
parseTester(result, n)
parseTester(result, n, testStatusIMPL)
proc runTest(filePath, fileName: string) =
test fileName:
let t = parseTester(filePath)
let t = parseTester(filePath, testStatusIMPL)
var db = newMemoryDB()
try:
var tb = initTreeBuilder(t.output, db, {wfEIP170})
@ -129,13 +129,48 @@ proc runTest(filePath, fileName: string) =
check root == t.rootHash
check t.error == false
except ParsingError, ContractCodeError:
debugEcho "Error detected ", getCurrentExceptionMsg()
echo "Exception detected ", getCurrentExceptionMsg()
check t.error == true
proc writeFuzzData(filePath, fileName: string) =
var testStatusIMPL: TestStatus
let t = parseTester(filePath, testStatusIMPL)
var db = newMemoryDB()
var tb = initTreeBuilder(t.output, db, {wfEIP170})
let root = tb.buildTree()
writeFile(filename, t.output)
proc fuzzTool(): bool =
var filename: string
var numArg = 0
for kind, key, val in getopt():
case kind
of cmdArgument:
inc numArg
case numArg
of 1:
if key != "fuzz":
quit(1)
of 2:
filename = key
else:
discard
of cmdLongOption, cmdShortOption:
discard
of cmdEnd: assert(false) # cannot happen
if filename != "":
echo "generate fuzz data"
writeFuzzData(filename, "fuzz.data")
return true
proc witnessJsonMain*() =
for x in walkDirRec("stateless" / "fixtures"):
let y = splitPath(x)
runTest(x, y.tail)
suite "test tree builder against json fixtures":
for x in walkDirRec("stateless" / "fixtures"):
let y = splitPath(x)
runTest(x, y.tail)
when isMainModule:
witnessJsonMain()
if not fuzzTool():
witnessJsonMain()

View File

@ -1,5 +1,5 @@
import
randutils, random, unittest, stew/byteutils,
randutils, random, unittest2, stew/byteutils,
eth/[common, rlp], eth/trie/[hexary, db, trie_defs, nibbles],
faststreams/inputs, nimcrypto/sysrand,
../stateless/[witness_from_tree, tree_from_witness],

View File

@ -11,12 +11,12 @@ import threadpool
# AppVeyor may go out of memory with the default of 4
setMinPoolSize(2)
proc executeMyself(numModules: int): int =
proc executeMyself(numModules: int, names: openArray[string]): int =
let appName = getAppFilename()
for i in 0..<numModules:
let execResult = execCmd(appName & " " & $i)
if execResult != 0:
stderr.writeLine("subtest no: " & $i & " failed")
stderr.writeLine("subtest no: " & $i & " failed: " & names[i])
result = result or execResult
proc getImportStmt(stmtList: NimNode): NimNode =
@ -35,9 +35,16 @@ proc ofStmt(idx: int, singleModule: NimNode): NimNode =
newCall(moduleMain)
)
proc toModuleNames(importStmt: NimNode): NimNode =
result = nnkBracket.newTree
for singleModule in importStmt:
let x = normalize(singleModule.toStrLit.strVal)
result.add newLit(x)
macro cliBuilder(stmtList: typed): untyped =
let importStmt = stmtList.getImportStmt
let moduleCount = importStmt.len
let moduleNames = importStmt.toModuleNames
# case paramStr(1).parseInt
var caseStmt = nnkCaseStmt.newTree(
@ -59,7 +66,8 @@ macro cliBuilder(stmtList: typed): untyped =
result = quote do:
if paramCount() == 0:
quit(executeMyself(`moduleCount`))
const names = `moduleNames`
quit(executeMyself(`moduleCount`, names))
else:
disableParamFiltering()
`caseStmt`
@ -103,4 +111,3 @@ cliBuilder:
../stateless/test_witness_keys,
../stateless/test_block_witness,
../stateless/test_witness_json