~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2007-2011 Canonical Ltd
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
16
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
17
"""HTTPS test server, available when ssl python module is available"""
18
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
19
import ssl
5560.1.1 by Vincent Ladeuil
Catch the bogus ssl exception for closed sockets.
20
import sys
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
21
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
22
from bzrlib.tests import (
23
    http_server,
24
    ssl_certs,
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
25
    test_server,
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
26
    )
27
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
28
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
29
class TestingHTTPSServerMixin:
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
30
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
31
    def __init__(self, key_file, cert_file):
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
32
        self.key_file = key_file
33
        self.cert_file = cert_file
34
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
35
    def _get_ssl_request (self, sock, addr):
36
        """Wrap the socket with SSL"""
37
        ssl_sock = ssl.wrap_socket(sock, server_side=True,
38
                                   keyfile=self.key_file,
39
                                   certfile=self.cert_file,
40
                                   do_handshake_on_connect=False)
41
        return ssl_sock, addr
42
43
    def verify_request(self, request, client_address):
44
        """Verify the request.
45
46
        Return True if we should proceed with this request, False if we should
47
        not even touch a single byte in the socket !
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
48
        """
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
49
        serving = test_server.TestingTCPServerMixin.verify_request(
50
            self, request, client_address)
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
51
        if serving:
52
            request.do_handshake()
53
        return serving
2929.3.27 by Vincent Ladeuil
Fixed as per Ian's review.
54
5560.1.1 by Vincent Ladeuil
Catch the bogus ssl exception for closed sockets.
55
    def ignored_exceptions_during_shutdown(self, e):
56
        if (sys.version < (2, 7) and isinstance(e, TypeError)
57
            and e.args[0] == "'member_descriptor' object is not callable"):
58
            # Fixed in python-2.7 (and some Ubuntu 2.6) there is a bug where
59
            # the ssl socket fail to raise a socket.error when trying to read
60
            # from a closed socket. This is rarely observed in practice but
61
            # still make valid selftest runs fail if not caught.
62
            return True
63
        base = test_server.TestingTCPServerMixin
64
        return base.ignored_exceptions_during_shutdown(self, e)
65
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
66
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
67
class TestingHTTPSServer(TestingHTTPSServerMixin,
68
                         http_server.TestingHTTPServer):
69
70
    def __init__(self, server_address, request_handler_class,
71
                 test_case_server, key_file, cert_file):
72
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
73
        http_server.TestingHTTPServer.__init__(
74
            self, server_address, request_handler_class, test_case_server)
75
5247.6.9 by Vincent Ladeuil
Fix vicious spurious spaces.
76
    def get_request(self):
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
77
        sock, addr = http_server.TestingHTTPServer.get_request(self)
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
78
        return self._get_ssl_request(sock, addr)
79
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
80
81
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
82
                                  http_server.TestingThreadingHTTPServer):
83
84
    def __init__(self, server_address, request_handler_class,
85
                 test_case_server, key_file, cert_file):
86
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
87
        http_server.TestingThreadingHTTPServer.__init__(
88
            self, server_address, request_handler_class, test_case_server)
89
5247.6.9 by Vincent Ladeuil
Fix vicious spurious spaces.
90
    def get_request(self):
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
91
        sock, addr = http_server.TestingThreadingHTTPServer.get_request(self)
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
92
        return self._get_ssl_request(sock, addr)
93
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
94
95
class HTTPSServer(http_server.HttpServer):
96
97
    _url_protocol = 'https'
98
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
99
    # The real servers depending on the protocol
100
    http_server_class = {'HTTP/1.0': TestingHTTPSServer,
101
                         'HTTP/1.1': TestingThreadingHTTPSServer,
102
                         }
103
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
104
    # Provides usable defaults since an https server requires both a
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
105
    # private key and a certificate to work.
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
106
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
3945.1.7 by Vincent Ladeuil
Test against https.
107
                 protocol_version=None,
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
108
                 key_file=ssl_certs.build_path('server_without_pass.key'),
109
                 cert_file=ssl_certs.build_path('server.crt')):
3945.1.7 by Vincent Ladeuil
Test against https.
110
        http_server.HttpServer.__init__(self, request_handler=request_handler,
111
                                        protocol_version=protocol_version)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
112
        self.key_file = key_file
113
        self.cert_file = cert_file
114
        self.temp_files = []
115
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
116
    def create_server(self):
117
        return self.server_class(
118
            (self.host, self.port), self.request_handler_class, self,
119
            self.key_file, self.cert_file)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
120
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
121
122
class HTTPSServer_urllib(HTTPSServer):
123
    """Subclass of HTTPSServer that gives https+urllib urls.
124
125
    This is for use in testing: connections to this server will always go
126
    through urllib where possible.
127
    """
128
129
    # urls returned by this server should require the urllib client impl
130
    _url_protocol = 'https+urllib'
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
131
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
132
133
class HTTPSServer_PyCurl(HTTPSServer):
134
    """Subclass of HTTPSServer that gives http+pycurl urls.
135
136
    This is for use in testing: connections to this server will always go
137
    through pycurl where possible.
138
    """
139
140
    # We don't care about checking the pycurl availability as
141
    # this server will be required only when pycurl is present
142
143
    # urls returned by this server should require the pycurl client impl
144
    _url_protocol = 'https+pycurl'