Skip to content

Commit

Permalink
Merge branch 'main' into dev/error-handling
Browse files Browse the repository at this point in the history
Merge main to test error handling additions
  • Loading branch information
tuturu-tech committed Feb 6, 2024
2 parents 13a5cfd + a70083c commit 47c0def
Show file tree
Hide file tree
Showing 13 changed files with 2,537 additions and 5 deletions.
12 changes: 10 additions & 2 deletions test_generator/fuzzers/Echidna.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,26 @@ def _parse_call_object(self, call_dict: dict[Any, Any]) -> tuple[str, str]:
time_delay = int(call_dict["delay"][0], 16)
block_delay = int(call_dict["delay"][1], 16)
has_delay = time_delay > 0 or block_delay > 0
value = int(call_dict["value"], 16)
caller = call_dict["src"]

if call_dict["call"]["tag"] == "NoCall":
template = jinja2.Template(templates["EMPTY"])
call_str = template.render(time_delay=time_delay, block_delay=block_delay)
return (call_str, "")

function_name = call_dict["call"]["contents"][0]

if not function_name:
template = jinja2.Template(templates["TRANSFER"])
call_str = template.render(
time_delay=time_delay, block_delay=block_delay, value=value, caller=caller
)
return (call_str, "")

function_parameters = call_dict["call"]["contents"][1]
if len(function_parameters) == 0:
function_parameters = ""
caller = call_dict["src"]
value = int(call_dict["value"], 16)

slither_entry_point: FunctionContract

Expand Down
12 changes: 9 additions & 3 deletions test_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ def create_poc(self) -> str:
full_path = os.path.join(self.fuzzer.reproducer_dir, entry)

if os.path.isfile(full_path):
with open(full_path, "r", encoding="utf-8") as file:
file_list.append(json.load(file))
try:
with open(full_path, "r", encoding="utf-8") as file:
file_list.append(json.load(file))
except Exception: # pylint: disable=broad-except
print(f"Fail on {full_path}")

# 2. Parse each reproducer file and add each test function to the functions list
for idx, file in enumerate(file_list):
tests_list.append(self.fuzzer.parse_reproducer(file, idx))
try:
tests_list.append(self.fuzzer.parse_reproducer(file, idx))
except Exception: # pylint: disable=broad-except
print(f"Parsing fail on {file}: index: {idx}")

# 4. Generate the test file
template = jinja2.Template(templates["CONTRACT"])
Expand Down
11 changes: 11 additions & 0 deletions test_generator/templates/foundry_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
{%- endif %}
"""

__TRANSFER__TEMPLATE: str = """
{%- if has_delay -%}
vm.warp(block.timestamp + {{time_delay}});
vm.roll(block.number + {{block_delay}});
{%- endif %}
vm.prank({{caller}});
(bool success, ) = payable(address(target)).call{value: {{value}}}("");
require(success, "Low level call failed.");
"""

__EMPTY_CALL_TEMPLATE: str = """
// This is an empty call which just increases the block number and timestamp
vm.warp(block.timestamp + {{time_delay}});
Expand All @@ -60,6 +70,7 @@
templates: dict = {
"CONTRACT": __CONTRACT_TEMPLATE,
"CALL": __CALL_TEMPLATE,
"TRANSFER": __TRANSFER__TEMPLATE,
"EMPTY_CALL": __EMPTY_CALL_TEMPLATE,
"TEST": __TEST_TEMPLATE,
"INTERFACE": __INTERFACE_TEMPLATE,
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ def structs_and_enums() -> TestGenerator:
corpus_dir = "corpus-struct"

return TestGenerator(target, target_path, corpus_dir)


@pytest.fixture
def value_transfer() -> TestGenerator:
"""Fixture for the ValueTransfer test contract"""
target = "ValueTransfer"
target_path = "./src/ValueTransfer.sol"
corpus_dir = "corpus-value"

return TestGenerator(target, target_path, corpus_dir)

Large diffs are not rendered by default.

1,022 changes: 1,022 additions & 0 deletions tests/test_data/echidna-corpora/corpus-value/covered.1707213004.html

Large diffs are not rendered by default.

Loading

0 comments on commit 47c0def

Please sign in to comment.