From 966ffaa3d602b10e1716824087cd1a80e85bfc2c Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 4 Feb 2017 13:34:50 +0100 Subject: coverage++ --- mitmproxy/proxy/protocol/base.py | 10 -- mitmproxy/proxy/root_context.py | 7 - test/mitmproxy/net/test_check.py | 1 + test/mitmproxy/net/test_socks.py | 7 + test/mitmproxy/test_flow.py | 13 +- test/mitmproxy/test_types_bidi.py | 13 -- test/mitmproxy/test_types_multidict.py | 207 ------------------------------ test/mitmproxy/test_types_serializable.py | 28 ---- test/mitmproxy/types/__init__.py | 0 test/mitmproxy/types/test_basethread.py | 7 + test/mitmproxy/types/test_bidi.py | 13 ++ test/mitmproxy/types/test_multidict.py | 207 ++++++++++++++++++++++++++++++ test/mitmproxy/types/test_serializable.py | 28 ++++ tox.ini | 5 +- 14 files changed, 278 insertions(+), 268 deletions(-) delete mode 100644 test/mitmproxy/test_types_bidi.py delete mode 100644 test/mitmproxy/test_types_multidict.py delete mode 100644 test/mitmproxy/test_types_serializable.py create mode 100644 test/mitmproxy/types/__init__.py create mode 100644 test/mitmproxy/types/test_basethread.py create mode 100644 test/mitmproxy/types/test_bidi.py create mode 100644 test/mitmproxy/types/test_multidict.py create mode 100644 test/mitmproxy/types/test_serializable.py diff --git a/mitmproxy/proxy/protocol/base.py b/mitmproxy/proxy/protocol/base.py index c535a1a3..93619171 100644 --- a/mitmproxy/proxy/protocol/base.py +++ b/mitmproxy/proxy/protocol/base.py @@ -74,16 +74,6 @@ class Layer(_LayerCodeCompletion): """ return getattr(self.ctx, name) - @property - def layers(self): - """ - List of all layers, including the current layer (``[self, self.ctx, self.ctx.ctx, ...]``) - """ - return [self] + self.ctx.layers - - def __repr__(self): - return type(self).__name__ - class ServerConnectionMixin: diff --git a/mitmproxy/proxy/root_context.py b/mitmproxy/proxy/root_context.py index f38f2a8c..180fc9ca 100644 --- a/mitmproxy/proxy/root_context.py +++ b/mitmproxy/proxy/root_context.py @@ -110,10 +110,3 @@ class RootContext: full_msg.append(" -> " + i) full_msg = "\n".join(full_msg) self.channel.tell("log", log.LogEntry(full_msg, level)) - - @property - def layers(self): - return [] - - def __repr__(self): - return "RootContext" diff --git a/test/mitmproxy/net/test_check.py b/test/mitmproxy/net/test_check.py index 36dca168..9dbc02e0 100644 --- a/test/mitmproxy/net/test_check.py +++ b/test/mitmproxy/net/test_check.py @@ -5,6 +5,7 @@ from mitmproxy.net import check def test_is_valid_host(): assert not check.is_valid_host(b"") + assert not check.is_valid_host(b"xn--ke.ws") assert check.is_valid_host(b"one.two") assert not check.is_valid_host(b"one" * 255) assert check.is_valid_host(b"one.two.") diff --git a/test/mitmproxy/net/test_socks.py b/test/mitmproxy/net/test_socks.py index 9b746b92..c1bd0603 100644 --- a/test/mitmproxy/net/test_socks.py +++ b/test/mitmproxy/net/test_socks.py @@ -179,6 +179,13 @@ def test_message_ipv6(): assert msg.addr.host == ipv6_addr +def test_message_invalid_host(): + raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample@com\xDE\xAD\xBE\xEF") + with pytest.raises(socks.SocksError) as exc_info: + socks.Message.from_file(raw) + assert exc_info.match("Invalid hostname: b'example@com'") + + def test_message_invalid_rsv(): raw = tutils.treader(b"\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") with pytest.raises(socks.SocksError): diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 102a714d..f546d61b 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -251,8 +251,19 @@ class TestSerialize: sio.write(b"bogus") sio.seek(0) r = mitmproxy.io.FlowReader(sio) - with pytest.raises(FlowReadException): + with pytest.raises(FlowReadException) as exc_info: list(r.stream()) + assert exc_info.match('Invalid data format') + + sio = io.BytesIO() + f = tflow.tdummyflow() + w = mitmproxy.io.FlowWriter(sio) + w.add(f) + sio.seek(0) + r = mitmproxy.io.FlowReader(sio) + with pytest.raises(FlowReadException) as exc_info: + list(r.stream()) + assert exc_info.match('Unknown flow type') f = FlowReadException("foo") assert str(f) == "foo" diff --git a/test/mitmproxy/test_types_bidi.py b/test/mitmproxy/test_types_bidi.py deleted file mode 100644 index e3a259fd..00000000 --- a/test/mitmproxy/test_types_bidi.py +++ /dev/null @@ -1,13 +0,0 @@ -import pytest -from mitmproxy.types import bidi - - -def test_bidi(): - b = bidi.BiDi(a=1, b=2) - assert b.a == 1 - assert b.get_name(1) == "a" - assert b.get_name(5) is None - with pytest.raises(AttributeError): - getattr(b, "c") - with pytest.raises(ValueError): - bidi.BiDi(one=1, two=1) diff --git a/test/mitmproxy/test_types_multidict.py b/test/mitmproxy/test_types_multidict.py deleted file mode 100644 index 9b13c5cd..00000000 --- a/test/mitmproxy/test_types_multidict.py +++ /dev/null @@ -1,207 +0,0 @@ -import pytest - -from mitmproxy.types import multidict - - -class _TMulti: - @staticmethod - def _kconv(key): - return key.lower() - - -class TMultiDict(_TMulti, multidict.MultiDict): - pass - - -class TestMultiDict: - @staticmethod - def _multi(): - return TMultiDict(( - ("foo", "bar"), - ("bar", "baz"), - ("Bar", "bam") - )) - - def test_init(self): - md = TMultiDict() - assert len(md) == 0 - - md = TMultiDict([("foo", "bar")]) - assert len(md) == 1 - assert md.fields == (("foo", "bar"),) - - def test_repr(self): - assert repr(self._multi()) == ( - "TMultiDict[('foo', 'bar'), ('bar', 'baz'), ('Bar', 'bam')]" - ) - - def test_getitem(self): - md = TMultiDict([("foo", "bar")]) - assert "foo" in md - assert "Foo" in md - assert md["foo"] == "bar" - - with pytest.raises(KeyError): - assert md["bar"] - - md_multi = TMultiDict( - [("foo", "a"), ("foo", "b")] - ) - assert md_multi["foo"] == "a" - - def test_setitem(self): - md = TMultiDict() - md["foo"] = "bar" - assert md.fields == (("foo", "bar"),) - - md["foo"] = "baz" - assert md.fields == (("foo", "baz"),) - - md["bar"] = "bam" - assert md.fields == (("foo", "baz"), ("bar", "bam")) - - def test_delitem(self): - md = self._multi() - del md["foo"] - assert "foo" not in md - assert "bar" in md - - with pytest.raises(KeyError): - del md["foo"] - - del md["bar"] - assert md.fields == () - - def test_iter(self): - md = self._multi() - assert list(md.__iter__()) == ["foo", "bar"] - - def test_len(self): - md = TMultiDict() - assert len(md) == 0 - - md = self._multi() - assert len(md) == 2 - - def test_eq(self): - assert TMultiDict() == TMultiDict() - assert not (TMultiDict() == 42) - - md1 = self._multi() - md2 = self._multi() - assert md1 == md2 - md1.fields = md1.fields[1:] + md1.fields[:1] - assert not (md1 == md2) - - def test_ne(self): - assert not TMultiDict() != TMultiDict() - assert TMultiDict() != self._multi() - assert TMultiDict() != 42 - - def test_hash(self): - """ - If a class defines mutable objects and implements an __eq__() method, - it should not implement __hash__(), since the implementation of hashable - collections requires that a key's hash value is immutable. - """ - with pytest.raises(TypeError): - assert hash(TMultiDict()) - - def test_get_all(self): - md = self._multi() - assert md.get_all("foo") == ["bar"] - assert md.get_all("bar") == ["baz", "bam"] - assert md.get_all("baz") == [] - - def test_set_all(self): - md = TMultiDict() - md.set_all("foo", ["bar", "baz"]) - assert md.fields == (("foo", "bar"), ("foo", "baz")) - - md = TMultiDict(( - ("a", "b"), - ("x", "x"), - ("c", "d"), - ("X", "X"), - ("e", "f"), - )) - md.set_all("x", ["1", "2", "3"]) - assert md.fields == ( - ("a", "b"), - ("x", "1"), - ("c", "d"), - ("X", "2"), - ("e", "f"), - ("x", "3"), - ) - md.set_all("x", ["4"]) - assert md.fields == ( - ("a", "b"), - ("x", "4"), - ("c", "d"), - ("e", "f"), - ) - - def test_add(self): - md = self._multi() - md.add("foo", "foo") - assert md.fields == ( - ("foo", "bar"), - ("bar", "baz"), - ("Bar", "bam"), - ("foo", "foo") - ) - - def test_insert(self): - md = TMultiDict([("b", "b")]) - md.insert(0, "a", "a") - md.insert(2, "c", "c") - assert md.fields == (("a", "a"), ("b", "b"), ("c", "c")) - - def test_keys(self): - md = self._multi() - assert list(md.keys()) == ["foo", "bar"] - assert list(md.keys(multi=True)) == ["foo", "bar", "Bar"] - - def test_values(self): - md = self._multi() - assert list(md.values()) == ["bar", "baz"] - assert list(md.values(multi=True)) == ["bar", "baz", "bam"] - - def test_items(self): - md = self._multi() - assert list(md.items()) == [("foo", "bar"), ("bar", "baz")] - assert list(md.items(multi=True)) == [("foo", "bar"), ("bar", "baz"), ("Bar", "bam")] - - def test_state(self): - md = self._multi() - assert len(md.get_state()) == 3 - assert md == TMultiDict.from_state(md.get_state()) - - md2 = TMultiDict() - assert md != md2 - md2.set_state(md.get_state()) - assert md == md2 - - -class TParent: - def __init__(self): - self.vals = tuple() - - def setter(self, vals): - self.vals = vals - - def getter(self): - return self.vals - - -class TestMultiDictView: - def test_modify(self): - p = TParent() - tv = multidict.MultiDictView(p.getter, p.setter) - assert len(tv) == 0 - tv["a"] = "b" - assert p.vals == (("a", "b"),) - tv["c"] = "b" - assert p.vals == (("a", "b"), ("c", "b")) - assert tv["a"] == "b" diff --git a/test/mitmproxy/test_types_serializable.py b/test/mitmproxy/test_types_serializable.py deleted file mode 100644 index dd4a3778..00000000 --- a/test/mitmproxy/test_types_serializable.py +++ /dev/null @@ -1,28 +0,0 @@ -from mitmproxy.types import serializable - - -class SerializableDummy(serializable.Serializable): - def __init__(self, i): - self.i = i - - def get_state(self): - return self.i - - def set_state(self, i): - self.i = i - - def from_state(self, state): - return type(self)(state) - - -class TestSerializable: - - def test_copy(self): - a = SerializableDummy(42) - assert a.i == 42 - b = a.copy() - assert b.i == 42 - - a.set_state(1) - assert a.i == 1 - assert b.i == 42 diff --git a/test/mitmproxy/types/__init__.py b/test/mitmproxy/types/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/mitmproxy/types/test_basethread.py b/test/mitmproxy/types/test_basethread.py new file mode 100644 index 00000000..a91588eb --- /dev/null +++ b/test/mitmproxy/types/test_basethread.py @@ -0,0 +1,7 @@ +import re +from mitmproxy.types import basethread + + +def test_basethread(): + t = basethread.BaseThread('foobar') + assert re.match('foobar - age: \d+s', t._threadinfo()) diff --git a/test/mitmproxy/types/test_bidi.py b/test/mitmproxy/types/test_bidi.py new file mode 100644 index 00000000..e3a259fd --- /dev/null +++ b/test/mitmproxy/types/test_bidi.py @@ -0,0 +1,13 @@ +import pytest +from mitmproxy.types import bidi + + +def test_bidi(): + b = bidi.BiDi(a=1, b=2) + assert b.a == 1 + assert b.get_name(1) == "a" + assert b.get_name(5) is None + with pytest.raises(AttributeError): + getattr(b, "c") + with pytest.raises(ValueError): + bidi.BiDi(one=1, two=1) diff --git a/test/mitmproxy/types/test_multidict.py b/test/mitmproxy/types/test_multidict.py new file mode 100644 index 00000000..9b13c5cd --- /dev/null +++ b/test/mitmproxy/types/test_multidict.py @@ -0,0 +1,207 @@ +import pytest + +from mitmproxy.types import multidict + + +class _TMulti: + @staticmethod + def _kconv(key): + return key.lower() + + +class TMultiDict(_TMulti, multidict.MultiDict): + pass + + +class TestMultiDict: + @staticmethod + def _multi(): + return TMultiDict(( + ("foo", "bar"), + ("bar", "baz"), + ("Bar", "bam") + )) + + def test_init(self): + md = TMultiDict() + assert len(md) == 0 + + md = TMultiDict([("foo", "bar")]) + assert len(md) == 1 + assert md.fields == (("foo", "bar"),) + + def test_repr(self): + assert repr(self._multi()) == ( + "TMultiDict[('foo', 'bar'), ('bar', 'baz'), ('Bar', 'bam')]" + ) + + def test_getitem(self): + md = TMultiDict([("foo", "bar")]) + assert "foo" in md + assert "Foo" in md + assert md["foo"] == "bar" + + with pytest.raises(KeyError): + assert md["bar"] + + md_multi = TMultiDict( + [("foo", "a"), ("foo", "b")] + ) + assert md_multi["foo"] == "a" + + def test_setitem(self): + md = TMultiDict() + md["foo"] = "bar" + assert md.fields == (("foo", "bar"),) + + md["foo"] = "baz" + assert md.fields == (("foo", "baz"),) + + md["bar"] = "bam" + assert md.fields == (("foo", "baz"), ("bar", "bam")) + + def test_delitem(self): + md = self._multi() + del md["foo"] + assert "foo" not in md + assert "bar" in md + + with pytest.raises(KeyError): + del md["foo"] + + del md["bar"] + assert md.fields == () + + def test_iter(self): + md = self._multi() + assert list(md.__iter__()) == ["foo", "bar"] + + def test_len(self): + md = TMultiDict() + assert len(md) == 0 + + md = self._multi() + assert len(md) == 2 + + def test_eq(self): + assert TMultiDict() == TMultiDict() + assert not (TMultiDict() == 42) + + md1 = self._multi() + md2 = self._multi() + assert md1 == md2 + md1.fields = md1.fields[1:] + md1.fields[:1] + assert not (md1 == md2) + + def test_ne(self): + assert not TMultiDict() != TMultiDict() + assert TMultiDict() != self._multi() + assert TMultiDict() != 42 + + def test_hash(self): + """ + If a class defines mutable objects and implements an __eq__() method, + it should not implement __hash__(), since the implementation of hashable + collections requires that a key's hash value is immutable. + """ + with pytest.raises(TypeError): + assert hash(TMultiDict()) + + def test_get_all(self): + md = self._multi() + assert md.get_all("foo") == ["bar"] + assert md.get_all("bar") == ["baz", "bam"] + assert md.get_all("baz") == [] + + def test_set_all(self): + md = TMultiDict() + md.set_all("foo", ["bar", "baz"]) + assert md.fields == (("foo", "bar"), ("foo", "baz")) + + md = TMultiDict(( + ("a", "b"), + ("x", "x"), + ("c", "d"), + ("X", "X"), + ("e", "f"), + )) + md.set_all("x", ["1", "2", "3"]) + assert md.fields == ( + ("a", "b"), + ("x", "1"), + ("c", "d"), + ("X", "2"), + ("e", "f"), + ("x", "3"), + ) + md.set_all("x", ["4"]) + assert md.fields == ( + ("a", "b"), + ("x", "4"), + ("c", "d"), + ("e", "f"), + ) + + def test_add(self): + md = self._multi() + md.add("foo", "foo") + assert md.fields == ( + ("foo", "bar"), + ("bar", "baz"), + ("Bar", "bam"), + ("foo", "foo") + ) + + def test_insert(self): + md = TMultiDict([("b", "b")]) + md.insert(0, "a", "a") + md.insert(2, "c", "c") + assert md.fields == (("a", "a"), ("b", "b"), ("c", "c")) + + def test_keys(self): + md = self._multi() + assert list(md.keys()) == ["foo", "bar"] + assert list(md.keys(multi=True)) == ["foo", "bar", "Bar"] + + def test_values(self): + md = self._multi() + assert list(md.values()) == ["bar", "baz"] + assert list(md.values(multi=True)) == ["bar", "baz", "bam"] + + def test_items(self): + md = self._multi() + assert list(md.items()) == [("foo", "bar"), ("bar", "baz")] + assert list(md.items(multi=True)) == [("foo", "bar"), ("bar", "baz"), ("Bar", "bam")] + + def test_state(self): + md = self._multi() + assert len(md.get_state()) == 3 + assert md == TMultiDict.from_state(md.get_state()) + + md2 = TMultiDict() + assert md != md2 + md2.set_state(md.get_state()) + assert md == md2 + + +class TParent: + def __init__(self): + self.vals = tuple() + + def setter(self, vals): + self.vals = vals + + def getter(self): + return self.vals + + +class TestMultiDictView: + def test_modify(self): + p = TParent() + tv = multidict.MultiDictView(p.getter, p.setter) + assert len(tv) == 0 + tv["a"] = "b" + assert p.vals == (("a", "b"),) + tv["c"] = "b" + assert p.vals == (("a", "b"), ("c", "b")) + assert tv["a"] == "b" diff --git a/test/mitmproxy/types/test_serializable.py b/test/mitmproxy/types/test_serializable.py new file mode 100644 index 00000000..dd4a3778 --- /dev/null +++ b/test/mitmproxy/types/test_serializable.py @@ -0,0 +1,28 @@ +from mitmproxy.types import serializable + + +class SerializableDummy(serializable.Serializable): + def __init__(self, i): + self.i = i + + def get_state(self): + return self.i + + def set_state(self, i): + self.i = i + + def from_state(self, state): + return type(self)(state) + + +class TestSerializable: + + def test_copy(self): + a = SerializableDummy(42) + assert a.i == 42 + b = a.copy() + assert b.i == 42 + + a.set_state(1) + assert a.i == 1 + assert b.i == 42 diff --git a/tox.ini b/tox.ini index fafa7220..a91f5d1a 100644 --- a/tox.ini +++ b/tox.ini @@ -14,16 +14,17 @@ commands = pytest --timeout 60 --cov-report='' --cov=mitmproxy --cov=pathod \ --full-cov=mitmproxy/addons/ \ --full-cov=mitmproxy/contentviews/ --no-full-cov=mitmproxy/contentviews/__init__.py --no-full-cov=mitmproxy/contentviews/protobuf.py --no-full-cov=mitmproxy/contentviews/wbxml.py --no-full-cov=mitmproxy/contentviews/xml_html.py \ - --full-cov=mitmproxy/net/ --no-full-cov=mitmproxy/net/check.py --no-full-cov=mitmproxy/net/socks.py --no-full-cov=mitmproxy/net/tcp.py --no-full-cov=mitmproxy/net/http/cookies.py --no-full-cov=mitmproxy/net/http/encoding.py --no-full-cov=mitmproxy/net/http/message.py --no-full-cov=mitmproxy/net/http/request.py --no-full-cov=mitmproxy/net/http/response.py --no-full-cov=mitmproxy/net/http/url.py \ + --full-cov=mitmproxy/net/ --no-full-cov=mitmproxy/net/tcp.py --no-full-cov=mitmproxy/net/http/cookies.py --no-full-cov=mitmproxy/net/http/encoding.py --no-full-cov=mitmproxy/net/http/message.py --no-full-cov=mitmproxy/net/http/request.py --no-full-cov=mitmproxy/net/http/response.py --no-full-cov=mitmproxy/net/http/url.py \ --full-cov=mitmproxy/proxy/ --no-full-cov=mitmproxy/proxy/protocol/ --no-full-cov=mitmproxy/proxy/config.py --no-full-cov=mitmproxy/proxy/root_context.py --no-full-cov=mitmproxy/proxy/server.py \ --full-cov=mitmproxy/script/ \ --full-cov=mitmproxy/test/ \ - --full-cov=mitmproxy/types/ --no-full-cov=mitmproxy/types/basethread.py \ + --full-cov=mitmproxy/types/ \ --full-cov=mitmproxy/utils/ \ --full-cov=mitmproxy/__init__.py \ --full-cov=mitmproxy/addonmanager.py \ --full-cov=mitmproxy/ctx.py \ --full-cov=mitmproxy/exceptions.py \ + --full-cov=mitmproxy/io.py \ --full-cov=mitmproxy/log.py \ --full-cov=mitmproxy/options.py \ --full-cov=pathod/ --no-full-cov=pathod/pathoc.py --no-full-cov=pathod/pathod.py --no-full-cov=pathod/test.py --no-full-cov=pathod/protocols/http2.py \ -- cgit v1.2.3