/brz/remove-bazaar

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