~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/codehosting/tests/test_safe_open.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-15 10:01:15 UTC
  • mfrom: (14521 devel)
  • mto: This revision was merged to the branch mainline in revision 14522.
  • Revision ID: jelmer@canonical.com-20111215100115-y6bg928fwdqd16ej
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from bzrlib.bzrdir import (
15
15
    BzrDir,
16
16
    BzrDirMetaFormat1,
17
 
    BzrProber,
18
17
    )
19
 
from bzrlib.controldir import ControlDirFormat
20
 
from bzrlib.errors import NotBranchError
21
18
from bzrlib.repofmt.knitpack_repo import RepositoryFormatKnitPack1
22
19
from bzrlib.tests import TestCaseWithTransport
23
20
from bzrlib.transport import chroot
58
55
                self._reference_values[references[i]] = references[i + 1]
59
56
            self.follow_reference_calls = []
60
57
 
61
 
        def followReference(self, url):
 
58
        def followReference(self, url, open_dir=None):
62
59
            self.follow_reference_calls.append(url)
63
60
            return self._reference_values[url]
64
61
 
71
68
    def testCheckInitialURL(self):
72
69
        # checkSource rejects all URLs that are not allowed.
73
70
        opener = self.makeBranchOpener(None, [], set(['a']))
74
 
        self.assertRaises(
75
 
            BadUrl, opener.checkAndFollowBranchReference, 'a')
 
71
        self.assertRaises(BadUrl, opener.checkAndFollowBranchReference, 'a')
76
72
 
77
73
    def testNotReference(self):
78
74
        # When branch references are forbidden, checkAndFollowBranchReference
79
75
        # does not raise on non-references.
80
76
        opener = self.makeBranchOpener(False, ['a', None])
81
 
        self.assertEquals(
82
 
            'a', opener.checkAndFollowBranchReference('a'))
 
77
        self.assertEquals('a', opener.checkAndFollowBranchReference('a'))
83
78
        self.assertEquals(['a'], opener.follow_reference_calls)
84
79
 
85
80
    def testBranchReferenceForbidden(self):
97
92
        # is allowed and the source URL points to a branch reference to a
98
93
        # permitted location.
99
94
        opener = self.makeBranchOpener(True, ['a', 'b', None])
100
 
        self.assertEquals(
101
 
            'b', opener.checkAndFollowBranchReference('a'))
 
95
        self.assertEquals('b', opener.checkAndFollowBranchReference('a'))
102
96
        self.assertEquals(['a', 'b'], opener.follow_reference_calls)
103
97
 
104
98
    def testCheckReferencedURLs(self):
106
100
        # to is safe.
107
101
        opener = self.makeBranchOpener(
108
102
            True, ['a', 'b', None], unsafe_urls=set('b'))
109
 
        self.assertRaises(
110
 
            BadUrl, opener.checkAndFollowBranchReference, 'a')
 
103
        self.assertRaises(BadUrl, opener.checkAndFollowBranchReference, 'a')
111
104
        self.assertEquals(['a'], opener.follow_reference_calls)
112
105
 
113
106
    def testSelfReferencingBranch(self):
130
123
        self.assertEquals(['a', 'b'], opener.follow_reference_calls)
131
124
 
132
125
 
133
 
class TrackingProber(BzrProber):
134
 
    """Subclass of BzrProber which tracks URLs it has been asked to open."""
135
 
 
136
 
    seen_urls = []
137
 
 
138
 
    @classmethod
139
 
    def probe_transport(klass, transport):
140
 
        klass.seen_urls.append(transport.base)
141
 
        return BzrProber.probe_transport(transport)
142
 
 
143
 
 
144
126
class TestSafeBranchOpenerStacking(TestCaseWithTransport):
145
127
 
146
128
    def setUp(self):
147
129
        super(TestSafeBranchOpenerStacking, self).setUp()
148
130
        SafeBranchOpener.install_hook()
149
131
 
150
 
    def makeBranchOpener(self, allowed_urls, probers=None):
 
132
    def makeBranchOpener(self, allowed_urls):
151
133
        policy = WhitelistPolicy(True, allowed_urls, True)
152
 
        return SafeBranchOpener(policy, probers)
 
134
        return SafeBranchOpener(policy)
153
135
 
154
136
    def makeBranch(self, path, branch_format, repository_format):
