~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2005-2011 Robey Pointer <robey@lag.net>
2221.5.1 by Dmitry Vasiliev
Added support for Putty's SSH implementation
2
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
3
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
8
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
13
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
17
18
import os
19
import socket
2321.3.7 by Alexander Belchenko
fixes for passing test_sftp_transport on win32 (thankyou John)
20
import sys
1871.1.3 by Robert Collins
proof of concept slowsocket wrapper.
21
import time
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
22
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
23
from bzrlib import (
24
    bzrdir,
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
25
    config,
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
26
    errors,
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
27
    tests,
28
    transport as _mod_transport,
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
29
    ui,
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
30
    )
31
from bzrlib.osutils import (
32
    lexists,
33
    )
34
from bzrlib.tests import (
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
35
    features,
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
36
    TestCaseWithTransport,
37
    TestCase,
38
    TestSkipped,
39
    )
2929.3.7 by Vincent Ladeuil
Rename bzrlib/test/HttpServer.py to bzrlib/tests/http_server.py and fix uses.
40
from bzrlib.tests.http_server import HttpServer
1711.2.132 by John Arbash Meinel
Clean up PEP8 and unused imports in bench_sftp.py, and missing import in bzrlib/tests/test_sftp_transport.py
41
import bzrlib.transport.http
2822.1.1 by v.ladeuil+lp at free
Fix #59150 (again) by handling paramiko availability for transport_util.py.
42
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
43
if features.paramiko.available():
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
44
    from bzrlib.transport import sftp as _mod_sftp
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
45
    from bzrlib.tests import stub_sftp
2822.1.1 by v.ladeuil+lp at free
Fix #59150 (again) by handling paramiko availability for transport_util.py.
46
1874.1.12 by Carl Friedrich Bolz
More fixes according to John's comments.
47
1874.1.14 by Carl Friedrich Bolz
Rename setup method to make its intent clearer. Some PEP 8 issues.
48
def set_test_transport_to_sftp(testcase):
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
49
    """A helper to set transports on test case instances."""
1874.1.6 by holger krekel
(cfbolz, hpk) Factor out common set_transport code.
50
    if getattr(testcase, '_get_remote_is_absolute', None) is None:
51
        testcase._get_remote_is_absolute = True
52
    if testcase._get_remote_is_absolute:
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
53
        testcase.transport_server = stub_sftp.SFTPAbsoluteServer
1874.1.6 by holger krekel
(cfbolz, hpk) Factor out common set_transport code.
54
    else:
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
55
        testcase.transport_server = stub_sftp.SFTPHomeDirServer
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
56
    testcase.transport_readonly_server = HttpServer
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
57
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
58
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
59
class TestCaseWithSFTPServer(TestCaseWithTransport):
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
60
    """A test case base class that provides a sftp server on localhost."""
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
61
62
    def setUp(self):
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
63
        super(TestCaseWithSFTPServer, self).setUp()
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
64
        self.requireFeature(features.paramiko)
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
65
        set_test_transport_to_sftp(self)
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
66
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
67
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
68
class SFTPLockTests(TestCaseWithSFTPServer):
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
69
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
70
    def test_sftp_locks(self):
71
        from bzrlib.errors import LockError
72
        t = self.get_transport()
73
74
        l = t.lock_write('bogus')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
75
        self.assertPathExists('bogus.write-lock')
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
76
77
        # Don't wait for the lock, locking an already locked
78
        # file should raise an assert
79
        self.assertRaises(LockError, t.lock_write, 'bogus')
80
81
        l.unlock()
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
82
        self.assertFalse(lexists('bogus.write-lock'))
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
83
84
        open('something.write-lock', 'wb').write('fake lock\n')
85
        self.assertRaises(LockError, t.lock_write, 'something')
86
        os.remove('something.write-lock')
87
88
        l = t.lock_write('something')
89
90
        l2 = t.lock_write('bogus')
91
92
        l.unlock()
93
        l2.unlock()
94
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
95
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
96
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
97
    """Test the SFTP transport with homedir based relative paths."""
98
99
    def test__remote_path(self):
