1
# Copyright (C) 2011,2012 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the SSL support in the urllib HTTP transport.
28
from bzrlib.errors import (
30
ConfigOptionValueError,
32
from bzrlib.tests import (
36
from bzrlib.transport.http import _urllib2_wrappers
39
class CaCertsConfigTests(TestCaseInTempDir):
41
def get_stack(self, content):
42
return config.MemoryStack(content.encode('utf-8'))
44
def test_default_exists(self):
45
"""Check that the default we provide exists for the tested platform."""
46
stack = self.get_stack("")
47
self.assertPathExists(stack.get('ssl.ca_certs'))
49
def test_specified(self):
50
self.build_tree(['cacerts.pem'])
51
path = os.path.join(self.test_dir, "cacerts.pem")
52
stack = self.get_stack("ssl.ca_certs = %s\n" % path)
53
self.assertEquals(path, stack.get('ssl.ca_certs'))
55
def test_specified_doesnt_exist(self):
56
stack = self.get_stack('')
57
# Disable the default value mechanism to force the behavior we want
58
self.overrideAttr(_urllib2_wrappers.opt_ssl_ca_certs, 'default',
59
os.path.join(self.test_dir, u"nonexisting.pem"))
62
self.warnings.append(args[0] % args[1:])
63
self.overrideAttr(trace, 'warning', warning)
64
self.assertEquals(None, stack.get('ssl.ca_certs'))
65
self.assertLength(1, self.warnings)
66
self.assertContainsRe(self.warnings[0],
67
"is not valid for \"ssl.ca_certs\"")
70
class CertReqsConfigTests(TestCaseInTempDir):
72
def test_default(self):
73
stack = config.MemoryStack("")
74
self.assertEquals(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
76
def test_from_string(self):
77
stack = config.MemoryStack("ssl.cert_reqs = none\n")
78
self.assertEquals(ssl.CERT_NONE, stack.get("ssl.cert_reqs"))
79
stack = config.MemoryStack("ssl.cert_reqs = required\n")
80
self.assertEquals(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
81
stack = config.MemoryStack("ssl.cert_reqs = invalid\n")
82
self.assertRaises(ConfigOptionValueError, stack.get, "ssl.cert_reqs")
85
class MatchHostnameTests(TestCase):
87
def test_no_certificate(self):
88
self.assertRaises(ValueError,
89
_urllib2_wrappers.match_hostname, {}, "example.com")
91
def test_no_valid_attributes(self):
92
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
93
{"Problem": "Solved"}, "example.com")
95
def test_common_name(self):
96
cert = {'subject': ((('commonName', 'example.com'),),)}
98
_urllib2_wrappers.match_hostname(cert, "example.com"))
99
self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,