From 7e6d014f8f0b4e89c7f4cd97e9ea4856a6771981 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 2 Nov 2016 10:06:25 +1300 Subject: tests: pathod/tutils.py -> pathod/tservers.py And remove all aliases for mitmproxy.test.tutils --- test/pathod/test_language_base.py | 2 +- test/pathod/test_language_generators.py | 2 +- test/pathod/test_language_http.py | 7 +- test/pathod/test_language_http2.py | 2 +- test/pathod/test_language_websocket.py | 9 +- test/pathod/test_pathoc.py | 12 +-- test/pathod/test_pathoc_cmdline.py | 4 +- test/pathod/test_pathod.py | 23 ++--- test/pathod/test_pathod_cmdline.py | 6 +- test/pathod/test_test.py | 6 +- test/pathod/test_utils.py | 2 +- test/pathod/tservers.py | 145 ++++++++++++++++++++++++++++++ test/pathod/tutils.py | 153 -------------------------------- 13 files changed, 184 insertions(+), 189 deletions(-) create mode 100644 test/pathod/tservers.py delete mode 100644 test/pathod/tutils.py (limited to 'test/pathod') diff --git a/test/pathod/test_language_base.py b/test/pathod/test_language_base.py index b8abc9ba..a77aced0 100644 --- a/test/pathod/test_language_base.py +++ b/test/pathod/test_language_base.py @@ -2,7 +2,7 @@ import os from pathod import language from pathod.language import base, exceptions -from . import tutils +from mitmproxy.test import tutils def parse_request(s): diff --git a/test/pathod/test_language_generators.py b/test/pathod/test_language_generators.py index 4ec6ec3f..b3ce0335 100644 --- a/test/pathod/test_language_generators.py +++ b/test/pathod/test_language_generators.py @@ -1,7 +1,7 @@ import os from pathod.language import generators -from . import tutils +from mitmproxy.test import tutils def test_randomgenerator(): diff --git a/test/pathod/test_language_http.py b/test/pathod/test_language_http.py index e1eabe4a..9a239bf5 100644 --- a/test/pathod/test_language_http.py +++ b/test/pathod/test_language_http.py @@ -2,7 +2,8 @@ import io from pathod import language from pathod.language import http, base -from . import tutils +from mitmproxy.test import tutils +from . import tservers def parse_request(s): @@ -302,8 +303,8 @@ def test_shortcuts(): assert next(language.parse_pathod( "400:l'foo'")).headers[0].key.val == b"Location" - assert b"Android" in tutils.render(parse_request("get:/:ua")) - assert b"User-Agent" in tutils.render(parse_request("get:/:ua")) + assert b"Android" in tservers.render(parse_request("get:/:ua")) + assert b"User-Agent" in tservers.render(parse_request("get:/:ua")) def test_user_agent(): diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index 7d3a8e60..8ab1acae 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -7,7 +7,7 @@ from pathod import language from pathod.language import http2 from pathod.protocols.http2 import HTTP2StateProtocol -from . import tutils +from mitmproxy.test import tutils def parse_request(s): diff --git a/test/pathod/test_language_websocket.py b/test/pathod/test_language_websocket.py index 49fbd4e6..e61413da 100644 --- a/test/pathod/test_language_websocket.py +++ b/test/pathod/test_language_websocket.py @@ -2,7 +2,8 @@ from pathod import language from pathod.language import websockets import mitmproxy.net.websockets -from . import tutils +from mitmproxy.test import tutils +from . import tservers def parse_request(s): @@ -62,7 +63,7 @@ class TestWebsocketFrame: def test_flags(self): wf = parse_request("wf:fin:mask:rsv1:rsv2:rsv3") - frm = mitmproxy.net.websockets.Frame.from_bytes(tutils.render(wf)) + frm = mitmproxy.net.websockets.Frame.from_bytes(tservers.render(wf)) assert frm.header.fin assert frm.header.mask assert frm.header.rsv1 @@ -70,7 +71,7 @@ class TestWebsocketFrame: assert frm.header.rsv3 wf = parse_request("wf:-fin:-mask:-rsv1:-rsv2:-rsv3") - frm = mitmproxy.net.websockets.Frame.from_bytes(tutils.render(wf)) + frm = mitmproxy.net.websockets.Frame.from_bytes(tservers.render(wf)) assert not frm.header.fin assert not frm.header.mask assert not frm.header.rsv1 @@ -80,7 +81,7 @@ class TestWebsocketFrame: def fr(self, spec, **kwargs): settings = language.base.Settings(**kwargs) wf = parse_request(spec) - return mitmproxy.net.websockets.Frame.from_bytes(tutils.render(wf, settings)) + return mitmproxy.net.websockets.Frame.from_bytes(tservers.render(wf, settings)) def test_construction(self): assert self.fr("wf:c1").header.opcode == 1 diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 90177ff6..69baae54 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -4,13 +4,13 @@ from mock import Mock from mitmproxy.net import http from mitmproxy.net import tcp from mitmproxy.net.http import http1 -from mitmproxy.test.tutils import raises from mitmproxy import exceptions from pathod import pathoc, language from pathod.protocols.http2 import HTTP2StateProtocol -from . import tutils +from mitmproxy.test import tutils +from . import tservers def test_response(): @@ -18,7 +18,7 @@ def test_response(): assert repr(r) -class PathocTestDaemon(tutils.DaemonTests): +class PathocTestDaemon(tservers.DaemonTests): def tval(self, requests, timeout=None, showssl=False, **kwargs): s = io.StringIO() c = pathoc.Pathoc( @@ -64,7 +64,7 @@ class TestDaemonSSL(PathocTestDaemon): def test_clientcert(self): self.tval( ["get:/p/200"], - clientcert=tutils.test_data.path("data/clientcert/client.pem"), + clientcert=tutils.test_data.path("pathod/data/clientcert/client.pem"), ) log = self.d.log() assert log[0]["request"]["clientcert"]["keyinfo"] @@ -171,12 +171,12 @@ class TestDaemon(PathocTestDaemon): to = ("foobar", 80) c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None) c.rfile, c.wfile = io.BytesIO(), io.BytesIO() - with raises("connect failed"): + with tutils.raises("connect failed"): c.http_connect(to) c.rfile = io.BytesIO( b"HTTP/1.1 500 OK\r\n" ) - with raises("connect failed"): + with tutils.raises("connect failed"): c.http_connect(to) c.rfile = io.BytesIO( b"HTTP/1.1 200 OK\r\n" diff --git a/test/pathod/test_pathoc_cmdline.py b/test/pathod/test_pathoc_cmdline.py index 7f51929f..3b54403f 100644 --- a/test/pathod/test_pathoc_cmdline.py +++ b/test/pathod/test_pathoc_cmdline.py @@ -3,7 +3,7 @@ import mock from pathod import pathoc_cmdline as cmdline -from . import tutils +from mitmproxy.test import tutils @mock.patch("argparse.ArgumentParser.error") @@ -52,7 +52,7 @@ def test_pathoc(perror): [ "pathoc", "foo.com:8888", - tutils.test_data.path("data/request") + tutils.test_data.path("pathod/data/request") ] ) assert len(list(a.requests)) == 1 diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py index 8ee7a2fd..6a4e1c62 100644 --- a/test/pathod/test_pathod.py +++ b/test/pathod/test_pathod.py @@ -3,8 +3,9 @@ import io from pathod import pathod from mitmproxy.net import tcp from mitmproxy import exceptions +from mitmproxy.test import tutils -from . import tutils +from . import tservers class TestPathod: @@ -24,7 +25,7 @@ class TestPathod: assert len(p.get_log()) <= p.LOGBUF -class TestTimeout(tutils.DaemonTests): +class TestTimeout(tservers.DaemonTests): timeout = 0.01 def test_timeout(self): @@ -36,7 +37,7 @@ class TestTimeout(tutils.DaemonTests): assert self.d.last_log()["type"] == "timeout" -class TestNotAfterConnect(tutils.DaemonTests): +class TestNotAfterConnect(tservers.DaemonTests): ssl = False ssloptions = dict( not_after_connect=True @@ -50,10 +51,10 @@ class TestNotAfterConnect(tutils.DaemonTests): assert r[0].status_code == 202 -class TestCustomCert(tutils.DaemonTests): +class TestCustomCert(tservers.DaemonTests): ssl = True ssloptions = dict( - certs=[(b"*", tutils.test_data.path("data/testkey.pem"))], + certs=[(b"*", tutils.test_data.path("pathod/data/testkey.pem"))], ) def test_connect(self): @@ -64,7 +65,7 @@ class TestCustomCert(tutils.DaemonTests): assert "test.com" in str(r.sslinfo.certchain[0].get_subject()) -class TestSSLCN(tutils.DaemonTests): +class TestSSLCN(tservers.DaemonTests): ssl = True ssloptions = dict( cn=b"foo.com" @@ -78,7 +79,7 @@ class TestSSLCN(tutils.DaemonTests): assert r.sslinfo.certchain[0].get_subject().CN == "foo.com" -class TestNohang(tutils.DaemonTests): +class TestNohang(tservers.DaemonTests): nohang = True def test_nohang(self): @@ -88,14 +89,14 @@ class TestNohang(tutils.DaemonTests): assert "Pauses have been disabled" in l["response"]["msg"] -class TestHexdump(tutils.DaemonTests): +class TestHexdump(tservers.DaemonTests): hexdump = True def test_hexdump(self): assert self.get(r"200:b'\xf0'") -class TestNocraft(tutils.DaemonTests): +class TestNocraft(tservers.DaemonTests): nocraft = True def test_nocraft(self): @@ -104,7 +105,7 @@ class TestNocraft(tutils.DaemonTests): assert b"Crafting disabled" in r.content -class CommonTests(tutils.DaemonTests): +class CommonTests(tservers.DaemonTests): def test_binarydata(self): assert self.get(r"200:b'\xf0'") @@ -252,7 +253,7 @@ class TestDaemonSSL(CommonTests): assert self.d.last_log()["cipher"][1] > 0 -class TestHTTP2(tutils.DaemonTests): +class TestHTTP2(tservers.DaemonTests): ssl = True nohang = True diff --git a/test/pathod/test_pathod_cmdline.py b/test/pathod/test_pathod_cmdline.py index 58123b37..290f5d9f 100644 --- a/test/pathod/test_pathod_cmdline.py +++ b/test/pathod/test_pathod_cmdline.py @@ -2,7 +2,7 @@ import mock from pathod import pathod_cmdline as cmdline -from . import tutils +from mitmproxy.test import tutils def test_parse_anchor_spec(): @@ -18,7 +18,7 @@ def test_pathod(perror): [ "pathod", "--cert", - tutils.test_data.path("data/testkey.pem") + tutils.test_data.path("pathod/data/testkey.pem") ] ) assert a.ssl_certs @@ -46,7 +46,7 @@ def test_pathod(perror): [ "pathod", "-a", - "foo=" + tutils.test_data.path("data/response") + "foo=" + tutils.test_data.path("pathod/data/response") ] ) assert a.anchors diff --git a/test/pathod/test_test.py b/test/pathod/test_test.py index d69e72f3..c2e1c6e9 100644 --- a/test/pathod/test_test.py +++ b/test/pathod/test_test.py @@ -2,7 +2,7 @@ import logging import requests from pathod import test -from . import tutils +from mitmproxy.test import tutils import requests.packages.urllib3 @@ -34,8 +34,8 @@ class TestDaemonManual: def test_startstop_ssl_explicit(self): ssloptions = dict( - certfile=tutils.test_data.path("data/testkey.pem"), - cacert=tutils.test_data.path("data/testkey.pem"), + certfile=tutils.test_data.path("pathod/data/testkey.pem"), + cacert=tutils.test_data.path("pathod/data/testkey.pem"), ssl_after_connect=False ) d = test.Daemon(ssl=ssloptions) diff --git a/test/pathod/test_utils.py b/test/pathod/test_utils.py index 2bdfe501..80fc2ed8 100644 --- a/test/pathod/test_utils.py +++ b/test/pathod/test_utils.py @@ -1,6 +1,6 @@ from pathod import utils -from . import tutils +from mitmproxy.test import tutils def test_membool(): diff --git a/test/pathod/tservers.py b/test/pathod/tservers.py new file mode 100644 index 00000000..fab09288 --- /dev/null +++ b/test/pathod/tservers.py @@ -0,0 +1,145 @@ +import tempfile +import re +import shutil +import requests +import io +import urllib + + +from mitmproxy.net import tcp +from mitmproxy.test import tutils + +from pathod import language +from pathod import pathoc +from pathod import pathod +from pathod import test + + +def treader(bytes): + """ + Construct a tcp.Read object from bytes. + """ + fp = io.BytesIO(bytes) + return tcp.Reader(fp) + + +class DaemonTests: + nohang = False + ssl = False + timeout = None + hexdump = False + ssloptions = None + nocraft = False + explain = True + + @classmethod + def setup_class(cls): + opts = cls.ssloptions or {} + cls.confdir = tempfile.mkdtemp() + opts["confdir"] = cls.confdir + so = pathod.SSLOptions(**opts) + cls.d = test.Daemon( + staticdir=tutils.test_data.path("pathod/data"), + anchors=[ + (re.compile("/anchor/.*"), "202:da") + ], + ssl=cls.ssl, + ssloptions=so, + sizelimit=1 * 1024 * 1024, + nohang=cls.nohang, + timeout=cls.timeout, + hexdump=cls.hexdump, + nocraft=cls.nocraft, + logreq=True, + logresp=True, + explain=cls.explain + ) + + @classmethod + def teardown_class(cls): + cls.d.shutdown() + shutil.rmtree(cls.confdir) + + def teardown(self): + self.d.wait_for_silence() + self.d.clear_log() + + def _getpath(self, path, params=None): + scheme = "https" if self.ssl else "http" + resp = requests.get( + "%s://localhost:%s/%s" % ( + scheme, + self.d.port, + path + ), + verify=False, + params=params + ) + return resp + + def getpath(self, path, params=None): + logfp = io.StringIO() + c = pathoc.Pathoc( + ("localhost", self.d.port), + ssl=self.ssl, + fp=logfp, + ) + with c.connect(): + if params: + path = path + "?" + urllib.parse.urlencode(params) + resp = c.request("get:%s" % path) + return resp + + def get(self, spec): + logfp = io.StringIO() + c = pathoc.Pathoc( + ("localhost", self.d.port), + ssl=self.ssl, + fp=logfp, + ) + with c.connect(): + resp = c.request( + "get:/p/%s" % urllib.parse.quote(spec) + ) + return resp + + def pathoc( + self, + specs, + timeout=None, + connect_to=None, + ssl=None, + ws_read_limit=None, + use_http2=False, + ): + """ + Returns a (messages, text log) tuple. + """ + if ssl is None: + ssl = self.ssl + logfp = io.StringIO() + c = pathoc.Pathoc( + ("localhost", self.d.port), + ssl=ssl, + ws_read_limit=ws_read_limit, + timeout=timeout, + fp=logfp, + use_http2=use_http2, + ) + with c.connect(connect_to): + ret = [] + for i in specs: + resp = c.request(i) + if resp: + ret.append(resp) + for frm in c.wait(): + ret.append(frm) + c.stop() + return ret, logfp.getvalue() + + +def render(r, settings=language.Settings()): + r = r.resolve(settings) + s = io.BytesIO() + assert language.serve(r, s, settings) + return s.getvalue() diff --git a/test/pathod/tutils.py b/test/pathod/tutils.py deleted file mode 100644 index ccc3df43..00000000 --- a/test/pathod/tutils.py +++ /dev/null @@ -1,153 +0,0 @@ -import tempfile -import re -import shutil -import requests -import io -import urllib - -from mitmproxy.utils import data - -from mitmproxy.net import tcp -from mitmproxy.test import tutils - -from pathod import language -from pathod import pathoc -from pathod import pathod -from pathod import test - - -def treader(bytes): - """ - Construct a tcp.Read object from bytes. - """ - fp = io.BytesIO(bytes) - return tcp.Reader(fp) - - -class DaemonTests: - nohang = False - ssl = False - timeout = None - hexdump = False - ssloptions = None - nocraft = False - explain = True - - @classmethod - def setup_class(cls): - opts = cls.ssloptions or {} - cls.confdir = tempfile.mkdtemp() - opts["confdir"] = cls.confdir - so = pathod.SSLOptions(**opts) - cls.d = test.Daemon( - staticdir=test_data.path("data"), - anchors=[ - (re.compile("/anchor/.*"), "202:da") - ], - ssl=cls.ssl, - ssloptions=so, - sizelimit=1 * 1024 * 1024, - nohang=cls.nohang, - timeout=cls.timeout, - hexdump=cls.hexdump, - nocraft=cls.nocraft, - logreq=True, - logresp=True, - explain=cls.explain - ) - - @classmethod - def teardown_class(cls): - cls.d.shutdown() - shutil.rmtree(cls.confdir) - - def teardown(self): - self.d.wait_for_silence() - self.d.clear_log() - - def _getpath(self, path, params=None): - scheme = "https" if self.ssl else "http" - resp = requests.get( - "%s://localhost:%s/%s" % ( - scheme, - self.d.port, - path - ), - verify=False, - params=params - ) - return resp - - def getpath(self, path, params=None): - logfp = io.StringIO() - c = pathoc.Pathoc( - ("localhost", self.d.port), - ssl=self.ssl, - fp=logfp, - ) - with c.connect(): - if params: - path = path + "?" + urllib.parse.urlencode(params) - resp = c.request("get:%s" % path) - return resp - - def get(self, spec): - logfp = io.StringIO() - c = pathoc.Pathoc( - ("localhost", self.d.port), - ssl=self.ssl, - fp=logfp, - ) - with c.connect(): - resp = c.request( - "get:/p/%s" % urllib.parse.quote(spec) - ) - return resp - - def pathoc( - self, - specs, - timeout=None, - connect_to=None, - ssl=None, - ws_read_limit=None, - use_http2=False, - ): - """ - Returns a (messages, text log) tuple. - """ - if ssl is None: - ssl = self.ssl - logfp = io.StringIO() - c = pathoc.Pathoc( - ("localhost", self.d.port), - ssl=ssl, - ws_read_limit=ws_read_limit, - timeout=timeout, - fp=logfp, - use_http2=use_http2, - ) - with c.connect(connect_to): - ret = [] - for i in specs: - resp = c.request(i) - if resp: - ret.append(resp) - for frm in c.wait(): - ret.append(frm) - c.stop() - return ret, logfp.getvalue() - - -tmpdir = tutils.tmpdir - -raises = tutils.raises - -test_data = data.Data(__name__) - - -def render(r, settings=language.Settings()): - r = r.resolve(settings) - s = io.BytesIO() - assert language.serve(r, s, settings) - return s.getvalue() -- cgit v1.2.3