~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transport.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-03-28 06:58:22 UTC
  • mfrom: (2379.2.3 hpss-chroot)
  • Revision ID: pqm@pqm.ubuntu.com-20070328065822-999550a858a3ced3
(robertc) Fix chroot urls to not expose the url of the transport they are protecting, allowing regular url operations to work on them. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import bzrlib
24
24
from bzrlib import urlutils
25
 
from bzrlib.errors import (NoSuchFile, FileExists,
 
25
from bzrlib.errors import (ConnectionError,
 
26
                           DependencyNotPresent,
 
27
                           FileExists,
 
28
                           InvalidURLJoin,
 
29
                           NoSuchFile,
 
30
                           PathNotChild,
26
31
                           TransportNotPossible,
27
 
                           ConnectionError,
28
 
                           DependencyNotPresent,
29
32
                           UnsupportedProtocol,
30
 
                           PathNotChild,
31
33
                           )
32
34
from bzrlib.tests import TestCase, TestCaseInTempDir
33
35
from bzrlib.transport import (_CoalescedOffset,
34
36
                              _get_protocol_handlers,
35
37
                              _get_transport_modules,
36
38
                              get_transport,
 
39
                              _protocol_handlers,
37
40
                              register_lazy_transport,
38
41
                              _set_protocol_handlers,
39
42
                              Transport,
40
43
                              )
 
44
from bzrlib.transport.chroot import ChrootServer
41
45
from bzrlib.transport.memory import MemoryTransport
42
46
from bzrlib.transport.local import (LocalTransport,
43
47
                                    EmulatedWin32LocalTransport)
67
71
            _set_protocol_handlers(my_handlers)
68
72
            register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
69
73
            register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
70
 
            self.assertEqual([SampleHandler.__module__],
 
74
            self.assertEqual([SampleHandler.__module__, 'bzrlib.transport.chroot'],
71
75
                             _get_transport_modules())
72
76
        finally:
73
77
            _set_protocol_handlers(handlers)
289
293
class ChrootDecoratorTransportTest(TestCase):
290
294
    """Chroot decoration specific tests."""
291
295
 
 
296
    def test_abspath(self):
 
297
        # The abspath is always relative to the chroot_url.
 
298
        server = ChrootServer(get_transport('memory:///foo/bar/'))
 
299
        server.setUp()
 
300
        transport = get_transport(server.get_url())
 
301
        self.assertEqual(server.get_url(), transport.abspath('/'))
 
302
 
 
303
        subdir_transport = transport.clone('subdir')
 
304
        self.assertEqual(server.get_url(), subdir_transport.abspath('/'))
 
305
        server.tearDown()
 
306
 
 
307
    def test_clone(self):
 
308
        server = ChrootServer(get_transport('memory:///foo/bar/'))
 
309
        server.setUp()
 
310
        transport = get_transport(server.get_url())
 
311
        # relpath from root and root path are the same
 
312
        relpath_cloned = transport.clone('foo')
 
313
        abspath_cloned = transport.clone('/foo')
 
314
        self.assertEqual(server, relpath_cloned.server)
 
315
        self.assertEqual(server, abspath_cloned.server)
 
316
        server.tearDown()
 
317
    
 
318
    def test_chroot_url_preserves_chroot(self):
 
319
        """Calling get_transport on a chroot transport's base should produce a
 
320
        transport with exactly the same behaviour as the original chroot
 
321
        transport.
 
322
 
 
323
        This is so that it is not possible to escape a chroot by doing::
 
324
            url = chroot_transport.base
 
325
            parent_url = urlutils.join(url, '..')
 
326
            new_transport = get_transport(parent_url)
 
327
        """
 
328
        server = ChrootServer(get_transport('memory:///path/subpath'))
 
329
        server.setUp()
 
330
        transport = get_transport(server.get_url())
 
331
        new_transport = get_transport(transport.base)
 
332
        self.assertEqual(transport.server, new_transport.server)
 
333
        self.assertEqual(transport.base, new_transport.base)
 
334
        server.tearDown()
 
335
        
 
336
    def test_urljoin_preserves_chroot(self):
 
337
        """Using urlutils.join(url, '..') on a chroot URL should not produce a
 
338
        URL that escapes the intended chroot.
 
339
 
 
340
        This is so that it is not possible to escape a chroot by doing::
 
341
            url = chroot_transport.base
 
342
            parent_url = urlutils.join(url, '..')
 
343
            new_transport = get_transport(parent_url)
 
344
        """
 
345
        server = ChrootServer(get_transport('memory:///path/'))
 
346
        server.setUp()
 
347
        transport = get_transport(server.get_url())
 
348
        self.assertRaises(
 
349
            InvalidURLJoin, urlutils.join, transport.base, '..')
 
350
        server.tearDown()
 
351
 
 
352
 
 
353
class ChrootServerTest(TestCase):
 
354
 
292
355
    def test_construct(self):
293
 
        from bzrlib.transport import chroot
294
 
        transport = chroot.ChrootTransportDecorator('chroot+memory:///pathA/')
295
 
        self.assertEqual('memory:///pathA/', transport.chroot_url)
296
 
 
297
 
        transport = chroot.ChrootTransportDecorator(
298
 
            'chroot+memory:///path/B', chroot='memory:///path/')
299
 
        self.assertEqual('memory:///path/', transport.chroot_url)
300
 
 
301
 
    def test_append_file(self):
302
 
        transport = get_transport('chroot+memory:///foo/bar')
303
 
        self.assertRaises(PathNotChild, transport.append_file, '/foo', None)
304
 
 
305
 
    def test_append_bytes(self):
306
 
        transport = get_transport('chroot+memory:///foo/bar')
307
 
        self.assertRaises(PathNotChild, transport.append_bytes, '/foo', 'bytes')
308
 
 
309
 
    def test_clone(self):
310
 
        transport = get_transport('chroot+memory:///foo/bar')
311
 
        self.assertRaises(PathNotChild, transport.clone, '/foo')
312
 
 
313
 
    def test_delete(self):
314
 
        transport = get_transport('chroot+memory:///foo/bar')
315
 
        self.assertRaises(PathNotChild, transport.delete, '/foo')
316
 
 
317
 
    def test_delete_tree(self):
318
 
        transport = get_transport('chroot+memory:///foo/bar')
319
 
        self.assertRaises(PathNotChild, transport.delete_tree, '/foo')
320
 
 
321
 
    def test_get(self):
322
 
        transport = get_transport('chroot+memory:///foo/bar')
323
 
        self.assertRaises(PathNotChild, transport.get, '/foo')
324
 
 
325
 
    def test_get_bytes(self):
326
 
        transport = get_transport('chroot+memory:///foo/bar')
327
 
        self.assertRaises(PathNotChild, transport.get_bytes, '/foo')
328
 
 
329
 
    def test_has(self):
330
 
        transport = get_transport('chroot+memory:///foo/bar')
331
 
        self.assertRaises(PathNotChild, transport.has, '/foo')
332
 
 
333
 
    def test_list_dir(self):
334
 
        transport = get_transport('chroot+memory:///foo/bar')
335
 
        self.assertRaises(PathNotChild, transport.list_dir, '/foo')
336
 
 
337
 
    def test_lock_read(self):
338
 
        transport = get_transport('chroot+memory:///foo/bar')
339
 
        self.assertRaises(PathNotChild, transport.lock_read, '/foo')
340
 
 
341
 
    def test_lock_write(self):
342
 
        transport = get_transport('chroot+memory:///foo/bar')
343
 
        self.assertRaises(PathNotChild, transport.lock_write, '/foo')
344
 
 
345
 
    def test_mkdir(self):
346
 
        transport = get_transport('chroot+memory:///foo/bar')
347
 
        self.assertRaises(PathNotChild, transport.mkdir, '/foo')
348
 
 
349
 
    def test_put_bytes(self):
350
 
        transport = get_transport('chroot+memory:///foo/bar')
351
 
        self.assertRaises(PathNotChild, transport.put_bytes, '/foo', 'bytes')
352
 
 
353
 
    def test_put_file(self):
354
 
        transport = get_transport('chroot+memory:///foo/bar')
355
 
        self.assertRaises(PathNotChild, transport.put_file, '/foo', None)
356
 
 
357
 
    def test_rename(self):
358
 
        transport = get_transport('chroot+memory:///foo/bar')
359
 
        self.assertRaises(PathNotChild, transport.rename, '/aaa', 'bbb')
360
 
        self.assertRaises(PathNotChild, transport.rename, 'ccc', '/d')
361
 
 
362
 
    def test_rmdir(self):
363
 
        transport = get_transport('chroot+memory:///foo/bar')
364
 
        self.assertRaises(PathNotChild, transport.rmdir, '/foo')
365
 
 
366
 
    def test_stat(self):
367
 
        transport = get_transport('chroot+memory:///foo/bar')
368
 
        self.assertRaises(PathNotChild, transport.stat, '/foo')
 
356
        backing_transport = MemoryTransport()
 
357
        server = ChrootServer(backing_transport)
 
358
        self.assertEqual(backing_transport, server.backing_transport)
 
359
 
 
360
    def test_setUp(self):
 
361
        backing_transport = MemoryTransport()
 
362
        server = ChrootServer(backing_transport)
 
363
        server.setUp()
 
364
        self.assertTrue(server.scheme in _protocol_handlers.keys())
 
365
 
 
366
    def test_tearDown(self):
 
367
        backing_transport = MemoryTransport()
 
368
        server = ChrootServer(backing_transport)
 
369
        server.setUp()
 
370
        server.tearDown()
 
371
        self.assertFalse(server.scheme in _protocol_handlers.keys())
 
372
 
 
373
    def test_get_url(self):
 
374
        backing_transport = MemoryTransport()
 
375
        server = ChrootServer(backing_transport)
 
376
        server.setUp()
 
377
        self.assertEqual('chroot-%d:///' % id(server), server.get_url())
 
378
        server.tearDown()
369
379
 
370
380
 
371
381
class ReadonlyDecoratorTransportTest(TestCase):
515
525
        super(TestTransportImplementation, self).setUp()
516
526
        self._server = self.transport_server()
517
527
        self._server.setUp()
 
528
        self.addCleanup(self._server.tearDown)
518
529
 
519
 
    def tearDown(self):
520
 
        super(TestTransportImplementation, self).tearDown()
521
 
        self._server.tearDown()
522
 
        
523
530
    def get_transport(self):
524
531
        """Return a connected transport to the local directory."""
525
532
        base_url = self._server.get_url()