~bzr-pqm/bzr/bzr.dev

2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
1
# Copyright (C) 2007 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
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,
24
    )
25
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
26
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
27
class TestingHTTPSServerMixin:
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
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
    def __init__(self, key_file, cert_file):
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
30
        self.key_file = key_file
31
        self.cert_file = cert_file
32
33
    def get_request (self):
34
        """Get the request and client address from the socket.
35
36
        This is called in response to a connection issued to the server, we
37
        wrap the socket with SSL.
38
        """
39
        sock, addr = self.socket.accept()
40
        sslconn = ssl.wrap_socket(sock, server_side=True,
41
                                  keyfile=self.key_file,
42
                                  certfile=self.cert_file)
43
        return sslconn, addr
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
44
2929.3.27 by Vincent Ladeuil
Fixed as per Ian's review.
45
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
46
class TestingHTTPSServer(TestingHTTPSServerMixin,
47
                         http_server.TestingHTTPServer):
48
49
    def __init__(self, server_address, request_handler_class,
50
                 test_case_server, key_file, cert_file):
51
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
52
        http_server.TestingHTTPServer.__init__(
53
            self, server_address, request_handler_class, test_case_server)
54
55
56
class TestingThreadingHTTPSServer(TestingHTTPSServerMixin,
57
                                  http_server.TestingThreadingHTTPServer):
58
59
    def __init__(self, server_address, request_handler_class,
60
                 test_case_server, key_file, cert_file):
61
        TestingHTTPSServerMixin.__init__(self, key_file, cert_file)
62
        http_server.TestingThreadingHTTPServer.__init__(
63
            self, server_address, request_handler_class, test_case_server)
64
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
65
66
class HTTPSServer(http_server.HttpServer):
67
68
    _url_protocol = 'https'
69
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
70
    # The real servers depending on the protocol
71
    http_server_class = {'HTTP/1.0': TestingHTTPSServer,
72
                         'HTTP/1.1': TestingThreadingHTTPSServer,
73
                         }
74
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
75
    # Provides usable defaults since an https server requires both a
76
    # private key and certificate to work.
77
    def __init__(self, request_handler=http_server.TestingHTTPRequestHandler,
3945.1.7 by Vincent Ladeuil
Test against https.
78
                 protocol_version=None,
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
79
                 key_file=ssl_certs.build_path('server_without_pass.key'),
80
                 cert_file=ssl_certs.build_path('server.crt')):
3945.1.7 by Vincent Ladeuil
Test against https.
81
        http_server.HttpServer.__init__(self, request_handler=request_handler,
82
                                        protocol_version=protocol_version)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
83
        self.key_file = key_file
84
        self.cert_file = cert_file
85
        self.temp_files = []
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
    def create_httpd(self, serv_cls, rhandler_cls):
88
        return serv_cls((self.host, self.port), self.request_handler,
89
                        self, self.key_file, self.cert_file)
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
90
2929.3.10 by Vincent Ladeuil
Add a fake https server and test facilities.
91
92
class HTTPSServer_urllib(HTTPSServer):
93
    """Subclass of HTTPSServer that gives https+urllib urls.
94
95
    This is for use in testing: connections to this server will always go
96
    through urllib where possible.
97
    """
98
99
    # urls returned by this server should require the urllib client impl
100
    _url_protocol = 'https+urllib'
2929.3.12 by Vincent Ladeuil
Implement an https server passing the same tests than http. Except
101
2929.3.19 by Vincent Ladeuil
Fix 1.1 related bugs in HTTP server, add HTTPS passing tests (by temporarily disabling pycurl certificate verification).
102
103
class HTTPSServer_PyCurl(HTTPSServer):
104
    """Subclass of HTTPSServer that gives http+pycurl urls.
105
106
    This is for use in testing: connections to this server will always go
107
    through pycurl where possible.
108
    """
109
110
    # We don't care about checking the pycurl availability as
111
    # this server will be required only when pycurl is present
112
113
    # urls returned by this server should require the pycurl client impl
114
    _url_protocol = 'https+pycurl'