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'))
58
55
self.overrideAttr(_urllib2_wrappers.opt_ssl_ca_certs, 'default',
59
56
os.path.join(self.test_dir, u"nonexisting.pem"))
61
59
def warning(*args):
62
60
self.warnings.append(args[0] % args[1:])
63
61
self.overrideAttr(trace, 'warning', warning)
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")
96
def test_wildcards_in_cert(self):
97
def ok(cert, hostname):
98
ssl.match_hostname(cert, hostname)
100
def not_ok(cert, hostname):
102
ssl.CertificateError,
103
ssl.match_hostname, cert, hostname)
105
# Python Issue #17980: avoid denials of service by refusing more than
106
# one wildcard per fragment.
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')
91
111
def test_no_valid_attributes(self):
92
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
112
self.assertRaises(ssl.CertificateError, ssl.match_hostname,
93
113
{"Problem": "Solved"}, "example.com")
95
115
def test_common_name(self):
96
116
cert = {'subject': ((('commonName', 'example.com'),),)}
97
117
self.assertIs(None,
98
_urllib2_wrappers.match_hostname(cert, "example.com"))
99
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
118
ssl.match_hostname(cert, "example.com"))
119
self.assertRaises(ssl.CertificateError, ssl.match_hostname,
100
120
cert, "example.org")