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

Use mypy to check the tests dir; fix some typing errors #1610

Merged
merged 12 commits into from
Feb 6, 2020
22 changes: 12 additions & 10 deletions manticore/utils/log.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import sys

from typing import List, Set, Tuple

manticore_verbosity = 0
DEFAULT_LOG_LEVEL = logging.WARNING
all_loggers = set()
all_loggers: Set[str] = set()
default_factory = logging.getLogRecordFactory()
logfmt = "%(asctime)s: [%(process)d] %(name)s:%(levelname)s %(message)s"
handler = logging.StreamHandler(sys.stdout)
Expand All @@ -16,7 +18,7 @@ class ContextFilter(logging.Filter):
This is a filter which injects contextual information into the log.
"""

def summarized_name(self, name):
def summarized_name(self, name: str) -> str:
"""
Produce a summarized record name
i.e. manticore.core.executor -> m.c.executor
Expand All @@ -42,7 +44,7 @@ def summarized_name(self, name):
colored_levelname_format = "\x1b[{}m{}:\x1b[0m"
plain_levelname_format = "{}:"

def colored_level_name(self, levelname):
def colored_level_name(self, levelname: str) -> str:
"""
Colors the logging level in the logging record
"""
Expand All @@ -51,7 +53,7 @@ def colored_level_name(self, levelname):
else:
return self.colored_levelname_format.format(self.color_map[levelname], levelname)

def filter(self, record):
def filter(self, record) -> bool:
record.name = self.summarized_name(record.name)
record.levelname = self.colored_level_name(record.levelname)
return True
Expand All @@ -65,7 +67,7 @@ class CustomLogger(logging.Logger):
Custom Logger class that can grab the correct verbosity level from this module
"""

def __init__(self, name, level=DEFAULT_LOG_LEVEL, *args):
def __init__(self, name: str, level=DEFAULT_LOG_LEVEL, *args) -> None:
super().__init__(name, min(get_verbosity(name), level), *args)
all_loggers.add(name)
self.initialized = False
Expand All @@ -79,11 +81,11 @@ def __init__(self, name, level=DEFAULT_LOG_LEVEL, *args):
logging.setLoggerClass(CustomLogger)


def disable_colors():
def disable_colors() -> None:
ContextFilter.colors_disabled = True


def get_levels():
def get_levels() -> List[List[Tuple[str, int]]]:
return [
# 0
[(x, DEFAULT_LOG_LEVEL) for x in all_loggers],
Expand Down Expand Up @@ -123,8 +125,8 @@ def get_levels():
]


def get_verbosity(logger_name):
def match(name, pattern):
def get_verbosity(logger_name: str) -> int:
def match(name: str, pattern: str):
"""
Pseudo globbing that only supports full fields. 'a.*.d' matches 'a.b.d'
but not 'a.b.c.d'.
Expand All @@ -146,7 +148,7 @@ def match(name, pattern):
return DEFAULT_LOG_LEVEL


def set_verbosity(setting):
def set_verbosity(setting: int) -> None:
global manticore_verbosity
manticore_verbosity = min(max(setting, 0), len(get_levels()) - 1)
for logger_name in all_loggers:
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[mypy]
python_version = 3.6
files = manticore
files = manticore, tests

# Generated file
[mypy-manticore.ethereum.parsetab]
Expand Down
22 changes: 11 additions & 11 deletions tests/ethereum/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@

from manticore.core.smtlib import operators, ConstraintSet
from manticore.ethereum import (
ManticoreEVM,
DetectIntegerOverflow,
DetectUnusedRetVal,
DetectSuicidal,
DetectDelegatecall,
DetectExternalCallAndLeak,
DetectEnvInstruction,
DetectRaceCondition,
DetectExternalCallAndLeak,
DetectIntegerOverflow,
DetectManipulableBalance,
Detector,
DetectRaceCondition,
DetectSuicidal,
DetectUnusedRetVal,
ManticoreEVM,
State,
)
from manticore.ethereum.plugins import LoopDepthLimiter, KeepOnlyIfStorageChanges

from manticore.utils import config, log

from typing import Type

consts = config.get_group("core")
consts.mprocessing = consts.mprocessing.single

Expand All @@ -39,11 +42,8 @@ def make_mock_evm_state():


class EthDetectorTest(unittest.TestCase):
"""
Subclasses must assign this class variable to the class for the detector
"""

