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
while isinstance(transport, pathfilter.PathFilteringTransport):
987
# Unwrap pathfiltered transports
988
transport = transport.server.backing_transport.clone(
989
transport._filter('.'))
991
# ReadonlySmartTCPServer_for_testing decorates the backing transport
992
# urls it is given by prepending readonly+. This is appropriate as the
993
# client shouldn't know that the server is readonly (or not readonly).
994
# We could register all servers twice, with readonly+ prepending, but
995
# that makes for a long list; this is about the same but easier to
997
if url.startswith('readonly+'):
998
url = url[len('readonly+'):]
999
self._preopen_isolate_url(url)
1001
def _preopen_isolate_url(self, url):
1002
if not self._directory_isolation:
1004
if self._directory_isolation == 'record':
1005
self._bzr_selftest_roots.append(url)
1007
# This prevents all transports, including e.g. sftp ones backed on disk
1008
# from working unless they are explicitly granted permission. We then
1009
# depend on the code that sets up test transports to check that they are
1010
# appropriately isolated and enable their use by calling
1011
# self.permit_transport()
1012
if not osutils.is_inside_any(self._bzr_selftest_roots, url):
1013
raise errors.BzrError("Attempt to escape test isolation: %r %r"
1014
% (url, self._bzr_selftest_roots))
1016
def record_directory_isolation(self):
1017
"""Gather accessed directories to permit later access.
1019
This is used for tests that access the branch bzr is running from.
1021
self._directory_isolation = "record"
939
1023
def start_server(self, transport_server, backing_server=None):
940
1024
"""Start transport_server for this test.
948
1032
transport_server.setUp(backing_server)
949
1033
self.addCleanup(transport_server.tearDown)
1034
# Obtain a real transport because if the server supplies a password, it
1035
# will be hidden from the base on the client side.
1036
t = get_transport(transport_server.get_url())
1037
# Some transport servers effectively chroot the backing transport;
1038
# others like SFTPServer don't - users of the transport can walk up the
1039
# transport to read the entire backing transport. This wouldn't matter
1040
# except that the workdir tests are given - and that they expect the
1041
# server's url to point at - is one directory under the safety net. So
1042
# Branch operations into the transport will attempt to walk up one
1043
# directory. Chrooting all servers would avoid this but also mean that
1044
# we wouldn't be testing directly against non-root urls. Alternatively
1045
# getting the test framework to start the server with a backing server
1046
# at the actual safety net directory would work too, but this then
1047
# means that the self.get_url/self.get_transport methods would need
1048
# to transform all their results. On balance its cleaner to handle it
1049
# here, and permit a higher url when we have one of these transports.
1050
if t.base.endswith('/work/'):
1051
# we have safety net/test root/work
1052
t = t.clone('../..')
1053
elif isinstance(transport_server, server.SmartTCPServer_for_testing):
1054
# The smart server adds a path similar to work, which is traversed
1055
# up from by the client. But the server is chrooted - the actual
1056
# backing transport is not escaped from, and VFS requests to the
1057
# root will error (because they try to escape the chroot).
1059
while t2.base != t.base:
1062
self.permit_url(t.base)
1064
def _track_transports(self):
1065
"""Install checks for transport usage."""
1066
# TestCase has no safe place it can write to.
1067
self._bzr_selftest_roots = []
1068
# Currently the easiest way to be sure that nothing is going on is to
1069
# hook into bzr dir opening. This leaves a small window of error for
1070
# transport tests, but they are well known, and we can improve on this
1072
bzrdir.BzrDir.hooks.install_named_hook("pre_open",
1073
self._preopen_isolate_transport, "Check bzr directories are safe.")
951
1075
def _ndiff_strings(self, a, b):
952
1076
"""Return ndiff between two strings containing lines.
1871
1995
return Popen(*args, **kwargs)
1997
def get_source_path(self):
1998
"""Return the path of the directory containing bzrlib."""
1999
return os.path.dirname(os.path.dirname(bzrlib.__file__))
1873
2001
def get_bzr_path(self):
1874
2002
"""Return the path of the 'bzr' executable for this test suite."""
1875
bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
2003
bzr_path = self.get_source_path()+'/bzr'
1876
2004
if not os.path.isfile(bzr_path):
1877
2005
# We are probably installed. Assume sys.argv is the right file
1878
2006
bzr_path = sys.argv[0]
2376
2507
if os.path.exists(name):
2377
2508
name = name_prefix + '_' + str(i)
2510
# now create test and home directories within this dir
2511
self.test_base_dir = name
2512
self.addCleanup(self.deleteTestDir)
2513
os.mkdir(self.test_base_dir)
2381
# now create test and home directories within this dir
2382
self.test_base_dir = name
2515
self.permit_dir(self.test_base_dir)
2516
# 'sprouting' and 'init' of a branch both walk up the tree to find
2517
# stacking policy to honour; create a bzr dir with an unshared
2518
# repository (but not a branch - our code would be trying to escape
2519
# then!) to stop them, and permit it to be read.
2520
# control = bzrdir.BzrDir.create(self.test_base_dir)
2521
# control.create_repository()
2383
2522
self.test_home_dir = self.test_base_dir + '/home'
2384
2523
os.mkdir(self.test_home_dir)
2385
2524
self.test_dir = self.test_base_dir + '/work'