/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for ssh transport"""
18
19
# all of this deals with byte strings so this is safe
20
from cStringIO import StringIO
21
22
import bzrlib
1752.2.3 by Martin Pool
(ssh) more development of error handling
23
from bzrlib import tests, errors
24
from bzrlib.transport import local, memory, ssh
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
25
26
class TestSSHTransport(tests.TestCase):
27
    
28
    def test_loopback_ssh_connection_exists(self):
29
        ssh.LoopbackSSHConnection()
30
31
    def test_ssh_query_version(self):
32
        """Feed a canned query version to a server"""
33
        to_server = StringIO('hello\0011\n')
34
        from_server = StringIO()
35
        server = ssh.Server(to_server, from_server, local.LocalTransport('file:///'))
36
        server._serve_one_request()
37
        self.assertEqual('bzr server\0011\n',
38
                         from_server.getvalue())
39
1752.2.2 by Martin Pool
SSH get method; more development of framework.
40
    def test_canned_get_response(self):
41
        transport = memory.MemoryTransport('memory:///')
42
        transport.put('hello', StringIO('contents\nof\nfile\n'))
43
        to_server = StringIO('get\001./hello\n')
44
        from_server = StringIO()
45
        server = ssh.Server(to_server, from_server, transport)
46
        server._serve_one_request()
47
        self.assertEqual('ok\n'
48
                         '17\n'
49
                         'contents\nof\nfile\n'
50
                         'done\n',
51
                         from_server.getvalue())
52
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
53
    def test_open_loopback_server(self):
54
        conn = ssh.LoopbackSSHConnection()
55
        version = conn.query_version()
56
        self.assertEqual(1, version)
57
58
    def test_server_shutdown_on_client_disconnect(self):
59
        conn = ssh.LoopbackSSHConnection()
60
        conn.disconnect()
61
        conn._server_thread.join()
62
        self.assertFalse(conn._server_thread.isAlive())
63
64
    def test_multiple_requests(self):
65
        conn = ssh.LoopbackSSHConnection()
66
        version = conn.query_version()
67
        self.assertEqual(1, version)
68
        version = conn.query_version()
69
        self.assertEqual(1, version)
70
71
    def test_ssh_transport_has(self):
72
        """Checking for file existence over ssh."""
73
        conn = ssh.LoopbackSSHConnection()
74
        conn.backing_transport.put("foo", StringIO("contents of foo\n"))
75
        self.assertTrue(conn.has("foo"))
76
        self.assertFalse(conn.has("non-foo"))
77
1752.2.2 by Martin Pool
SSH get method; more development of framework.
78
    def test_ssh_transport_get(self):
79
        """Read back a file over ssh."""
80
        conn = ssh.LoopbackSSHConnection()
81
        conn.backing_transport.put("foo", StringIO("contents\nof\nfoo\n"))
82
        fp = conn.get("foo")
83
        self.assertEqual('contents\nof\nfoo\n', fp.read())
84
        
1752.2.3 by Martin Pool
(ssh) more development of error handling
85
    def test_get_error_enoent(self):
1752.2.2 by Martin Pool
SSH get method; more development of framework.
86
        """Error reported from server getting nonexistent file."""
87
        conn = ssh.LoopbackSSHConnection()
1752.2.3 by Martin Pool
(ssh) more development of error handling
88
        try:
89
            conn.get('not a file')
90
        except errors.NoSuchFile, e:
91
            self.assertEqual('not a file', e.path)
92
        else:
93
            self.fail("get did not raise expected error")
94
95
    def test_get_error_unexpected(self):
96
        """Error reported by server with no specific representation"""
97
        class FlakyTransport(object):
98
            def get(self, path):
99
                raise Exception("some random exception from inside server")
100
        conn = ssh.LoopbackSSHConnection(transport=FlakyTransport())
101
        try:
102
            conn.get('something')
103
        except errors.TransportError, e:
104
            self.assertContainsRe(str(e), 'some random exception')
105
        else:
106
            self.fail("get did not raise expected error")
107
1752.2.2 by Martin Pool
SSH get method; more development of framework.
108
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
109
    # TODO: Try sending multiple requests; they should all get answers.
110
111
    # TODO: If the server raises an error within its processing that should be
112
    # caught and propagated back to the client.