2015-11-17 03:08:12 +01:00
|
|
|
import io
|
|
|
|
import socket
|
2017-09-24 14:11:26 +02:00
|
|
|
from socket import AF_INET, AF_INET6
|
2017-11-07 00:00:06 +01:00
|
|
|
import errno
|
2015-11-17 03:08:12 +01:00
|
|
|
|
2019-02-10 23:59:13 +01:00
|
|
|
from mock import patch, call
|
2015-11-17 03:08:12 +01:00
|
|
|
import sshuttle.helpers
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_log(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.log("message")
|
2015-12-15 04:23:42 +01:00
|
|
|
sshuttle.helpers.log("abc")
|
2015-12-13 23:21:15 +01:00
|
|
|
sshuttle.helpers.log("message 1\n")
|
|
|
|
sshuttle.helpers.log("message 2\nline2\nline3\n")
|
|
|
|
sshuttle.helpers.log("message 3\nline2\nline3")
|
2015-11-17 03:08:12 +01:00
|
|
|
assert mock_stdout.mock_calls == [
|
|
|
|
call.flush(),
|
2015-12-13 23:21:15 +01:00
|
|
|
call.flush(),
|
|
|
|
call.flush(),
|
|
|
|
call.flush(),
|
2015-12-15 04:23:42 +01:00
|
|
|
call.flush(),
|
2015-11-17 03:08:12 +01:00
|
|
|
]
|
|
|
|
assert mock_stderr.mock_calls == [
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write('prefix: message\n'),
|
2015-11-17 03:08:12 +01:00
|
|
|
call.flush(),
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write('prefix: abc\n'),
|
2015-12-15 04:23:42 +01:00
|
|
|
call.flush(),
|
2015-12-13 23:21:15 +01:00
|
|
|
call.write('prefix: message 1\n'),
|
|
|
|
call.flush(),
|
|
|
|
call.write('prefix: message 2\n'),
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write(' line2\n'),
|
|
|
|
call.write(' line3\n'),
|
2015-12-13 23:21:15 +01:00
|
|
|
call.flush(),
|
|
|
|
call.write('prefix: message 3\n'),
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write(' line2\n'),
|
|
|
|
call.write(' line3\n'),
|
2015-12-13 23:21:15 +01:00
|
|
|
call.flush(),
|
2015-11-17 03:08:12 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=1)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug1(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug1("message")
|
|
|
|
assert mock_stdout.mock_calls == [
|
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
assert mock_stderr.mock_calls == [
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write('prefix: message\n'),
|
2015-11-17 03:08:12 +01:00
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=0)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug1_nop(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug1("message")
|
|
|
|
assert mock_stdout.mock_calls == []
|
|
|
|
assert mock_stderr.mock_calls == []
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=2)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug2(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug2("message")
|
|
|
|
assert mock_stdout.mock_calls == [
|
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
assert mock_stderr.mock_calls == [
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write('prefix: message\n'),
|
2015-11-17 03:08:12 +01:00
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=1)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug2_nop(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug2("message")
|
|
|
|
assert mock_stdout.mock_calls == []
|
|
|
|
assert mock_stderr.mock_calls == []
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=3)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug3(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug3("message")
|
|
|
|
assert mock_stdout.mock_calls == [
|
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
assert mock_stderr.mock_calls == [
|
2020-12-29 18:58:44 +01:00
|
|
|
call.write('prefix: message\n'),
|
2015-11-17 03:08:12 +01:00
|
|
|
call.flush(),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.logprefix', new='prefix: ')
|
|
|
|
@patch('sshuttle.helpers.verbose', new=2)
|
|
|
|
@patch('sshuttle.helpers.sys.stdout')
|
|
|
|
@patch('sshuttle.helpers.sys.stderr')
|
|
|
|
def test_debug3_nop(mock_stderr, mock_stdout):
|
|
|
|
sshuttle.helpers.debug3("message")
|
|
|
|
assert mock_stdout.mock_calls == []
|
|
|
|
assert mock_stderr.mock_calls == []
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.open', create=True)
|
|
|
|
def test_resolvconf_nameservers(mock_open):
|
|
|
|
mock_open.return_value = io.StringIO(u"""
|
|
|
|
# Generated by NetworkManager
|
|
|
|
search pri
|
|
|
|
nameserver 192.168.1.1
|
|
|
|
nameserver 192.168.2.1
|
|
|
|
nameserver 192.168.3.1
|
|
|
|
nameserver 192.168.4.1
|
|
|
|
nameserver 2404:6800:4004:80c::1
|
|
|
|
nameserver 2404:6800:4004:80c::2
|
|
|
|
nameserver 2404:6800:4004:80c::3
|
|
|
|
nameserver 2404:6800:4004:80c::4
|
|
|
|
""")
|
|
|
|
|
2020-11-04 02:27:57 +01:00
|
|
|
ns = sshuttle.helpers.resolvconf_nameservers(False)
|
2015-11-17 03:08:12 +01:00
|
|
|
assert ns == [
|
2017-09-24 14:11:26 +02:00
|
|
|
(AF_INET, u'192.168.1.1'), (AF_INET, u'192.168.2.1'),
|
|
|
|
(AF_INET, u'192.168.3.1'), (AF_INET, u'192.168.4.1'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::1'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::2'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::3'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::4')
|
2015-11-17 03:08:12 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@patch('sshuttle.helpers.open', create=True)
|
|
|
|
def test_resolvconf_random_nameserver(mock_open):
|
|
|
|
mock_open.return_value = io.StringIO(u"""
|
|
|
|
# Generated by NetworkManager
|
|
|
|
search pri
|
|
|
|
nameserver 192.168.1.1
|
|
|
|
nameserver 192.168.2.1
|
|
|
|
nameserver 192.168.3.1
|
|
|
|
nameserver 192.168.4.1
|
|
|
|
nameserver 2404:6800:4004:80c::1
|
|
|
|
nameserver 2404:6800:4004:80c::2
|
|
|
|
nameserver 2404:6800:4004:80c::3
|
|
|
|
nameserver 2404:6800:4004:80c::4
|
|
|
|
""")
|
2020-11-04 02:27:57 +01:00
|
|
|
ns = sshuttle.helpers.resolvconf_random_nameserver(False)
|
2015-11-17 03:08:12 +01:00
|
|
|
assert ns in [
|
2017-09-24 14:11:26 +02:00
|
|
|
(AF_INET, u'192.168.1.1'), (AF_INET, u'192.168.2.1'),
|
|
|
|
(AF_INET, u'192.168.3.1'), (AF_INET, u'192.168.4.1'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::1'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::2'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::3'),
|
|
|
|
(AF_INET6, u'2404:6800:4004:80c::4')
|
2015-11-17 03:08:12 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-11-07 00:00:06 +01:00
|
|
|
@patch('sshuttle.helpers.socket.socket.bind')
|
|
|
|
def test_islocal(mock_bind):
|
|
|
|
bind_error = socket.error(errno.EADDRNOTAVAIL)
|
|
|
|
mock_bind.side_effect = [None, bind_error, None, bind_error]
|
|
|
|
|
2017-09-24 14:11:26 +02:00
|
|
|
assert sshuttle.helpers.islocal("127.0.0.1", AF_INET)
|
|
|
|
assert not sshuttle.helpers.islocal("192.0.2.1", AF_INET)
|
|
|
|
assert sshuttle.helpers.islocal("::1", AF_INET6)
|
|
|
|
assert not sshuttle.helpers.islocal("2001:db8::1", AF_INET6)
|
2015-11-17 03:08:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_family_ip_tuple():
|
|
|
|
assert sshuttle.helpers.family_ip_tuple("127.0.0.1") \
|
2017-09-24 14:11:26 +02:00
|
|
|
== (AF_INET, "127.0.0.1")
|
2015-11-17 03:08:12 +01:00
|
|
|
assert sshuttle.helpers.family_ip_tuple("192.168.2.6") \
|
2017-09-24 14:11:26 +02:00
|
|
|
== (AF_INET, "192.168.2.6")
|
2015-11-17 03:08:12 +01:00
|
|
|
assert sshuttle.helpers.family_ip_tuple("::1") \
|
2017-09-24 14:11:26 +02:00
|
|
|
== (AF_INET6, "::1")
|
2015-11-17 03:08:12 +01:00
|
|
|
assert sshuttle.helpers.family_ip_tuple("2404:6800:4004:80c::1") \
|
2017-09-24 14:11:26 +02:00
|
|
|
== (AF_INET6, "2404:6800:4004:80c::1")
|
2015-11-17 03:08:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_family_to_string():
|
2017-09-24 14:11:26 +02:00
|
|
|
assert sshuttle.helpers.family_to_string(AF_INET) == "AF_INET"
|
|
|
|
assert sshuttle.helpers.family_to_string(AF_INET6) == "AF_INET6"
|
2020-06-03 04:53:46 +02:00
|
|
|
expected = 'AddressFamily.AF_UNIX'
|
2015-11-17 03:12:41 +01:00
|
|
|
assert sshuttle.helpers.family_to_string(socket.AF_UNIX) == expected
|