2016-03-16 08:24:43 +01:00
|
|
|
import io
|
|
|
|
import socket
|
2019-02-10 23:59:13 +01:00
|
|
|
|
2021-01-16 01:59:28 +01:00
|
|
|
from unittest.mock import patch, Mock
|
2016-03-16 07:38:33 +01:00
|
|
|
|
2019-02-10 23:59:13 +01:00
|
|
|
import sshuttle.server
|
|
|
|
|
2016-03-16 07:38:33 +01:00
|
|
|
|
|
|
|
def test__ipmatch():
|
2015-12-05 04:40:26 +01:00
|
|
|
assert sshuttle.server._ipmatch("1.2.3.4") is not None
|
|
|
|
assert sshuttle.server._ipmatch("::1") is None # ipv6 not supported
|
|
|
|
assert sshuttle.server._ipmatch("42 Example Street, Melbourne") is None
|
2016-03-16 08:24:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test__ipstr():
|
|
|
|
assert sshuttle.server._ipstr("1.2.3.4", 24) == "1.2.3.4/24"
|
|
|
|
assert sshuttle.server._ipstr("1.2.3.4", 32) == "1.2.3.4"
|
|
|
|
|
|
|
|
|
|
|
|
def test__maskbits():
|
2015-12-05 04:40:26 +01:00
|
|
|
netmask = sshuttle.server._ipmatch("255.255.255.0")
|
2016-03-16 08:24:43 +01:00
|
|
|
sshuttle.server._maskbits(netmask)
|
|
|
|
|
|
|
|
|
2017-02-09 02:29:56 +01:00
|
|
|
@patch('sshuttle.server.which', side_effect=lambda x: x == 'netstat')
|
2016-03-16 08:24:43 +01:00
|
|
|
@patch('sshuttle.server.ssubprocess.Popen')
|
2017-02-09 02:29:56 +01:00
|
|
|
def test_listroutes_netstat(mock_popen, mock_which):
|
2016-03-16 08:24:43 +01:00
|
|
|
mock_pobj = Mock()
|
|
|
|
mock_pobj.stdout = io.BytesIO(b"""
|
|
|
|
Kernel IP routing table
|
|
|
|
Destination Gateway Genmask Flags MSS Window irtt Iface
|
|
|
|
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0
|
|
|
|
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
|
|
|
|
""")
|
|
|
|
mock_pobj.wait.return_value = 0
|
|
|
|
mock_popen.return_value = mock_pobj
|
|
|
|
|
2017-02-09 02:29:56 +01:00
|
|
|
routes = sshuttle.server.list_routes()
|
2016-03-16 08:24:43 +01:00
|
|
|
|
2017-02-09 02:29:56 +01:00
|
|
|
assert list(routes) == [
|
2016-03-16 08:24:43 +01:00
|
|
|
(socket.AF_INET, '192.168.1.0', 24)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-02-09 02:29:56 +01:00
|
|
|
@patch('sshuttle.server.which', side_effect=lambda x: x == 'ip')
|
2016-03-16 08:24:43 +01:00
|
|
|
@patch('sshuttle.server.ssubprocess.Popen')
|
2017-02-09 02:29:56 +01:00
|
|
|
def test_listroutes_iproute(mock_popen, mock_which):
|
2016-03-16 08:24:43 +01:00
|
|
|
mock_pobj = Mock()
|
|
|
|
mock_pobj.stdout = io.BytesIO(b"""
|
2019-02-10 23:59:13 +01:00
|
|
|
default via 192.168.1.1 dev wlan0 proto static
|
2017-02-09 02:29:56 +01:00
|
|
|
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.1
|
2016-03-16 08:24:43 +01:00
|
|
|
""")
|
|
|
|
mock_pobj.wait.return_value = 0
|
|
|
|
mock_popen.return_value = mock_pobj
|
|
|
|
|
|
|
|
routes = sshuttle.server.list_routes()
|
|
|
|
|
|
|
|
assert list(routes) == [
|
|
|
|
(socket.AF_INET, '192.168.1.0', 24)
|
|
|
|
]
|