936
946
def _lock_broken(self, result):
937
947
self._lock_actions.append(('broken', result))
949
def permit_dir(self, name):
950
"""Permit a directory to be used by this test. See permit_url."""
951
name_transport = get_transport(name)
952
self.permit_url(name)
953
self.permit_url(name_transport.base)
955
def permit_url(self, url):
956
"""Declare that url is an ok url to use in this test.
958
Do this for memory transports, temporary test directory etc.
960
Do not do this for the current working directory, /tmp, or any other
961
preexisting non isolated url.
963
if not url.endswith('/'):
965
self._bzr_selftest_roots.append(url)
967
def permit_source_tree_branch_repo(self):
968
"""Permit the source tree bzr is running from to be opened.
970
Some code such as bzrlib.version attempts to read from the bzr branch
971
that bzr is executing from (if any). This method permits that directory
972
to be used in the test suite.
974
path = self.get_source_path()
975
self.record_directory_isolation()
978
workingtree.WorkingTree.open(path)
979
except (errors.NotBranchError, errors.NoWorkingTree):
982
self.enable_directory_isolation()
984
def _preopen_isolate_transport(self, transport):
985
"""Check that all transport openings are done in the test work area."""
986
if isinstance(transport, chroot.ChrootTransport):
987
# Unwrap chrooted transports
988
url = transport.server.backing_transport.clone(
989
transport._safe_relpath('.')).base
992
# ReadonlySmartTCPServer_for_testing decorates the backing transport
993
# urls it is given by prepending readonly+. This is appropriate as the
994
# client shouldn't know that the server is readonly (or not readonly).
995
# We could register all servers twice, with readonly+ prepending, but
996
# that makes for a long list; this is about the same but easier to
998
if url.startswith('readonly+'):
999
url = url[len('readonly+'):]
1000
self._preopen_isolate_url(url)
1002
def _preopen_isolate_url(self, url):
1003
if not self._directory_isolation:
1005
if self._directory_isolation == 'record':
1006
self._bzr_selftest_roots.append(url)
1008
# This prevents all transports, including e.g. sftp ones backed on disk
1009
# from working unless they are explicitly granted permission. We then
1010
# depend on the code that sets up test transports to check that they are
1011
# appropriately isolated and enable their use by calling
1012
# self.permit_transport()
1013
if not osutils.is_inside_any(self._bzr_selftest_roots, url):
1014
raise errors.BzrError("Attempt to escape test isolation: %r %r"
1015
% (url, self._bzr_selftest_roots))
1017
def record_directory_isolation(self):
1018
"""Gather accessed directories to permit later access.
1020
This is used for tests that access the branch bzr is running from.
1022
self._directory_isolation = "record"
939
1024
def start_server(self, transport_server, backing_server=None):
940
1025
"""Start transport_server for this test.
948
1033
transport_server.setUp(backing_server)
949
1034
self.addCleanup(transport_server.tearDown)
1035
# Obtain a real transport because if the server supplies a password, it
1036
# will be hidden from the base on the client side.
1037
t = get_transport(transport_server.get_url())
1038
# Some transport servers effectively chroot the backing transport;
1039
# others like SFTPServer don't - users of the transport can walk up the
1040
# transport to read the entire backing transport. This wouldn't matter
1041
# except that the workdir tests are given - and that they expect the
1042
# server's url to point at - is one directory under the safety net. So
1043
# Branch operations into the transport will attempt to walk up one
1044
# directory. Chrooting all servers would avoid this but also mean that
1045
# we wouldn't be testing directly against non-root urls. Alternatively
1046
# getting the test framework to start the server with a backing server
1047
# at the actual safety net directory would work too, but this then
1048
# means that the self.get_url/self.get_transport methods would need
1049
# to transform all their results. On balance its cleaner to handle it
1050
# here, and permit a higher url when we have one of these transports.
1051
if t.base.endswith('/work/'):
1052
# we have safety net/test root/work
1053
t = t.clone('../..')
1054
elif isinstance(transport_server, server.SmartTCPServer_for_testing):
1055
# The smart server adds a path similar to work, which is traversed
1056
# up from by the client. But the server is chrooted - the actual
1057
# backing transport is not escaped from, and VFS requests to the
1058
# root will error (because they try to escape the chroot).
1060
while t2.base != t.base:
1063
self.permit_url(t.base)
1065
def _track_transports(self):
1066
"""Install checks for transport usage."""
1067
# TestCase has no safe place it can write to.
1068
self._bzr_selftest_roots = []
1069
# Currently the easiest way to be sure that nothing is going on is to
1070
# hook into bzr dir opening. This leaves a small window of error for
1071
# transport tests, but they are well known, and we can improve on this
1073
bzrdir.BzrDir.hooks.install_named_hook("pre_open",
1074
self._preopen_isolate_transport, "Check bzr directories are safe.")
951
1076
def _ndiff_strings(self, a, b):
952
1077
"""Return ndiff between two strings containing lines.
1871
1996
return Popen(*args, **kwargs)
1998
def get_source_path(self):
1999
"""Return the path of the directory containing bzrlib."""
2000
return os.path.dirname(os.path.dirname(bzrlib.__file__))
1873
2002
def get_bzr_path(self):
1874
2003
"""Return the path of the 'bzr' executable for this test suite."""
1875
bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
2004
bzr_path = self.get_source_path()+'/bzr'
1876
2005
if not os.path.isfile(bzr_path):
1877
2006
# We are probably installed. Assume sys.argv is the right file
1878
2007
bzr_path = sys.argv[0]
2376
2508
if os.path.exists(name):
2377
2509
name = name_prefix + '_' + str(i)
2511
# now create test and home directories within this dir
2512
self.test_base_dir = name
2513
self.addCleanup(self.deleteTestDir)
2514
os.mkdir(self.test_base_dir)
2381
# now create test and home directories within this dir
2382
self.test_base_dir = name
2516
self.permit_dir(self.test_base_dir)
2517
# 'sprouting' and 'init' of a branch both walk up the tree to find
2518
# stacking policy to honour; create a bzr dir with an unshared
2519
# repository (but not a branch - our code would be trying to escape
2520
# then!) to stop them, and permit it to be read.
2521
# control = bzrdir.BzrDir.create(self.test_base_dir)
2522
# control.create_repository()
2383
2523
self.test_home_dir = self.test_base_dir + '/home'
2384
2524
os.mkdir(self.test_home_dir)
2385
2525
self.test_dir = self.test_base_dir + '/work'