Add linter fixes for tests.

This commit is contained in:
Andris Raugulis 2016-10-25 17:19:08 +03:00
parent 84dfdcaf5e
commit 4684ff0113
8 changed files with 52 additions and 40 deletions

View File

@ -1905,6 +1905,6 @@ def audit(aconf, sshv=None):
utils = Utils() utils = Utils()
out = Output() out = Output()
if __name__ == '__main__': if __name__ == '__main__': # pragma: nocover
conf = AuditConf.from_cmdline(sys.argv[1:], usage) conf = AuditConf.from_cmdline(sys.argv[1:], usage)
audit(conf) audit(conf)

View File

@ -3,13 +3,15 @@
import pytest import pytest
# pylint: disable=attribute-defined-outside-init
class TestAuditConf(object): class TestAuditConf(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):
self.AuditConf = ssh_audit.AuditConf self.AuditConf = ssh_audit.AuditConf
self.usage = ssh_audit.usage self.usage = ssh_audit.usage
def _test_conf(self, conf, **kwargs): @classmethod
def _test_conf(cls, conf, **kwargs):
options = { options = {
'host': None, 'host': None,
'port': 22, 'port': 22,
@ -66,7 +68,7 @@ class TestAuditConf(object):
excinfo.match(r'.*invalid level.*') excinfo.match(r'.*invalid level.*')
def test_audit_conf_cmdline(self): def test_audit_conf_cmdline(self):
c = lambda x: self.AuditConf.from_cmdline(x.split(), self.usage) c = lambda x: self.AuditConf.from_cmdline(x.split(), self.usage) # noqa
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
conf = c('') conf = c('')
with pytest.raises(SystemExit): with pytest.raises(SystemExit):

View File

@ -3,13 +3,14 @@
import pytest import pytest
# pylint: disable=line-too-long,attribute-defined-outside-init
class TestBanner(object): class TestBanner(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):
self.ssh = ssh_audit.SSH self.ssh = ssh_audit.SSH
def test_simple_banners(self): def test_simple_banners(self):
banner = lambda x: self.ssh.Banner.parse(x) banner = lambda x: self.ssh.Banner.parse(x) # noqa
b = banner('SSH-2.0-OpenSSH_7.3') b = banner('SSH-2.0-OpenSSH_7.3')
assert b.protocol == (2, 0) assert b.protocol == (2, 0)
assert b.software == 'OpenSSH_7.3' assert b.software == 'OpenSSH_7.3'
@ -27,12 +28,12 @@ class TestBanner(object):
assert str(b) == 'SSH-1.5-Cisco-1.25' assert str(b) == 'SSH-1.5-Cisco-1.25'
def test_invalid_banners(self): def test_invalid_banners(self):
b = lambda x: self.ssh.Banner.parse(x) b = lambda x: self.ssh.Banner.parse(x) # noqa
assert b('Something') is None assert b('Something') is None
assert b('SSH-XXX-OpenSSH_7.3') is None assert b('SSH-XXX-OpenSSH_7.3') is None
def test_banners_with_spaces(self): def test_banners_with_spaces(self):
b = lambda x: self.ssh.Banner.parse(x) b = lambda x: self.ssh.Banner.parse(x) # noqa
s = 'SSH-2.0-OpenSSH_4.3p2' s = 'SSH-2.0-OpenSSH_4.3p2'
assert str(b('SSH-2.0-OpenSSH_4.3p2 ')) == s assert str(b('SSH-2.0-OpenSSH_4.3p2 ')) == s
assert str(b('SSH-2.0- OpenSSH_4.3p2')) == s assert str(b('SSH-2.0- OpenSSH_4.3p2')) == s
@ -43,7 +44,7 @@ class TestBanner(object):
assert str(b('SSH-2.0- OpenSSH_4.3p2 Debian-9etch3 on i686-pc-linux-gnu ')) == s assert str(b('SSH-2.0- OpenSSH_4.3p2 Debian-9etch3 on i686-pc-linux-gnu ')) == s
def test_banners_without_software(self): def test_banners_without_software(self):
b = lambda x: self.ssh.Banner.parse(x) b = lambda x: self.ssh.Banner.parse(x) # noqa
assert b('SSH-2.0').protocol == (2, 0) assert b('SSH-2.0').protocol == (2, 0)
assert b('SSH-2.0').software is None assert b('SSH-2.0').software is None
assert b('SSH-2.0').comments is None assert b('SSH-2.0').comments is None
@ -54,13 +55,13 @@ class TestBanner(object):
assert str(b('SSH-2.0-')) == 'SSH-2.0-' assert str(b('SSH-2.0-')) == 'SSH-2.0-'
def test_banners_with_comments(self): def test_banners_with_comments(self):
b = lambda x: self.ssh.Banner.parse(x) b = lambda x: self.ssh.Banner.parse(x) # noqa
assert repr(b('SSH-2.0-OpenSSH_7.2p2 Ubuntu-1')) == '<Banner(protocol=2.0, software=OpenSSH_7.2p2, comments=Ubuntu-1)>' assert repr(b('SSH-2.0-OpenSSH_7.2p2 Ubuntu-1')) == '<Banner(protocol=2.0, software=OpenSSH_7.2p2, comments=Ubuntu-1)>'
assert repr(b('SSH-1.99-OpenSSH_3.4p1 Debian 1:3.4p1-1.woody.3')) == '<Banner(protocol=1.99, software=OpenSSH_3.4p1, comments=Debian 1:3.4p1-1.woody.3)>' assert repr(b('SSH-1.99-OpenSSH_3.4p1 Debian 1:3.4p1-1.woody.3')) == '<Banner(protocol=1.99, software=OpenSSH_3.4p1, comments=Debian 1:3.4p1-1.woody.3)>'
assert repr(b('SSH-1.5-1.3.7 F-SECURE SSH')) == '<Banner(protocol=1.5, software=1.3.7, comments=F-SECURE SSH)>' assert repr(b('SSH-1.5-1.3.7 F-SECURE SSH')) == '<Banner(protocol=1.5, software=1.3.7, comments=F-SECURE SSH)>'
def test_banners_with_multiple_protocols(self): def test_banners_with_multiple_protocols(self):
b = lambda x: self.ssh.Banner.parse(x) b = lambda x: self.ssh.Banner.parse(x) # noqa
assert str(b('SSH-1.99-SSH-1.99-OpenSSH_3.6.1p2')) == 'SSH-1.99-OpenSSH_3.6.1p2' assert str(b('SSH-1.99-SSH-1.99-OpenSSH_3.6.1p2')) == 'SSH-1.99-OpenSSH_3.6.1p2'
assert str(b('SSH-2.0-SSH-2.0-OpenSSH_4.3p2 Debian-9')) == 'SSH-2.0-OpenSSH_4.3p2 Debian-9' assert str(b('SSH-2.0-SSH-2.0-OpenSSH_4.3p2 Debian-9')) == 'SSH-2.0-OpenSSH_4.3p2 Debian-9'
assert str(b('SSH-1.99-SSH-2.0-dropbear_0.5')) == 'SSH-1.99-dropbear_0.5' assert str(b('SSH-1.99-SSH-2.0-dropbear_0.5')) == 'SSH-1.99-dropbear_0.5'

View File

@ -1,9 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest
import re import re
import pytest
# pylint: disable=attribute-defined-outside-init,bad-whitespace
class TestBuffer(object): class TestBuffer(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):
@ -11,7 +12,8 @@ class TestBuffer(object):
self.wbuf = ssh_audit.WriteBuf self.wbuf = ssh_audit.WriteBuf
self.utf8rchar = b'\xef\xbf\xbd' self.utf8rchar = b'\xef\xbf\xbd'
def _b(self, v): @classmethod
def _b(cls, v):
v = re.sub(r'\s', '', v) v = re.sub(r'\s', '', v)
data = [int(v[i * 2:i * 2 + 2], 16) for i in range(len(v) // 2)] data = [int(v[i * 2:i * 2 + 2], 16) for i in range(len(v) // 2)]
return bytes(bytearray(data)) return bytes(bytearray(data))
@ -26,8 +28,8 @@ class TestBuffer(object):
assert r.unread_len == 0 assert r.unread_len == 0
def test_byte(self): def test_byte(self):
w = lambda x: self.wbuf().write_byte(x).write_flush() w = lambda x: self.wbuf().write_byte(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_byte() r = lambda x: self.rbuf(x).read_byte() # noqa
tc = [(0x00, '00'), tc = [(0x00, '00'),
(0x01, '01'), (0x01, '01'),
(0x10, '10'), (0x10, '10'),
@ -37,8 +39,8 @@ class TestBuffer(object):
assert r(self._b(p[1])) == p[0] assert r(self._b(p[1])) == p[0]
def test_bool(self): def test_bool(self):
w = lambda x: self.wbuf().write_bool(x).write_flush() w = lambda x: self.wbuf().write_bool(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_bool() r = lambda x: self.rbuf(x).read_bool() # noqa
tc = [(True, '01'), tc = [(True, '01'),
(False, '00')] (False, '00')]
for p in tc: for p in tc:
@ -46,8 +48,8 @@ class TestBuffer(object):
assert r(self._b(p[1])) == p[0] assert r(self._b(p[1])) == p[0]
def test_int(self): def test_int(self):
w = lambda x: self.wbuf().write_int(x).write_flush() w = lambda x: self.wbuf().write_int(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_int() r = lambda x: self.rbuf(x).read_int() # noqa
tc = [(0x00, '00 00 00 00'), tc = [(0x00, '00 00 00 00'),
(0x01, '00 00 00 01'), (0x01, '00 00 00 01'),
(0xabcd, '00 00 ab cd'), (0xabcd, '00 00 ab cd'),
@ -57,8 +59,8 @@ class TestBuffer(object):
assert r(self._b(p[1])) == p[0] assert r(self._b(p[1])) == p[0]
def test_string(self): def test_string(self):
w = lambda x: self.wbuf().write_string(x).write_flush() w = lambda x: self.wbuf().write_string(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_string() r = lambda x: self.rbuf(x).read_string() # noqa
tc = [(u'abc1', '00 00 00 04 61 62 63 31'), tc = [(u'abc1', '00 00 00 04 61 62 63 31'),
(b'abc2', '00 00 00 04 61 62 63 32')] (b'abc2', '00 00 00 04 61 62 63 32')]
for p in tc: for p in tc:
@ -69,34 +71,35 @@ class TestBuffer(object):
assert r(self._b(p[1])) == v assert r(self._b(p[1])) == v
def test_list(self): def test_list(self):
w = lambda x: self.wbuf().write_list(x).write_flush() w = lambda x: self.wbuf().write_list(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_list() r = lambda x: self.rbuf(x).read_list() # noqa
tc = [(['d', 'ef', 'ault'], '00 00 00 09 64 2c 65 66 2c 61 75 6c 74')] tc = [(['d', 'ef', 'ault'], '00 00 00 09 64 2c 65 66 2c 61 75 6c 74')]
for p in tc: for p in tc:
assert w(p[0]) == self._b(p[1]) assert w(p[0]) == self._b(p[1])
assert r(self._b(p[1])) == p[0] assert r(self._b(p[1])) == p[0]
def test_list_nonutf8(self): def test_list_nonutf8(self):
r = lambda x: self.rbuf(x).read_list() r = lambda x: self.rbuf(x).read_list() # noqa
src = self._b('00 00 00 04 de ad be ef') src = self._b('00 00 00 04 de ad be ef')
dst = [(b'\xde\xad' + self.utf8rchar + self.utf8rchar).decode('utf-8')] dst = [(b'\xde\xad' + self.utf8rchar + self.utf8rchar).decode('utf-8')]
assert r(src) == dst assert r(src) == dst
def test_line(self): def test_line(self):
w = lambda x: self.wbuf().write_line(x).write_flush() w = lambda x: self.wbuf().write_line(x).write_flush() # noqa
r = lambda x: self.rbuf(x).read_line() r = lambda x: self.rbuf(x).read_line() # noqa
tc = [(u'example line', '65 78 61 6d 70 6c 65 20 6c 69 6e 65 0d 0a')] tc = [(u'example line', '65 78 61 6d 70 6c 65 20 6c 69 6e 65 0d 0a')]
for p in tc: for p in tc:
assert w(p[0]) == self._b(p[1]) assert w(p[0]) == self._b(p[1])
assert r(self._b(p[1])) == p[0] assert r(self._b(p[1])) == p[0]
def test_line_nonutf8(self): def test_line_nonutf8(self):
r = lambda x: self.rbuf(x).read_line() r = lambda x: self.rbuf(x).read_line() # noqa
src = self._b('de ad be af') src = self._b('de ad be af')
dst = (b'\xde\xad' + self.utf8rchar + self.utf8rchar).decode('utf-8') dst = (b'\xde\xad' + self.utf8rchar + self.utf8rchar).decode('utf-8')
assert r(src) == dst assert r(src) == dst
def test_bitlen(self): def test_bitlen(self):
# pylint: disable=protected-access
class Py26Int(int): class Py26Int(int):
def bit_length(self): def bit_length(self):
raise AttributeError raise AttributeError
@ -104,8 +107,8 @@ class TestBuffer(object):
assert self.wbuf._bitlength(Py26Int(42)) == 6 assert self.wbuf._bitlength(Py26Int(42)) == 6
def test_mpint1(self): def test_mpint1(self):
mpint1w = lambda x: self.wbuf().write_mpint1(x).write_flush() mpint1w = lambda x: self.wbuf().write_mpint1(x).write_flush() # noqa
mpint1r = lambda x: self.rbuf(x).read_mpint1() mpint1r = lambda x: self.rbuf(x).read_mpint1() # noqa
tc = [(0x0, '00 00'), tc = [(0x0, '00 00'),
(0x1234, '00 0d 12 34'), (0x1234, '00 0d 12 34'),
(0x12345, '00 11 01 23 45'), (0x12345, '00 11 01 23 45'),
@ -115,8 +118,8 @@ class TestBuffer(object):
assert mpint1r(self._b(p[1])) == p[0] assert mpint1r(self._b(p[1])) == p[0]
def test_mpint2(self): def test_mpint2(self):
mpint2w = lambda x: self.wbuf().write_mpint2(x).write_flush() mpint2w = lambda x: self.wbuf().write_mpint2(x).write_flush() # noqa
mpint2r = lambda x: self.rbuf(x).read_mpint2() mpint2r = lambda x: self.rbuf(x).read_mpint2() # noqa
tc = [(0x0, '00 00 00 00'), tc = [(0x0, '00 00 00 00'),
(0x80, '00 00 00 02 00 80'), (0x80, '00 00 00 02 00 80'),
(0x9a378f9b2e332a7, '00 00 00 08 09 a3 78 f9 b2 e3 32 a7'), (0x9a378f9b2e332a7, '00 00 00 08 09 a3 78 f9 b2 e3 32 a7'),

View File

@ -1,8 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest, socket import socket
import pytest
# pylint: disable=attribute-defined-outside-init
class TestErrors(object): class TestErrors(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):

View File

@ -1,9 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
import pytest, io, sys import pytest
# pylint: disable=attribute-defined-outside-init
class TestOutput(object): class TestOutput(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):
@ -23,7 +24,7 @@ class TestOutput(object):
def test_output_buffer_no_flush(self, output_spy): def test_output_buffer_no_flush(self, output_spy):
output_spy.begin() output_spy.begin()
with self.OutputBuffer() as obuf: with self.OutputBuffer():
print(u'abc') print(u'abc')
assert output_spy.flush() == [] assert output_spy.flush() == []

View File

@ -3,19 +3,21 @@
import pytest import pytest
# pylint: disable=line-too-long,attribute-defined-outside-init
class TestSoftware(object): class TestSoftware(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):
self.ssh = ssh_audit.SSH self.ssh = ssh_audit.SSH
def test_unknown_software(self): def test_unknown_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
assert ps('SSH-1.5') is None assert ps('SSH-1.5') is None
assert ps('SSH-1.99-AlfaMegaServer') is None assert ps('SSH-1.99-AlfaMegaServer') is None
assert ps('SSH-2.0-BetaMegaServer 0.0.1') is None assert ps('SSH-2.0-BetaMegaServer 0.0.1') is None
def test_openssh_software(self): def test_openssh_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # pylint: disable=too-many-statements
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-2.0-OpenSSH_7.3') s = ps('SSH-2.0-OpenSSH_7.3')
assert s.vendor is None assert s.vendor is None
@ -102,7 +104,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(product=OpenSSH, version=5.9, patch=CASPUR)>' assert repr(s) == '<Software(product=OpenSSH, version=5.9, patch=CASPUR)>'
def test_dropbear_software(self): def test_dropbear_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-2.0-dropbear_2016.74') s = ps('SSH-2.0-dropbear_2016.74')
assert s.vendor is None assert s.vendor is None
@ -153,7 +155,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(product=Dropbear SSH, version=2014.66, patch=agbn_1)>' assert repr(s) == '<Software(product=Dropbear SSH, version=2014.66, patch=agbn_1)>'
def test_libssh_software(self): def test_libssh_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-2.0-libssh-0.2') s = ps('SSH-2.0-libssh-0.2')
assert s.vendor is None assert s.vendor is None
@ -179,7 +181,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(product=libssh, version=0.7.3)>' assert repr(s) == '<Software(product=libssh, version=0.7.3)>'
def test_romsshell_software(self): def test_romsshell_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-2.0-RomSShell_5.40') s = ps('SSH-2.0-RomSShell_5.40')
assert s.vendor == 'Allegro Software' assert s.vendor == 'Allegro Software'
@ -194,7 +196,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(vendor=Allegro Software, product=RomSShell, version=5.40)>' assert repr(s) == '<Software(vendor=Allegro Software, product=RomSShell, version=5.40)>'
def test_hp_ilo_software(self): def test_hp_ilo_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-2.0-mpSSH_0.2.1') s = ps('SSH-2.0-mpSSH_0.2.1')
assert s.vendor == 'HP' assert s.vendor == 'HP'
@ -209,7 +211,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(vendor=HP, product=iLO (Integrated Lights-Out) sshd, version=0.2.1)>' assert repr(s) == '<Software(vendor=HP, product=iLO (Integrated Lights-Out) sshd, version=0.2.1)>'
def test_cisco_software(self): def test_cisco_software(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# common # common
s = ps('SSH-1.5-Cisco-1.25') s = ps('SSH-1.5-Cisco-1.25')
assert s.vendor == 'Cisco' assert s.vendor == 'Cisco'
@ -224,7 +226,7 @@ class TestSoftware(object):
assert repr(s) == '<Software(vendor=Cisco, product=IOS/PIX sshd, version=1.25)>' assert repr(s) == '<Software(vendor=Cisco, product=IOS/PIX sshd, version=1.25)>'
def test_sofware_os(self): def test_sofware_os(self):
ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) ps = lambda x: self.ssh.Software.parse(self.ssh.Banner.parse(x)) # noqa
# unknown # unknown
s = ps('SSH-2.0-OpenSSH_3.7.1 MegaOperatingSystem 123') s = ps('SSH-2.0-OpenSSH_3.7.1 MegaOperatingSystem 123')
assert s.os is None assert s.os is None

View File

@ -3,6 +3,7 @@
import pytest import pytest
# pylint: disable=attribute-defined-outside-init
class TestVersionCompare(object): class TestVersionCompare(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, ssh_audit): def init(self, ssh_audit):