Skip to content

Commit 021e6f5

Browse files
committed
Add more tests.
1 parent 9cc6d63 commit 021e6f5

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

sshuttle/tests/test_helpers.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
from mock import patch, call
2+
import io
3+
import socket
4+
5+
import sshuttle.helpers
6+
7+
8+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
9+
@patch('sshuttle.helpers.sys.stdout')
10+
@patch('sshuttle.helpers.sys.stderr')
11+
def test_log(mock_stderr, mock_stdout):
12+
sshuttle.helpers.log("message")
13+
assert mock_stdout.mock_calls == [
14+
call.flush(),
15+
]
16+
assert mock_stderr.mock_calls == [
17+
call.write('prefix: message'),
18+
call.flush(),
19+
]
20+
21+
22+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
23+
@patch('sshuttle.helpers.verbose', new=1)
24+
@patch('sshuttle.helpers.sys.stdout')
25+
@patch('sshuttle.helpers.sys.stderr')
26+
def test_debug1(mock_stderr, mock_stdout):
27+
sshuttle.helpers.debug1("message")
28+
assert mock_stdout.mock_calls == [
29+
call.flush(),
30+
]
31+
assert mock_stderr.mock_calls == [
32+
call.write('prefix: message'),
33+
call.flush(),
34+
]
35+
36+
37+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
38+
@patch('sshuttle.helpers.verbose', new=0)
39+
@patch('sshuttle.helpers.sys.stdout')
40+
@patch('sshuttle.helpers.sys.stderr')
41+
def test_debug1_nop(mock_stderr, mock_stdout):
42+
sshuttle.helpers.debug1("message")
43+
assert mock_stdout.mock_calls == []
44+
assert mock_stderr.mock_calls == []
45+
46+
47+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
48+
@patch('sshuttle.helpers.verbose', new=2)
49+
@patch('sshuttle.helpers.sys.stdout')
50+
@patch('sshuttle.helpers.sys.stderr')
51+
def test_debug2(mock_stderr, mock_stdout):
52+
sshuttle.helpers.debug2("message")
53+
assert mock_stdout.mock_calls == [
54+
call.flush(),
55+
]
56+
assert mock_stderr.mock_calls == [
57+
call.write('prefix: message'),
58+
call.flush(),
59+
]
60+
61+
62+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
63+
@patch('sshuttle.helpers.verbose', new=1)
64+
@patch('sshuttle.helpers.sys.stdout')
65+
@patch('sshuttle.helpers.sys.stderr')
66+
def test_debug2_nop(mock_stderr, mock_stdout):
67+
sshuttle.helpers.debug2("message")
68+
assert mock_stdout.mock_calls == []
69+
assert mock_stderr.mock_calls == []
70+
71+
72+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
73+
@patch('sshuttle.helpers.verbose', new=3)
74+
@patch('sshuttle.helpers.sys.stdout')
75+
@patch('sshuttle.helpers.sys.stderr')
76+
def test_debug3(mock_stderr, mock_stdout):
77+
sshuttle.helpers.debug3("message")
78+
assert mock_stdout.mock_calls == [
79+
call.flush(),
80+
]
81+
assert mock_stderr.mock_calls == [
82+
call.write('prefix: message'),
83+
call.flush(),
84+
]
85+
86+
87+
@patch('sshuttle.helpers.logprefix', new='prefix: ')
88+
@patch('sshuttle.helpers.verbose', new=2)
89+
@patch('sshuttle.helpers.sys.stdout')
90+
@patch('sshuttle.helpers.sys.stderr')
91+
def test_debug3_nop(mock_stderr, mock_stdout):
92+
sshuttle.helpers.debug3("message")
93+
assert mock_stdout.mock_calls == []
94+
assert mock_stderr.mock_calls == []
95+
96+
97+
@patch('sshuttle.helpers.open', create=True)
98+
def test_resolvconf_nameservers(mock_open):
99+
mock_open.return_value = io.StringIO(u"""
100+
# Generated by NetworkManager
101+
search pri
102+
nameserver 192.168.1.1
103+
nameserver 192.168.2.1
104+
nameserver 192.168.3.1
105+
nameserver 192.168.4.1
106+
nameserver 2404:6800:4004:80c::1
107+
nameserver 2404:6800:4004:80c::2
108+
nameserver 2404:6800:4004:80c::3
109+
nameserver 2404:6800:4004:80c::4
110+
""")
111+
112+
ns = sshuttle.helpers.resolvconf_nameservers()
113+
assert ns == [
114+
(2, u'192.168.1.1'), (2, u'192.168.2.1'),
115+
(2, u'192.168.3.1'), (2, u'192.168.4.1'),
116+
(10, u'2404:6800:4004:80c::1'), (10, u'2404:6800:4004:80c::2'),
117+
(10, u'2404:6800:4004:80c::3'), (10, u'2404:6800:4004:80c::4')
118+
]
119+
120+
121+
@patch('sshuttle.helpers.open', create=True)
122+
def test_resolvconf_random_nameserver(mock_open):
123+
mock_open.return_value = io.StringIO(u"""
124+
# Generated by NetworkManager
125+
search pri
126+
nameserver 192.168.1.1
127+
nameserver 192.168.2.1
128+
nameserver 192.168.3.1
129+
nameserver 192.168.4.1
130+
nameserver 2404:6800:4004:80c::1
131+
nameserver 2404:6800:4004:80c::2
132+
nameserver 2404:6800:4004:80c::3
133+
nameserver 2404:6800:4004:80c::4
134+
""")
135+
ns = sshuttle.helpers.resolvconf_random_nameserver()
136+
assert ns in [
137+
(2, u'192.168.1.1'), (2, u'192.168.2.1'),
138+
(2, u'192.168.3.1'), (2, u'192.168.4.1'),
139+
(10, u'2404:6800:4004:80c::1'), (10, u'2404:6800:4004:80c::2'),
140+
(10, u'2404:6800:4004:80c::3'), (10, u'2404:6800:4004:80c::4')
141+
]
142+
143+
144+
def test_islocal():
145+
assert sshuttle.helpers.islocal("127.0.0.1", socket.AF_INET)
146+
assert not sshuttle.helpers.islocal("192.0.2.1", socket.AF_INET)
147+
assert sshuttle.helpers.islocal("::1", socket.AF_INET6)
148+
assert not sshuttle.helpers.islocal("2001:db8::1", socket.AF_INET6)
149+
150+
151+
def test_family_ip_tuple():
152+
assert sshuttle.helpers.family_ip_tuple("127.0.0.1") \
153+
== (socket.AF_INET, "127.0.0.1")
154+
assert sshuttle.helpers.family_ip_tuple("192.168.2.6") \
155+
== (socket.AF_INET, "192.168.2.6")
156+
assert sshuttle.helpers.family_ip_tuple("::1") \
157+
== (socket.AF_INET6, "::1")
158+
assert sshuttle.helpers.family_ip_tuple("2404:6800:4004:80c::1") \
159+
== (socket.AF_INET6, "2404:6800:4004:80c::1")
160+
161+
162+
def test_family_to_string():
163+
assert sshuttle.helpers.family_to_string(socket.AF_INET) == "AF_INET"
164+
assert sshuttle.helpers.family_to_string(socket.AF_INET6) == "AF_INET6"
165+
assert sshuttle.helpers.family_to_string(socket.AF_UNIX) == "1"

0 commit comments

Comments
 (0)