From ae7e9efb5ca014291e879694414bbffc982f2a0d Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Wed, 24 May 2017 14:09:41 +0200 Subject: fix various fd/socket leaks --- test/mitmproxy/addons/test_clientplayback.py | 7 ++++--- test/mitmproxy/addons/test_save.py | 5 +++-- test/mitmproxy/addons/test_serverplayback.py | 7 ++++--- test/mitmproxy/addons/test_view.py | 7 ++++--- test/mitmproxy/contentviews/test_protobuf.py | 4 +++- test/mitmproxy/net/test_tcp.py | 10 ++++++---- test/mitmproxy/platform/test_pf.py | 5 +++-- test/pathod/language/test_base.py | 6 ++++-- test/pathod/language/test_generators.py | 4 +--- test/pathod/protocols/test_http2.py | 2 +- 10 files changed, 33 insertions(+), 24 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py index 7ffda317..6089b2d5 100644 --- a/test/mitmproxy/addons/test_clientplayback.py +++ b/test/mitmproxy/addons/test_clientplayback.py @@ -10,9 +10,10 @@ from mitmproxy.test import taddons def tdump(path, flows): - w = io.FlowWriter(open(path, "wb")) - for i in flows: - w.add(i) + with open(path, "wb") as f: + w = io.FlowWriter(f) + for i in flows: + w.add(i) class MockThread(): diff --git a/test/mitmproxy/addons/test_save.py b/test/mitmproxy/addons/test_save.py index 85c2a398..a4e425cd 100644 --- a/test/mitmproxy/addons/test_save.py +++ b/test/mitmproxy/addons/test_save.py @@ -26,8 +26,9 @@ def test_configure(tmpdir): def rd(p): - x = io.FlowReader(open(p, "rb")) - return list(x.stream()) + with open(p, "rb") as f: + x = io.FlowReader(f) + return list(x.stream()) def test_tcp(tmpdir): diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py index 3ceab3fa..7605a5d9 100644 --- a/test/mitmproxy/addons/test_serverplayback.py +++ b/test/mitmproxy/addons/test_serverplayback.py @@ -11,9 +11,10 @@ from mitmproxy import io def tdump(path, flows): - w = io.FlowWriter(open(path, "wb")) - for i in flows: - w.add(i) + with open(path, "wb") as f: + w = io.FlowWriter(f) + for i in flows: + w.add(i) def test_load_file(tmpdir): diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index 6da13650..d5a3a456 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -132,9 +132,10 @@ def test_filter(): def tdump(path, flows): - w = io.FlowWriter(open(path, "wb")) - for i in flows: - w.add(i) + with open(path, "wb") as f: + w = io.FlowWriter(f) + for i in flows: + w.add(i) def test_create(): diff --git a/test/mitmproxy/contentviews/test_protobuf.py b/test/mitmproxy/contentviews/test_protobuf.py index 31e382ec..71e51576 100644 --- a/test/mitmproxy/contentviews/test_protobuf.py +++ b/test/mitmproxy/contentviews/test_protobuf.py @@ -17,7 +17,9 @@ def test_view_protobuf_request(): m.configure_mock(**attrs) n.return_value = m - content_type, output = v(open(p, "rb").read()) + with open(p, "rb") as f: + data = f.read() + content_type, output = v(data) assert content_type == "Protobuf" assert output[0] == [('text', b'1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"')] diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py index 81d51888..234e8afb 100644 --- a/test/mitmproxy/net/test_tcp.py +++ b/test/mitmproxy/net/test_tcp.py @@ -34,7 +34,7 @@ class ClientCipherListHandler(tcp.BaseHandler): sni = None def handle(self): - self.wfile.write("%s" % self.connection.get_cipher_list()) + self.wfile.write(str(self.connection.get_cipher_list()).encode()) self.wfile.flush() @@ -391,14 +391,15 @@ class TestSNI(tservers.ServerTestBase): class TestServerCipherList(tservers.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cipher_list='AES256-GCM-SHA384' + cipher_list=b'AES256-GCM-SHA384' ) def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl(sni="foo.com") - assert c.rfile.readline() == b"['AES256-GCM-SHA384']" + expected = b"['AES256-GCM-SHA384']" + assert c.rfile.read(len(expected) + 2) == expected class TestServerCurrentCipher(tservers.ServerTestBase): @@ -424,7 +425,7 @@ class TestServerCurrentCipher(tservers.ServerTestBase): class TestServerCipherListError(tservers.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cipher_list='bogus' + cipher_list=b'bogus' ) def test_echo(self): @@ -632,6 +633,7 @@ class TestTCPServer: with s.handler_counter: with pytest.raises(exceptions.Timeout): s.wait_for_silence() + s.shutdown() class TestFileLike: diff --git a/test/mitmproxy/platform/test_pf.py b/test/mitmproxy/platform/test_pf.py index f644bcc5..3292d345 100644 --- a/test/mitmproxy/platform/test_pf.py +++ b/test/mitmproxy/platform/test_pf.py @@ -9,10 +9,11 @@ class TestLookup: def test_simple(self): if sys.platform == "freebsd10": p = tutils.test_data.path("mitmproxy/data/pf02") - d = open(p, "rb").read() else: p = tutils.test_data.path("mitmproxy/data/pf01") - d = open(p, "rb").read() + with open(p, "rb") as f: + d = f.read() + assert pf.lookup("192.168.1.111", 40000, d) == ("5.5.5.5", 80) with pytest.raises(Exception, match="Could not resolve original destination"): pf.lookup("192.168.1.112", 40000, d) diff --git a/test/pathod/language/test_base.py b/test/pathod/language/test_base.py index ec460b07..910d298a 100644 --- a/test/pathod/language/test_base.py +++ b/test/pathod/language/test_base.py @@ -202,12 +202,14 @@ class TestMisc: e.parseString("m@1") s = base.Settings(staticdir=str(tmpdir)) - tmpdir.join("path").write_binary(b"a" * 20, ensure=True) + with open(str(tmpdir.join("path")), 'wb') as f: + f.write(b"a" * 20) v = e.parseString("m