mirror of
https://github.com/sshuttle/sshuttle.git
synced 2025-04-16 15:28:31 +02:00
Add more tests.
This commit is contained in:
parent
9cc6d63684
commit
021e6f57af
165
sshuttle/tests/test_helpers.py
Normal file
165
sshuttle/tests/test_helpers.py
Normal file
@ -0,0 +1,165 @@
|
||||
from mock import patch, call
|
||||
import io
|
||||
import socket
|
||||
|
||||
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")
|
||||
assert mock_stdout.mock_calls == [
|
||||
call.flush(),
|
||||
]
|
||||
assert mock_stderr.mock_calls == [
|
||||
call.write('prefix: message'),
|
||||
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_debug1(mock_stderr, mock_stdout):
|
||||
sshuttle.helpers.debug1("message")
|
||||
assert mock_stdout.mock_calls == [
|
||||
call.flush(),
|
||||
]
|
||||
assert mock_stderr.mock_calls == [
|
||||
call.write('prefix: message'),
|
||||
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 == [
|
||||
call.write('prefix: message'),
|
||||
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 == [
|
||||
call.write('prefix: message'),
|
||||
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
|
||||
""")
|
||||
|
||||
ns = sshuttle.helpers.resolvconf_nameservers()
|
||||
assert ns == [
|
||||
(2, u'192.168.1.1'), (2, u'192.168.2.1'),
|
||||
(2, u'192.168.3.1'), (2, u'192.168.4.1'),
|
||||
(10, u'2404:6800:4004:80c::1'), (10, u'2404:6800:4004:80c::2'),
|
||||
(10, u'2404:6800:4004:80c::3'), (10, u'2404:6800:4004:80c::4')
|
||||
]
|
||||
|
||||
|
||||
@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
|
||||
""")
|
||||
ns = sshuttle.helpers.resolvconf_random_nameserver()
|
||||
assert ns in [
|
||||
(2, u'192.168.1.1'), (2, u'192.168.2.1'),
|
||||
(2, u'192.168.3.1'), (2, u'192.168.4.1'),
|
||||
(10, u'2404:6800:4004:80c::1'), (10, u'2404:6800:4004:80c::2'),
|
||||
(10, u'2404:6800:4004:80c::3'), (10, u'2404:6800:4004:80c::4')
|
||||
]
|
||||
|
||||
|
||||
def test_islocal():
|
||||
assert sshuttle.helpers.islocal("127.0.0.1", socket.AF_INET)
|
||||
assert not sshuttle.helpers.islocal("192.0.2.1", socket.AF_INET)
|
||||
assert sshuttle.helpers.islocal("::1", socket.AF_INET6)
|
||||
assert not sshuttle.helpers.islocal("2001:db8::1", socket.AF_INET6)
|
||||
|
||||
|
||||
def test_family_ip_tuple():
|
||||
assert sshuttle.helpers.family_ip_tuple("127.0.0.1") \
|
||||
== (socket.AF_INET, "127.0.0.1")
|
||||
assert sshuttle.helpers.family_ip_tuple("192.168.2.6") \
|
||||
== (socket.AF_INET, "192.168.2.6")
|
||||
assert sshuttle.helpers.family_ip_tuple("::1") \
|
||||
== (socket.AF_INET6, "::1")
|
||||
assert sshuttle.helpers.family_ip_tuple("2404:6800:4004:80c::1") \
|
||||
== (socket.AF_INET6, "2404:6800:4004:80c::1")
|
||||
|
||||
|
||||
def test_family_to_string():
|
||||
assert sshuttle.helpers.family_to_string(socket.AF_INET) == "AF_INET"
|
||||
assert sshuttle.helpers.family_to_string(socket.AF_INET6) == "AF_INET6"
|
||||
assert sshuttle.helpers.family_to_string(socket.AF_UNIX) == "1"
|
Loading…
Reference in New Issue
Block a user