summaryrefslogtreecommitdiffstats
path: root/test/test_version_compare.py
blob: f6f0441573a0912f3720947b2a016f46650c16a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import pytest

from ssh_audit.banner import Banner
from ssh_audit.software import Software


# pylint: disable=attribute-defined-outside-init
class TestVersionCompare:
    @pytest.fixture(autouse=True)
    def init(self, ssh_audit):
        self.software = Software
        self.banner = Banner

    def get_dropbear_software(self, v):
        b = self.banner.parse('SSH-2.0-dropbear_{}'.format(v))
        return self.software.parse(b)

    def get_openssh_software(self, v):
        b = self.banner.parse('SSH-2.0-OpenSSH_{}'.format(v))
        return self.software.parse(b)

    def get_libssh_software(self, v):
        b = self.banner.parse('SSH-2.0-libssh-{}'.format(v))
        return self.software.parse(b)

    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(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.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 = []
        for i in range(28, 44):
            versions.append('0.{}'.format(i))
        for i in range(1, 5):
            versions.append('0.44test{}'.format(i))
        for i in range(44, 49):
            versions.append('0.{}'.format(i))
        versions.append('0.48.1')
        for i in range(49, 54):
            versions.append('0.{}'.format(i))
        versions.append('0.53.1')
        for v in ['2011.54', '2012.55']:
            versions.append(v)
        for i in range(56, 61):
            versions.append('2013.{}'.format(i))
        for v in ['2013.61test', '2013.62']:
            versions.append(v)
        for i in range(63, 67):
            versions.append('2014.{}'.format(i))
        for i in range(67, 72):
            versions.append('2015.{}'.format(i))
        for i in range(72, 75):
            versions.append('2016.{}'.format(i))
        length = len(versions)
        for i in range(length):
            v = versions[i]
            s = self.get_dropbear_software(v)
            assert s.compare_version(v) == 0
            if i - 1 >= 0:
                vbefore = versions[i - 1]
                assert s.compare_version(vbefore) > 0
            if i + 1 < length:
                vnext = versions[i + 1]
                assert s.compare_version(vnext) < 0

    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.1') == 0
        assert s2.compare_version('2.1.1p2') == 0
        assert s1.compare_version('2.1.1p1') == 0  # OpenBSD version and p1 version should be the same.
        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 = []
        for v in ['1.2.3', '2.1.0', '2.1.1', '2.2.0', '2.3.0']:
            versions.append(v)
        for v in ['2.5.0', '2.5.1', '2.5.2', '2.9', '2.9.9']:
            versions.append(v)
        for v in ['3.0', '3.0.1', '3.0.2', '3.1', '3.2.2', '3.2.3']:
            versions.append(v)
        for i in range(3, 7):
            versions.append('3.{}'.format(i))
        for v in ['3.6.1', '3.7.0', '3.7.1']:
            versions.append(v)
        for i in range(8, 10):
            versions.append('3.{}'.format(i))
        for i in range(0, 10):
            versions.append('4.{}'.format(i))
        for i in range(0, 10):
            versions.append('5.{}'.format(i))
        for i in range(0, 10):
            versions.append('6.{}'.format(i))
        for i in range(0, 4):
            versions.append('7.{}'.format(i))
        length = len(versions)
        for i in range(length):
            v = versions[i]
            s = self.get_openssh_software(v)
            assert s.compare_version(v) == 0
            if i - 1 >= 0:
                vbefore = versions[i - 1]
                assert s.compare_version(vbefore) > 0
            if i + 1 < length:
                vnext = versions[i + 1]
                assert s.compare_version(vnext) < 0

    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 = []
        for v in ['0.2', '0.3']:
            versions.append(v)
        for i in range(1, 5):
            versions.append('0.3.{}'.format(i))
        for i in range(0, 9):
            versions.append('0.4.{}'.format(i))
        for i in range(0, 6):
            versions.append('0.5.{}'.format(i))
        for i in range(0, 6):
            versions.append('0.6.{}'.format(i))
        for i in range(0, 5):
            versions.append('0.7.{}'.format(i))
        length = len(versions)
        for i in range(length):
            v = versions[i]
            s = self.get_libssh_software(v)
            assert s.compare_version(v) == 0
            if i - 1 >= 0:
                vbefore = versions[i - 1]
                assert s.compare_version(vbefore) > 0
            if i + 1 < length:
                vnext = versions[i + 1]
                assert s.compare_version(vnext) < 0