From 2b8921690c60b7aa7a3b1e032510d61f73239283 Mon Sep 17 00:00:00 2001 From: kdeme Date: Tue, 1 Oct 2019 18:11:28 +0200 Subject: [PATCH] default init when init template is not used --- tests/fuzzing/fuzztest.nim | 35 ++++++++++++++++++++++++----------- tests/fuzzing/rlp/fuzz.nim | 4 ---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/tests/fuzzing/fuzztest.nim b/tests/fuzzing/fuzztest.nim index a8efeed..5939623 100644 --- a/tests/fuzzing/fuzztest.nim +++ b/tests/fuzzing/fuzztest.nim @@ -27,7 +27,31 @@ proc readStdin*(): seq[byte] = proc NimMain() {.importc: "NimMain".} +# The default init, gets redefined when init template is used. +template initImpl(): untyped = + when defined(standalone): + discard + else: + proc fuzzerInit(): cint {.exportc: "LLVMFuzzerInitialize".} = + NimMain() + + return 0 + +template init*(body: untyped) = + when defined(standalone): + template initImpl(): untyped = fuzz: `body` + else: + template initImpl() = + proc fuzzerInit(): cint {.exportc: "LLVMFuzzerInitialize".} = + NimMain() + + `body` + + return 0 + template test*(body: untyped): untyped = + mixin initImpl + initImpl() when defined(standalone): var payload {.inject.} = readStdin() @@ -39,14 +63,3 @@ template test*(body: untyped): untyped = makeOpenArray(data, len) `body` - -template init*(body: untyped): untyped = - when defined(standalone): - fuzz: `body` - else: - proc fuzzerInit(): cint {.exportc: "LLVMFuzzerInitialize".} = - NimMain() - - `body` - - return 0 \ No newline at end of file diff --git a/tests/fuzzing/rlp/fuzz.nim b/tests/fuzzing/rlp/fuzz.nim index af70ae3..44a7074 100644 --- a/tests/fuzzing/rlp/fuzz.nim +++ b/tests/fuzzing/rlp/fuzz.nim @@ -1,9 +1,5 @@ import chronicles, eth/rlp, ../fuzztest -# TODO: have a default init as such -init: - discard - test: try: var rlp = rlpFromBytes(@payload.toRange)