From a83936a94c1a380fb6f61b2d3f2fd6085ad200a7 Mon Sep 17 00:00:00 2001 From: Joran Honig Date: Sun, 28 Oct 2018 17:02:39 +0100 Subject: [PATCH] Fix missing import and style --- tests/analysis/test_delegatecall.py | 10 +--- tests/disassembler/asm.py | 78 ++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/tests/analysis/test_delegatecall.py b/tests/analysis/test_delegatecall.py index c9f1b660..c5669b7c 100644 --- a/tests/analysis/test_delegatecall.py +++ b/tests/analysis/test_delegatecall.py @@ -10,7 +10,7 @@ from mythril.laser.ethereum.state import GlobalState, Environment, Account import pytest from unittest.mock import MagicMock, patch import pytest_mock - +from mythril.disassembler.disassembly import Disassembly def test_concrete_call(): # arrange @@ -109,7 +109,6 @@ def test_symbolic_call_storage_to(mocker): state = GlobalState(None, environment, None) state.mstate.memory = ["placeholder", "calldata_bling_0"] - node = Node("example") node.contract_name = "the contract name" node.function_name = "the function name" @@ -117,14 +116,12 @@ def test_symbolic_call_storage_to(mocker): to = Variable("storage_1", VarType.SYMBOLIC) call = Call(node, state, None, "Type: ", to, None) - mocker.patch.object(SymExecWrapper, "__init__", lambda x, y: None) statespace = SymExecWrapper(1) mocker.patch.object(statespace, "find_storage_write") statespace.find_storage_write.return_value = "Function name" - # act issues = _symbolic_call(call, state, address, statespace) @@ -153,7 +150,6 @@ def test_symbolic_call_calldata_to(mocker): state = GlobalState(None, environment, None) state.mstate.memory = ["placeholder", "calldata_bling_0"] - node = Node("example") node.contract_name = "the contract name" node.function_name = "the function name" @@ -161,14 +157,12 @@ def test_symbolic_call_calldata_to(mocker): to = Variable("calldata", VarType.SYMBOLIC) call = Call(node, state, None, "Type: ", to, None) - mocker.patch.object(SymExecWrapper, "__init__", lambda x, y: None) statespace = SymExecWrapper(1) - mocker.patch.object(statespace, 'find_storage_write') + mocker.patch.object(statespace, "find_storage_write") statespace.find_storage_write.return_value = "Function name" - # act issues = _symbolic_call(call, state, address, statespace) diff --git a/tests/disassembler/asm.py b/tests/disassembler/asm.py index 960c27a9..0959c5b4 100644 --- a/tests/disassembler/asm.py +++ b/tests/disassembler/asm.py @@ -1,7 +1,7 @@ from mythril.disassembler.asm import * import pytest -valid_names = [("PUSH1", 0x60), ("STOP", 0x0), ("RETURN", 0xf3)] +valid_names = [("PUSH1", 0x60), ("STOP", 0x0), ("RETURN", 0xF3)] @pytest.mark.parametrize("operation_name, hex_value", valid_names) @@ -22,36 +22,90 @@ def test_get_unknown_opcode(): sequence_match_test_data = [ # Normal no match - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], 1, False), + ( + (["PUSH1"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + 1, + False, + ), # Normal match - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}], 1, True), + ( + (["PUSH1"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}], + 1, + True, + ), # Out of bounds pattern - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], 3, False), - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], 2, False), + ( + (["PUSH1"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + 3, + False, + ), + ( + (["PUSH1"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + 2, + False, + ), # Double option match - ((["PUSH1", "PUSH3"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}], 1, True), - ((["PUSH1", "PUSH3"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], 1, True), + ( + (["PUSH1", "PUSH3"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}], + 1, + True, + ), + ( + (["PUSH1", "PUSH3"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + 1, + True, + ), # Double option no match - ((["PUSH1", "PUSH3"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], 0, False), + ( + (["PUSH1", "PUSH3"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + 0, + False, + ), ] -@pytest.mark.parametrize("pattern, instruction_list, index, expected_result", sequence_match_test_data) +@pytest.mark.parametrize( + "pattern, instruction_list, index, expected_result", sequence_match_test_data +) 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 + find_sequence_match_test_data = [ # Normal no match - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], []), + ( + (["PUSH1"], ["EQ"]), + [{"opcode": "PUSH1"}, {"opcode": "PUSH3"}, {"opcode": "EQ"}], + [], + ), # Normal match - ((["PUSH1"], ["EQ"]), [{"opcode": "PUSH1"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}, {"opcode": "PUSH1"}, {"opcode": "EQ"}], [1, 3]), + ( + (["PUSH1"], ["EQ"]), + [ + {"opcode": "PUSH1"}, + {"opcode": "PUSH1"}, + {"opcode": "EQ"}, + {"opcode": "PUSH1"}, + {"opcode": "EQ"}, + ], + [1, 3], + ), ] -@pytest.mark.parametrize("pattern, instruction_list, expected_result", find_sequence_match_test_data) +@pytest.mark.parametrize( + "pattern, instruction_list, expected_result", find_sequence_match_test_data +) def test_find_op_code_sequence(pattern, instruction_list, expected_result): # Act return_value = list(find_op_code_sequence(pattern, instruction_list))