Skip to content

Commit c5a92e0

Browse files
committed
Tests for match_hostname backport
Signed-off-by: Joffrey F <[email protected]>
1 parent 0a5815b commit c5a92e0

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

tests/unit/ssladapter_test.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from docker.ssladapter import ssladapter
2+
from docker.ssladapter.ssl_match_hostname import (
3+
match_hostname, CertificateError
4+
)
5+
6+
try:
7+
from ssl import OP_NO_SSLv3, OP_NO_SSLv2, OP_NO_TLSv1
8+
except ImportError:
9+
OP_NO_SSLv2 = 0x1000000
10+
OP_NO_SSLv3 = 0x2000000
11+
OP_NO_TLSv1 = 0x4000000
12+
13+
from .. import base
14+
15+
16+
class SSLAdapterTest(base.BaseTestCase):
17+
def test_only_uses_tls(self):
18+
ssl_context = ssladapter.urllib3.util.ssl_.create_urllib3_context()
19+
20+
assert ssl_context.options & OP_NO_SSLv3
21+
assert ssl_context.options & OP_NO_SSLv2
22+
assert not ssl_context.options & OP_NO_TLSv1
23+
24+
25+
class MatchHostnameTest(base.BaseTestCase):
26+
cert = {
27+
'issuer': (
28+
(('countryName', u'US'),),
29+
(('stateOrProvinceName', u'California'),),
30+
(('localityName', u'San Francisco'),),
31+
(('organizationName', u'Docker Inc'),),
32+
(('organizationalUnitName', u'Docker-Python'),),
33+
(('commonName', u'localhost'),),
34+
(('emailAddress', u'[email protected]'),)
35+
),
36+
'notAfter': 'Mar 25 23:08:23 2030 GMT',
37+
'notBefore': u'Mar 25 23:08:23 2016 GMT',
38+
'serialNumber': u'BD5F894C839C548F',
39+
'subject': (
40+
(('countryName', u'US'),),
41+
(('stateOrProvinceName', u'California'),),
42+
(('localityName', u'San Francisco'),),
43+
(('organizationName', u'Docker Inc'),),
44+
(('organizationalUnitName', u'Docker-Python'),),
45+
(('commonName', u'localhost'),),
46+
(('emailAddress', u'[email protected]'),)
47+
),
48+
'subjectAltName': (
49+
('DNS', u'localhost'),
50+
('DNS', u'*.gensokyo.jp'),
51+
('IP Address', u'127.0.0.1'),
52+
),
53+
'version': 3
54+
}
55+
56+
def test_match_ip_address_success(self):
57+
assert match_hostname(self.cert, '127.0.0.1') is None
58+
59+
def test_match_localhost_success(self):
60+
assert match_hostname(self.cert, 'localhost') is None
61+
62+
def test_match_dns_success(self):
63+
assert match_hostname(self.cert, 'touhou.gensokyo.jp') is None
64+
65+
def test_match_ip_address_failure(self):
66+
self.assertRaises(
67+
CertificateError, match_hostname, self.cert, '192.168.0.25'
68+
)
69+
70+
def test_match_dns_failure(self):
71+
self.assertRaises(
72+
CertificateError, match_hostname, self.cert, 'foobar.co.uk'
73+
)

tests/unit/utils_test.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@
1212
import pytest
1313
import six
1414

15-
try:
16-
from ssl import OP_NO_SSLv3, OP_NO_SSLv2, OP_NO_TLSv1
17-
except ImportError:
18-
OP_NO_SSLv2 = 0x1000000
19-
OP_NO_SSLv3 = 0x2000000
20-
OP_NO_TLSv1 = 0x4000000
21-
2215
from docker.client import Client
2316
from docker.constants import DEFAULT_DOCKER_API_VERSION
2417
from docker.errors import DockerException, InvalidVersion
25-
from docker.ssladapter import ssladapter
2618
from docker.utils import (
2719
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
2820
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
@@ -962,12 +954,3 @@ def test_tar_with_directory_symlinks(self):
962954
self.assertEqual(
963955
sorted(tar_data.getnames()), ['bar', 'bar/foo', 'foo']
964956
)
965-
966-
967-
class SSLAdapterTest(base.BaseTestCase):
968-
def test_only_uses_tls(self):
969-
ssl_context = ssladapter.urllib3.util.ssl_.create_urllib3_context()
970-
971-
assert ssl_context.options & OP_NO_SSLv3
972-
assert ssl_context.options & OP_NO_SSLv2
973-
assert not ssl_context.options & OP_NO_TLSv1

0 commit comments

Comments
 (0)