From 0f9b31a95834a15869575016767a19fb945a535e Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 19 May 2020 11:34:40 +0700 Subject: [PATCH] add fuzz test tool --- stateless/test_fuzz.nim | 11 +++++++++++ stateless/test_witness_json.nim | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 stateless/test_fuzz.nim diff --git a/stateless/test_fuzz.nim b/stateless/test_fuzz.nim new file mode 100644 index 000000000..33e2e08aa --- /dev/null +++ b/stateless/test_fuzz.nim @@ -0,0 +1,11 @@ +import + testutils/fuzzing, eth/trie/db, + ./tree_from_witness, ./witness_types + +test: + var db = newMemoryDB() + try: + var tb = initTreeBuilder(payload, db, {wfEIP170}) + let root = tb.buildTree() + except ParsingError, ContractCodeError: + debugEcho "Error detected ", getCurrentExceptionMsg() diff --git a/stateless/test_witness_json.nim b/stateless/test_witness_json.nim index 63d80440d..4b1a2049c 100644 --- a/stateless/test_witness_json.nim +++ b/stateless/test_witness_json.nim @@ -1,6 +1,6 @@ import eth/common, eth/trie/db, json, os, unittest, - ../stateless/[tree_from_witness], + ./tree_from_witness, parseopt, ./witness_types, stew/byteutils type @@ -132,7 +132,38 @@ proc runTest(filePath, fileName: string) = debugEcho "Error detected ", getCurrentExceptionMsg() check t.error == true +proc writeFuzzData(filePath, fileName: string) = + let t = parseTester(filePath) + var db = newMemoryDB() + var tb = initTreeBuilder(t.output, db, {wfEIP170}) + let root = tb.buildTree() + writeFile(filename, t.output) + proc witnessJsonMain*() = + 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 + for x in walkDirRec("stateless" / "fixtures"): let y = splitPath(x) runTest(x, y.tail)