Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transfer #6

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions test_generator/fuzzers/Echidna.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,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 = None

Expand Down
12 changes: 9 additions & 3 deletions test_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,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
Loading