~bzr-pqm/bzr/bzr.dev

5247.1.1 by Vincent Ladeuil
Merge previous attempt into current trunk
1
# Copyright (C) 2007-2010 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
20
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
21
from bzrlib.tests import (
22
    http_server,
23
    ssl_certs,
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
24
    test_server,
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
25
    )
26
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
27
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
28
class TestingHTTPSServerMixin:
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
29
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
30
    def __init__(self, key_file, cert_file):
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
31
        self.key_file = key_file
32
        self.cert_file = cert_file
33
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
34
    def _get_ssl_request (self, sock, addr):
35
        """Wrap the socket with SSL"""
36
        ssl_sock = ssl.wrap_socket(sock, server_side=True,
37
                                   keyfile=self.key_file,
38
                                   certfile=self.cert_file,
39
                                   do_handshake_on_connect=False)
40
        return ssl_sock, addr
41
42
    def verify_request(self, request, client_address):
43
        """Verify the request.
44
45
        Return True if we should proceed with this request, False if we should
46
        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
47
        """
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
48
        serving = test_server.TestingTCPServerMixin.verify_request(
49
            self, request, client_address)
4731.2.12 by Vincent Ladeuil
Start adding a more precise synchronization between the various test threads.
50
        if serving:
51
            request.do_handshake()
52
        return serving
2929.3.27 by Vincent Ladeuil
Fixed as per Ian's review.
53
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
54
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
55
class TestingHTTPSServer(TestingHTTPSServerMixin,
56
                         http_server.TestingHTTPServer):
57
58
    def __init__(self, server_address, request_handler_class,
59
                 test_case_server, key_file, cert_file):
60
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
61
        http_server.TestingHTTPServer.__init__(
62
            self, server_address, request_handler_class, test_case_server)
63
5247.6.9 by Vincent Ladeuil
Fix vicious spurious spaces.
64
    def get_request(self):
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
65
        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.
66
        return self._get_ssl_request(sock, addr)
67
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
68
69
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
70
                                  http_server.TestingThreadingHTTPServer):
71
72
    def __init__(self, server_address, request_handler_class,
73
                 test_case_server, key_file, cert_file):
74
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
75
        http_server.TestingThreadingHTTPServer.__init__(
76
            self, server_address, request_handler_class, test_case_server)
77
5247.6.9 by Vincent Ladeuil
Fix vicious spurious spaces.
78
    def get_request(self):
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
79
        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.
80
        return self._get_ssl_request(sock, addr)
81
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
82
83
class HTTPSServer(http_server.HttpServer):
84
85
    _url_protocol = 'https'
86
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
87
    # The real servers depending on the protocol
88
    http_server_class = {'HTTP/1.0': TestingHTTPSServer,
89
                         'HTTP/1.1': TestingThreadingHTTPSServer,
90
                         }
91
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
92
    # 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.
93
    # private key and a certificate to work.
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
94
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
3945.1.7 by Vincent Ladeuil
Test against https.
95
                 protocol_version=None,
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
96
                 key_file=ssl_certs.build_path('server_without_pass.key'),
97
                 cert_file=ssl_certs.build_path('server.crt')):
3945.1.7 by Vincent Ladeuil
Test against https.
98
        http_server.HttpServer.__init__(self, request_handler=request_handler,
99
                                        protocol_version=protocol_version)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
100
        self.key_file = key_file
101
        self.cert_file = cert_file
102
        self.temp_files = []
103
5247.3.16 by Vincent Ladeuil
All http tests passing (including https).
104
    def create_server(self):
105
        return self.server_class(
106
            (self.host, self.port), self.request_handler_class, self,
107
            self.key_file, self.cert_file)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
108
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
109
110
class HTTPSServer_urllib(HTTPSServer):
111
    """Subclass of HTTPSServer that gives https+urllib urls.
112
113
    This is for use in testing: connections to this server will always go
114
    through urllib where possible.
115
    """
116
117
    # urls returned by this server should require the urllib client impl
118
    _url_protocol = 'https+urllib'
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
119
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
120
121
class HTTPSServer_PyCurl(HTTPSServer):
122
    """Subclass of HTTPSServer that gives http+pycurl urls.
123
124
    This is for use in testing: connections to this server will always go
125
    through pycurl where possible.
126
    """
127
128
    # We don't care about checking the pycurl availability as
129
    # this server will be required only when pycurl is present
130
131
    # urls returned by this server should require the pycurl client impl
132
    _url_protocol = 'https+pycurl'