1
# Copyright (C) 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the branch open with specific URL policy code."""
19
from bzrlib import urlutils
20
from bzrlib.branch import (
22
BranchReferenceFormat,
24
from bzrlib.bzrdir import (
27
from bzrlib.controldir import (
31
from bzrlib.errors import NotBranchError
32
from bzrlib.url_policy_open import (
36
BranchReferenceForbidden,
41
from bzrlib.tests import (
43
TestCaseWithTransport,
45
from bzrlib.transport import chroot
48
class TestBranchOpenerCheckAndFollowBranchReference(TestCase):
49
"""Unit tests for `BranchOpener.check_and_follow_branch_reference`."""
52
super(TestBranchOpenerCheckAndFollowBranchReference, self).setUp()
53
BranchOpener.install_hook()
55
class StubbedBranchOpener(BranchOpener):
56
"""BranchOpener that provides canned answers.
58
We implement the methods we need to to be able to control all the
59
inputs to the `follow_reference` method, which is what is
60
being tested in this class.
63
def __init__(self, references, policy):
64
parent_cls = TestBranchOpenerCheckAndFollowBranchReference
65
super(parent_cls.StubbedBranchOpener, self).__init__(policy)
66
self._reference_values = {}
67
for i in range(len(references) - 1):
68
self._reference_values[references[i]] = references[i + 1]
69
self.follow_reference_calls = []
71
def follow_reference(self, url):
72
self.follow_reference_calls.append(url)
73
return self._reference_values[url]
75
def make_branch_opener(self, should_follow_references, references,
77
policy = _BlacklistPolicy(should_follow_references, unsafe_urls)
78
opener = self.StubbedBranchOpener(references, policy)
81
def test_check_initial_url(self):
82
# check_and_follow_branch_reference rejects all URLs that are not
84
opener = self.make_branch_opener(None, [], set(['a']))
86
BadUrl, opener.check_and_follow_branch_reference, 'a')
88
def test_not_reference(self):
89
# When branch references are forbidden, check_and_follow_branch_reference
90
# does not raise on non-references.
91
opener = self.make_branch_opener(False, ['a', None])
93
'a', opener.check_and_follow_branch_reference('a'))
94
self.assertEquals(['a'], opener.follow_reference_calls)
96
def test_branch_reference_forbidden(self):
97
# check_and_follow_branch_reference raises BranchReferenceForbidden if
98
# branch references are forbidden and the source URL points to a
100
opener = self.make_branch_opener(False, ['a', 'b'])
102
BranchReferenceForbidden,
103
opener.check_and_follow_branch_reference, 'a')
104
self.assertEquals(['a'], opener.follow_reference_calls)
106
def test_allowed_reference(self):
107
# check_and_follow_branch_reference does not raise if following references
108
# is allowed and the source URL points to a branch reference to a
109
# permitted location.
110
opener = self.make_branch_opener(True, ['a', 'b', None])
112
'b', opener.check_and_follow_branch_reference('a'))
113
self.assertEquals(['a', 'b'], opener.follow_reference_calls)
115
def test_check_referenced_urls(self):
116
# check_and_follow_branch_reference checks if the URL a reference points
118
opener = self.make_branch_opener(
119
True, ['a', 'b', None], unsafe_urls=set('b'))
121
BadUrl, opener.check_and_follow_branch_reference, 'a')
122
self.assertEquals(['a'], opener.follow_reference_calls)
124
def test_self_referencing_branch(self):
125
# check_and_follow_branch_reference raises BranchReferenceLoopError if
126
# following references is allowed and the source url points to a
127
# self-referencing branch reference.
128
opener = self.make_branch_opener(True, ['a', 'a'])
130
BranchLoopError, opener.check_and_follow_branch_reference, 'a')
131
self.assertEquals(['a'], opener.follow_reference_calls)
133
def test_branch_reference_loop(self):
134
# check_and_follow_branch_reference raises BranchReferenceLoopError if
135
# following references is allowed and the source url points to a loop
136
# of branch references.
137
references = ['a', 'b', 'a']
138
opener = self.make_branch_opener(True, references)
140
BranchLoopError, opener.check_and_follow_branch_reference, 'a')
141
self.assertEquals(['a', 'b'], opener.follow_reference_calls)
144
class TrackingProber(BzrProber):
145
"""Subclass of BzrProber which tracks URLs it has been asked to open."""
150
def probe_transport(klass, transport):
151
klass.seen_urls.append(transport.base)
152
return BzrProber.probe_transport(transport)
155
class TestBranchOpenerStacking(TestCaseWithTransport):
158
super(TestBranchOpenerStacking, self).setUp()
159
BranchOpener.install_hook()
161
def make_branch_opener(self, allowed_urls, probers=None):
162
policy = WhitelistPolicy(True, allowed_urls, True)
163
return BranchOpener(policy, probers)
165
def test_probers(self):
166
# Only the specified probers should be used
167
b = self.make_branch('branch')
168
opener = self.make_branch_opener([b.base], probers=[])
169
self.assertRaises(NotBranchError, opener.open, b.base)
170
opener = self.make_branch_opener([b.base], probers=[BzrProber])
171
self.assertEquals(b.base, opener.open(b.base).base)
173
def test_default_probers(self):
174
# If no probers are specified to the constructor
175
# of BranchOpener, then a safe set will be used,
176
# rather than all probers registered in bzr.
177
self.addCleanup(ControlDirFormat.unregister_prober, TrackingProber)
178
ControlDirFormat.register_prober(TrackingProber)
179
# Open a location without any branches, so that all probers are
181
# First, check that the TrackingProber tracks correctly.
182
TrackingProber.seen_urls = []
183
opener = self.make_branch_opener(["."], probers=[TrackingProber])
184
self.assertRaises(NotBranchError, opener.open, ".")
185
self.assertEquals(1, len(TrackingProber.seen_urls))
186
TrackingProber.seen_urls = []
187
# And make sure it's registered in such a way that ControlDir.open would
189
self.assertRaises(NotBranchError, ControlDir.open, ".")
190
self.assertEquals(1, len(TrackingProber.seen_urls))
192
def test_allowed_url(self):
193
# the opener does not raise an exception for branches stacked on
194
# branches with allowed URLs.
195
stacked_on_branch = self.make_branch('base-branch', format='1.6')
196
stacked_branch = self.make_branch('stacked-branch', format='1.6')
197
stacked_branch.set_stacked_on_url(stacked_on_branch.base)
198
opener = self.make_branch_opener(
199
[stacked_branch.base, stacked_on_branch.base])
200
# This doesn't raise an exception.
201
opener.open(stacked_branch.base)
203
def test_nstackable_repository(self):
204
# treats branches with UnstackableRepositoryFormats as
206
branch = self.make_branch('unstacked', format='knit')
207
opener = self.make_branch_opener([branch.base])
208
# This doesn't raise an exception.
209
opener.open(branch.base)
211
def test_allowed_relative_url(self):
212
# passes on absolute urls to check_one_url, even if the
213
# value of stacked_on_location in the config is set to a relative URL.
214
stacked_on_branch = self.make_branch('base-branch', format='1.6')
215
stacked_branch = self.make_branch('stacked-branch', format='1.6')
216
stacked_branch.set_stacked_on_url('../base-branch')
217
opener = self.make_branch_opener(
218
[stacked_branch.base, stacked_on_branch.base])
219
# Note that stacked_on_branch.base is not '../base-branch', it's an
221
self.assertNotEqual('../base-branch', stacked_on_branch.base)
222
# This doesn't raise an exception.
223
opener.open(stacked_branch.base)
225
def test_allowed_relative_nested(self):
226
# Relative URLs are resolved relative to the stacked branch.
227
self.get_transport().mkdir('subdir')
228
a = self.make_branch('subdir/a', format='1.6')
229
b = self.make_branch('b', format='1.6')
230
b.set_stacked_on_url('../subdir/a')
231
c = self.make_branch('subdir/c', format='1.6')
232
c.set_stacked_on_url('../../b')
233
opener = self.make_branch_opener([c.base, b.base, a.base])
234
# This doesn't raise an exception.
237
def test_forbidden_url(self):
238
# raises a BadUrl exception if a branch is stacked on a
239
# branch with a forbidden URL.
240
stacked_on_branch = self.make_branch('base-branch', format='1.6')
241
stacked_branch = self.make_branch('stacked-branch', format='1.6')
242
stacked_branch.set_stacked_on_url(stacked_on_branch.base)
243
opener = self.make_branch_opener([stacked_branch.base])
244
self.assertRaises(BadUrl, opener.open, stacked_branch.base)
246
def test_forbidden_url_nested(self):
247
# raises a BadUrl exception if a branch is stacked on a
248
# branch that is in turn stacked on a branch with a forbidden URL.
249
a = self.make_branch('a', format='1.6')
250
b = self.make_branch('b', format='1.6')
251
b.set_stacked_on_url(a.base)
252
c = self.make_branch('c', format='1.6')
253
c.set_stacked_on_url(b.base)
254
opener = self.make_branch_opener([c.base, b.base])
255
self.assertRaises(BadUrl, opener.open, c.base)
257
def test_self_stacked_branch(self):
258
# raises StackingLoopError if a branch is stacked on
259
# itself. This avoids infinite recursion errors.
260
a = self.make_branch('a', format='1.6')
261
# Bazaar 1.17 and up make it harder to create branches like this.
262
# It's still worth testing that we don't blow up in the face of them,
263
# so we grovel around a bit to create one anyway.
264
a.get_config().set_user_option('stacked_on_location', a.base)
265
opener = self.make_branch_opener([a.base])
266
self.assertRaises(BranchLoopError, opener.open, a.base)
268
def test_loop_stacked_branch(self):
269
# raises StackingLoopError if a branch is stacked in such
270
# a way so that it is ultimately stacked on itself. e.g. a stacked on
272
a = self.make_branch('a', format='1.6')
273
b = self.make_branch('b', format='1.6')
274
a.set_stacked_on_url(b.base)
275
b.set_stacked_on_url(a.base)
276
opener = self.make_branch_opener([a.base, b.base])
277
self.assertRaises(BranchLoopError, opener.open, a.base)
278
self.assertRaises(BranchLoopError, opener.open, b.base)
280
def test_custom_opener(self):
281
# A custom function for opening a control dir can be specified.
282
a = self.make_branch('a', format='2a')
283
b = self.make_branch('b', format='2a')
284
b.set_stacked_on_url(a.base)
286
TrackingProber.seen_urls = []
287
opener = self.make_branch_opener(
288
[a.base, b.base], probers=[TrackingProber])
291
set(TrackingProber.seen_urls), set([b.base, a.base]))
293
def test_custom_opener_with_branch_reference(self):
294
# A custom function for opening a control dir can be specified.
295
a = self.make_branch('a', format='2a')
296
b_dir = self.make_bzrdir('b')
297
b = BranchReferenceFormat().initialize(b_dir, target_branch=a)
298
TrackingProber.seen_urls = []
299
opener = self.make_branch_opener(
300
[a.base, b.base], probers=[TrackingProber])
303
set(TrackingProber.seen_urls), set([b.base, a.base]))
306
class TestOpenOnlyScheme(TestCaseWithTransport):
307
"""Tests for `open_only_scheme`."""
310
super(TestOpenOnlyScheme, self).setUp()
311
BranchOpener.install_hook()
313
def test_hook_does_not_interfere(self):
314
# The transform_fallback_location hook does not interfere with regular
315
# stacked branch access outside of open_only_scheme.
316
self.make_branch('stacked')
317
self.make_branch('stacked-on')
318
Branch.open('stacked').set_stacked_on_url('../stacked-on')
319
Branch.open('stacked')
321
def get_chrooted_scheme(self, relpath):
322
"""Create a server that is chrooted to `relpath`.
324
:return: ``(scheme, get_url)`` where ``scheme`` is the scheme of the
325
chroot server and ``get_url`` returns URLs on said server.
327
transport = self.get_transport(relpath)
328
chroot_server = chroot.ChrootServer(transport)
329
chroot_server.start_server()
330
self.addCleanup(chroot_server.stop_server)
332
def get_url(relpath):
333
return chroot_server.get_url() + relpath
335
return urlutils.URL.from_string(chroot_server.get_url()).scheme, get_url
337
def test_stacked_within_scheme(self):
338
# A branch that is stacked on a URL of the same scheme is safe to
340
self.get_transport().mkdir('inside')
341
self.make_branch('inside/stacked')
342
self.make_branch('inside/stacked-on')
343
scheme, get_chrooted_url = self.get_chrooted_scheme('inside')
344
Branch.open(get_chrooted_url('stacked')).set_stacked_on_url(
345
get_chrooted_url('stacked-on'))
346
open_only_scheme(scheme, get_chrooted_url('stacked'))
348
def test_stacked_outside_scheme(self):
349
# A branch that is stacked on a URL that is not of the same scheme is
351
self.get_transport().mkdir('inside')
352
self.get_transport().mkdir('outside')
353
self.make_branch('inside/stacked')
354
self.make_branch('outside/stacked-on')
355
scheme, get_chrooted_url = self.get_chrooted_scheme('inside')
356
Branch.open(get_chrooted_url('stacked')).set_stacked_on_url(
357
self.get_url('outside/stacked-on'))
359
BadUrl, open_only_scheme, scheme, get_chrooted_url('stacked'))