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 |
BzrDir, |
|
26 |
BzrProber, |
|
27 |
)
|
|
28 |
from bzrlib.controldir import ControlDirFormat |
|
29 |
from bzrlib.errors import NotBranchError |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
30 |
from bzrlib.url_policy_open import ( |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
31 |
BadUrl, |
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
32 |
_BlacklistPolicy, |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
33 |
BranchLoopError, |
34 |
BranchReferenceForbidden, |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
35 |
open_only_scheme, |
36 |
BranchOpener, |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
37 |
WhitelistPolicy, |
38 |
)
|
|
39 |
from bzrlib.tests import ( |
|
40 |
TestCase, |
|
41 |
TestCaseWithTransport, |
|
42 |
)
|
|
43 |
from bzrlib.transport import chroot |
|
44 |
||
45 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
46 |
class TestBranchOpenerCheckAndFollowBranchReference(TestCase): |
47 |
"""Unit tests for `BranchOpener.check_and_follow_branch_reference`."""
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
48 |
|
49 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
50 |
super(TestBranchOpenerCheckAndFollowBranchReference, self).setUp() |
51 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
52 |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
53 |
class StubbedBranchOpener(BranchOpener): |
54 |
"""BranchOpener that provides canned answers.
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
55 |
|
56 |
We implement the methods we need to to be able to control all the
|
|
57 |
inputs to the `follow_reference` method, which is what is
|
|
58 |
being tested in this class.
|
|
59 |
"""
|
|
60 |
||
61 |
def __init__(self, references, policy): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
62 |
parent_cls = TestBranchOpenerCheckAndFollowBranchReference |
63 |
super(parent_cls.StubbedBranchOpener, self).__init__(policy) |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
64 |
self._reference_values = {} |
65 |
for i in range(len(references) - 1): |
|
66 |
self._reference_values[references[i]] = references[i + 1] |
|
67 |
self.follow_reference_calls = [] |
|
68 |
||
69 |
def follow_reference(self, url): |
|
70 |
self.follow_reference_calls.append(url) |
|
71 |
return self._reference_values[url] |
|
72 |
||
73 |
def make_branch_opener(self, should_follow_references, references, |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
74 |
unsafe_urls=None): |
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
75 |
policy = _BlacklistPolicy(should_follow_references, unsafe_urls) |
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
76 |
opener = self.StubbedBranchOpener(references, policy) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
77 |
return opener |
78 |
||
79 |
def test_check_initial_url(self): |
|
6402.3.8
by Jelmer Vernooij
make BlacklistPolicy private |
80 |
# check_and_follow_branch_reference rejects all URLs that are not
|
81 |
# allowed.
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
82 |
opener = self.make_branch_opener(None, [], set(['a'])) |
83 |
self.assertRaises( |
|
84 |
BadUrl, opener.check_and_follow_branch_reference, 'a') |
|
85 |
||
86 |
def test_not_reference(self): |
|
87 |
# When branch references are forbidden, check_and_follow_branch_reference
|
|
88 |
# does not raise on non-references.
|
|
89 |
opener = self.make_branch_opener(False, ['a', None]) |
|
90 |
self.assertEquals( |
|
91 |
'a', opener.check_and_follow_branch_reference('a')) |
|
92 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
93 |
||
94 |
def test_branch_reference_forbidden(self): |
|
95 |
# check_and_follow_branch_reference raises BranchReferenceForbidden if
|
|
96 |
# branch references are forbidden and the source URL points to a
|
|
97 |
# branch reference.
|
|
98 |
opener = self.make_branch_opener(False, ['a', 'b']) |
|
99 |
self.assertRaises( |
|
100 |
BranchReferenceForbidden, |
|
101 |
opener.check_and_follow_branch_reference, 'a') |
|
102 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
103 |
||
104 |
def test_allowed_reference(self): |
|
105 |
# check_and_follow_branch_reference does not raise if following references
|
|
106 |
# is allowed and the source URL points to a branch reference to a
|
|
107 |
# permitted location.
|
|
108 |
opener = self.make_branch_opener(True, ['a', 'b', None]) |
|
109 |
self.assertEquals( |
|
110 |
'b', opener.check_and_follow_branch_reference('a')) |
|
111 |
self.assertEquals(['a', 'b'], opener.follow_reference_calls) |
|
112 |
||
113 |
def test_check_referenced_urls(self): |
|
114 |
# check_and_follow_branch_reference checks if the URL a reference points
|
|
115 |
# to is safe.
|
|
116 |
opener = self.make_branch_opener( |
|
117 |
True, ['a', 'b', None], unsafe_urls=set('b')) |
|
118 |
self.assertRaises( |
|
119 |
BadUrl, opener.check_and_follow_branch_reference, 'a') |
|
120 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
121 |
||
122 |
def test_self_referencing_branch(self): |
|
123 |
# check_and_follow_branch_reference raises BranchReferenceLoopError if
|
|
124 |
# following references is allowed and the source url points to a
|
|
125 |
# self-referencing branch reference.
|
|
126 |
opener = self.make_branch_opener(True, ['a', 'a']) |
|
127 |
self.assertRaises( |
|
128 |
BranchLoopError, opener.check_and_follow_branch_reference, 'a') |
|
129 |
self.assertEquals(['a'], opener.follow_reference_calls) |
|
130 |
||
131 |
def test_branch_reference_loop(self): |
|
132 |
# check_and_follow_branch_reference raises BranchReferenceLoopError if
|
|
133 |
# following references is allowed and the source url points to a loop
|
|
134 |
# of branch references.
|
|
135 |
references = ['a', 'b', 'a'] |
|
136 |
opener = self.make_branch_opener(True, references) |
|
137 |
self.assertRaises( |
|
138 |
BranchLoopError, opener.check_and_follow_branch_reference, 'a') |
|
139 |
self.assertEquals(['a', 'b'], opener.follow_reference_calls) |
|
140 |
||
141 |
||
142 |
class TrackingProber(BzrProber): |
|
143 |
"""Subclass of BzrProber which tracks URLs it has been asked to open."""
|
|
144 |
||
145 |
seen_urls = [] |
|
146 |
||
147 |
@classmethod
|
|
148 |
def probe_transport(klass, transport): |
|
149 |
klass.seen_urls.append(transport.base) |
|
150 |
return BzrProber.probe_transport(transport) |
|
151 |
||
152 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
153 |
class TestBranchOpenerStacking(TestCaseWithTransport): |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
154 |
|
155 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
156 |
super(TestBranchOpenerStacking, self).setUp() |
157 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
158 |
|
159 |
def make_branch_opener(self, allowed_urls, probers=None): |
|
160 |
policy = WhitelistPolicy(True, allowed_urls, True) |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
161 |
return BranchOpener(policy, probers) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
162 |
|
163 |
def test_probers(self): |
|
164 |
# Only the specified probers should be used
|
|
165 |
b = self.make_branch('branch') |
|
166 |
opener = self.make_branch_opener([b.base], probers=[]) |
|
167 |
self.assertRaises(NotBranchError, opener.open, b.base) |
|
168 |
opener = self.make_branch_opener([b.base], probers=[BzrProber]) |
|
169 |
self.assertEquals(b.base, opener.open(b.base).base) |
|
170 |
||
171 |
def test_default_probers(self): |
|
172 |
# If no probers are specified to the constructor
|
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
173 |
# of BranchOpener, then a safe set will be used,
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
174 |
# rather than all probers registered in bzr.
|
175 |
self.addCleanup(ControlDirFormat.unregister_prober, TrackingProber) |
|
176 |
ControlDirFormat.register_prober(TrackingProber) |
|
177 |
# Open a location without any branches, so that all probers are
|
|
178 |
# tried.
|
|
179 |
# First, check that the TrackingProber tracks correctly.
|
|
180 |
TrackingProber.seen_urls = [] |
|
181 |
opener = self.make_branch_opener(["."], probers=[TrackingProber]) |
|
182 |
self.assertRaises(NotBranchError, opener.open, ".") |
|
183 |
self.assertEquals(1, len(TrackingProber.seen_urls)) |
|
184 |
TrackingProber.seen_urls = [] |
|
185 |
# And make sure it's registered in such a way that BzrDir.open would
|
|
186 |
# use it.
|
|
187 |
self.assertRaises(NotBranchError, BzrDir.open, ".") |
|
188 |
self.assertEquals(1, len(TrackingProber.seen_urls)) |
|
189 |
||
190 |
def test_allowed_url(self): |
|
191 |
# the opener does not raise an exception for branches stacked on
|
|
192 |
# branches with allowed URLs.
|
|
193 |
stacked_on_branch = self.make_branch('base-branch', format='1.6') |
|
194 |
stacked_branch = self.make_branch('stacked-branch', format='1.6') |
|
195 |
stacked_branch.set_stacked_on_url(stacked_on_branch.base) |
|
196 |
opener = self.make_branch_opener( |
|
197 |
[stacked_branch.base, stacked_on_branch.base]) |
|
198 |
# This doesn't raise an exception.
|
|
199 |
opener.open(stacked_branch.base) |
|
200 |
||
201 |
def test_nstackable_repository(self): |
|
202 |
# treats branches with UnstackableRepositoryFormats as
|
|
203 |
# being not stacked.
|
|
204 |
branch = self.make_branch('unstacked', format='knit') |
|
205 |
opener = self.make_branch_opener([branch.base]) |
|
206 |
# This doesn't raise an exception.
|
|
207 |
opener.open(branch.base) |
|
208 |
||
209 |
def test_allowed_relative_url(self): |
|
210 |
# passes on absolute urls to check_one_url, even if the
|
|
211 |
# value of stacked_on_location in the config is set to a relative URL.
|
|
212 |
stacked_on_branch = self.make_branch('base-branch', format='1.6') |
|
213 |
stacked_branch = self.make_branch('stacked-branch', format='1.6') |
|
214 |
stacked_branch.set_stacked_on_url('../base-branch') |
|
215 |
opener = self.make_branch_opener( |
|
216 |
[stacked_branch.base, stacked_on_branch.base]) |
|
217 |
# Note that stacked_on_branch.base is not '../base-branch', it's an
|
|
218 |
# absolute URL.
|
|
219 |
self.assertNotEqual('../base-branch', stacked_on_branch.base) |
|
220 |
# This doesn't raise an exception.
|
|
221 |
opener.open(stacked_branch.base) |
|
222 |
||
223 |
def test_allowed_relative_nested(self): |
|
224 |
# Relative URLs are resolved relative to the stacked branch.
|
|
225 |
self.get_transport().mkdir('subdir') |
|
226 |
a = self.make_branch('subdir/a', format='1.6') |
|
227 |
b = self.make_branch('b', format='1.6') |
|
228 |
b.set_stacked_on_url('../subdir/a') |
|
229 |
c = self.make_branch('subdir/c', format='1.6') |
|
230 |
c.set_stacked_on_url('../../b') |
|
231 |
opener = self.make_branch_opener([c.base, b.base, a.base]) |
|
232 |
# This doesn't raise an exception.
|
|
233 |
opener.open(c.base) |
|
234 |
||
235 |
def test_forbidden_url(self): |
|
236 |
# raises a BadUrl exception if a branch is stacked on a
|
|
237 |
# branch with a forbidden URL.
|
|
238 |
stacked_on_branch = self.make_branch('base-branch', format='1.6') |
|
239 |
stacked_branch = self.make_branch('stacked-branch', format='1.6') |
|
240 |
stacked_branch.set_stacked_on_url(stacked_on_branch.base) |
|
241 |
opener = self.make_branch_opener([stacked_branch.base]) |
|
242 |
self.assertRaises(BadUrl, opener.open, stacked_branch.base) |
|
243 |
||
244 |
def test_forbidden_url_nested(self): |
|
245 |
# raises a BadUrl exception if a branch is stacked on a
|
|
246 |
# branch that is in turn stacked on a branch with a forbidden URL.
|
|
247 |
a = self.make_branch('a', format='1.6') |
|
248 |
b = self.make_branch('b', format='1.6') |
|
249 |
b.set_stacked_on_url(a.base) |
|
250 |
c = self.make_branch('c', format='1.6') |
|
251 |
c.set_stacked_on_url(b.base) |
|
252 |
opener = self.make_branch_opener([c.base, b.base]) |
|
253 |
self.assertRaises(BadUrl, opener.open, c.base) |
|
254 |
||
255 |
def test_self_stacked_branch(self): |
|
256 |
# raises StackingLoopError if a branch is stacked on
|
|
257 |
# itself. This avoids infinite recursion errors.
|
|
258 |
a = self.make_branch('a', format='1.6') |
|
259 |
# Bazaar 1.17 and up make it harder to create branches like this.
|
|
260 |
# It's still worth testing that we don't blow up in the face of them,
|
|
261 |
# so we grovel around a bit to create one anyway.
|
|
262 |
a.get_config().set_user_option('stacked_on_location', a.base) |
|
263 |
opener = self.make_branch_opener([a.base]) |
|
264 |
self.assertRaises(BranchLoopError, opener.open, a.base) |
|
265 |
||
266 |
def test_loop_stacked_branch(self): |
|
267 |
# raises StackingLoopError if a branch is stacked in such
|
|
268 |
# a way so that it is ultimately stacked on itself. e.g. a stacked on
|
|
269 |
# b stacked on a.
|
|
270 |
a = self.make_branch('a', format='1.6') |
|
271 |
b = self.make_branch('b', format='1.6') |
|
272 |
a.set_stacked_on_url(b.base) |
|
273 |
b.set_stacked_on_url(a.base) |
|
274 |
opener = self.make_branch_opener([a.base, b.base]) |
|
275 |
self.assertRaises(BranchLoopError, opener.open, a.base) |
|
276 |
self.assertRaises(BranchLoopError, opener.open, b.base) |
|
277 |
||
278 |
def test_custom_opener(self): |
|
279 |
# A custom function for opening a control dir can be specified.
|
|
280 |
a = self.make_branch('a', format='2a') |
|
281 |
b = self.make_branch('b', format='2a') |
|
282 |
b.set_stacked_on_url(a.base) |
|
283 |
||
284 |
TrackingProber.seen_urls = [] |
|
285 |
opener = self.make_branch_opener( |
|
286 |
[a.base, b.base], probers=[TrackingProber]) |
|
287 |
opener.open(b.base) |
|
288 |
self.assertEquals( |
|
289 |
set(TrackingProber.seen_urls), set([b.base, a.base])) |
|
290 |
||
291 |
def test_custom_opener_with_branch_reference(self): |
|
292 |
# A custom function for opening a control dir can be specified.
|
|
293 |
a = self.make_branch('a', format='2a') |
|
294 |
b_dir = self.make_bzrdir('b') |
|
295 |
b = BranchReferenceFormat().initialize(b_dir, target_branch=a) |
|
296 |
TrackingProber.seen_urls = [] |
|
297 |
opener = self.make_branch_opener( |
|
298 |
[a.base, b.base], probers=[TrackingProber]) |
|
299 |
opener.open(b.base) |
|
300 |
self.assertEquals( |
|
301 |
set(TrackingProber.seen_urls), set([b.base, a.base])) |
|
302 |
||
303 |
||
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
304 |
class TestOpenOnlyScheme(TestCaseWithTransport): |
305 |
"""Tests for `open_only_scheme`."""
|
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
306 |
|
307 |
def setUp(self): |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
308 |
super(TestOpenOnlyScheme, self).setUp() |
309 |
BranchOpener.install_hook() |
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
310 |
|
311 |
def test_hook_does_not_interfere(self): |
|
312 |
# The transform_fallback_location hook does not interfere with regular
|
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
313 |
# stacked branch access outside of open_only_scheme.
|
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
314 |
self.make_branch('stacked') |
315 |
self.make_branch('stacked-on') |
|
316 |
Branch.open('stacked').set_stacked_on_url('../stacked-on') |
|
317 |
Branch.open('stacked') |
|
318 |
||
319 |
def get_chrooted_scheme(self, relpath): |
|
320 |
"""Create a server that is chrooted to `relpath`.
|
|
321 |
||
322 |
:return: ``(scheme, get_url)`` where ``scheme`` is the scheme of the
|
|
323 |
chroot server and ``get_url`` returns URLs on said server.
|
|
324 |
"""
|
|
325 |
transport = self.get_transport(relpath) |
|
326 |
chroot_server = chroot.ChrootServer(transport) |
|
327 |
chroot_server.start_server() |
|
328 |
self.addCleanup(chroot_server.stop_server) |
|
329 |
||
330 |
def get_url(relpath): |
|
6402.3.2
by Jelmer Vernooij
bzrify |
331 |
return chroot_server.get_url() + relpath |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
332 |
|
333 |
return urlutils.URL.from_string(chroot_server.get_url()).scheme, get_url |
|
334 |
||
335 |
def test_stacked_within_scheme(self): |
|
336 |
# A branch that is stacked on a URL of the same scheme is safe to
|
|
337 |
# open.
|
|
338 |
self.get_transport().mkdir('inside') |
|
339 |
self.make_branch('inside/stacked') |
|
340 |
self.make_branch('inside/stacked-on') |
|
341 |
scheme, get_chrooted_url = self.get_chrooted_scheme('inside') |
|
342 |
Branch.open(get_chrooted_url('stacked')).set_stacked_on_url( |
|
343 |
get_chrooted_url('stacked-on')) |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
344 |
open_only_scheme(scheme, get_chrooted_url('stacked')) |
6402.3.1
by Jelmer Vernooij
Add safe_open class to bzr. |
345 |
|
346 |
def test_stacked_outside_scheme(self): |
|
347 |
# A branch that is stacked on a URL that is not of the same scheme is
|
|
348 |
# not safe to open.
|
|
349 |
self.get_transport().mkdir('inside') |
|
350 |
self.get_transport().mkdir('outside') |
|
351 |
self.make_branch('inside/stacked') |
|
352 |
self.make_branch('outside/stacked-on') |
|
353 |
scheme, get_chrooted_url = self.get_chrooted_scheme('inside') |
|
354 |
Branch.open(get_chrooted_url('stacked')).set_stacked_on_url( |
|
355 |
self.get_url('outside/stacked-on')) |
|
356 |
self.assertRaises( |
|
6402.3.10
by Jelmer Vernooij
Name changes suggested by mgz. |
357 |
BadUrl, open_only_scheme, scheme, get_chrooted_url('stacked')) |