~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Copyright (C) 2010, 2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import errno
import socket
import SocketServer
import threading


from bzrlib import (
    osutils,
    tests,
    )
from bzrlib.tests import test_server
from bzrlib.tests.scenarios import load_tests_apply_scenarios


load_tests = load_tests_apply_scenarios


def portable_socket_pair():
    """Return a pair of TCP sockets connected to each other.

    Unlike socket.socketpair, this should work on Windows.
    """
    listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listen_sock.bind(('127.0.0.1', 0))
    listen_sock.listen(1)
    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_sock.connect(listen_sock.getsockname())
    server_sock, addr = listen_sock.accept()
    listen_sock.close()
    return server_sock, client_sock


class TCPClient(object):

    def __init__(self):
        self.sock = None

    def connect(self, addr):
        if self.sock is not None:
            raise AssertionError('Already connected to %r'
                                 % (self.sock.getsockname(),))
        self.sock = osutils.connect_socket(addr)

    def disconnect(self):
        if self.sock is not None:
            try:
                self.sock.shutdown(socket.SHUT_RDWR)
                self.sock.close()
            except socket.error, e:
                if e[0] in (errno.EBADF, errno.ENOTCONN):
                    # Right, the socket is already down
                    pass
                else:
                    raise
            self.sock = None

    def write(self, s):
        return self.sock.sendall(s)

    def read(self, bufsize=4096):
        return self.sock.recv(bufsize)


class TCPConnectionHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        self.done = False
        self.handle_connection()
        while not self.done:
            self.handle_connection()

    def readline(self):
        # TODO: We should be buffering any extra data sent, etc. However, in
        #       practice, we don't send extra content, so we haven't bothered
        #       to implement it yet.
        req = self.request.recv(4096)
        # An empty string is allowed, to indicate the end of the connection
        if not req or (req.endswith('\n') and req.count('\n') == 1):
            return req
        raise ValueError('[%r] not a simple line' % (req,))

    def handle_connection(self):
        req = self.readline()
        if not req:
            self.done = True
        elif req == 'ping\n':
            self.request.sendall('pong\n')
        else:
            raise ValueError('[%s] not understood' % req)