2823.1.4 by Vincent Ladeuil
Use assertIsSameRealPath to avoid OSX aliasing (specifically /tmp
100
        if sys.platform == 'darwin':
2823.1.11 by Vincent Ladeuil
Review feedback.
101
            # This test is about sftp absolute path handling. There is already
102
            # (in this test) a TODO about windows needing an absolute path
103
            # without drive letter. To me, using self.test_dir is a trick to
104
            # get an absolute path for comparison purposes.  That fails for OSX
105
            # because the sftp server doesn't resolve the links (and it doesn't
106
            # have to). --vila 20070924
2823.1.8 by Vincent Ladeuil
Rewrite expected failure message
107
            self.knownFailure('Mac OSX symlinks /tmp to /private/tmp,'
108
                              ' testing against self.test_dir'
109
                              ' is not appropriate')
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
110
        t = self.get_transport()
2321.3.7 by Alexander Belchenko
fixes for passing test_sftp_transport on win32 (thankyou John)
111
        # This test require unix-like absolute path
112
        test_dir = self.test_dir
113
        if sys.platform == 'win32':
114
            # using hack suggested by John Meinel.
115
            # TODO: write another mock server for this test
116
            #       and use absolute path without drive letter
117
            test_dir = '/' + test_dir
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
118
        # try what is currently used:
119
        # remote path = self._abspath(relpath)
2823.1.14 by Vincent Ladeuil
Fix 141382 by comparing real paths.
120
        self.assertIsSameRealPath(test_dir + '/relative',
121
                                  t._remote_path('relative'))
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
122
        # we dont os.path.join because windows gives us the wrong path
2321.3.7 by Alexander Belchenko
fixes for passing test_sftp_transport on win32 (thankyou John)
123
        root_segments = test_dir.split('/')
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
124
        root_parent = '/'.join(root_segments[:-1])
125
        # .. should be honoured
2823.1.14 by Vincent Ladeuil
Fix 141382 by comparing real paths.
126
        self.assertIsSameRealPath(root_parent + '/sibling',
127
                                  t._remote_path('../sibling'))
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
128
        # /  should be illegal ?
129
        ### FIXME decide and then test for all transports. RBC20051208
130
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
131
1986.1.10 by Robert Collins
Merge from bzr.dev, fixing found bugs handling 'has('/')' in MemoryTransport and SFTP transports.
132
class SFTPTransportTestRelativeRoot(TestCaseWithSFTPServer):
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
133
    """Test the SFTP transport with homedir based relative paths."""
134
135
    def setUp(self):
2485.8.43 by Vincent Ladeuil
Cleaning.
136
        # Only SFTPHomeDirServer is tested here
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
137
        self._get_remote_is_absolute = False
1986.1.10 by Robert Collins
Merge from bzr.dev, fixing found bugs handling 'has('/')' in MemoryTransport and SFTP transports.
138
        super(SFTPTransportTestRelativeRoot, self).setUp()
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
139
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
140
    def test__remote_path_relative_root(self):
141
        # relative paths are preserved
142
        t = self.get_transport('')
6055.2.1 by Jelmer Vernooij
Add UnparsedUrl.
143
        self.assertEqual('/~/', t._parsed_url.path)
2485.8.27 by Vincent Ladeuil
Hearing jam saying "vila, you're trying too hard", I simplified again.
144
        # the remote path should be relative to home dir
145
        # (i.e. not begining with a '/')
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
146
        self.assertEqual('a', t._remote_path('a'))
147
148
1185.49.14 by John Arbash Meinel
[merge] bzr.dev
149
class SFTPNonServerTest(TestCase):
1185.58.12 by John Arbash Meinel
Changing so that sftp tests are skipped rather than hidden when paramiko isn't present
150
    def setUp(self):
151
        TestCase.setUp(self)
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
152
        self.requireFeature(features.paramiko)
1185.58.12 by John Arbash Meinel
Changing so that sftp tests are skipped rather than hidden when paramiko isn't present
153
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
154
    def test_parse_url_with_home_dir(self):
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
155
        s = _mod_sftp.SFTPTransport(
156
            'sftp://ro%62ey:h%40t@example.com:2222/~/relative')
6055.2.1 by Jelmer Vernooij
Add UnparsedUrl.
157
        self.assertEquals(s._parsed_url.host, 'example.com')
158
        self.assertEquals(s._parsed_url.port, 2222)
159
        self.assertEquals(s._parsed_url.user, 'robey')
160
        self.assertEquals(s._parsed_url.password, 'h@t')
161
        self.assertEquals(s._parsed_url.path, '/~/relative/')
1185.49.23 by John Arbash Meinel
bugreport from Matthieu Moy: relpath was failing, but throwing an unhelpful exception.
162
163
    def test_relpath(self):
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
164
        s = _mod_sftp.SFTPTransport('sftp://user@host.com/abs/path')
2485.8.20 by Vincent Ladeuil
Refactor SFTPTransport. Test suite passes.
165
        self.assertRaises(errors.PathNotChild, s.relpath,
166
                          'sftp://user@host.com/~/rel/path/sub')
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
167
2013.1.2 by John Arbash Meinel
Add a test that we can always fall back to the paramiko vendor
168
    def test_get_paramiko_vendor(self):
169
        """Test that if no 'ssh' is available we get builtin paramiko"""
170
        from bzrlib.transport import ssh
171
        # set '.' as the only location in the path, forcing no 'ssh' to exist
5570.3.12 by Vincent Ladeuil
Replace osutils.set_or_unset_env calls with self.overrideEnv.
172
        self.overrideAttr(ssh, '_ssh_vendor_manager')
5570.3.13 by Vincent Ladeuil
Fix typo.
173
        self.overrideEnv('PATH', '.')
5570.3.12 by Vincent Ladeuil
Replace osutils.set_or_unset_env calls with self.overrideEnv.
174
        ssh._ssh_vendor_manager.clear_cache()
175
        vendor = ssh._get_ssh_vendor()
176
        self.assertIsInstance(vendor, ssh.ParamikoVendor)
2013.1.2 by John Arbash Meinel
Add a test that we can always fall back to the paramiko vendor
177
1986.1.10 by Robert Collins
Merge from bzr.dev, fixing found bugs handling 'has('/')' in MemoryTransport and SFTP transports.
178
    def test_abspath_root_sibling_server(self):
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
179
        server = stub_sftp.SFTPSiblingAbsoluteServer()
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
180
        server.start_server()
5570.3.12 by Vincent Ladeuil
Replace osutils.set_or_unset_env calls with self.overrideEnv.
181
        self.addCleanup(server.stop_server)
182
183
        transport = _mod_transport.get_transport(server.get_url())
184
        self.assertFalse(transport.abspath('/').endswith('/~/'))
185
        self.assertTrue(transport.abspath('/').endswith('/'))
186
        del transport
1986.1.10 by Robert Collins
Merge from bzr.dev, fixing found bugs handling 'has('/')' in MemoryTransport and SFTP transports.
187
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
188
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
189
class SFTPBranchTest(TestCaseWithSFTPServer):
190
    """Test some stuff when accessing a bzr Branch over sftp"""
191
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
192
    def test_push_support(self):
193
        self.build_tree(['a/', 'a/foo'])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
194
        t = bzrdir.BzrDir.create_standalone_workingtree('a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
195
        b = t.branch
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
196
        t.add('foo')
197
        t.commit('foo', rev_id='a1')
198
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
199
        b2 = bzrdir.BzrDir.create_branch_and_repo(self.get_url('/b'))
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
200
        b2.pull(b)
201
202
        self.assertEquals(b2.revision_history(), ['a1'])
203
1185.31.48 by John Arbash Meinel
Added a small test to sftp to make sure some replacing was going on in the remote side.
204
        open('a/foo', 'wt').write('something new in foo\n')
205
        t.commit('new', rev_id='a2')
206
        b2.pull(b)
207
208
        self.assertEquals(b2.revision_history(), ['a1', 'a2'])
209
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
210
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
211
class SSHVendorConnection(TestCaseWithSFTPServer):
212
    """Test that the ssh vendors can all connect.
213
214
    Verify that a full-handshake (SSH over loopback TCP) sftp connection works.
215
216
    We have 3 sftp implementations in the test suite:
217
      'loopback': Doesn't use ssh, just uses a local socket. Most tests are
218
                  done this way to save the handshaking time, so it is not
219
                  tested again here
220
      'none':     This uses paramiko's built-in ssh client and server, and layers
221
                  sftp on top of it.
222
      None:       If 'ssh' exists on the machine, then it will be spawned as a
223
                  child process.
224
    """
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
225
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
226
    def setUp(self):
227
        super(SSHVendorConnection, self).setUp()
228
229
        def create_server():
230
            """Just a wrapper so that when created, it will set _vendor"""
231
            # SFTPFullAbsoluteServer can handle any vendor,
232
            # it just needs to be set between the time it is instantiated
233
            # and the time .setUp() is called
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
234
            server = stub_sftp.SFTPFullAbsoluteServer()
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
235
            server._vendor = self._test_vendor
236
            return server
237
        self._test_vendor = 'loopback'
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
238
        self.vfs_transport_server = create_server
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
239
        f = open('a_file', 'wb')
240
        try:
241
            f.write('foobar\n')
242
        finally:
243
            f.close()
244
245
    def set_vendor(self, vendor):
246
        self._test_vendor = vendor
247
248
    def test_connection_paramiko(self):
1951.1.8 by Andrew Bennetts
Make _get_ssh_vendor return the vendor object, rather than just a string.
249
        from bzrlib.transport import ssh
250
        self.set_vendor(ssh.ParamikoVendor())
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
251
        t = self.get_transport()
252
        self.assertEqual('foobar\n', t.get('a_file').read())
253
254
    def test_connection_vendor(self):
255
        raise TestSkipped("We don't test spawning real ssh,"
256
                          " because it prompts for a password."
257
                          " Enable this test if we figure out"
258
                          " how to prevent this.")
259
        self.set_vendor(None)
260
        t = self.get_transport()
261
        self.assertEqual('foobar\n', t.get('a_file').read())
262
263
264
class SSHVendorBadConnection(TestCaseWithTransport):
265
    """Test that the ssh vendors handle bad connection properly
266
267
    We don't subclass TestCaseWithSFTPServer, because we don't actually
268
    need an SFTP connection.
269
    """
270
271
    def setUp(self):
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
272
        self.requireFeature(features.paramiko)
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
273
        super(SSHVendorBadConnection, self).setUp()
274
1185.49.35 by John Arbash Meinel
Update tests to use a truly unused port
275
        # open a random port, so we know nobody else is using it
276
        # but don't actually listen on the port.
277
        s = socket.socket()
278
        s.bind(('localhost', 0))
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
279
        self.addCleanup(s.close)
1185.49.35 by John Arbash Meinel
Update tests to use a truly unused port
280
        self.bogus_url = 'sftp://%s:%s/' % s.getsockname()
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
281
282
    def set_vendor(self, vendor):
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
283
        from bzrlib.transport import ssh
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
284
        self.overrideAttr(ssh._ssh_vendor_manager, '_cached_ssh_vendor', vendor)
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
285
286
    def test_bad_connection_paramiko(self):
287
        """Test that a real connection attempt raises the right error"""
1951.1.8 by Andrew Bennetts
Make _get_ssh_vendor return the vendor object, rather than just a string.
288
        from bzrlib.transport import ssh
289
        self.set_vendor(ssh.ParamikoVendor())
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
290
        t = _mod_transport.get_transport(self.bogus_url)
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
291
        self.assertRaises(errors.ConnectionError, t.get, 'foobar')
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
292
293
    def test_bad_connection_ssh(self):
294
        """None => auto-detect vendor"""
295
        self.set_vendor(None)
1185.49.33 by John Arbash Meinel
Spawn another bzr instance using run_bzr_subprocess, so we don't get stipple
296
        # This is how I would normally test the connection code
297
        # it makes it very clear what we are testing.
298
        # However, 'ssh' will create stipple on the output, so instead
299
        # I'm using run_bzr_subprocess, and parsing the output
300
        # try:
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
301
        #     t = _mod_transport.get_transport(self.bogus_url)
1185.49.33 by John Arbash Meinel
Spawn another bzr instance using run_bzr_subprocess, so we don't get stipple
302
        # except errors.ConnectionError:
303
        #     # Correct error
304
        #     pass
305
        # except errors.NameError, e:
306
        #     if 'SSHException' in str(e):
307
        #         raise TestSkipped('Known NameError bug in paramiko 1.6.1')
308
        #     raise
309
        # else:
310
        #     self.fail('Excepted ConnectionError to be raised')
311
2665.4.1 by Aaron Bentley
teach run_bzr_subprocess to accept either a list of strings or a string
312
        out, err = self.run_bzr_subprocess(['log', self.bogus_url], retcode=3)
1185.49.33 by John Arbash Meinel
Spawn another bzr instance using run_bzr_subprocess, so we don't get stipple
313
        self.assertEqual('', out)
314
        if "NameError: global name 'SSHException'" in err:
315
            # We aren't fixing this bug, because it is a bug in
316
            # paramiko, but we know about it, so we don't have to
317
            # fail the test
318
            raise TestSkipped('Known NameError bug with paramiko-1.6.1')
2052.4.4 by John Arbash Meinel
Create a SocketConnectionError to make creating nice errors easier
319
        self.assertContainsRe(err, r'bzr: ERROR: Unable to connect to SSH host'
320
                                   r' 127\.0\.0\.1:\d+; ')
1185.49.32 by John Arbash Meinel
Update tests to show that all ssh vendor failed connections work correctly, has some stipple from real ssh
321
1871.1.3 by Robert Collins
proof of concept slowsocket wrapper.
322
323
class SFTPLatencyKnob(TestCaseWithSFTPServer):
324
    """Test that the testing SFTPServer's latency knob works."""
325
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
326
    def test_latency_knob_slows_transport(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
327
        # change the latency knob to 500ms. We take about 40ms for a
1871.1.3 by Robert Collins
proof of concept slowsocket wrapper.
328
        # loopback connection ordinarily.
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
329
        start_time = time.time()
330
        self.get_server().add_latency = 0.5
331
        transport = self.get_transport()
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
332
        transport.has('not me') # Force connection by issuing a request
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
333
        with_latency_knob_time = time.time() - start_time
334
        self.assertTrue(with_latency_knob_time > 0.4)
335
336
    def test_default(self):
337
        # This test is potentially brittle: under extremely high machine load
338
        # it could fail, but that is quite unlikely
2631.1.1 by Aaron Bentley
Disable timing-sensitive test
339
        raise TestSkipped('Timing-sensitive test')
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
340
        start_time = time.time()
341
        transport = self.get_transport()
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
342
        transport.has('not me') # Force connection by issuing a request
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
343
        regular_time = time.time() - start_time
344
        self.assertTrue(regular_time < 0.5)
345
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
346
347
class FakeSocket(object):
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
348
    """Fake socket object used to test the SocketDelay wrapper without
349
    using a real socket.
350
    """
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
351
352
    def __init__(self):
353
        self._data = ""
354
355
    def send(self, data, flags=0):
356
        self._data += data
357
        return len(data)
358
359
    def sendall(self, data, flags=0):
360
        self._data += data
361
        return len(data)
362
363
    def recv(self, size, flags=0):
364
        if size < len(self._data):
365
            result = self._data[:size]
366
            self._data = self._data[size:]
367
            return result
368
        else:
369
            result = self._data
370
            self._data = ""
371
            return result
372
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
373
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
374
class TestSocketDelay(TestCase):
1874.1.9 by Carl Friedrich Bolz
Try to fix all the issues outline by john and Robert.
375
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
376
    def setUp(self):
377
        TestCase.setUp(self)
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
378
        self.requireFeature(features.paramiko)
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
379
380
    def test_delay(self):
381
        sending = FakeSocket()
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
382
        receiving = stub_sftp.SocketDelay(sending, 0.1, bandwidth=1000000,
383
                                          really_sleep=False)
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
384
        # check that simulated time is charged only per round-trip:
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
385
        t1 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
386
        receiving.send("connect1")
387
        self.assertEqual(sending.recv(1024), "connect1")
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
388
        t2 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
389
        self.assertAlmostEqual(t2 - t1, 0.1)
390
        receiving.send("connect2")
391
        self.assertEqual(sending.recv(1024), "connect2")
392
        sending.send("hello")
393
        self.assertEqual(receiving.recv(1024), "hello")
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
394
        t3 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
395
        self.assertAlmostEqual(t3 - t2, 0.1)
396
        sending.send("hello")
397
        self.assertEqual(receiving.recv(1024), "hello")
398
        sending.send("hello")
399
        self.assertEqual(receiving.recv(1024), "hello")
400
        sending.send("hello")
401
        self.assertEqual(receiving.recv(1024), "hello")
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
402
        t4 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
403
        self.assertAlmostEqual(t4, t3)
404
405
    def test_bandwidth(self):
406
        sending = FakeSocket()
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
407
        receiving = stub_sftp.SocketDelay(sending, 0, bandwidth=8.0/(1024*1024),
408
                                          really_sleep=False)
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
409
        # check that simulated time is charged only per round-trip:
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
410
        t1 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
411
        receiving.send("connect")
412
        self.assertEqual(sending.recv(1024), "connect")
413
        sending.send("a" * 100)
414
        self.assertEqual(receiving.recv(1024), "a" * 100)
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
415
        t2 = stub_sftp.SocketDelay.simulated_time
1874.1.2 by Carl Friedrich Bolz
Refined the SocketDelay to charge latency only once per round-trip and to
416
        self.assertAlmostEqual(t2 - t1, 100 + 7)
417
1874.1.3 by Carl Friedrich Bolz
Merge bzr.dev.
418
3815.2.4 by Martin Pool
merge fix for out-of-order SFTP readv
419
class ReadvFile(object):
5807.5.5 by Martin
Add close method to ReadvFile test object to fix failure
420
    """An object that acts like Paramiko's SFTPFile when readv() is used"""
3815.2.4 by Martin Pool
merge fix for out-of-order SFTP readv
421
422
    def __init__(self, data):
423
        self._data = data
424
425
    def readv(self, requests):
426
        for start, length in requests:
427
            yield self._data[start:start+length]
428
5807.5.5 by Martin
Add close method to ReadvFile test object to fix failure
429
    def close(self):
430
        pass
431
3815.2.4 by Martin Pool
merge fix for out-of-order SFTP readv
432
3882.7.16 by Martin Pool
Update SFTP tests to accommodate progress reporting
433
def _null_report_activity(*a, **k):
434
    pass
435
436
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
437
class Test_SFTPReadvHelper(tests.TestCase):
438
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
439
    def checkGetRequests(self, expected_requests, offsets):
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
440
        self.requireFeature(features.paramiko)
3882.7.16 by Martin Pool
Update SFTP tests to accommodate progress reporting
441
        helper = _mod_sftp._SFTPReadvHelper(offsets, 'artificial_test',
442
            _null_report_activity)
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
443
        self.assertEqual(expected_requests, helper._get_requests())
444
445
    def test__get_requests(self):
446
        # Small single requests become a single readv request
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
447
        self.checkGetRequests([(0, 100)],
448
                              [(0, 20), (30, 50), (20, 10), (80, 20)])
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
449
        # Non-contiguous ranges are given as multiple requests
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
450
        self.checkGetRequests([(0, 20), (30, 50)],
451
                              [(10, 10), (30, 20), (0, 10), (50, 30)])
3686.1.2 by John Arbash Meinel
Start moving the readv code into a helper.
452
        # Ranges larger than _max_request_size (32kB) are broken up into
453
        # multiple requests, even if it actually spans multiple logical
454
        # requests
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
455
        self.checkGetRequests([(0, 32768), (32768, 32768), (65536, 464)],
456
                              [(0, 40000), (40000, 100), (40100, 1900),
457
                               (42000, 24000)])
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
458
3815.2.4 by Martin Pool
merge fix for out-of-order SFTP readv
459
    def checkRequestAndYield(self, expected, data, offsets):
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
460
        self.requireFeature(features.paramiko)
3882.7.16 by Martin Pool
Update SFTP tests to accommodate progress reporting
461
        helper = _mod_sftp._SFTPReadvHelper(offsets, 'artificial_test',
462
            _null_report_activity)
3815.2.4 by Martin Pool
merge fix for out-of-order SFTP readv
463
        data_f = ReadvFile(data)
464
        result = list(helper.request_and_yield_offsets(data_f))
465
        self.assertEqual(expected, result)
466
467
    def test_request_and_yield_offsets(self):
468
        data = 'abcdefghijklmnopqrstuvwxyz'
469
        self.checkRequestAndYield([(0, 'a'), (5, 'f'), (10, 'klm')], data,
470
                                  [(0, 1), (5, 1), (10, 3)])
471
        # Should combine requests, and split them again
472
        self.checkRequestAndYield([(0, 'a'), (1, 'b'), (10, 'klm')], data,
473
                                  [(0, 1), (1, 1), (10, 3)])
474
        # Out of order requests. The requests should get combined, but then be
475
        # yielded out-of-order. We also need one that is at the end of a
476
        # previous range. See bug #293746
477
        self.checkRequestAndYield([(0, 'a'), (10, 'k'), (4, 'efg'), (1, 'bcd')],
478
                                  data, [(0, 1), (10, 1), (4, 3), (1, 3)])
479
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
480
481
class TestUsesAuthConfig(TestCaseWithSFTPServer):
3777.1.4 by Aaron Bentley
bzr+ssh and sftp both use ssh scheme.
482
    """Test that AuthenticationConfig can supply default usernames."""
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
483
3777.1.2 by Aaron Bentley
Make testing more thorough
484
    def get_transport_for_connection(self, set_config):
5247.4.18 by Vincent Ladeuil
Replace SocketListener by TestingTCPServerInAThread and fallouts,
485
        port = self.get_server().port
3777.1.2 by Aaron Bentley
Make testing more thorough
486
        if set_config:
487
            conf = config.AuthenticationConfig()
488
            conf._get_config().update(
3777.1.4 by Aaron Bentley
bzr+ssh and sftp both use ssh scheme.
489
                {'sftptest': {'scheme': 'ssh', 'port': port, 'user': 'bar'}})
3777.1.2 by Aaron Bentley
Make testing more thorough
490
            conf._save()
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
491
        t = _mod_transport.get_transport('sftp://localhost:%d' % port)
3777.1.2 by Aaron Bentley
Make testing more thorough
492
        # force a connection to be performed.
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
493
        t.has('foo')
3777.1.2 by Aaron Bentley
Make testing more thorough
494
        return t
495
496
    def test_sftp_uses_config(self):
497
        t = self.get_transport_for_connection(set_config=True)
3777.1.1 by Aaron Bentley
Use auth.conf for sftp
498
        self.assertEqual('bar', t._get_credentials()[0])
3777.1.2 by Aaron Bentley
Make testing more thorough
499
500
    def test_sftp_is_none_if_no_config(self):
501
        t = self.get_transport_for_connection(set_config=False)
4304.2.1 by Vincent Ladeuil
Fix bug #367726 by reverting some default user handling introduced
502
        self.assertIs(None, t._get_credentials()[0])
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
503
504
    def test_sftp_doesnt_prompt_username(self):
505
        stdout = tests.StringIOWrapper()
506
        ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n', stdout=stdout)
507
        t = self.get_transport_for_connection(set_config=False)
4304.2.1 by Vincent Ladeuil
Fix bug #367726 by reverting some default user handling introduced
508
        self.assertIs(None, t._get_credentials()[0])
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
509
        # No prompts should've been printed, stdin shouldn't have been read
510
        self.assertEquals("", stdout.getvalue())
511
        self.assertEquals(0, ui.ui_factory.stdin.tell())