diff --git a/stateless/test_fuzz.nim b/stateless/test_fuzz.nim new file mode 100644 index 000000000..6ed5176fb --- /dev/null +++ b/stateless/test_fuzz.nim @@ -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() diff --git a/stateless/test_witness_json.nim b/stateless/test_witness_json.nim index 63d80440d..52d5c9999 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], + 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() diff --git a/stateless/test_witness_keys.nim b/stateless/test_witness_keys.nim index 5458da586..93e160916 100644 --- a/stateless/test_witness_keys.nim +++ b/stateless/test_witness_keys.nim @@ -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], diff --git a/tests/all_tests.nim b/tests/all_tests.nim index 7bbdf0a99..6b8a38a4d 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -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..