~bzr-pqm/bzr/bzr.dev

6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
1
# Copyright (C) 2011 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
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
21
import os
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
22
import ssl
23
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
24
from bzrlib import (
25
    config,
26
    trace,
27
    )
6238.2.11 by Jelmer Vernooij
add basic tests for match_hostname.
28
from bzrlib.errors import (
29
    CertificateError,
30
    ConfigOptionValueError,
31
    )
32
from bzrlib.tests import (
33
    TestCase,
34
    TestCaseInTempDir,
35
    )
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
36
from bzrlib.transport.http import _urllib2_wrappers
37
38
39
class CaCertsConfigTests(TestCaseInTempDir):
40
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
41
    def get_stack(self, content):
42
        return config.MemoryStack(content.encode('utf-8'))
43
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
44
    def test_default_raises_value_error(self):
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
45
        stack = self.get_stack("")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
46
        self.overrideAttr(_urllib2_wrappers, "DEFAULT_CA_PATH",
47
                "/i-do-not-exist")
48
        self.assertRaises(ValueError, stack.get, 'ssl.ca_certs')
49
50
    def test_default_exists(self):
51
        self.build_tree(['cacerts.pem'])
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
52
        stack = self.get_stack("")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
53
        path = os.path.join(self.test_dir, "cacerts.pem")
54
        self.overrideAttr(_urllib2_wrappers, "DEFAULT_CA_PATH", path)
55
        self.assertEquals(path, stack.get('ssl.ca_certs'))
56
57
    def test_specified(self):
58
        self.build_tree(['cacerts.pem'])
59
        path = os.path.join(self.test_dir, "cacerts.pem")
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
60
        stack = self.get_stack("ssl.ca_certs = %s\n" % path)
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
61
        self.assertEquals(path, stack.get('ssl.ca_certs'))
62
63
    def test_specified_doesnt_exist(self):
64
        path = os.path.join(self.test_dir, "nonexisting.pem")
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
65
        stack = self.get_stack("ssl.ca_certs = %s\n" % path)
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
66
        self.warnings = []
67
        def warning(*args):
68
            self.warnings.append(args[0] % args[1:])
69
        self.overrideAttr(trace, 'warning', warning)
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
70
        self.assertEquals(_urllib2_wrappers.DEFAULT_CA_PATH,
71
                          stack.get('ssl.ca_certs'))
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
72
        self.assertLength(1, self.warnings)
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
73
        self.assertContainsRe(self.warnings[0],
74
                              "is not valid for \"ssl.ca_certs\"")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
75
76
77
class CertReqsConfigTests(TestCaseInTempDir):
78
79
    def test_default(self):
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
80
        stack = config.MemoryStack("")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
81
        self.assertEquals(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
82
83
    def test_from_string(self):
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
84
        stack = config.MemoryStack("ssl.cert_reqs = none\n")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
85
        self.assertEquals(ssl.CERT_NONE, stack.get("ssl.cert_reqs"))
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
86
        stack = config.MemoryStack("ssl.cert_reqs = optional\n")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
87
        self.assertEquals(ssl.CERT_OPTIONAL, stack.get("ssl.cert_reqs"))
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
88
        stack = config.MemoryStack("ssl.cert_reqs = required\n")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
89
        self.assertEquals(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs"))
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
90
        stack = config.MemoryStack("ssl.cert_reqs = invalid\n")
6238.2.10 by Jelmer Vernooij
Add more tests for ssl.ca_certs option.
91
        self.assertRaises(ConfigOptionValueError, stack.get, "ssl.cert_reqs")
6238.2.11 by Jelmer Vernooij
add basic tests for match_hostname.
92
93
94
class MatchHostnameTests(TestCase):
95
96
    def test_no_certificate(self):
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
97
        self.assertRaises(ValueError,
98
                          _urllib2_wrappers.match_hostname, {}, "example.com")
6238.2.11 by Jelmer Vernooij
add basic tests for match_hostname.
99
100
    def test_no_valid_attributes(self):
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
101
        self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
102
                          {"Problem": "Solved"}, "example.com")
6238.2.11 by Jelmer Vernooij
add basic tests for match_hostname.
103
104
    def test_common_name(self):
105
        cert = {'subject': ((('commonName', 'example.com'),),)}
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
106
        self.assertIs(None,
107
                      _urllib2_wrappers.match_hostname(cert, "example.com"))
6238.2.11 by Jelmer Vernooij
add basic tests for match_hostname.
108
        self.assertRaises(CertificateError, _urllib2_wrappers.match_hostname,
6238.2.19 by Vincent Ladeuil
Just hack the tests until they pass.
109
                          cert, "example.org")