29
from ..transport.http import ssl, opt_ssl_ca_certs
28
from brzlib.errors import (
29
ConfigOptionValueError,
31
from brzlib import tests
32
from brzlib.transport.http import _urllib2_wrappers
33
from brzlib.transport.http._urllib2_wrappers import ssl
32
36
class CaCertsConfigTests(tests.TestCaseInTempDir):
48
52
def test_specified_doesnt_exist(self):
49
53
stack = self.get_stack('')
50
54
# Disable the default value mechanism to force the behavior we want
51
self.overrideAttr(opt_ssl_ca_certs, 'default',
55
self.overrideAttr(_urllib2_wrappers.opt_ssl_ca_certs, 'default',
52
56
os.path.join(self.test_dir, u"nonexisting.pem"))
64
68
class CertReqsConfigTests(tests.TestCaseInTempDir):
66
70
def test_default(self):
67
stack = config.MemoryStack(b"")
71
stack = config.MemoryStack("")
68
72
self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
70
74
def test_from_string(self):
71
stack = config.MemoryStack(b"ssl.cert_reqs = none\n")
75
stack = config.MemoryStack("ssl.cert_reqs = none\n")
72
76
self.assertEqual(ssl.CERT_NONE, stack.get("ssl.cert_reqs"))
73
stack = config.MemoryStack(b"ssl.cert_reqs = required\n")
77
stack = config.MemoryStack("ssl.cert_reqs = required\n")
74
78
self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
75
stack = config.MemoryStack(b"ssl.cert_reqs = invalid\n")
76
self.assertRaises(config.ConfigOptionValueError, stack.get,
79
stack = config.MemoryStack("ssl.cert_reqs = invalid\n")
80
self.assertRaises(ConfigOptionValueError, stack.get, "ssl.cert_reqs")
80
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')
82
92
def test_no_certificate(self):
83
93
self.assertRaises(ValueError,
84
94
ssl.match_hostname, {}, "example.com")
95
105
# Python Issue #17980: avoid denials of service by refusing more than
96
106
# one wildcard per fragment.
97
if sys.version_info[:2] >= (3, 7):
98
# Python 3.7 dropped support for partial wildcards, see
99
# https://docs.python.org/3/whatsnew/3.7.html#ssl
100
not_ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com')
102
ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com')
107
ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com')
103
108
not_ok({'subject': ((('commonName', 'a*b.co*'),),)}, 'axxb.com')
104
109
not_ok({'subject': ((('commonName', 'a*b*.com'),),)}, 'axxbxxc.com')