288
306
class ChrootDecoratorTransportTest(TestCase):
289
307
"""Chroot decoration specific tests."""
309
def test_abspath(self):
310
# The abspath is always relative to the chroot_url.
311
server = ChrootServer(get_transport('memory:///foo/bar/'))
313
transport = get_transport(server.get_url())
314
self.assertEqual(server.get_url(), transport.abspath('/'))
316
subdir_transport = transport.clone('subdir')
317
self.assertEqual(server.get_url(), subdir_transport.abspath('/'))
320
def test_clone(self):
321
server = ChrootServer(get_transport('memory:///foo/bar/'))
323
transport = get_transport(server.get_url())
324
# relpath from root and root path are the same
325
relpath_cloned = transport.clone('foo')
326
abspath_cloned = transport.clone('/foo')
327
self.assertEqual(server, relpath_cloned.server)
328
self.assertEqual(server, abspath_cloned.server)
331
def test_chroot_url_preserves_chroot(self):
332
"""Calling get_transport on a chroot transport's base should produce a
333
transport with exactly the same behaviour as the original chroot
336
This is so that it is not possible to escape a chroot by doing::
337
url = chroot_transport.base
338
parent_url = urlutils.join(url, '..')
339
new_transport = get_transport(parent_url)
341
server = ChrootServer(get_transport('memory:///path/subpath'))
343
transport = get_transport(server.get_url())
344
new_transport = get_transport(transport.base)
345
self.assertEqual(transport.server, new_transport.server)
346
self.assertEqual(transport.base, new_transport.base)
349
def test_urljoin_preserves_chroot(self):
350
"""Using urlutils.join(url, '..') on a chroot URL should not produce a
351
URL that escapes the intended chroot.
353
This is so that it is not possible to escape a chroot by doing::
354
url = chroot_transport.base
355
parent_url = urlutils.join(url, '..')
356
new_transport = get_transport(parent_url)
358
server = ChrootServer(get_transport('memory:///path/'))
360
transport = get_transport(server.get_url())
362
InvalidURLJoin, urlutils.join, transport.base, '..')
366
class ChrootServerTest(TestCase):
291
368
def test_construct(self):
292
from bzrlib.transport import chroot
293
transport = chroot.ChrootTransportDecorator('chroot+memory:///pathA/')
294
self.assertEqual('memory:///pathA/', transport.chroot_url)
296
transport = chroot.ChrootTransportDecorator(
297
'chroot+memory:///path/B', chroot='memory:///path/')
298
self.assertEqual('memory:///path/', transport.chroot_url)
300
def test_append_file(self):
301
transport = get_transport('chroot+memory:///foo/bar')
302
self.assertRaises(PathNotChild, transport.append_file, '/foo', None)
304
def test_append_bytes(self):
305
transport = get_transport('chroot+memory:///foo/bar')
306
self.assertRaises(PathNotChild, transport.append_bytes, '/foo', 'bytes')
308
def test_clone(self):
309
transport = get_transport('chroot+memory:///foo/bar')
310
self.assertRaises(PathNotChild, transport.clone, '/foo')
312
def test_delete(self):
313
transport = get_transport('chroot+memory:///foo/bar')
314
self.assertRaises(PathNotChild, transport.delete, '/foo')
316
def test_delete_tree(self):
317
transport = get_transport('chroot+memory:///foo/bar')
318
self.assertRaises(PathNotChild, transport.delete_tree, '/foo')
321
transport = get_transport('chroot+memory:///foo/bar')
322
self.assertRaises(PathNotChild, transport.get, '/foo')
324
def test_get_bytes(self):
325
transport = get_transport('chroot+memory:///foo/bar')
326
self.assertRaises(PathNotChild, transport.get_bytes, '/foo')
329
transport = get_transport('chroot+memory:///foo/bar')
330
self.assertRaises(PathNotChild, transport.has, '/foo')
332
def test_list_dir(self):
333
transport = get_transport('chroot+memory:///foo/bar')
334
self.assertRaises(PathNotChild, transport.list_dir, '/foo')
336
def test_lock_read(self):
337
transport = get_transport('chroot+memory:///foo/bar')
338
self.assertRaises(PathNotChild, transport.lock_read, '/foo')
340
def test_lock_write(self):
341
transport = get_transport('chroot+memory:///foo/bar')
342
self.assertRaises(PathNotChild, transport.lock_write, '/foo')
344
def test_mkdir(self):
345
transport = get_transport('chroot+memory:///foo/bar')
346
self.assertRaises(PathNotChild, transport.mkdir, '/foo')
348
def test_put_bytes(self):
349
transport = get_transport('chroot+memory:///foo/bar')
350
self.assertRaises(PathNotChild, transport.put_bytes, '/foo', 'bytes')
352
def test_put_file(self):
353
transport = get_transport('chroot+memory:///foo/bar')
354
self.assertRaises(PathNotChild, transport.put_file, '/foo', None)
356
def test_rename(self):
357
transport = get_transport('chroot+memory:///foo/bar')
358
self.assertRaises(PathNotChild, transport.rename, '/aaa', 'bbb')
359
self.assertRaises(PathNotChild, transport.rename, 'ccc', '/d')
361
def test_rmdir(self):
362
transport = get_transport('chroot+memory:///foo/bar')
363
self.assertRaises(PathNotChild, transport.rmdir, '/foo')
366
transport = get_transport('chroot+memory:///foo/bar')
367
self.assertRaises(PathNotChild, transport.stat, '/foo')
369
backing_transport = MemoryTransport()
370
server = ChrootServer(backing_transport)
371
self.assertEqual(backing_transport, server.backing_transport)
373
def test_setUp(self):
374
backing_transport = MemoryTransport()
375
server = ChrootServer(backing_transport)
377
self.assertTrue(server.scheme in _get_protocol_handlers().keys())
379
def test_tearDown(self):
380
backing_transport = MemoryTransport()
381
server = ChrootServer(backing_transport)
384
self.assertFalse(server.scheme in _get_protocol_handlers().keys())
386
def test_get_url(self):
387
backing_transport = MemoryTransport()
388
server = ChrootServer(backing_transport)
390
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
370
394
class ReadonlyDecoratorTransportTest(TestCase):