Skip to content

Commit c619840

Browse files
committed
Test PF on non-darwin.
1 parent e63e121 commit c619840

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

sshuttle/tests/test_methods_pf.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def test_firewall_command(mock_pf_get_dev, mock_ioctl, mock_stdout):
9898

9999
def pfctl(args, stdin=None):
100100
if args == '-s all':
101-
return (b'another mary had a little lamb\n', b'little lamb\n')
101+
return (b'INFO:\nStatus: Disabled\nanother mary had a little lamb\n',
102+
b'little lamb\n')
102103
if args == '-E':
103104
return (b'\n', b'Token : abcdefg\n')
104105
return None
@@ -109,7 +110,7 @@ def pfctl(args, stdin=None):
109110
@patch('sshuttle.methods.pf.pfctl')
110111
@patch('sshuttle.methods.pf.ioctl')
111112
@patch('sshuttle.methods.pf.pf_get_dev')
112-
def test_setup_firewall(mock_pf_get_dev, mock_ioctl, mock_pfctl):
113+
def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
113114
mock_pfctl.side_effect = pfctl
114115

115116
method = get_method('pf')
@@ -183,3 +184,84 @@ def test_setup_firewall(mock_pf_get_dev, mock_ioctl, mock_pfctl):
183184
mock_pf_get_dev.reset_mock()
184185
mock_pfctl.reset_mock()
185186
mock_ioctl.reset_mock()
187+
188+
189+
@patch('sshuttle.helpers.verbose', new=3)
190+
@patch('sshuttle.methods.pf.sys.platform', 'notdarwin')
191+
@patch('sshuttle.methods.pf.pfctl')
192+
@patch('sshuttle.methods.pf.ioctl')
193+
@patch('sshuttle.methods.pf.pf_get_dev')
194+
def test_setup_firewall_notdarwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
195+
mock_pfctl.side_effect = pfctl
196+
197+
method = get_method('pf')
198+
assert method.name == 'pf'
199+
200+
with pytest.raises(Exception) as excinfo:
201+
method.setup_firewall(
202+
1024, 1026,
203+
[(10, u'2404:6800:4004:80c::33')],
204+
10,
205+
[(10, 64, False, u'2404:6800:4004:80c::'),
206+
(10, 128, True, u'2404:6800:4004:80c::101f')],
207+
True)
208+
assert str(excinfo.value) \
209+
== 'Address family "AF_INET6" unsupported by pf method_name'
210+
assert mock_pf_get_dev.mock_calls == []
211+
assert mock_ioctl.mock_calls == []
212+
assert mock_pfctl.mock_calls == []
213+
214+
with pytest.raises(Exception) as excinfo:
215+
method.setup_firewall(
216+
1025, 1027,
217+
[(2, u'1.2.3.33')],
218+
2,
219+
[(2, 24, False, u'1.2.3.0'), (2, 32, True, u'1.2.3.66')],
220+
True)
221+
assert str(excinfo.value) == 'UDP not supported by pf method_name'
222+
assert mock_pf_get_dev.mock_calls == []
223+
assert mock_ioctl.mock_calls == []
224+
assert mock_pfctl.mock_calls == []
225+
226+
method.setup_firewall(
227+
1025, 1027,
228+
[(2, u'1.2.3.33')],
229+
2,
230+
[(2, 24, False, u'1.2.3.0'), (2, 32, True, u'1.2.3.66')],
231+
False)
232+
assert mock_ioctl.mock_calls == [
233+
call(mock_pf_get_dev(), 3295691827, ANY),
234+
call(mock_pf_get_dev(), 3424666650, ANY),
235+
call(mock_pf_get_dev(), 3424666650, ANY),
236+
call(mock_pf_get_dev(), 3295691827, ANY),
237+
call(mock_pf_get_dev(), 3424666650, ANY),
238+
call(mock_pf_get_dev(), 3424666650, ANY),
239+
]
240+
assert mock_pfctl.mock_calls == [
241+
call('-s all'),
242+
call('-a sshuttle -f /dev/stdin',
243+
b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n'
244+
b'table <dns_servers> {1.2.3.33}\n'
245+
b'rdr pass on lo0 proto tcp '
246+
b'to <forward_subnets> -> 127.0.0.1 port 1025\n'
247+
b'rdr pass on lo0 proto udp '
248+
b'to <dns_servers> port 53 -> 127.0.0.1 port 1027\n'
249+
b'pass out route-to lo0 inet proto tcp '
250+
b'to <forward_subnets> keep state\n'
251+
b'pass out route-to lo0 inet proto udp '
252+
b'to <dns_servers> port 53 keep state\n'),
253+
call('-e'),
254+
]
255+
mock_pf_get_dev.reset_mock()
256+
mock_ioctl.reset_mock()
257+
mock_pfctl.reset_mock()
258+
259+
method.restore_firewall(1025, 2, False)
260+
assert mock_ioctl.mock_calls == []
261+
assert mock_pfctl.mock_calls == [
262+
call('-a sshuttle -F all'),
263+
call("-d"),
264+
]
265+
mock_pf_get_dev.reset_mock()
266+
mock_pfctl.reset_mock()
267+
mock_ioctl.reset_mock()

0 commit comments

Comments
 (0)