mirror of https://github.com/ConsenSys/mythril
blockchainethereumsmart-contractssoliditysecurityprogram-analysissecurity-analysissymbolic-execution
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.0 KiB
125 lines
3.0 KiB
6 years ago
|
from mythril.disassembler.asm import *
|
||
|
import pytest
|
||
|
|
||
6 years ago
|
valid_names = [("PUSH1", 0x60), ("STOP", 0x0), ("RETURN", 0xF3)]
|
||
6 years ago
|
|
||
|
|
||
|
@pytest.mark.parametrize("operation_name, hex_value", valid_names)
|
||
|
def test_get_opcode(operation_name: str, hex_value: int):
|
||
|
# Act
|
||
|
return_value = get_opcode_from_name(operation_name)
|
||
|
# Assert
|
||
|
assert return_value == hex_value
|
||
|
|
||
|
|
||
|
def test_get_unknown_opcode():
|
||
|
operation_name = "definitely unknown"
|
||
|
|
||
|
# Act
|
||
|
with pytest.raises(RuntimeError):
|
||
|
get_opcode_from_name(operation_name)
|
||
6 years ago
|
|
||
|
|
||
|
sequence_match_test_data = [
|
||
|
# Normal no match
|
||
6 years ago
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
1,
|
||
|
False,
|
||
|
),
|
||
6 years ago
|
# Normal match
|
||
6 years ago
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}],
|
||
|
1,
|
||
|
True,
|
||
|
),
|
||
6 years ago
|
# Out of bounds pattern
|
||
6 years ago
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
3,
|
||
|
False,
|
||
|
),
|
||
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
2,
|
||
|
False,
|
||
|
),
|
||
6 years ago
|
# Double option match
|
||
6 years ago
|
(
|
||
|
(["PUSH1", "PUSH3"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}],
|
||
|
1,
|
||
|
True,
|
||
|
),
|
||
|
(
|
||
|
(["PUSH1", "PUSH3"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
1,
|
||
|
True,
|
||
|
),
|
||
6 years ago
|
# Double option no match
|
||
6 years ago
|
(
|
||
|
(["PUSH1", "PUSH3"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
0,
|
||
|
False,
|
||
|
),
|
||
6 years ago
|
]
|
||
|
|
||
|
|
||
6 years ago
|
@pytest.mark.parametrize(
|
||
|
"pattern, instruction_list, index, expected_result", sequence_match_test_data
|
||
|
)
|
||
6 years ago
|
def test_is_sequence_match(pattern, instruction_list, index, expected_result):
|
||
|
# Act
|
||
|
return_value = is_sequence_match(pattern, instruction_list, index)
|
||
|
# Assert
|
||
|
assert return_value == expected_result
|
||
6 years ago
|
|
||
6 years ago
|
|
||
6 years ago
|
find_sequence_match_test_data = [
|
||
|
# Normal no match
|
||
6 years ago
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}],
|
||
|
[],
|
||
|
),
|
||
6 years ago
|
# Normal match
|
||
6 years ago
|
(
|
||
|
(["PUSH1"], ["EQ"]),
|
||
|
[
|
||
|
{"opcode": "PUSH1"},
|
||
|
{"opcode": "PUSH1"},
|
||
|
{"opcode": "EQ"},
|
||
|
{"opcode": "PUSH1"},
|
||
|
{"opcode": "EQ"},
|
||
|
],
|
||
|
[1, 3],
|
||
|
),
|
||
6 years ago
|
]
|
||
|
|
||
|
|
||
6 years ago
|
@pytest.mark.parametrize(
|
||
|
"pattern, instruction_list, expected_result", find_sequence_match_test_data
|
||
|
)
|
||
6 years ago
|
def test_find_op_code_sequence(pattern, instruction_list, expected_result):
|
||
|
# Act
|
||
|
return_value = list(find_op_code_sequence(pattern, instruction_list))
|
||
|
|
||
|
# Assert
|
||
|
assert return_value == expected_result
|
||
6 years ago
|
|
||
|
|
||
|
def test_disassemble():
|
||
|
# Act
|
||
|
instruction_list = disassemble(b"\x00\x16\x06")
|
||
|
|
||
|
# Assert
|
||
|
assert instruction_list[0]["opcode"] == "STOP"
|
||
|
assert instruction_list[1]["opcode"] == "AND"
|
||
|
assert instruction_list[2]["opcode"] == "MOD"
|