DETECTOR_CLASS = None
# Subclasses must assign this class variable to the class for the detector
DETECTOR_CLASS: Type[Detector]

def setUp(self):
self.mevm = ManticoreEVM()
Expand Down
12 changes: 0 additions & 12 deletions tests/ethereum/test_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@


class EthSha3TestSymbolicate(unittest.TestCase):
"""
Subclasses must assign this class variable to the class for the detector
"""

DETECTOR_CLASS = None

def setUp(self):
evm_consts = config.get_group("evm")
evm_consts.sha3 = evm_consts.sha3.symbolicate
Expand Down Expand Up @@ -426,12 +420,6 @@ def test_essence3(self):


class EthSha3TestConcrete(unittest.TestCase):
"""
Subclasses must assign this class variable to the class for the detector
"""

DETECTOR_CLASS = None

def setUp(self):
evm_consts = config.get_group("evm")
evm_consts.sha3 = evm_consts.sha3.concretize
Expand Down
4 changes: 2 additions & 2 deletions tests/native/test_cpu_automatic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest
from manticore.native.cpu.x86 import *
from manticore.core.smtlib import *
import manticore.core.smtlib
from manticore.native.memory import *

solver = solver.Z3Solver.instance()
solver = manticore.core.smtlib.solver.Z3Solver.instance()


class CPUTest(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/native/test_cpu_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from manticore.native.memory import *
from manticore.core.smtlib import BitVecOr, operator, Bool
from manticore.core.smtlib.solver import Z3Solver
from .mockmem import Memory
from .mockmem import Memory as MockMemory
from functools import reduce

solver = Z3Solver.instance()
Expand Down Expand Up @@ -186,7 +186,7 @@ def write(self, value):
return self.value

def setUp(self):
mem = Memory()
mem = MockMemory()
self.cpu = I386Cpu(mem) # TODO reset cpu in between tests...
# TODO mock getchar/putchar in case the instruction accesses memory directly

Expand Down
2 changes: 1 addition & 1 deletion tests/native/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ def testmprotectFailSymbReading(self):
# No Access Reading <4160741376>
# self.assertRaisesRegexp(MemoryException, r"No access reading.*", mem.__getitem__, x)
with self.assertRaisesRegex(
InvalidSymbolicMemoryAccess, "Invalid symbolic memory access.*".format(addr)
InvalidSymbolicMemoryAccess, f"Invalid symbolic memory access.*<{addr:x}>"
):
_ = mem[x]
# mem[addr] = 'a'
Expand Down
53 changes: 0 additions & 53 deletions tests/native/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,59 +210,6 @@ def testContextSerialization(self):
new_new_state = pickle.loads(new_new_file)
self.assertEqual(new_new_state.context["step"], 30)

def testContextSerialization(self):
import pickle as pickle

initial_file = ""
new_file = ""
new_new_file = ""
constraints = ConstraintSet()
initial_state = State(constraints, FakePlatform())
initial_state.context["step"] = 10
initial_file = pickle_dumps(initial_state)
with initial_state as new_state:
self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 10)

new_state.context["step"] = 20

self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 20)
new_file = pickle_dumps(new_state)

with new_state as new_new_state:
self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 20)
self.assertEqual(new_new_state.context["step"], 20)

new_new_state.context["step"] += 10

self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 20)
self.assertEqual(new_new_state.context["step"], 30)

new_new_file = pickle_dumps(new_new_state)

self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 20)
self.assertEqual(new_new_state.context["step"], 30)

self.assertEqual(initial_state.context["step"], 10)
self.assertEqual(new_state.context["step"], 20)

self.assertEqual(initial_state.context["step"], 10)

del initial_state
del new_state
del new_new_state

initial_state = pickle.loads(initial_file)
self.assertEqual(initial_state.context["step"], 10)
new_state = pickle.loads(new_file)
self.assertEqual(new_state.context["step"], 20)
new_new_state = pickle.loads(new_new_file)
self.assertEqual(new_new_state.context["step"], 30)


class StateMergeTest(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion tests/other/utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_parse(self):
self.assertEqual(g.var3, [1, 2, 3])
self.assertEqual(g.var4, [])

def test_parse(self):
def test_parse_badconfig(self):
conf = "bad config"
f = io.StringIO(conf)
# this shouldn't raise
Expand Down