From 3bfb975ed9b4024cbc762324539868e901cf8a04 Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Sun, 10 Feb 2019 23:59:13 +0100 Subject: Fix/pep8 (#277) * re-organized imports according to pep8 * fixed all remaining pep8 issues * moved common config into setup.cfg, additionally test `tests` * removed --select=X -- the errors selected where by default not in flake8's --ignore list so effectively had no effect * update .travis.yml to reflect changes in tox.ini * make travis just use tox in order to avoid code duplaction * replace py.test with pytest * fixed .travis.yml * try different pypy toxenv * hopefully fixed testenv for pypy * added pypy basepython, removed unused python2.6 * install dev package before testing (fixes missing coverage) * fixed empty exception pass blocks with noqa * Added dummy log message on empty try-except-pass blocks to make dodacy happy :( * Replaced Exception with BaseException --- tests/client/test_firewall.py | 7 ++++--- tests/client/test_helpers.py | 2 +- tests/client/test_methods_nat.py | 8 ++++---- tests/client/test_methods_pf.py | 4 ++-- tests/client/test_methods_tproxy.py | 4 ++-- tests/client/test_options.py | 25 ++++++++++++++----------- tests/client/test_sdnotify.py | 3 ++- tests/server/test_server.py | 6 ++++-- 8 files changed, 33 insertions(+), 26 deletions(-) (limited to 'tests') diff --git a/tests/client/test_firewall.py b/tests/client/test_firewall.py index 3174e9c..e293be4 100644 --- a/tests/client/test_firewall.py +++ b/tests/client/test_firewall.py @@ -1,7 +1,7 @@ -from mock import Mock, patch, call import io from socket import AF_INET, AF_INET6 +from mock import Mock, patch, call import sshuttle.firewall @@ -85,8 +85,9 @@ def test_subnet_weight(): (AF_INET, 0, 1, '0.0.0.0', 0, 0) ] - assert subnets_sorted == \ - sorted(subnets, key=sshuttle.firewall.subnet_weight, reverse=True) + assert subnets_sorted == sorted(subnets, + key=sshuttle.firewall.subnet_weight, + reverse=True) @patch('sshuttle.firewall.rewrite_etc_hosts') diff --git a/tests/client/test_helpers.py b/tests/client/test_helpers.py index 7359ec2..3a64142 100644 --- a/tests/client/test_helpers.py +++ b/tests/client/test_helpers.py @@ -1,10 +1,10 @@ -from mock import patch, call import sys import io import socket from socket import AF_INET, AF_INET6 import errno +from mock import patch, call import sshuttle.helpers diff --git a/tests/client/test_methods_nat.py b/tests/client/test_methods_nat.py index b18c05e..af64c11 100644 --- a/tests/client/test_methods_nat.py +++ b/tests/client/test_methods_nat.py @@ -1,9 +1,9 @@ -import pytest -from mock import Mock, patch, call import socket from socket import AF_INET, AF_INET6 import struct +import pytest +from mock import Mock, patch, call from sshuttle.helpers import Fatal from sshuttle.methods import get_method @@ -124,7 +124,7 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt): ] assert mock_ipt_ttl.mock_calls == [ call(AF_INET, 'nat', '-A', 'sshuttle-1025', '-j', 'REDIRECT', - '--dest', u'1.2.3.0/24', '-p', 'tcp', '--dport', '8000:9000', + '--dest', u'1.2.3.0/24', '-p', 'tcp', '--dport', '8000:9000', '--to-ports', '1025'), call(AF_INET, 'nat', '-A', 'sshuttle-1025', '-j', 'REDIRECT', '--dest', u'1.2.3.33/32', '-p', 'udp', @@ -140,7 +140,7 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt): call(AF_INET, 'nat', '-I', 'OUTPUT', '1', '-j', 'sshuttle-1025'), call(AF_INET, 'nat', '-I', 'PREROUTING', '1', '-j', 'sshuttle-1025'), call(AF_INET, 'nat', '-A', 'sshuttle-1025', '-j', 'RETURN', - '--dest', u'1.2.3.66/32', '-p', 'tcp', '--dport', '8080:8080') + '--dest', u'1.2.3.66/32', '-p', 'tcp', '--dport', '8080:8080') ] mock_ipt_chain_exists.reset_mock() mock_ipt_ttl.reset_mock() diff --git a/tests/client/test_methods_pf.py b/tests/client/test_methods_pf.py index 5375d11..36803ac 100644 --- a/tests/client/test_methods_pf.py +++ b/tests/client/test_methods_pf.py @@ -1,8 +1,8 @@ -import pytest -from mock import Mock, patch, call, ANY import socket from socket import AF_INET, AF_INET6 +import pytest +from mock import Mock, patch, call, ANY from sshuttle.methods import get_method from sshuttle.helpers import Fatal from sshuttle.methods.pf import FreeBsd, Darwin, OpenBsd diff --git a/tests/client/test_methods_tproxy.py b/tests/client/test_methods_tproxy.py index 2f44b57..32521bf 100644 --- a/tests/client/test_methods_tproxy.py +++ b/tests/client/test_methods_tproxy.py @@ -132,7 +132,7 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt): call(AF_INET6, 'mangle', '-F', 'sshuttle-t-1024'), call(AF_INET6, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1024'), call(AF_INET6, 'mangle', '-I', 'PREROUTING', '1', '-j', - 'sshuttle-t-1024'), + 'sshuttle-t-1024'), call(AF_INET6, 'mangle', '-A', 'sshuttle-d-1024', '-j', 'MARK', '--set-mark', '1'), call(AF_INET6, 'mangle', '-A', 'sshuttle-d-1024', '-j', 'ACCEPT'), @@ -232,7 +232,7 @@ def test_setup_firewall(mock_ipt_chain_exists, mock_ipt_ttl, mock_ipt): call(AF_INET, 'mangle', '-F', 'sshuttle-t-1025'), call(AF_INET, 'mangle', '-I', 'OUTPUT', '1', '-j', 'sshuttle-m-1025'), call(AF_INET, 'mangle', '-I', 'PREROUTING', '1', '-j', - 'sshuttle-t-1025'), + 'sshuttle-t-1025'), call(AF_INET, 'mangle', '-A', 'sshuttle-d-1025', '-j', 'MARK', '--set-mark', '1'), call(AF_INET, 'mangle', '-A', 'sshuttle-d-1025', '-j', 'ACCEPT'), diff --git a/tests/client/test_options.py b/tests/client/test_options.py index ccfd526..02f4db2 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -1,7 +1,9 @@ import socket +from argparse import ArgumentTypeError as Fatal + import pytest + import sshuttle.options -from argparse import ArgumentTypeError as Fatal _ip4_reprs = { '0.0.0.0': '0.0.0.0', @@ -25,6 +27,7 @@ _ip6_reprs = { _ip6_swidths = (48, 64, 96, 115, 128) + def test_parse_subnetport_ip4(): for ip_repr, ip in _ip4_reprs.items(): assert sshuttle.options.parse_subnetport(ip_repr) \ @@ -41,7 +44,7 @@ def test_parse_subnetport_ip4_with_mask(): '/'.join((ip_repr, str(swidth))) ) == (socket.AF_INET, ip, swidth, 0, 0) assert sshuttle.options.parse_subnetport('0/0') \ - == (socket.AF_INET, '0.0.0.0', 0, 0, 0) + == (socket.AF_INET, '0.0.0.0', 0, 0, 0) with pytest.raises(Fatal) as excinfo: sshuttle.options.parse_subnetport('10.0.0.0/33') assert str(excinfo.value) == 'width 33 is not between 0 and 32' @@ -50,17 +53,17 @@ def test_parse_subnetport_ip4_with_mask(): def test_parse_subnetport_ip4_with_port(): for ip_repr, ip in _ip4_reprs.items(): assert sshuttle.options.parse_subnetport(':'.join((ip_repr, '80'))) \ - == (socket.AF_INET, ip, 32, 80, 80) + == (socket.AF_INET, ip, 32, 80, 80) assert sshuttle.options.parse_subnetport(':'.join((ip_repr, '80-90')))\ - == (socket.AF_INET, ip, 32, 80, 90) + == (socket.AF_INET, ip, 32, 80, 90) def test_parse_subnetport_ip4_with_mask_and_port(): for ip_repr, ip in _ip4_reprs.items(): assert sshuttle.options.parse_subnetport(ip_repr + '/32:80') \ - == (socket.AF_INET, ip, 32, 80, 80) + == (socket.AF_INET, ip, 32, 80, 80) assert sshuttle.options.parse_subnetport(ip_repr + '/16:80-90') \ - == (socket.AF_INET, ip, 16, 80, 90) + == (socket.AF_INET, ip, 16, 80, 90) def test_parse_subnetport_ip6(): @@ -76,7 +79,7 @@ def test_parse_subnetport_ip6_with_mask(): '/'.join((ip_repr, str(swidth))) ) == (socket.AF_INET6, ip, swidth, 0, 0) assert sshuttle.options.parse_subnetport('::/0') \ - == (socket.AF_INET6, '::', 0, 0, 0) + == (socket.AF_INET6, '::', 0, 0, 0) with pytest.raises(Fatal) as excinfo: sshuttle.options.parse_subnetport('fc00::/129') assert str(excinfo.value) == 'width 129 is not between 0 and 128' @@ -85,14 +88,14 @@ def test_parse_subnetport_ip6_with_mask(): def test_parse_subnetport_ip6_with_port(): for ip_repr, ip in _ip6_reprs.items(): assert sshuttle.options.parse_subnetport('[' + ip_repr + ']:80') \ - == (socket.AF_INET6, ip, 128, 80, 80) + == (socket.AF_INET6, ip, 128, 80, 80) assert sshuttle.options.parse_subnetport('[' + ip_repr + ']:80-90') \ - == (socket.AF_INET6, ip, 128, 80, 90) + == (socket.AF_INET6, ip, 128, 80, 90) def test_parse_subnetport_ip6_with_mask_and_port(): for ip_repr, ip in _ip6_reprs.items(): assert sshuttle.options.parse_subnetport('[' + ip_repr + '/128]:80') \ - == (socket.AF_INET6, ip, 128, 80, 80) + == (socket.AF_INET6, ip, 128, 80, 80) assert sshuttle.options.parse_subnetport('[' + ip_repr + '/16]:80-90')\ - == (socket.AF_INET6, ip, 16, 80, 90) + == (socket.AF_INET6, ip, 16, 80, 90) diff --git a/tests/client/test_sdnotify.py b/tests/client/test_sdnotify.py index 6585fff..b664ee7 100644 --- a/tests/client/test_sdnotify.py +++ b/tests/client/test_sdnotify.py @@ -1,6 +1,7 @@ -from mock import Mock, patch, call import socket +from mock import Mock, patch, call + import sshuttle.sdnotify diff --git a/tests/server/test_server.py b/tests/server/test_server.py index 0923986..86f421c 100644 --- a/tests/server/test_server.py +++ b/tests/server/test_server.py @@ -1,8 +1,10 @@ import io import socket -import sshuttle.server + from mock import patch, Mock +import sshuttle.server + def test__ipmatch(): assert sshuttle.server._ipmatch("1.2.3.4") is not None @@ -45,7 +47,7 @@ Destination Gateway Genmask Flags MSS Window irtt Iface def test_listroutes_iproute(mock_popen, mock_which): mock_pobj = Mock() mock_pobj.stdout = io.BytesIO(b""" -default via 192.168.1.1 dev wlan0 proto static +default via 192.168.1.1 dev wlan0 proto static 192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.1 """) mock_pobj.wait.return_value = 0 -- cgit v1.2.3