From 04214eaf89001fe35eb29137228cbefaf30f43d5 Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:56:48 +0900 Subject: [PATCH 1/7] test hosts with no port specified --- tests/client/test_options.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index 6f86a8a..1b5f4a7 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -105,3 +105,11 @@ def test_parse_subnetport_ip6_with_mask_and_port(): def test_convert_arg_line_to_args_skips_comments(): parser = sshuttle.options.MyArgumentParser() assert parser.convert_arg_line_to_args("# whatever something") == [] + + +def test_parse_subnetport_host(): + assert set(sshuttle.options.parse_subnetport('example.com')) \ + == set([ + (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 0, 0), + (socket.AF_INET, '93.184.216.34', 32, 0, 0), + ]) From 2f026c84af9b471c407261756b3ec7c32f203282 Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:56:58 +0900 Subject: [PATCH 2/7] test hosts with port specified --- tests/client/test_options.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index 1b5f4a7..c367213 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -113,3 +113,16 @@ def test_parse_subnetport_host(): (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 0, 0), (socket.AF_INET, '93.184.216.34', 32, 0, 0), ]) + + +def test_parse_subnetport_host_with_port(): + assert set(sshuttle.options.parse_subnetport('example.com:80')) \ + == set([ + (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 80, 80), + (socket.AF_INET, '93.184.216.34', 32, 80, 80), + ]) + assert set(sshuttle.options.parse_subnetport('example.com:80-90')) \ + == set([ + (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 80, 90), + (socket.AF_INET, '93.184.216.34', 32, 80, 90), + ]) From 19e2a1810d40dc5b2dd1596d9cb805480455ed16 Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Wed, 9 Feb 2022 21:24:03 +0900 Subject: [PATCH 3/7] add getaddrinfo mock for test-cases with hosts --- tests/client/test_options.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index c367213..91e2871 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -27,6 +27,15 @@ _ip6_reprs = { _ip6_swidths = (48, 64, 96, 115, 128) +def _mock_getaddrinfo(host, *_): + return { + "example.com": [ + (socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('2606:2800:220:1:248:1893:25c8:1946', 0, 0, 0)), + (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('93.184.216.34', 0)), + ], + }.get(host, []) + + def test_parse_subnetport_ip4(): for ip_repr, ip in _ip4_reprs.items(): assert sshuttle.options.parse_subnetport(ip_repr) \ From e5eb5afef063aa7ffaf5098000ab3435f8c8591d Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Wed, 9 Feb 2022 21:25:42 +0900 Subject: [PATCH 4/7] use mocked getaddrinfo to make host name resolution stable --- tests/client/test_options.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index 91e2871..5e045b8 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -1,5 +1,6 @@ import socket from argparse import ArgumentTypeError as Fatal +from unittest.mock import patch import pytest @@ -116,7 +117,8 @@ def test_convert_arg_line_to_args_skips_comments(): assert parser.convert_arg_line_to_args("# whatever something") == [] -def test_parse_subnetport_host(): +@patch('sshuttle.options.socket.getaddrinfo', side_effect = _mock_getaddrinfo) +def test_parse_subnetport_host(mock_getaddrinfo): assert set(sshuttle.options.parse_subnetport('example.com')) \ == set([ (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 0, 0), @@ -124,7 +126,8 @@ def test_parse_subnetport_host(): ]) -def test_parse_subnetport_host_with_port(): +@patch('sshuttle.options.socket.getaddrinfo', side_effect = _mock_getaddrinfo) +def test_parse_subnetport_host_with_port(mock_getaddrinfo): assert set(sshuttle.options.parse_subnetport('example.com:80')) \ == set([ (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 80, 80), From b9b89c3f55f9088408c89010e15e52a81d4ec52e Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Wed, 9 Feb 2022 21:26:51 +0900 Subject: [PATCH 5/7] add another example for host resolution tests --- tests/client/test_options.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index 5e045b8..85dc88d 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -34,6 +34,10 @@ def _mock_getaddrinfo(host, *_): (socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('2606:2800:220:1:248:1893:25c8:1946', 0, 0, 0)), (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('93.184.216.34', 0)), ], + "my.local": [ + (socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0)), + (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 0)), + ], }.get(host, []) @@ -124,6 +128,11 @@ def test_parse_subnetport_host(mock_getaddrinfo): (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 0, 0), (socket.AF_INET, '93.184.216.34', 32, 0, 0), ]) + assert set(sshuttle.options.parse_subnetport('my.local')) \ + == set([ + (socket.AF_INET6, '::1', 128, 0, 0), + (socket.AF_INET, '127.0.0.1', 32, 0, 0), + ]) @patch('sshuttle.options.socket.getaddrinfo', side_effect = _mock_getaddrinfo) @@ -138,3 +147,13 @@ def test_parse_subnetport_host_with_port(mock_getaddrinfo): (socket.AF_INET6, '2606:2800:220:1:248:1893:25c8:1946', 128, 80, 90), (socket.AF_INET, '93.184.216.34', 32, 80, 90), ]) + assert set(sshuttle.options.parse_subnetport('my.local:445')) \ + == set([ + (socket.AF_INET6, '::1', 128, 445, 445), + (socket.AF_INET, '127.0.0.1', 32, 445, 445), + ]) + assert set(sshuttle.options.parse_subnetport('my.local:445-450')) \ + == set([ + (socket.AF_INET6, '::1', 128, 445, 450), + (socket.AF_INET, '127.0.0.1', 32, 445, 450), + ]) From 1d4c059f445aab6a42fe29306baba5795f2567b7 Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Thu, 10 Feb 2022 08:36:14 +0900 Subject: [PATCH 6/7] format styles: E251 unexpected spaces around keyword / parameter equals (flake8) --- tests/client/test_options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/client/test_options.py b/tests/client/test_options.py index 85dc88d..6969ce1 100644 --- a/tests/client/test_options.py +++ b/tests/client/test_options.py @@ -121,7 +121,7 @@ def test_convert_arg_line_to_args_skips_comments(): assert parser.convert_arg_line_to_args("# whatever something") == [] -@patch('sshuttle.options.socket.getaddrinfo', side_effect = _mock_getaddrinfo) +@patch('sshuttle.options.socket.getaddrinfo', side_effect=_mock_getaddrinfo) def test_parse_subnetport_host(mock_getaddrinfo): assert set(sshuttle.options.parse_subnetport('example.com')) \ == set([ @@ -135,7 +135,7 @@ def test_parse_subnetport_host(mock_getaddrinfo): ]) -@patch('sshuttle.options.socket.getaddrinfo', side_effect = _mock_getaddrinfo) +@patch('sshuttle.options.socket.getaddrinfo', side_effect=_mock_getaddrinfo) def test_parse_subnetport_host_with_port(mock_getaddrinfo): assert set(sshuttle.options.parse_subnetport('example.com:80')) \ == set([ From 2f5c946b488cfffba713949d432c488c8c0b148f Mon Sep 17 00:00:00 2001 From: mangano-ito <47137677+mangano-ito@users.noreply.github.com> Date: Thu, 10 Feb 2022 08:40:14 +0900 Subject: [PATCH 7/7] define flake8 max line length longer (79 to 128) --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 8b398f0..dbdf161 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,6 +12,7 @@ identity=0x1784577F811F6EAC count=true show-source=true statistics=true +max-line-length=128 [tool:pytest] addopts = --cov=sshuttle --cov-branch --cov-report=term-missing