/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
23
import bzrlib.tests as tests
24
import bzrlib.transport.ssh as ssh
1752.2.2 by Martin Pool
SSH get method; more development of framework.
25
from bzrlib.transport import local, memory
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
26
27
class TestSSHTransport(tests.TestCase):
28
    
29
    def test_loopback_ssh_connection_exists(self):
30
        ssh.LoopbackSSHConnection()
31
32
    def test_ssh_query_version(self):
33
        """Feed a canned query version to a server"""
34
        to_server = StringIO('hello\0011\n')
35
        from_server = StringIO()
36
        server = ssh.Server(to_server, from_server, local.LocalTransport('file:///'))
37
        server._serve_one_request()
38
        self.assertEqual('bzr server\0011\n',
39
                         from_server.getvalue())
40
1752.2.2 by Martin Pool
SSH get method; more development of framework.
41
    def test_canned_get_response(self):
42
        transport = memory.MemoryTransport('memory:///')
43
        transport.put('hello', StringIO('contents\nof\nfile\n'))
44
        to_server = StringIO('get\001./hello\n')
45
        from_server = StringIO()
46
        server = ssh.Server(to_server, from_server, transport)
47
        server._serve_one_request()
48
        self.assertEqual('ok\n'
49
                         '17\n'
50
                         'contents\nof\nfile\n'
51
                         'done\n',
52
                         from_server.getvalue())
53
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
54
    def test_open_loopback_server(self):
55
        conn = ssh.LoopbackSSHConnection()
56
        version = conn.query_version()
57
        self.assertEqual(1, version)
58
59
    def test_server_shutdown_on_client_disconnect(self):
60
        conn = ssh.LoopbackSSHConnection()
61
        conn.disconnect()
62
        conn._server_thread.join()
63
        self.assertFalse(conn._server_thread.isAlive())
64
65
    def test_multiple_requests(self):
66
        conn = ssh.LoopbackSSHConnection()
67
        version = conn.query_version()
68
        self.assertEqual(1, version)
69
        version = conn.query_version()
70
        self.assertEqual(1, version)
71
72
    def test_ssh_transport_has(self):
73
        """Checking for file existence over ssh."""
74
        conn = ssh.LoopbackSSHConnection()
75
        conn.backing_transport.put("foo", StringIO("contents of foo\n"))
76
        self.assertTrue(conn.has("foo"))
77
        self.assertFalse(conn.has("non-foo"))
78
1752.2.2 by Martin Pool
SSH get method; more development of framework.
79
    def test_ssh_transport_get(self):
80
        """Read back a file over ssh."""
81
        conn = ssh.LoopbackSSHConnection()
82
        conn.backing_transport.put("foo", StringIO("contents\nof\nfoo\n"))
83
        fp = conn.get("foo")
84
        self.assertEqual('contents\nof\nfoo\n', fp.read())
85
        
86
    def test_get_error(self):
87
        """Error reported from server getting nonexistent file."""
88
        return
89
        conn = ssh.LoopbackSSHConnection()
90
        fp = conn.get("non-foo")
91
        # self.assertEqual('contents of foo\n', fp.read())
92
1752.2.1 by Martin Pool
Start implementation of a simple ssh server.
93
    # TODO: Try sending multiple requests; they should all get answers.
94
95
    # TODO: If the server raises an error within its processing that should be
96
    # caught and propagated back to the client.