2016-11-02 18:28:16 +01:00
|
|
|
import socket
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=attribute-defined-outside-init,protected-access
|
2020-06-15 23:05:31 +02:00
|
|
|
class TestResolve:
|
2020-06-13 03:01:10 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def init(self, ssh_audit):
|
|
|
|
self.AuditConf = ssh_audit.AuditConf
|
|
|
|
self.audit = ssh_audit.audit
|
2021-03-02 17:25:37 +01:00
|
|
|
self.OutputBuffer = ssh_audit.OutputBuffer
|
2020-10-15 20:34:23 +02:00
|
|
|
self.ssh_socket = ssh_audit.SSH_Socket
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def _conf(self):
|
|
|
|
conf = self.AuditConf('localhost', 22)
|
|
|
|
conf.colors = False
|
|
|
|
conf.batch = True
|
|
|
|
return conf
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_error(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.gsock.addrinfodata['localhost#22'] = socket.gaierror(8, 'hostname nor servname provided, or not known')
|
|
|
|
conf = self._conf()
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
with pytest.raises(SystemExit):
|
2021-02-23 22:05:01 +01:00
|
|
|
list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
lines = output_spy.flush()
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'hostname nor servname provided' in lines[-1]
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_hostname_without_records(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.gsock.addrinfodata['localhost#22'] = []
|
|
|
|
conf = self._conf()
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 0
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_ipv4(self, virtual_socket):
|
|
|
|
conf = self._conf()
|
|
|
|
conf.ipv4 = True
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 1
|
|
|
|
assert r[0] == (socket.AF_INET, ('127.0.0.1', 22))
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_ipv6(self, virtual_socket):
|
|
|
|
conf = self._conf()
|
|
|
|
conf.ipv6 = True
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 1
|
|
|
|
assert r[0] == (socket.AF_INET6, ('::1', 22))
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_ipv46_both(self, virtual_socket):
|
|
|
|
conf = self._conf()
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 2
|
|
|
|
assert r[0] == (socket.AF_INET, ('127.0.0.1', 22))
|
|
|
|
assert r[1] == (socket.AF_INET6, ('::1', 22))
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
def test_resolve_ipv46_order(self, virtual_socket):
|
|
|
|
conf = self._conf()
|
|
|
|
conf.ipv4 = True
|
|
|
|
conf.ipv6 = True
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 2
|
|
|
|
assert r[0] == (socket.AF_INET, ('127.0.0.1', 22))
|
|
|
|
assert r[1] == (socket.AF_INET6, ('::1', 22))
|
|
|
|
conf = self._conf()
|
|
|
|
conf.ipv6 = True
|
|
|
|
conf.ipv4 = True
|
2021-03-02 17:25:37 +01:00
|
|
|
s = self.ssh_socket(self.OutputBuffer(), 'localhost', 22, conf.ip_version_preference)
|
2021-02-23 22:05:01 +01:00
|
|
|
r = list(s._resolve())
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(r) == 2
|
|
|
|
assert r[0] == (socket.AF_INET6, ('::1', 22))
|
|
|
|
assert r[1] == (socket.AF_INET, ('127.0.0.1', 22))
|