~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_https_urllib.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011, 2012, 2013, 2016 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Tests for the SSL support in the urllib HTTP transport.
 
18
 
 
19
"""
 
20
 
 
21
import os
 
22
import sys
 
23
 
 
24
from bzrlib import (
 
25
    config,
 
26
    trace,
 
27
)
 
28
from bzrlib.errors import (
 
29
    ConfigOptionValueError,
 
30
)
 
31
from bzrlib import tests
 
32
from bzrlib.transport.http import _urllib2_wrappers
 
33
from bzrlib.transport.http._urllib2_wrappers import ssl
 
34
 
 
35
 
 
36
class CaCertsConfigTests(tests.TestCaseInTempDir):
 
37
 
 
38
    def get_stack(self, content):
 
39
        return config.MemoryStack(content.encode('utf-8'))
 
40
 
 
41
    def test_default_exists(self):
 
42
        """Check that the default we provide exists for the tested platform."""
 
43
        stack = self.get_stack("")
 
44
        self.assertPathExists(stack.get('ssl.ca_certs'))
 
45
 
 
46
    def test_specified(self):
 
47
        self.build_tree(['cacerts.pem'])
 
48
        path = os.path.join(self.test_dir, "cacerts.pem")
 
49
        stack = self.get_stack("ssl.ca_certs = %s\n" % path)
 
50
        self.assertEqual(path, stack.get('ssl.ca_certs'))
 
51
 
 
52
    def test_specified_doesnt_exist(self):
 
53
        stack = self.get_stack('')
 
54
        # Disable the default value mechanism to force the behavior we want
 
55
        self.overrideAttr(_urllib2_wrappers.opt_ssl_ca_certs, 'default',
 
56
                          os.path.join(self.test_dir, u"nonexisting.pem"))
 
57
        self.warnings = []
 
58
 
 
59
        def warning(*args):
 
60
            self.warnings.append(args[0] % args[1:])
 
61
        self.overrideAttr(trace, 'warning', warning)
 
62
        self.assertEqual(None, stack.get('ssl.ca_certs'))
 
63
        self.assertLength(1, self.warnings)
 
64
        self.assertContainsRe(self.warnings[0],
 
65
                              "is not valid for \"ssl.ca_certs\"")
 
66
 
 
67
 
 
68
class CertReqsConfigTests(tests.TestCaseInTempDir):
 
69
 
 
70
    def test_default(self):
 
71
        stack = config.MemoryStack("")
 
72
        self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
 
73
 
 
74
    def test_from_string(self):
 
75
        stack = config.MemoryStack("ssl.cert_reqs = none\n")
 
76
        self.assertEqual(ssl.CERT_NONE, stack.get("ssl.cert_reqs"))
 
77
        stack = config.MemoryStack("ssl.cert_reqs = required\n")
 
78
        self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
 
79
        stack = config.MemoryStack("ssl.cert_reqs = invalid\n")
 
80
        self.assertRaises(ConfigOptionValueError, stack.get, "ssl.cert_reqs")
 
81
 
 
82
 
 
83
class MatchHostnameTests(tests.TestCase):
 
84
 
 
85
    def setUp(self):
 
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')
 
91
 
 
92
    def test_no_certificate(self):
 
93
        self.assertRaises(ValueError,
 
94
                          ssl.match_hostname, {}, "example.com")
 
95
 
 
96
    def test_wildcards_in_cert(self):
 
97
        def ok(cert, hostname):
 
98
            ssl.match_hostname(cert, hostname)
 
99
 
 
100
        def not_ok(cert, hostname):
 
101
            self.assertRaises(
 
102
                ssl.CertificateError,
 
103
                ssl.match_hostname, cert, hostname)
 
104
 
 
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')
 
110
 
 
111
    def test_no_valid_attributes(self):
 
112
        self.assertRaises(ssl.CertificateError, ssl.match_hostname,
 
113
                          {"Problem": "Solved"}, "example.com")
 
114
 
 
115
    def test_common_name(self):
 
116
        cert = {'subject': ((('commonName', 'example.com'),),)}
 
117
        self.assertIs(None,
 
118
                      ssl.match_hostname(cert, "example.com"))
 
119
        self.assertRaises(ssl.CertificateError, ssl.match_hostname,
 
120
                          cert, "example.org")