class TestTCPServerInAThread(tests.TestCase):

    scenarios = [
        (name, {'server_class': getattr(test_server, name)})
        for name in
        ('TestingTCPServer', 'TestingThreadingTCPServer')]

    # Set by load_tests()
    server_class = None

    def get_server(self, server_class=None, connection_handler_class=None):
        if server_class is not None:
            self.server_class = server_class
        if connection_handler_class is None:
            connection_handler_class = TCPConnectionHandler
        server = test_server.TestingTCPServerInAThread(
            ('localhost', 0), self.server_class, connection_handler_class)
        server.start_server()
        self.addCleanup(server.stop_server)
        return server

    def get_client(self):
        client = TCPClient()
        self.addCleanup(client.disconnect)
        return client

    def get_server_connection(self, server, conn_rank):
        return server.server.clients[conn_rank]

    def assertClientAddr(self, client, server, conn_rank):
        conn = self.get_server_connection(server, conn_rank)
        self.assertEquals(client.sock.getsockname(), conn[1])

    def test_start_stop(self):
        server = self.get_server()
        client = self.get_client()
        server.stop_server()
        # since the server doesn't accept connections anymore attempting to
        # connect should fail
        client = self.get_client()
        self.assertRaises(socket.error,
                          client.connect, (server.host, server.port))

    def test_client_talks_server_respond(self):
        server = self.get_server()
        client = self.get_client()
        client.connect((server.host, server.port))
        self.assertIs(None, client.write('ping\n'))
        resp = client.read()
        self.assertClientAddr(client, server, 0)
        self.assertEquals('pong\n', resp)

    def test_server_fails_to_start(self):
        class CantStart(Exception):
            pass

        class CantStartServer(test_server.TestingTCPServer):

            def server_bind(self):
                raise CantStart()

        # The exception is raised in the main thread
        self.assertRaises(CantStart,
                          self.get_server, server_class=CantStartServer)

    def test_server_fails_while_serving_or_stopping(self):
        class CantConnect(Exception):
            pass

        class FailingConnectionHandler(TCPConnectionHandler):

            def handle(self):
                raise CantConnect()

        server = self.get_server(
            connection_handler_class=FailingConnectionHandler)
        # The server won't fail until a client connect
        client = self.get_client()
        client.connect((server.host, server.port))
        # We make sure the server wants to handle a request, but the request is
        # guaranteed to fail. However, the server should make sure that the
        # connection gets closed, and stop_server should then raise the
        # original exception.
        client.write('ping\n')
        try:
            self.assertEqual('', client.read())
        except socket.error, e:
            # On Windows, failing during 'handle' means we get
            # 'forced-close-of-connection'. Possibly because we haven't
            # processed the write request before we close the socket.
            WSAECONNRESET = 10054
            if e.errno in (WSAECONNRESET,):
                pass
        # Now the server has raised the exception in its own thread
        self.assertRaises(CantConnect, server.stop_server)

    def test_server_crash_while_responding(self):
        sync = threading.Event()
        sync.clear()
        class FailToRespond(Exception):
            pass

        class FailingDuringResponseHandler(TCPConnectionHandler):

            def handle_connection(self):
                req = self.readline()
                threading.currentThread().set_sync_event(sync)
                raise FailToRespond()

        server = self.get_server(
            connection_handler_class=FailingDuringResponseHandler)
        client = self.get_client()
        client.connect((server.host, server.port))
        client.write('ping\n')
        sync.wait()
        self.assertEqual('', client.read()) # connection closed
        self.assertRaises(FailToRespond, server.pending_exception)

    def test_exception_swallowed_while_serving(self):
        sync = threading.Event()
        sync.clear()
        class CantServe(Exception):
            pass

        class FailingWhileServingConnectionHandler(TCPConnectionHandler):

            def handle(self):
                # We want to sync with the thread that is serving the
                # connection.
                threading.currentThread().set_sync_event(sync)
                raise CantServe()

        server = self.get_server(
            connection_handler_class=FailingWhileServingConnectionHandler)
        # Install the exception swallower
        server.set_ignored_exceptions(CantServe)
        client = self.get_client()
        # Connect to the server so the exception is raised there
        client.connect((server.host, server.port))
        # Wait for the exception to propagate.
        sync.wait()
        self.assertEqual('', client.read()) # connection closed
        # The connection wasn't served properly but the exception should have
        # been swallowed.
        server.pending_exception()

    def test_handle_request_closes_if_it_doesnt_process(self):
        server = self.get_server()
        client = self.get_client()
        server.server.serving = False
        client.connect((server.host, server.port))
        self.assertEqual('', client.read())


class TestTestingSmartServer(tests.TestCase):

    def test_sets_client_timeout(self):
        server = test_server.TestingSmartServer(('localhost', 0), None, None,
            root_client_path='/no-such-client/path')
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
                         server._client_timeout)
        sock = socket.socket()
        h = server._make_handler(sock)
        self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
                         h._client_timeout)


class FakeServer(object):
    """Minimal implementation to pass to TestingSmartConnectionHandler"""
    backing_transport = None
    root_client_path = '/'


class TestTestingSmartConnectionHandler(tests.TestCase):

    def test_connection_timeout_suppressed(self):
        self.overrideAttr(test_server, '_DEFAULT_TESTING_CLIENT_TIMEOUT', 0.01)
        s = FakeServer()
        server_sock, client_sock = portable_socket_pair()
        # This should timeout quickly, but not generate an exception.
        handler = test_server.TestingSmartConnectionHandler(server_sock,
            server_sock.getpeername(), s)

    def test_connection_shutdown_while_serving_no_error(self):
        s = FakeServer()
        server_sock, client_sock = portable_socket_pair()
        class ShutdownConnectionHandler(
            test_server.TestingSmartConnectionHandler):

            def _build_protocol(self):
                self.finished = True
                return super(ShutdownConnectionHandler, self)._build_protocol()
        # This should trigger shutdown after the entering _build_protocol, and
        # we should exit cleanly, without raising an exception.
        handler = ShutdownConnectionHandler(server_sock,
            server_sock.getpeername(), s)