Skip to content

Commit

Permalink
add test for #2307
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalpharush committed Apr 7, 2024
1 parent b106acf commit 4cf8c9a
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions tests/unit/core/test_using_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def test_using_for_top_level_same_name(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_top_level_implicit_conversion(solc_binary_path) -> None:
Expand All @@ -41,10 +42,11 @@ def test_using_for_top_level_implicit_conversion(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint16)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "f":
return
assert False
found = True
assert found


def test_using_for_alias_top_level(solc_binary_path) -> None:
Expand All @@ -55,17 +57,18 @@ def test_using_for_alias_top_level(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
ok = False
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "b":
ok = True
if not ok:
assert False
found = True
assert found

found = False
topLevelCall = contract_c.get_function_from_full_name("topLevel(uint256)")
for ir in topLevelCall.all_slithir_operations():
if isinstance(ir, InternalCall) and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_alias_contract(solc_binary_path) -> None:
Expand All @@ -76,17 +79,19 @@ def test_using_for_alias_contract(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("C")[0]
libCall = contract_c.get_function_from_full_name("libCall(uint256)")
ok = False
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "Lib" and ir.function_name == "b":
ok = True
if not ok:
assert False
found = True

assert found

found = False
topLevelCall = contract_c.get_function_from_full_name("topLevel(uint256)")
for ir in topLevelCall.all_slithir_operations():
if isinstance(ir, InternalCall) and ir.function_name == "a":
return
assert False
found = True
assert found


def test_using_for_in_library(solc_binary_path) -> None:
Expand All @@ -96,7 +101,36 @@ def test_using_for_in_library(solc_binary_path) -> None:
)
contract_c = slither.get_contract_from_name("A")[0]
libCall = contract_c.get_function_from_full_name("a(uint256)")
found = False
for ir in libCall.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.destination == "B" and ir.function_name == "b":
return
assert False
found = True
assert found


def test_using_for_constant_folding(slither_from_solidity_source) -> None:
# https://github.com/crytic/slither/issues/2307
source = """
library SafeMath {
uint256 private constant twelve = 12;
struct A {uint256 a;}
function add(A[twelve] storage z) internal { }
}
contract MathContract {
uint256 private constant twelve = 12;
using SafeMath for SafeMath.A[twelve];
SafeMath.A[twelve] public z;
function safeAdd() public {
z.add();
}
}
"""
with slither_from_solidity_source(source) as slither:
contract = slither.get_contract_from_name("MathContract")[0]
add = contract.get_function_from_full_name("safeAdd()")
found = False
for ir in add.all_slithir_operations():
if isinstance(ir, LibraryCall) and ir.function_name == "add":
found = True
assert found

0 comments on commit 4cf8c9a

Please sign in to comment.