|
| 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 | + ) |
0 commit comments