diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_app.py | 5 | ||||
-rw-r--r-- | test/test_console_contentview.py | 99 | ||||
-rw-r--r-- | test/test_console_search.py | 176 | ||||
-rw-r--r-- | test/test_protocol_http.py | 3 | ||||
-rw-r--r-- | test/test_proxy.py | 11 | ||||
-rw-r--r-- | test/test_server.py | 17 | ||||
-rw-r--r-- | test/tservers.py | 17 |
7 files changed, 206 insertions, 122 deletions
diff --git a/test/test_app.py b/test/test_app.py index f0eab7cc..0b6ed14c 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -9,11 +9,8 @@ class TestApp(tservers.HTTPProxTest): assert self.app("/").status_code == 200 def test_cert(self): - path = tutils.test_data.path("data/confdir/") + "mitmproxy-ca-cert." with tutils.tmpdir() as d: for ext in ["pem", "p12"]: resp = self.app("/cert/%s" % ext) assert resp.status_code == 200 - with open(path + ext, "rb") as f: - assert resp.content == f.read() - + assert resp.content diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index 07ecf1d0..0aabd2c5 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -120,7 +120,7 @@ class TestContentView: def test_view_css(self): v = cv.ViewCSS() - with open('./test/data/1.css', 'r') as fp: + with open(tutils.test_data.path('data/1.css'), 'r') as fp: fixture_1 = fp.read() result = v([], 'a', 100) @@ -276,100 +276,3 @@ if cv.ViewProtobuf.is_available(): def test_get_by_shortcut(): assert cv.get_by_shortcut("h") -def test_search_highlights(): - # Default text in requests is content. We will search for nt once, and - # expect the first bit to be highlighted. We will do it again and expect the - # second to be. - f = tutils.tflowview() - - f.search("nt") - text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() == ('content', [(None, 2), (f.highlight_color, 2)]) - - f.search("nt") - text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert text_object.get_text() == ('content', [(None, 5), (f.highlight_color, 2)]) - -def test_search_returns_useful_messages(): - f = tutils.tflowview() - - # original string is content. this string should not be in there. - response = f.search("oranges and other fruit.") - assert response == "no matches for 'oranges and other fruit.'" - -def test_search_highlights_clears_prev(): - f = tutils.tflowview(request_contents="this is string\nstring is string") - - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) - - # search again, it should not be highlighted again. - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() != ('this is string', [(None, 8), (f.highlight_color, 6)]) - -def test_search_highlights_multi_line(): - f = tutils.tflowview(request_contents="this is string\nstring is string") - - # should highlight the first line. - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) - - # should highlight second line, first appearance of string. - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) - - # should highlight third line, second appearance of string. - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert text_object.get_text() == ('string is string', [(None, 10), (f.highlight_color, 6)]) - -def test_search_loops(): - f = tutils.tflowview(request_contents="this is string\nstring is string") - - # get to the end. - f.search("string") - f.search("string") - f.search("string") - - # should highlight the first line. - message = f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) - assert message == "search hit BOTTOM, continuing at TOP" - -def test_search_focuses(): - f = tutils.tflowview(request_contents="this is string\nstring is string") - - # should highlight the first line. - f.search("string") - - # should be focusing on the 2nd text line. - f.search("string") - text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert f.last_displayed_body.focus == text_object - -def test_search_does_not_crash_on_bad(): - """ - this used to crash, kept for reference. - """ - - f = tutils.tflowview(request_contents="this is string\nstring is string\n"+("A" * cv.VIEW_CUTOFF)+"AFTERCUTOFF") - f.search("AFTERCUTOFF") - - # pretend F - f.state.add_flow_setting( - f.flow, - (f.state.view_flow_mode, "fullcontents"), - True - ) - f.master.refresh_flow(f.flow) - - # text changed, now this string will exist. can happen when user presses F - # for full text view - f.search("AFTERCUTOFF") - - diff --git a/test/test_console_search.py b/test/test_console_search.py new file mode 100644 index 00000000..f4e9ff9b --- /dev/null +++ b/test/test_console_search.py @@ -0,0 +1,176 @@ + +import sys +import libmproxy.console.contentview as cv +from libmproxy import utils, flow, encoding +import tutils + +def test_search_highlights(): + # Default text in requests is content. We will search for nt once, and + # expect the first bit to be highlighted. We will do it again and expect the + # second to be. + f = tutils.tflowview() + + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('content', [(None, 2), (f.highlight_color, 2)]) + + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('content', [(None, 5), (f.highlight_color, 2)]) + +def test_search_returns_useful_messages(): + f = tutils.tflowview() + + # original string is content. this string should not be in there. + test_string = "oranges and other fruit." + response = f.search(test_string) + assert response == "no matches for '%s'" % test_string + +def test_search_highlights_clears_prev(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # search again, it should not be highlighted again. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() != ('this is string', [(None, 8), (f.highlight_color, 6)]) + +def test_search_highlights_multi_line(flow=None): + f = flow if flow else tutils.tflowview(request_contents="this is string\nstring is string") + + # should highlight the first line. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # should highlight second line, first appearance of string. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) + + # should highlight third line, second appearance of string. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 10), (f.highlight_color, 6)]) + +def test_search_loops(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # get to the end. + f.search("string") + f.search("string") + f.search("string") + + # should highlight the first line. + message = f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + assert message == "search hit BOTTOM, continuing at TOP" + +def test_search_focuses(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # should highlight the first line. + f.search("string") + + # should be focusing on the 2nd text line. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert f.last_displayed_body.focus == text_object + +def test_search_does_not_crash_on_bad(): + """ + this used to crash, kept for reference. + """ + + f = tutils.tflowview(request_contents="this is string\nstring is string\n"+("A" * cv.VIEW_CUTOFF)+"AFTERCUTOFF") + f.search("AFTERCUTOFF") + + # pretend F + f.state.add_flow_setting( + f.flow, + (f.state.view_flow_mode, "fullcontents"), + True + ) + f.master.refresh_flow(f.flow) + + # text changed, now this string will exist. can happen when user presses F + # for full text view + f.search("AFTERCUTOFF") + +def test_search_backwards(): + f = tutils.tflowview(request_contents="content, content") + + first_match = ('content, content', [(None, 2), (f.highlight_color, 2)]) + + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == first_match + + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('content, content', [(None, 5), (f.highlight_color, 2)]) + + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == first_match + +def test_search_back_multiline(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # shared assertions. highlight and pointers should now be on the third + # 'string' appearance + test_search_highlights_multi_line(f) + + # should highlight second line, first appearance of string. + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) + + # should highlight the first line again. + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + +def test_search_back_multi_multi_line(): + """ + same as above for some bugs the above won't catch. + """ + f = tutils.tflowview(request_contents="this is string\nthis is string\nthis is string") + + f.search("string") + f.search_again() + f.search_again() + + # should be on second line + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # first line now + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + +def test_search_backwards_wraps(): + """ + when searching past line 0, it should loop. + """ + f = tutils.tflowview(request_contents="this is string\nthis is string\nthis is string") + + # should be on second line + f.search("string") + f.search_again() + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # should be on third now. + f.search_again(backwards=True) + message = f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 2) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + assert message == "search hit TOP, continuing at BOTTOM" + diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 1d8872cc..3f37928c 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -88,7 +88,6 @@ class TestHTTPResponse: class TestInvalidRequests(tservers.HTTPProxTest): ssl = True - def test_double_connect(self): p = self.pathoc() r = p.request("connect:'%s:%s'" % ("127.0.0.1", self.server2.port)) @@ -117,9 +116,7 @@ class TestProxyChaining(tservers.HTTPChainProxyTest): class TestProxyChainingSSL(tservers.HTTPChainProxyTest): ssl = True - def test_simple(self): - p = self.pathoc() req = p.request("get:'/p/418:b\"content\"'") assert req.content == "content" diff --git a/test/test_proxy.py b/test/test_proxy.py index c42d66e7..b15e3f84 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -70,10 +70,6 @@ class TestProcessProxyOptions: def test_simple(self): assert self.p() - def test_cert(self): - self.assert_noerr("--cert", tutils.test_data.path("data/testkey.pem")) - self.assert_err("does not exist", "--cert", "nonexistent") - def test_confdir(self): with tutils.tmpdir() as confdir: self.assert_noerr("--confdir", confdir) @@ -90,11 +86,16 @@ class TestProcessProxyOptions: self.assert_err("invalid reverse proxy", "-P", "reverse") self.assert_noerr("-P", "http://localhost") - def test_certs(self): + def test_client_certs(self): with tutils.tmpdir() as confdir: self.assert_noerr("--client-certs", confdir) self.assert_err("directory does not exist", "--client-certs", "nonexistent") + def test_certs(self): + with tutils.tmpdir() as confdir: + self.assert_noerr("--cert", tutils.test_data.path("data/testkey.pem")) + self.assert_err("does not exist", "--cert", "nonexistent") + def test_auth(self): p = self.assert_noerr("--nonanonymous") assert p.authenticator diff --git a/test/test_server.py b/test/test_server.py index 2f9e6728..ed21e75c 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -206,13 +206,21 @@ class TestHTTPSCertfile(tservers.HTTPProxTest, CommonMixin): def test_certfile(self): assert self.pathod("304") -class TestHTTPSNoCommonName(tservers.HTTPProxTest, CommonMixin): + +class TestHTTPSNoCommonName(tservers.HTTPProxTest): """ Test what happens if we get a cert without common name back. """ ssl = True - ssloptions=pathod.SSLOptions(certfile=tutils.test_data.path("data/no_common_name.pem"), - keyfile=tutils.test_data.path("data/no_common_name.pem")) + ssloptions=pathod.SSLOptions( + certs = [ + ("*", tutils.test_data.path("data/no_common_name.pem")) + ] + ) + def test_http(self): + f = self.pathod("202") + assert f.sslinfo.certchain[0].get_subject().CN == "127.0.0.1" + class TestReverse(tservers.ReverseProxTest, CommonMixin): reverse = True @@ -370,7 +378,6 @@ class TestTransparentResolveError(tservers.TransparentProxTest): assert self.pathod("304").status_code == 502 - class MasterIncomplete(tservers.TestMaster): def handle_request(self, m): resp = tutils.tresp() @@ -383,5 +390,3 @@ class TestIncompleteResponse(tservers.HTTPProxTest): def test_incomplete(self): assert self.pathod("200").status_code == 502 - - diff --git a/test/tservers.py b/test/tservers.py index 812e8921..cf9b3f73 100644 --- a/test/tservers.py +++ b/test/tservers.py @@ -1,4 +1,6 @@ +import os.path import threading, Queue +import shutil, tempfile import flask import libpathod.test, libpathod.pathoc from libmproxy import proxy, flow, controller @@ -28,7 +30,6 @@ class TestMaster(flow.FlowMaster): self.apps.add(testapp, "testapp", 80) self.apps.add(errapp, "errapp", 80) self.clear_log() - self.start_app(APP_HOST, APP_PORT, False) def handle_request(self, m): flow.FlowMaster.handle_request(self, m) @@ -73,25 +74,31 @@ class ProxTestBase(object): ssl = None ssloptions = False clientcerts = False - certfile = None no_upstream_cert = False authenticator = None masterclass = TestMaster + externalapp = False @classmethod def setupAll(cls): cls.server = libpathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) cls.server2 = libpathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) pconf = cls.get_proxy_config() + cls.confdir = os.path.join(tempfile.gettempdir(), "mitmproxy") config = proxy.ProxyConfig( no_upstream_cert = cls.no_upstream_cert, - cacert = tutils.test_data.path("data/confdir/mitmproxy-ca.pem"), + confdir = cls.confdir, authenticator = cls.authenticator, **pconf ) tmaster = cls.masterclass(config) + tmaster.start_app(APP_HOST, APP_PORT, cls.externalapp) cls.proxy = ProxyThread(tmaster) cls.proxy.start() + @classmethod + def tearDownAll(cls): + shutil.rmtree(cls.confdir) + @property def master(cls): return cls.proxy.tmaster @@ -126,8 +133,6 @@ class ProxTestBase(object): d = dict() if cls.clientcerts: d["clientcerts"] = tutils.test_data.path("data/clientcert") - if cls.certfile: - d["certfile"] =tutils.test_data.path("data/testkey.pem") return d @@ -252,7 +257,6 @@ class ChainProxTest(ProxTestBase): """ n = 2 chain_config = [lambda: proxy.ProxyConfig( - cacert = tutils.test_data.path("data/confdir/mitmproxy-ca.pem"), )] * n @classmethod def setupAll(cls): @@ -265,6 +269,7 @@ class ChainProxTest(ProxTestBase): cls.chain[-1].port ) tmaster = cls.masterclass(config) + tmaster.start_app(APP_HOST, APP_PORT, cls.externalapp) cls.chain.append(ProxyThread(tmaster)) cls.chain[-1].start() |