24
24
from bzrlib import (
28
28
from bzrlib.errors import (
30
29
ConfigOptionValueError,
32
from bzrlib.tests import (
31
from bzrlib import tests
36
32
from bzrlib.transport.http import _urllib2_wrappers
39
class CaCertsConfigTests(TestCaseInTempDir):
33
from bzrlib.transport.http._urllib2_wrappers import ssl
36
class CaCertsConfigTests(tests.TestCaseInTempDir):
41
38
def get_stack(self, content):
42
39
return config.MemoryStack(content.encode('utf-8'))
82
80
self.assertRaises(ConfigOptionValueError, stack.get, "ssl.cert_reqs")
85
class MatchHostnameTests(TestCase):
83
class MatchHostnameTests(tests.TestCase):
86
super(MatchHostnameTests, self).setUp()
87
if sys.version_info < (2, 7, 9):
88
raise tests.TestSkipped(
89
'python version too old to provide proper'
90
' https hostname verification')
87
92
def test_no_certificate(self):
88
93
self.assertRaises(ValueError,
89
_urllib2_wrappers.match_hostname, {}, "example.com")
94
ssl.match_hostname, {}, "example.com")
91
96
def test_wildcards_in_cert(self):
92
97
def ok(cert, hostname):
93
_urllib2_wrappers.match_hostname(cert, hostname)
98
ssl.match_hostname(cert, hostname)
100
def not_ok(cert, hostname):
102
ssl.CertificateError,
103
ssl.match_hostname, cert, hostname)
95
105
# Python Issue #17980: avoid denials of service by refusing more than
96
106
# one wildcard per fragment.
97
cert = {'subject': ((('commonName', 'a*b.com'),),)}
99
cert = {'subject': ((('commonName', 'a*b.co*'),),)}
101
cert = {'subject': ((('commonName', 'a*b*.com'),),)}
103
_urllib2_wrappers.match_hostname(cert, 'axxbxxc.com')
104
except ValueError as e:
105
self.assertIn("too many wildcards", str(e))
107
ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com')
108
not_ok({'subject': ((('commonName', 'a*b.co*'),),)}, 'axxb.com')
109
not_ok({'subject': ((('commonName', 'a*b*.com'),),)}, 'axxbxxc.com')
107
111
def test_no_valid_attributes(self):
108
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
112
self.assertRaises(ssl.CertificateError, ssl.match_hostname,
109
113
{"Problem": "Solved"}, "example.com")
111
115
def test_common_name(self):
112
116
cert = {'subject': ((('commonName', 'example.com'),),)}
113
117
self.assertIs(None,
114
_urllib2_wrappers.match_hostname(cert, "example.com"))
115
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
118
ssl.match_hostname(cert, "example.com"))
119
self.assertRaises(ssl.CertificateError, ssl.match_hostname,
116
120
cert, "example.org")