6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
1 |
# Copyright (C) 2011 Canonical Ltd
|
2 |
#
|
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
17 |
"""Tests for the branch open with specific URL policy code."""
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
18 |
|
19 |
from bzrlib import urlutils |
|
20 |
from bzrlib.branch import ( |
|
21 |
Branch, |
|
22 |
BranchReferenceFormat, |
|
23 |
)
|
|
24 |
from bzrlib.bzrdir import ( |
|
25 |
BzrProber, |
|
26 |
)
|
|
6472.2.2
by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places. |
27 |
from bzrlib.controldir import ( |
28 |
ControlDir, |
|
29 |
ControlDirFormat, |
|
30 |
)
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
31 |
from bzrlib.errors import NotBranchError |
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
32 |
from bzrlib.url_policy_open import ( |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
33 |
BadUrl, |
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
34 |
_BlacklistPolicy, |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
35 |
BranchLoopError, |
36 |
BranchReferenceForbidden, |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
37 |
open_only_scheme, |
38 |
BranchOpener, |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
39 |
WhitelistPolicy, |
40 |
)
|
|
41 |
from bzrlib.tests import ( |
|
42 |
TestCase, |
|
43 |
TestCaseWithTransport, |
|
44 |
)
|
|
45 |
from bzrlib.transport import chroot |
|
46 |
||
47 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
48 |
class TestBranchOpenerCheckAndFollowBranchReference(TestCase): |
49 |
"""Unit tests for `BranchOpener.check_and_follow_branch_reference`."""
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
50 |
|
51 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
52 |
super(TestBranchOpenerCheckAndFollowBranchReference, self).setUp() |
53 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
54 |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
55 |
class StubbedBranchOpener(BranchOpener): |
56 |
"""BranchOpener that provides canned answers.
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
57 |
|
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.
|
|
61 |
"""
|
|
62 |
||
63 |
def __init__(self, references, policy): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
64 |
parent_cls = TestBranchOpenerCheckAndFollowBranchReference |
65 |
super(parent_cls.StubbedBranchOpener, self).__init__(policy) |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
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 = [] |
|
70 |
||
71 |
def follow_reference(self, url): |
|
72 |
self.follow_reference_calls.append(url) |
|
73 |
return self._reference_values[url] |
|
74 |
||
75 |
def make_branch_opener(self, should_follow_references, references, |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
76 |
unsafe_urls=None): |
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
77 |
policy = _BlacklistPolicy(should_follow_references, unsafe_urls) |
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
78 |
opener = self.StubbedBranchOpener(references, policy) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
79 |
return opener |
80 |
||
81 |
def test_check_initial_url(self): |
|
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
82 |
# check_and_follow_branch_reference rejects all URLs that are not
|
83 |
# allowed.
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
84 |
opener = self.make_branch_opener(None, [], set(['a'])) |
85 |
self.assertRaises( |
|
86 |
BadUrl, opener.check_and_follow_branch_reference, 'a') |
|
87 |
||
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]) |
|
92 |
self.assertEquals( |
|
93 |
'a', opener.check_and_follow_branch_reference('a')) |
|
94 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
95 |
||
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
|
|
99 |
# branch reference.
|
|
100 |
opener = self.make_branch_opener(False, ['a', 'b']) |
|
101 |
self.assertRaises( |
|
102 |
BranchReferenceForbidden, |
|
103 |
opener.check_and_follow_branch_reference, 'a') |
|
104 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
105 |
||
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]) |
|
111 |
self.assertEquals( |
|
112 |
'b', opener.check_and_follow_branch_reference('a')) |
|
113 |
self.assertEquals(['a', 'b'], opener.follow_reference_calls) |
|
114 |
||
115 |
def test_check_referenced_urls(self): |
|
116 |
# check_and_follow_branch_reference checks if the URL a reference points
|
|
117 |
# to is safe.
|
|
118 |
opener = self.make_branch_opener( |
|
119 |
True, ['a', 'b', None], unsafe_urls=set('b')) |
|
120 |
self.assertRaises( |
|
121 |
BadUrl, opener.check_and_follow_branch_reference, 'a') |
|
122 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
123 |
||
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']) |
|
129 |
self.assertRaises( |
|
130 |
BranchLoopError, opener.check_and_follow_branch_reference, 'a') |
|
131 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
132 |
||
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) |
|
139 |
self.assertRaises( |
|
140 |
BranchLoopError, opener.check_and_follow_branch_reference, 'a') |
|
141 |
self.assertEquals(['a', 'b'], opener.follow_reference_calls) |
|
142 |
||
143 |
||
144 |
class TrackingProber(BzrProber): |
|
145 |
"""Subclass of BzrProber which tracks URLs it has been asked to open."""
|
|
146 |
||
147 |
seen_urls = [] |
|
148 |
||
149 |
@classmethod
|
|
150 |
def probe_transport(klass, transport): |
|
151 |
klass.seen_urls.append(transport.base) |
|
152 |
return BzrProber.probe_transport(transport) |
|
153 |
||
154 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
155 |
class TestBranchOpenerStacking(TestCaseWithTransport): |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
156 |
|
157 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
158 |
super(TestBranchOpenerStacking, self).setUp() |
159 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
160 |
|
161 |
def make_branch_opener(self, allowed_urls, probers=None): |
|
162 |
policy = WhitelistPolicy(True, allowed_urls, True) |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
163 |
return BranchOpener(policy, probers) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
164 |
|
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) |
|
172 |
||
173 |
def test_default_probers(self): |
|
174 |
# If no probers are specified to the constructor
|
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
175 |
# of BranchOpener, then a safe set will be used,
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
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
|
|
180 |
# tried.
|
|
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 = [] |
|
6472.2.2
by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places. |
187 |
# And make sure it's registered in such a way that ControlDir.open would
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
188 |
# use it.
|
6472.2.2
by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places. |
189 |
self.assertRaises(NotBranchError, ControlDir.open, ".") |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
190 |
self.assertEquals(1, len(TrackingProber.seen_urls)) |
191 |
||
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) |
|
202 |
||
203 |
def test_nstackable_repository(self): |
|
204 |
# treats branches with UnstackableRepositoryFormats as
|
|
205 |
# being not stacked.
|
|
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) |
|
210 |
||
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
|
|
220 |
# absolute URL.
|
|
221 |
self.assertNotEqual('../base-branch', stacked_on_branch.base) |
|
222 |
# This doesn't raise an exception.
|
|
223 |
opener.open(stacked_branch.base) |
|
224 |
||
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.
|
|
235 |
opener.open(c.base) |
|
236 |
||
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) |
|
245 |
||
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) |
|
256 |
||
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) |
|
267 |
||
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
|
|
271 |
# b stacked on a.
|
|
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) |
|
279 |
||
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) |
|
285 |
||
286 |
TrackingProber.seen_urls = [] |
|
287 |
opener = self.make_branch_opener( |
|
288 |
[a.base, b.base], probers=[TrackingProber]) |
|
289 |
opener.open(b.base) |
|
290 |
self.assertEquals( |
|
291 |
set(TrackingProber.seen_urls), set([b.base, a.base])) |
|
292 |
||
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]) |
|
301 |
opener.open(b.base) |
|
302 |
self.assertEquals( |
|
303 |
set(TrackingProber.seen_urls), set([b.base, a.base])) |
|
304 |
||
305 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
306 |
class TestOpenOnlyScheme(TestCaseWithTransport): |
307 |
"""Tests for `open_only_scheme`."""
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
308 |
|
309 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
310 |
super(TestOpenOnlyScheme, self).setUp() |
311 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
312 |
|
313 |
def test_hook_does_not_interfere(self): |
|
314 |
# The transform_fallback_location hook does not interfere with regular
|
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
315 |
# stacked branch access outside of open_only_scheme.
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
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') |
|
320 |
||
321 |
def get_chrooted_scheme(self, relpath): |
|
322 |
"""Create a server that is chrooted to `relpath`.
|
|
323 |
||
324 |
:return: ``(scheme, get_url)`` where ``scheme`` is the scheme of the
|
|
325 |
chroot server and ``get_url`` returns URLs on said server.
|
|
326 |
"""
|
|
327 |
transport = self.get_transport(relpath) |
|
328 |
chroot_server = chroot.ChrootServer(transport) |
|
329 |
chroot_server.start_server() |
|
330 |
self.addCleanup(chroot_server.stop_server) |
|
331 |
||
332 |
def get_url(relpath): |
|
6402.3.2
by Jelmer Vernooij
bzrify |
333 |
return chroot_server.get_url() + relpath |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
334 |
|
335 |
return urlutils.URL.from_string(chroot_server.get_url()).scheme, get_url |
|
336 |
||
337 |
def test_stacked_within_scheme(self): |
|
338 |
# A branch that is stacked on a URL of the same scheme is safe to
|
|
339 |
# open.
|
|
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')) |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
346 |
open_only_scheme(scheme, get_chrooted_url('stacked')) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
347 |
|
348 |
def test_stacked_outside_scheme(self): |
|
349 |
# A branch that is stacked on a URL that is not of the same scheme is
|
|
350 |
# not safe to open.
|
|
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')) |
|
358 |
self.assertRaises( |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
359 |
BadUrl, open_only_scheme, scheme, get_chrooted_url('stacked')) |