155
137
        """Make a Bazaar branch at 'path' with the given formats."""
159
141
        repository_format.initialize(bzrdir)
160
142
        return bzrdir.create_branch()
161
143
 
162
 
    def testProbers(self):
163
 
        # Only the specified probers should be used
164
 
        b = self.make_branch('branch')
165
 
        opener = self.makeBranchOpener([b.base], probers=[])
166
 
        self.assertRaises(NotBranchError, opener.open, b.base)
167
 
        opener = self.makeBranchOpener([b.base], probers=[BzrProber])
168
 
        self.assertEquals(b.base, opener.open(b.base).base)
169
 
 
170
 
    def testDefaultProbers(self):
171
 
        # If no probers are specified to the constructor
172
 
        # of SafeBranchOpener, then a safe set will be used,
173
 
        # rather than all probers registered in bzr.
174
 
        self.addCleanup(ControlDirFormat.unregister_prober, TrackingProber)
175
 
        ControlDirFormat.register_prober(TrackingProber)
176
 
        # Open a location without any branches, so that all probers are
177
 
        # tried.
178
 
        # First, check that the TrackingProber tracks correctly.
179
 
        TrackingProber.seen_urls = []
180
 
        opener = self.makeBranchOpener(["."], probers=[TrackingProber])
181
 
        self.assertRaises(NotBranchError, opener.open, ".")
182
 
        self.assertEquals(1, len(TrackingProber.seen_urls))
183
 
        TrackingProber.seen_urls = []
184
 
        # And make sure it's registered in such a way that BzrDir.open would
185
 
        # use it.
186
 
        self.assertRaises(NotBranchError, BzrDir.open, ".")
187
 
        self.assertEquals(1, len(TrackingProber.seen_urls))
188
 
        TrackingProber.seen_urls = []
189
 
        # Make sure that SafeBranchOpener doesn't use it if no
190
 
        # probers were specified
191
 
        opener = self.makeBranchOpener(["."])
192
 
        self.assertRaises(NotBranchError, opener.open, ".")
193
 
        self.assertEquals(0, len(TrackingProber.seen_urls))
194
 
 
195
144
    def testAllowedURL(self):
196
145
        # checkSource does not raise an exception for branches stacked on
197
146
        # branches with allowed URLs.
286
235
        a = self.make_branch('a', format='2a')
287
236
        b = self.make_branch('b', format='2a')
288
237
        b.set_stacked_on_url(a.base)
289
 
 
290
 
        TrackingProber.seen_urls = []
291
 
        opener = self.makeBranchOpener(
292
 
            [a.base, b.base], probers=[TrackingProber])
293
 
        opener.open(b.base)
294
 
        self.assertEquals(
295
 
            set(TrackingProber.seen_urls), set([b.base, a.base]))
 
238
        seen_urls = set()
 
239
 
 
240
        def open_dir(url):
 
241
            seen_urls.add(url)
 
242
            return BzrDir.open(url)
 
243
 
 
244
        opener = self.makeBranchOpener([a.base, b.base])
 
245
        opener.open(b.base, open_dir=open_dir)
 
246
        self.assertEquals(seen_urls, set([b.base, a.base]))
296
247
 
297
248
    def testCustomOpenerWithBranchReference(self):
298
249
        # A custom function for opening a control dir can be specified.
299
250
        a = self.make_branch('a', format='2a')
300
251
        b_dir = self.make_bzrdir('b')
301
252
        b = BranchReferenceFormat().initialize(b_dir, target_branch=a)
302
 
        TrackingProber.seen_urls = []
303
 
        opener = self.makeBranchOpener(
304
 
            [a.base, b.base], probers=[TrackingProber])
305
 
        opener.open(b.base)
306
 
        self.assertEquals(
307
 
            set(TrackingProber.seen_urls), set([b.base, a.base]))
 
253
        seen_urls = set()
 
254
 
 
255
        def open_dir(url):
 
256
            seen_urls.add(url)
 
257
            return BzrDir.open(url)
 
258
 
 
259
        opener = self.makeBranchOpener([a.base, b.base])
 
260
        opener.open(b.base, open_dir=open_dir)
 
261
        self.assertEquals(seen_urls, set([b.base, a.base]))
308
262
 
309
263
 
310
264
class TestSafeOpen(TestCaseWithTransport):