diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-12-09 21:26:02 +0100 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-12-09 21:26:02 +0100 |
commit | a7ba2f7b46b98d8d688706adf5b1d9495218a91d (patch) | |
tree | 028ae2275ccfd4f2cab9d33eb5dbc64820e26afe /test | |
parent | f53f079f917603a37fa92718e22af1c1c25988fa (diff) | |
download | mitmproxy-a7ba2f7b46b98d8d688706adf5b1d9495218a91d.tar.gz mitmproxy-a7ba2f7b46b98d8d688706adf5b1d9495218a91d.tar.bz2 mitmproxy-a7ba2f7b46b98d8d688706adf5b1d9495218a91d.zip |
update tests, increase coverage, add type info
Diffstat (limited to 'test')
22 files changed, 355 insertions, 333 deletions
diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index 760efa08..8b15c85b 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -131,7 +131,7 @@ def test_echo_request_line(): class TestContentView: - @mock.patch("mitmproxy.contentviews.ViewAuto.__call__") + @mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") def test_contentview(self, view_auto): view_auto.side_effect = exceptions.ContentViewException("") sio = io.StringIO() diff --git a/test/mitmproxy/contentviews/__init__.py b/test/mitmproxy/contentviews/__init__.py new file mode 100644 index 00000000..9adc57ec --- /dev/null +++ b/test/mitmproxy/contentviews/__init__.py @@ -0,0 +1,9 @@ +def full_eval(instance): + def call(data, **metadata): + x = instance(data, **metadata) + if x is None: + return None + name, generator = x + return name, list(generator) + + return call diff --git a/test/mitmproxy/contentviews/test_api.py b/test/mitmproxy/contentviews/test_api.py new file mode 100644 index 00000000..37bdd40f --- /dev/null +++ b/test/mitmproxy/contentviews/test_api.py @@ -0,0 +1,85 @@ +import mock + +from mitmproxy import contentviews +from mitmproxy.exceptions import ContentViewException +from mitmproxy.net.http import Headers +from mitmproxy.test import tutils + + +class TestContentView(contentviews.View): + name = "test" + prompt = ("t", "test") + content_types = ["test/123"] + + +def test_add_remove(): + tcv = TestContentView() + contentviews.add(tcv) + + # repeated addition causes exception + with tutils.raises(ContentViewException): + contentviews.add(tcv) + + # Same shortcut doesn't work either. + with tutils.raises(ContentViewException): + contentviews.add(TestContentView()) + + contentviews.remove(tcv) + + +def test_get_content_view(): + desc, lines, err = contentviews.get_content_view( + contentviews.get("Raw"), + b"[1, 2, 3]", + ) + assert "Raw" in desc + assert list(lines) + assert not err + + desc, lines, err = contentviews.get_content_view( + contentviews.get("Auto"), + b"[1, 2, 3]", + headers=Headers(content_type="application/json") + ) + assert desc == "JSON" + + desc, lines, err = contentviews.get_content_view( + contentviews.get("JSON"), + b"[1, 2", + ) + assert "Couldn't parse" in desc + + with mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") as view_auto: + view_auto.side_effect = ValueError + + desc, lines, err = contentviews.get_content_view( + contentviews.get("Auto"), + b"[1, 2", + ) + assert err + assert "Couldn't parse" in desc + + +def test_get_message_content_view(): + r = tutils.treq() + desc, lines, err = contentviews.get_message_content_view("raw", r) + assert desc == "Raw" + + desc, lines, err = contentviews.get_message_content_view("unknown", r) + assert desc == "Raw" + + r.encode("gzip") + desc, lines, err = contentviews.get_message_content_view("raw", r) + assert desc == "[decoded gzip] Raw" + + r.headers["content-encoding"] = "deflate" + desc, lines, err = contentviews.get_message_content_view("raw", r) + assert desc == "[cannot decode] Raw" + + r.content = None + desc, lines, err = contentviews.get_message_content_view("raw", r) + assert list(lines) == [[("error", "content missing")]] + + +def test_get_by_shortcut(): + assert contentviews.get_by_shortcut("h") diff --git a/test/mitmproxy/contentviews/test_auto.py b/test/mitmproxy/contentviews/test_auto.py new file mode 100644 index 00000000..a077affa --- /dev/null +++ b/test/mitmproxy/contentviews/test_auto.py @@ -0,0 +1,47 @@ +from mitmproxy.contentviews import auto +from mitmproxy.net import http +from mitmproxy.types import multidict +from . import full_eval + + +def test_view_auto(): + v = full_eval(auto.ViewAuto()) + f = v( + b"foo", + headers=http.Headers() + ) + assert f[0] == "Raw" + + f = v( + b"<html></html>", + headers=http.Headers(content_type="text/html") + ) + assert f[0] == "HTML" + + f = v( + b"foo", + headers=http.Headers(content_type="text/flibble") + ) + assert f[0] == "Raw" + + f = v( + b"<xml></xml>", + headers=http.Headers(content_type="text/flibble") + ) + assert f[0].startswith("XML") + + f = v(b"\xFF" * 30) + assert f[0] == "Hex" + + f = v( + b"", + headers=http.Headers() + ) + assert f[0] == "No content" + + f = v( + b"", + headers=http.Headers(), + query=multidict.MultiDict([("foo", "bar")]), + ) + assert f[0] == "Query" diff --git a/test/mitmproxy/contentviews/test_css.py b/test/mitmproxy/contentviews/test_css.py new file mode 100644 index 00000000..ecb9259b --- /dev/null +++ b/test/mitmproxy/contentviews/test_css.py @@ -0,0 +1,29 @@ +from mitmproxy.contentviews import css +from mitmproxy.test import tutils +from . import full_eval + +try: + import cssutils +except: + cssutils = None + + +def test_view_css(): + v = full_eval(css.ViewCSS()) + + with open(tutils.test_data.path('mitmproxy/data/1.css'), 'r') as fp: + fixture_1 = fp.read() + + result = v('a') + + if cssutils: + assert len(list(result[1])) == 0 + else: + assert len(list(result[1])) == 1 + + result = v(fixture_1) + + if cssutils: + assert len(list(result[1])) > 1 + else: + assert len(list(result[1])) == 1 diff --git a/test/mitmproxy/contentviews/test_hex.py b/test/mitmproxy/contentviews/test_hex.py new file mode 100644 index 00000000..4292007e --- /dev/null +++ b/test/mitmproxy/contentviews/test_hex.py @@ -0,0 +1,7 @@ +from mitmproxy.contentviews import hex +from . import full_eval + + +def test_view_hex(): + v = full_eval(hex.ViewHex()) + assert v(b"foo") diff --git a/test/mitmproxy/contentviews/test_html.py b/test/mitmproxy/contentviews/test_html.py new file mode 100644 index 00000000..8d5818e5 --- /dev/null +++ b/test/mitmproxy/contentviews/test_html.py @@ -0,0 +1,18 @@ +from mitmproxy.contentviews import html +from . import full_eval + + +def test_view_html(): + v = full_eval(html.ViewHTML()) + s = b"<html><br><br></br><p>one</p></html>" + assert v(s) + + s = b"gobbledygook" + assert not v(s) + + +def test_view_html_outline(): + v = full_eval(html.ViewHTMLOutline()) + s = b"<html><br><br></br><p>one</p></html>" + assert v(s) + assert v(b'\xfe') diff --git a/test/mitmproxy/contentviews/test_image.py b/test/mitmproxy/contentviews/test_image.py new file mode 100644 index 00000000..9e7e28f5 --- /dev/null +++ b/test/mitmproxy/contentviews/test_image.py @@ -0,0 +1,17 @@ +from mitmproxy.contentviews import image +from mitmproxy.test import tutils +from . import full_eval + + +def test_view_image(): + v = full_eval(image.ViewImage()) + for img in [ + "mitmproxy/data/image.png", + "mitmproxy/data/image.gif", + "mitmproxy/data/image-err1.jpg", + "mitmproxy/data/image.ico" + ]: + with open(tutils.test_data.path(img), "rb") as f: + assert v(f.read()) + + assert not v(b"flibble") diff --git a/test/mitmproxy/contentviews/test_javascript.py b/test/mitmproxy/contentviews/test_javascript.py new file mode 100644 index 00000000..43039c93 --- /dev/null +++ b/test/mitmproxy/contentviews/test_javascript.py @@ -0,0 +1,10 @@ +from mitmproxy.contentviews import javascript +from . import full_eval + + +def test_view_javascript(): + v = full_eval(javascript.ViewJavaScript()) + assert v(b"[1, 2, 3]") + assert v(b"[1, 2, 3") + assert v(b"function(a){[1, 2, 3]}") + assert v(b"\xfe") # invalid utf-8 diff --git a/test/mitmproxy/contentviews/test_json.py b/test/mitmproxy/contentviews/test_json.py new file mode 100644 index 00000000..5e87b570 --- /dev/null +++ b/test/mitmproxy/contentviews/test_json.py @@ -0,0 +1,16 @@ +from mitmproxy.contentviews import json +from . import full_eval + + +def test_pretty_json(): + assert json.pretty_json(b'{"foo": 1}') + assert not json.pretty_json(b"moo") + assert json.pretty_json(b'{"foo" : "\xe4\xb8\x96\xe7\x95\x8c"}') # utf8 with chinese characters + assert not json.pretty_json(b'{"foo" : "\xFF"}') + + +def test_view_json(): + v = full_eval(json.ViewJSON()) + assert v(b"{}") + assert not v(b"{") + assert v(b"[1, 2, 3, 4, 5]") diff --git a/test/mitmproxy/contentviews/test_multipart.py b/test/mitmproxy/contentviews/test_multipart.py new file mode 100644 index 00000000..48a5ccc9 --- /dev/null +++ b/test/mitmproxy/contentviews/test_multipart.py @@ -0,0 +1,25 @@ +from mitmproxy.contentviews import multipart +from mitmproxy.net import http +from . import full_eval + + +def test_view_multipart(): + view = full_eval(multipart.ViewMultipart()) + v = b""" +--AaB03x +Content-Disposition: form-data; name="submit-name" + +Larry +--AaB03x + """.strip() + h = http.Headers(content_type="multipart/form-data; boundary=AaB03x") + assert view(v, headers=h) + + h = http.Headers() + assert not view(v, headers=h) + + h = http.Headers(content_type="multipart/form-data") + assert not view(v, headers=h) + + h = http.Headers(content_type="unparseable") + assert not view(v, headers=h) diff --git a/test/mitmproxy/contentviews/test_protobuf.py b/test/mitmproxy/contentviews/test_protobuf.py new file mode 100644 index 00000000..1224b8db --- /dev/null +++ b/test/mitmproxy/contentviews/test_protobuf.py @@ -0,0 +1,12 @@ +from mitmproxy.contentviews import protobuf +from mitmproxy.test import tutils +from . import full_eval + +if protobuf.ViewProtobuf.is_available(): + def test_view_protobuf_request(): + v = full_eval(protobuf.ViewProtobuf()) + + p = tutils.test_data.path("mitmproxy/data/protobuf01") + content_type, output = v(open(p, "rb").read()) + assert content_type == "Protobuf" + assert output.next()[0][1] == '1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"' diff --git a/test/mitmproxy/contentviews/test_query.py b/test/mitmproxy/contentviews/test_query.py new file mode 100644 index 00000000..d2bddd05 --- /dev/null +++ b/test/mitmproxy/contentviews/test_query.py @@ -0,0 +1,13 @@ +from mitmproxy.contentviews import query +from mitmproxy.types import multidict +from . import full_eval + + +def test_view_query(): + d = "" + v = full_eval(query.ViewQuery()) + f = v(d, query=multidict.MultiDict([("foo", "bar")])) + assert f[0] == "Query" + assert f[1] == [[("header", "foo: "), ("text", "bar")]] + + assert v(d) == ("Query", []) diff --git a/test/mitmproxy/contentviews/test_raw.py b/test/mitmproxy/contentviews/test_raw.py new file mode 100644 index 00000000..0e6e1b34 --- /dev/null +++ b/test/mitmproxy/contentviews/test_raw.py @@ -0,0 +1,7 @@ +from mitmproxy.contentviews import raw +from . import full_eval + + +def test_view_raw(): + v = full_eval(raw.ViewRaw()) + assert v(b"foo") diff --git a/test/mitmproxy/contentviews/test_urlencoded.py b/test/mitmproxy/contentviews/test_urlencoded.py new file mode 100644 index 00000000..d01f9aaa --- /dev/null +++ b/test/mitmproxy/contentviews/test_urlencoded.py @@ -0,0 +1,15 @@ +from mitmproxy.contentviews import urlencoded +from mitmproxy.net.http import url +from . import full_eval + + +def test_view_urlencoded(): + v = full_eval(urlencoded.ViewURLEncoded()) + + d = url.encode([("one", "two"), ("three", "four")]).encode() + assert v(d) + + d = url.encode([("adsfa", "")]).encode() + assert v(d) + + assert not v(b"\xFF\x00") diff --git a/test/mitmproxy/contentviews/test_xml.py b/test/mitmproxy/contentviews/test_xml.py new file mode 100644 index 00000000..680134cb --- /dev/null +++ b/test/mitmproxy/contentviews/test_xml.py @@ -0,0 +1,17 @@ +from mitmproxy.contentviews import xml +from . import full_eval + + +def test_view_xml(): + v = full_eval(xml.ViewXML()) + assert v(b"<foo></foo>") + assert not v(b"<foo>") + s = b"""<?xml version="1.0" encoding="UTF-8"?> + <?xml-stylesheet title="XSL_formatting"?> + <rss + xmlns:media="http://search.yahoo.com/mrss/" + xmlns:atom="http://www.w3.org/2005/Atom" + version="2.0"> + </rss> + """ + assert v(s) diff --git a/test/mitmproxy/data/amf01 b/test/mitmproxy/data/amf01 Binary files differdeleted file mode 100644 index c8fc261d..00000000 --- a/test/mitmproxy/data/amf01 +++ /dev/null diff --git a/test/mitmproxy/data/amf02 b/test/mitmproxy/data/amf02 Binary files differdeleted file mode 100644 index ba69f130..00000000 --- a/test/mitmproxy/data/amf02 +++ /dev/null diff --git a/test/mitmproxy/data/amf03 b/test/mitmproxy/data/amf03 Binary files differdeleted file mode 100644 index d9fa736a..00000000 --- a/test/mitmproxy/data/amf03 +++ /dev/null diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py deleted file mode 100644 index 1f16765b..00000000 --- a/test/mitmproxy/test_contentview.py +++ /dev/null @@ -1,284 +0,0 @@ -import mock -from mitmproxy.exceptions import ContentViewException -from mitmproxy.net.http import Headers -from mitmproxy.net.http import url -from mitmproxy.types import multidict - -import mitmproxy.contentviews as cv -from mitmproxy.test import tutils - -try: - import pyamf -except ImportError: - pyamf = None - -try: - import cssutils -except: - cssutils = None - - -class TestContentView: - - def test_view_auto(self): - v = cv.ViewAuto() - f = v( - b"foo", - headers=Headers() - ) - assert f[0] == "Raw" - - f = v( - b"<html></html>", - headers=Headers(content_type="text/html") - ) - assert f[0] == "HTML" - - f = v( - b"foo", - headers=Headers(content_type="text/flibble") - ) - assert f[0] == "Raw" - - f = v( - b"<xml></xml>", - headers=Headers(content_type="text/flibble") - ) - assert f[0].startswith("XML") - - f = v( - b"", - headers=Headers() - ) - assert f[0] == "No content" - - f = v( - b"", - headers=Headers(), - query=multidict.MultiDict([("foo", "bar")]), - ) - assert f[0] == "Query" - - def test_view_urlencoded(self): - d = url.encode([("one", "two"), ("three", "four")]).encode() - v = cv.ViewURLEncoded() - assert v(d) - d = url.encode([("adsfa", "")]).encode() - v = cv.ViewURLEncoded() - assert v(d) - - def test_view_html(self): - v = cv.ViewHTML() - s = b"<html><br><br></br><p>one</p></html>" - assert v(s) - - s = b"gobbledygook" - assert not v(s) - - def test_view_html_outline(self): - v = cv.ViewHTMLOutline() - s = b"<html><br><br></br><p>one</p></html>" - assert v(s) - assert v(b'\xfe') - - def test_view_json(self): - cv.VIEW_CUTOFF = 100 - v = cv.ViewJSON() - assert v(b"{}") - assert not v(b"{") - assert v(b"[1, 2, 3, 4, 5]") - - def test_view_xml(self): - v = cv.ViewXML() - assert v(b"<foo></foo>") - assert not v(b"<foo>") - s = b"""<?xml version="1.0" encoding="UTF-8"?> - <?xml-stylesheet title="XSL_formatting"?> - <rss - xmlns:media="http://search.yahoo.com/mrss/" - xmlns:atom="http://www.w3.org/2005/Atom" - version="2.0"> - </rss> - """ - assert v(s) - - def test_view_raw(self): - v = cv.ViewRaw() - assert v(b"foo") - - def test_view_javascript(self): - v = cv.ViewJavaScript() - assert v(b"[1, 2, 3]") - assert v(b"[1, 2, 3") - assert v(b"function(a){[1, 2, 3]}") - assert v(b"\xfe") # invalid utf-8 - - def test_view_css(self): - v = cv.ViewCSS() - - with open(tutils.test_data.path('mitmproxy/data/1.css'), 'r') as fp: - fixture_1 = fp.read() - - result = v('a') - - if cssutils: - assert len(list(result[1])) == 0 - else: - assert len(list(result[1])) == 1 - - result = v(fixture_1) - - if cssutils: - assert len(list(result[1])) > 1 - else: - assert len(list(result[1])) == 1 - - def test_view_hex(self): - v = cv.ViewHex() - assert v(b"foo") - - def test_view_image(self): - v = cv.ViewImage() - p = tutils.test_data.path("mitmproxy/data/image.png") - assert v(open(p, "rb").read()) - - p = tutils.test_data.path("mitmproxy/data/image.gif") - assert v(open(p, "rb").read()) - - p = tutils.test_data.path("mitmproxy/data/image-err1.jpg") - assert v(open(p, "rb").read()) - - p = tutils.test_data.path("mitmproxy/data/image.ico") - assert v(open(p, "rb").read()) - - assert not v(b"flibble") - - def test_view_multipart(self): - view = cv.ViewMultipart() - v = b""" ---AaB03x -Content-Disposition: form-data; name="submit-name" - -Larry ---AaB03x - """.strip() - h = Headers(content_type="multipart/form-data; boundary=AaB03x") - assert view(v, headers=h) - - h = Headers() - assert not view(v, headers=h) - - h = Headers(content_type="multipart/form-data") - assert not view(v, headers=h) - - h = Headers(content_type="unparseable") - assert not view(v, headers=h) - - def test_view_query(self): - d = "" - v = cv.ViewQuery() - f = v(d, query=multidict.MultiDict([("foo", "bar")])) - assert f[0] == "Query" - assert [x for x in f[1]] == [[("header", "foo: "), ("text", "bar")]] - - def test_add_cv(self): - class TestContentView(cv.View): - name = "test" - prompt = ("t", "test") - - tcv = TestContentView() - cv.add(tcv) - - # repeated addition causes exception - tutils.raises( - ContentViewException, - cv.add, - tcv - ) - - -def test_get_content_view(): - desc, lines, err = cv.get_content_view( - cv.get("Raw"), - b"[1, 2, 3]", - ) - assert "Raw" in desc - assert list(lines) - assert not err - - desc, lines, err = cv.get_content_view( - cv.get("Auto"), - b"[1, 2, 3]", - headers=Headers(content_type="application/json") - ) - assert desc == "JSON" - - desc, lines, err = cv.get_content_view( - cv.get("JSON"), - b"[1, 2", - ) - assert "Couldn't parse" in desc - - with mock.patch("mitmproxy.contentviews.ViewAuto.__call__") as view_auto: - view_auto.side_effect = ValueError - - desc, lines, err = cv.get_content_view( - cv.get("Auto"), - b"[1, 2", - ) - assert err - assert "Couldn't parse" in desc - - -def test_get_message_content_view(): - r = tutils.treq() - desc, lines, err = cv.get_message_content_view("raw", r) - assert desc == "Raw" - - r.encode("gzip") - desc, lines, err = cv.get_message_content_view("raw", r) - assert desc == "[decoded gzip] Raw" - - r.headers["content-encoding"] = "deflate" - desc, lines, err = cv.get_message_content_view("raw", r) - assert desc == "[cannot decode] Raw" - - r.content = None - desc, lines, err = cv.get_message_content_view("raw", r) - assert list(lines) == [[("error", "content missing")]] - - -if pyamf: - def test_view_amf_request(): - v = cv.ViewAMF() - - p = tutils.test_data.path("mitmproxy/data/amf01") - assert v(open(p, "rb").read()) - - p = tutils.test_data.path("mitmproxy/data/amf02") - assert v(open(p, "rb").read()) - - def test_view_amf_response(): - v = cv.ViewAMF() - p = tutils.test_data.path("mitmproxy/data/amf03") - assert v(open(p, "rb").read()) - -if cv.ViewProtobuf.is_available(): - def test_view_protobuf_request(): - v = cv.ViewProtobuf() - - p = tutils.test_data.path("mitmproxy/data/protobuf01") - content_type, output = v(open(p, "rb").read()) - assert content_type == "Protobuf" - assert output.next()[0][1] == '1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"' - - -def test_get_by_shortcut(): - assert cv.get_by_shortcut("h") - - -def test_pretty_json(): - assert cv.pretty_json(b'{"foo": 1}') - assert not cv.pretty_json(b"moo") - assert cv.pretty_json(b'{"foo" : "\xe4\xb8\x96\xe7\x95\x8c"}') # utf8 with chinese characters - assert not cv.pretty_json(b'{"foo" : "\xFF"}') diff --git a/test/mitmproxy/test_custom_contentview.py b/test/mitmproxy/test_custom_contentview.py deleted file mode 100644 index 28f7fb33..00000000 --- a/test/mitmproxy/test_custom_contentview.py +++ /dev/null @@ -1,48 +0,0 @@ -import mitmproxy.contentviews as cv -from mitmproxy.net.http import Headers - - -def test_custom_views(): - class ViewNoop(cv.View): - name = "noop" - prompt = ("noop", "n") - content_types = ["text/none"] - - def __call__(self, data, **metadata): - return "noop", cv.format_text(data) - - view_obj = ViewNoop() - - cv.add(view_obj) - - assert cv.get("noop") - - r = cv.get_content_view( - cv.get("noop"), - "[1, 2, 3]", - headers=Headers( - content_type="text/plain" - ) - ) - assert "noop" in r[0] - - # now try content-type matching - r = cv.get_content_view( - cv.get("Auto"), - "[1, 2, 3]", - headers=Headers( - content_type="text/none" - ) - ) - assert "noop" in r[0] - - # now try removing the custom view - cv.remove(view_obj) - r = cv.get_content_view( - cv.get("Auto"), - b"[1, 2, 3]", - headers=Headers( - content_type="text/none" - ) - ) - assert "noop" not in r[0] diff --git a/test/mitmproxy/utils/test_sliding_window.py b/test/mitmproxy/utils/test_sliding_window.py new file mode 100644 index 00000000..23c76032 --- /dev/null +++ b/test/mitmproxy/utils/test_sliding_window.py @@ -0,0 +1,27 @@ +from mitmproxy.utils import sliding_window + + +def test_simple(): + y = list(sliding_window.window(range(1000, 1005), 1, 2)) + assert y == [ + # prev this next next2 + (None, 1000, 1001, 1002), + (1000, 1001, 1002, 1003), + (1001, 1002, 1003, 1004), + (1002, 1003, 1004, None), + (1003, 1004, None, None) + ] + + +def test_is_lazy(): + done = False + + def gen(): + nonlocal done + done = True + yield 42 + + x = sliding_window.window(gen(), 1, 1) + assert not done + assert list(x) + assert done |