21
21
from cStringIO import StringIO
28
from bzrlib.errors import (ConnectionError,
24
from bzrlib import urlutils
25
from bzrlib.errors import (NoSuchFile, FileExists,
34
26
TransportNotPossible,
36
28
DependencyNotPresent,
38
29
UnsupportedProtocol,
40
31
from bzrlib.tests import TestCase, TestCaseInTempDir
41
32
from bzrlib.transport import (_CoalescedOffset,
42
33
_get_protocol_handlers,
43
_set_protocol_handlers,
44
34
_get_transport_modules,
47
36
register_lazy_transport,
48
register_transport_proto,
49
_clear_protocol_handlers,
37
_set_protocol_handlers,
52
from bzrlib.transport.chroot import ChrootServer
53
40
from bzrlib.transport.memory import MemoryTransport
54
from bzrlib.transport.local import (LocalTransport,
55
EmulatedWin32LocalTransport)
41
from bzrlib.transport.local import LocalTransport
58
44
# TODO: Should possibly split transport-specific tests into their own files.
64
50
def test__get_set_protocol_handlers(self):
65
51
handlers = _get_protocol_handlers()
66
self.assertNotEqual([], handlers.keys( ))
52
self.assertNotEqual({}, handlers)
68
_clear_protocol_handlers()
69
self.assertEqual([], _get_protocol_handlers().keys())
54
_set_protocol_handlers({})
55
self.assertEqual({}, _get_protocol_handlers())
71
57
_set_protocol_handlers(handlers)
75
61
class SampleHandler(object):
76
62
"""I exist, isnt that enough?"""
78
_clear_protocol_handlers()
79
register_transport_proto('foo')
65
_set_protocol_handlers(my_handlers)
80
66
register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
81
register_transport_proto('bar')
82
67
register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
83
self.assertEqual([SampleHandler.__module__, 'bzrlib.transport.chroot'],
68
self.assertEqual([SampleHandler.__module__],
84
69
_get_transport_modules())
86
71
_set_protocol_handlers(handlers)
110
94
"""Transport with missing dependency causes no error"""
111
95
saved_handlers = _get_protocol_handlers()
113
_clear_protocol_handlers()
114
register_transport_proto('foo')
97
_set_protocol_handlers({})
115
98
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
116
99
'BackupTransportHandler')
117
100
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
122
105
_set_protocol_handlers(saved_handlers)
124
def test_LateReadError(self):
125
"""The LateReadError helper should raise on read()."""
126
a_file = LateReadError('a path')
129
except ReadError, error:
130
self.assertEqual('a path', error.path)
131
self.assertRaises(ReadError, a_file.read, 40)
134
107
def test__combine_paths(self):
135
108
t = Transport('/')
136
109
self.assertEqual('/home/sarah/project/foo',
316
283
self.assertEqual(7, transport.stat('foo').st_size)
317
284
self.assertEqual(6, transport.stat('bar').st_size)
320
class ChrootDecoratorTransportTest(TestCase):
321
"""Chroot decoration specific tests."""
323
def test_abspath(self):
324
# The abspath is always relative to the chroot_url.
325
server = ChrootServer(get_transport('memory:///foo/bar/'))
327
transport = get_transport(server.get_url())
328
self.assertEqual(server.get_url(), transport.abspath('/'))
330
subdir_transport = transport.clone('subdir')
331
self.assertEqual(server.get_url(), subdir_transport.abspath('/'))
334
def test_clone(self):
335
server = ChrootServer(get_transport('memory:///foo/bar/'))
337
transport = get_transport(server.get_url())
338
# relpath from root and root path are the same
339
relpath_cloned = transport.clone('foo')
340
abspath_cloned = transport.clone('/foo')
341
self.assertEqual(server, relpath_cloned.server)
342
self.assertEqual(server, abspath_cloned.server)
345
def test_chroot_url_preserves_chroot(self):
346
"""Calling get_transport on a chroot transport's base should produce a
347
transport with exactly the same behaviour as the original chroot
350
This is so that it is not possible to escape a chroot by doing::
351
url = chroot_transport.base
352
parent_url = urlutils.join(url, '..')
353
new_transport = get_transport(parent_url)
355
server = ChrootServer(get_transport('memory:///path/subpath'))
357
transport = get_transport(server.get_url())
358
new_transport = get_transport(transport.base)
359
self.assertEqual(transport.server, new_transport.server)
360
self.assertEqual(transport.base, new_transport.base)
363
def test_urljoin_preserves_chroot(self):
364
"""Using urlutils.join(url, '..') on a chroot URL should not produce a
365
URL that escapes the intended chroot.
367
This is so that it is not possible to escape a chroot by doing::
368
url = chroot_transport.base
369
parent_url = urlutils.join(url, '..')
370
new_transport = get_transport(parent_url)
372
server = ChrootServer(get_transport('memory:///path/'))
374
transport = get_transport(server.get_url())
376
InvalidURLJoin, urlutils.join, transport.base, '..')
380
class ChrootServerTest(TestCase):
382
def test_construct(self):
383
backing_transport = MemoryTransport()
384
server = ChrootServer(backing_transport)
385
self.assertEqual(backing_transport, server.backing_transport)
387
def test_setUp(self):
388
backing_transport = MemoryTransport()
389
server = ChrootServer(backing_transport)
391
self.assertTrue(server.scheme in _get_protocol_handlers().keys())
393
def test_tearDown(self):
394
backing_transport = MemoryTransport()
395
server = ChrootServer(backing_transport)
398
self.assertFalse(server.scheme in _get_protocol_handlers().keys())
400
def test_get_url(self):
401
backing_transport = MemoryTransport()
402
server = ChrootServer(backing_transport)
404
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
408
287
class ReadonlyDecoratorTransportTest(TestCase):
409
288
"""Readonly decoration specific tests."""
552
431
super(TestTransportImplementation, self).setUp()
553
432
self._server = self.transport_server()
554
433
self._server.setUp()
555
self.addCleanup(self._server.tearDown)
557
def get_transport(self, relpath=None):
558
"""Return a connected transport to the local directory.
560
:param relpath: a path relative to the base url.
436
super(TestTransportImplementation, self).tearDown()
437
self._server.tearDown()
439
def get_transport(self):
440
"""Return a connected transport to the local directory."""
562
441
base_url = self._server.get_url()
563
url = self._adjust_url(base_url, relpath)
564
442
# try getting the transport via the regular interface:
565
t = get_transport(url)
443
t = get_transport(base_url)
566
444
if not isinstance(t, self.transport_class):
567
445
# we did not get the correct transport class type. Override the
568
446
# regular connection behaviour by direct construction.
569
t = self.transport_class(url)
447
t = self.transport_class(base_url)
590
468
t = get_transport(here_url)
591
469
self.assertIsInstance(t, LocalTransport)
592
470
self.assertEquals(t.base, here_url)
594
def test_local_abspath(self):
595
here = os.path.abspath('.')
596
t = get_transport(here)
597
self.assertEquals(t.local_abspath(''), here)
600
class TestWin32LocalTransport(TestCase):
602
def test_unc_clone_to_root(self):
603
# Win32 UNC path like \\HOST\path
604
# clone to root should stop at least at \\HOST part
606
t = EmulatedWin32LocalTransport('file://HOST/path/to/some/dir/')
609
self.assertEquals(t.base, 'file://HOST/')
610
# make sure we reach the root
612
self.assertEquals(t.base, 'file://HOST/')
615
def get_test_permutations():
616
"""Return transport permutations to be used in testing.
618
This module registers some transports, but they're only for testing
619
registration. We don't really want to run all the transport tests against