From 262c65b7bebb269d91a4e0936f80270434d4b502 Mon Sep 17 00:00:00 2001 From: Andris Raugulis Date: Wed, 5 Oct 2016 04:09:50 +0300 Subject: [PATCH] Fix version comparison and update tests. --- ssh-audit.py | 2 +- test/test_version_compare.py | 49 ++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ssh-audit.py b/ssh-audit.py index b5f913f..fbfeb62 100755 --- a/ssh-audit.py +++ b/ssh-audit.py @@ -506,7 +506,7 @@ class SSH(object): if other is None: return 1 if isinstance(other, self.__class__): - other = '{0}{1}'.format(other.version, other.patch) + other = '{0}{1}'.format(other.version, other.patch or '') else: other = str(other) mx = re.match(r'^([\d\.]+\d+)(.*)$', other) diff --git a/test/test_version_compare.py b/test/test_version_compare.py index a5a9790..2f74310 100644 --- a/test/test_version_compare.py +++ b/test/test_version_compare.py @@ -22,32 +22,63 @@ class TestVersionCompare(object): def test_dropbear_compare_version_pre_years(self): s = self.get_dropbear_software('0.44') + assert s.compare_version(None) == 1 + assert s.compare_version('') == 1 assert s.compare_version('0.43') > 0 assert s.compare_version('0.44') == 0 + assert s.compare_version(s) == 0 assert s.compare_version('0.45') < 0 assert s.between_versions('0.43', '0.45') + assert s.between_versions('0.43', '0.43') is False + assert s.between_versions('0.45', '0.43') is False def test_dropbear_compare_version_with_years(self): s = self.get_dropbear_software('2015.71') - assert s.compare_version('2014.67') > 0 + assert s.compare_version(None) == 1 + assert s.compare_version('') == 1 + assert s.compare_version('2014.66') > 0 assert s.compare_version('2015.71') == 0 + assert s.compare_version(s) == 0 assert s.compare_version('2016.74') < 0 - assert s.between_versions('2014.67', '2016.74') + assert s.between_versions('2014.66', '2016.74') + assert s.between_versions('2014.66', '2015.69') is False + assert s.between_versions('2016.74', '2014.66') is False def test_dropbear_compare_version_mixed(self): s = self.get_dropbear_software('0.53.1') + assert s.compare_version(None) == 1 + assert s.compare_version('') == 1 assert s.compare_version('0.53') > 0 assert s.compare_version('0.53.1') == 0 + assert s.compare_version(s) == 0 assert s.compare_version('2011.54') < 0 assert s.between_versions('0.53', '2011.54') + assert s.between_versions('0.53', '0.53') is False + assert s.between_versions('2011.54', '0.53') is False def test_dropbear_compare_version_patchlevel(self): s1 = self.get_dropbear_software('0.44') s2 = self.get_dropbear_software('0.44test3') + assert s1.compare_version(None) == 1 + assert s1.compare_version('') == 1 + assert s1.compare_version('0.44') == 0 + assert s1.compare_version(s1) == 0 assert s1.compare_version('0.43') > 0 assert s1.compare_version('0.44test4') > 0 + assert s1.between_versions('0.44test4', '0.45') + assert s1.between_versions('0.43', '0.44test4') is False + assert s1.between_versions('0.45', '0.44test4') is False + assert s2.compare_version(None) == 1 + assert s2.compare_version('') == 1 + assert s2.compare_version('0.44test3') == 0 + assert s2.compare_version(s2) == 0 assert s2.compare_version('0.44') < 0 assert s2.compare_version('0.44test4') < 0 + assert s2.between_versions('0.43', '0.44') + assert s2.between_versions('0.43', '0.44test2') is False + assert s2.between_versions('0.44', '0.43') is False + assert s1.compare_version(s2) > 0 + assert s2.compare_version(s1) < 0 def test_dropbear_compare_version_sequential(self): versions = [] @@ -87,19 +118,28 @@ class TestVersionCompare(object): def test_openssh_compare_version_simple(self): s = self.get_openssh_software('3.7.1') + assert s.compare_version(None) == 1 + assert s.compare_version('') == 1 assert s.compare_version('3.7') > 0 assert s.compare_version('3.7.1') == 0 + assert s.compare_version(s) == 0 assert s.compare_version('3.8') < 0 assert s.between_versions('3.7', '3.8') + assert s.between_versions('3.6', '3.7') is False + assert s.between_versions('3.8', '3.7') is False def test_openssh_compare_version_patchlevel(self): s1 = self.get_openssh_software('2.1.1') s2 = self.get_openssh_software('2.1.1p2') + assert s1.compare_version(s1) == 0 + assert s2.compare_version(s2) == 0 assert s1.compare_version('2.1.1p1') == 0 assert s1.compare_version('2.1.1p2') == 0 assert s2.compare_version('2.1.1') == 0 assert s2.compare_version('2.1.1p1') > 0 assert s2.compare_version('2.1.1p3') < 0 + assert s1.compare_version(s2) == 0 + assert s2.compare_version(s1) == 0 def test_openbsd_compare_version_sequential(self): versions = [] @@ -137,10 +177,15 @@ class TestVersionCompare(object): def test_libssh_compare_version_simple(self): s = self.get_libssh_software('0.3') + assert s.compare_version(None) == 1 + assert s.compare_version('') == 1 assert s.compare_version('0.2') > 0 assert s.compare_version('0.3') == 0 + assert s.compare_version(s) == 0 assert s.compare_version('0.3.1') < 0 assert s.between_versions('0.2', '0.3.1') + assert s.between_versions('0.1', '0.2') is False + assert s.between_versions('0.3.1', '0.2') is False def test_libssh_compare_version_sequential(self): versions = []