~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Martin Pool
  • Date: 2006-01-13 09:57:13 UTC
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060113095713-1fa5912229a3898e
Review updates of pycurl transport

Split them out into 

  bzrlib.transport.http             common base
  bzrlib.transport.http._urllib     pure python
  bzrlib.transport.http._pycurl     calls pycurl

Update to work with robert's nice transport test multiplexer.

Add PyCurlTransport.has() which does just a HEAD request; should be faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 by Canonical Ltd
2
2
# -*- coding: utf-8 -*-
3
 
#
 
3
 
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
6
6
# the Free Software Foundation; either version 2 of the License, or
7
7
# (at your option) any later version.
8
 
#
 
8
 
9
9
# This program is distributed in the hope that it will be useful,
10
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
12
# GNU General Public License for more details.
13
 
#
 
13
 
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
23
 
24
24
In the future, when we have Repository/Branch/Checkout information, the
25
25
permissions should be inherited individually, rather than all be the same.
 
26
 
 
27
TODO: jam 20051215 There are no tests for ftp yet, because we have no ftp server
 
28
TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just 
 
29
                   defined by the local umask. This isn't terrible, is it
 
30
                   the truly desired behavior?
26
31
"""
27
32
 
28
 
# TODO: jam 20051215 There are no tests for ftp yet, because we have no ftp server
29
 
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just 
30
 
#                    defined by the local umask. This isn't terrible, is it
31
 
#                    the truly desired behavior?
32
 
 
33
33
import os
34
34
import sys
35
35
import stat
36
 
from StringIO import StringIO
37
36
 
38
37
from bzrlib.branch import Branch
39
 
from bzrlib.bzrdir import BzrDir
40
 
from bzrlib.lockable_files import LockableFiles, TransportLock
41
 
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
38
from bzrlib.tests import TestCaseInTempDir, TestSkipped
42
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
43
 
from bzrlib.transport import get_transport
44
 
from bzrlib.workingtree import WorkingTree
 
40
from bzrlib.tests.test_transport import check_mode
45
41
 
46
42
 
47
43
def chmod_r(base, file_mode, dir_mode):
67
63
    :param include_base: If false, only check the subdirectories
68
64
    """
69
65
    assert os.path.isdir(base)
70
 
    t = get_transport(".")
71
66
    if include_base:
72
 
        test.assertTransportMode(t, base, dir_mode)
 
67
        check_mode(test, base, dir_mode)
73
68
    for root, dirs, files in os.walk(base):
74
69
        for d in dirs:
75
70
            p = os.path.join(root, d)
76
 
            test.assertTransportMode(t, p, dir_mode)
 
71
            check_mode(test, p, dir_mode)
77
72
        for f in files:
78
73
            p = os.path.join(root, f)
79
 
            test.assertTransportMode(t, p, file_mode)
80
 
 
81
 
 
82
 
class TestPermissions(TestCaseWithTransport):
 
74
            check_mode(test, p, file_mode)
 
75
 
 
76
 
 
77
def assertEqualMode(test, mode, mode_test):
 
78
    test.assertEqual(mode, mode_test,
 
79
                     'mode mismatch %o != %o' % (mode, mode_test))
 
80
 
 
81
 
 
82
class TestPermissions(TestCaseInTempDir):
83
83
 
84
84
    def test_new_files(self):
85
85
        if sys.platform == 'win32':
86
86
            raise TestSkipped('chmod has no effect on win32')
87
87
 
88
 
        t = self.make_branch_and_tree('.')
89
 
        b = t.branch
 
88
        b = Branch.initialize(u'.')
 
89
        t = b.working_tree()
90
90
        open('a', 'wb').write('foo\n')
91
91
        t.add('a')
92
92
        t.commit('foo')
93
93
 
 
94
        # Delete them because we are modifying the filesystem underneath them
 
95
        del b, t 
94
96
        chmod_r('.bzr', 0644, 0755)
95
97
        check_mode_r(self, '.bzr', 0644, 0755)
96
98
 
97
 
        # although we are modifying the filesystem
98
 
        # underneath the objects, they are not locked, and thus it must
99
 
        # be safe for most operations. But here we want to observe a 
100
 
        # mode change in the control bits, which current do not refresh
101
 
        # when a new lock is taken out.
102
 
        t = WorkingTree.open('.')
103
 
        b = t.branch
104
 
        self.assertEqualMode(0755, b.control_files._dir_mode)
105
 
        self.assertEqualMode(0644, b.control_files._file_mode)
 
99
        b = Branch.open('.')
 
100
        t = b.working_tree()
 
101
        assertEqualMode(self, 0755, b._dir_mode)
 
102
        assertEqualMode(self, 0644, b._file_mode)
106
103
 
107
104
        # Modifying a file shouldn't break the permissions
108
105
        open('a', 'wb').write('foo2\n')
116
113
        t.commit('new b')
117
114
        check_mode_r(self, '.bzr', 0644, 0755)
118
115
 
 
116
        del b, t
119
117
        # Recursively update the modes of all files
120
118
        chmod_r('.bzr', 0664, 0775)
121
119
        check_mode_r(self, '.bzr', 0664, 0775)
122
 
        t = WorkingTree.open('.')
123
 
        b = t.branch
124
 
        self.assertEqualMode(0775, b.control_files._dir_mode)
125
 
        self.assertEqualMode(0664, b.control_files._file_mode)
 
120
        b = Branch.open('.')
 
121
        t = b.working_tree()
 
122
        assertEqualMode(self, 0775, b._dir_mode)
 
123
        assertEqualMode(self, 0664, b._file_mode)
126
124
 
127
125
        open('a', 'wb').write('foo3\n')
128
126
        t.commit('foo3')
134
132
        check_mode_r(self, '.bzr', 0664, 0775)
135
133
 
136
134
        # Test the group sticky bit
 
135
        del b, t
137
136
        # Recursively update the modes of all files
138
137
        chmod_r('.bzr', 0664, 02775)
139
138
        check_mode_r(self, '.bzr', 0664, 02775)
140
 
        t = WorkingTree.open('.')
141
 
        b = t.branch
142
 
        self.assertEqualMode(02775, b.control_files._dir_mode)
143
 
        self.assertEqualMode(0664, b.control_files._file_mode)
 
139
        b = Branch.open('.')
 
140
        t = b.working_tree()
 
141
        assertEqualMode(self, 02775, b._dir_mode)
 
142
        assertEqualMode(self, 0664, b._file_mode)
144
143
 
145
144
        open('a', 'wb').write('foo4\n')
146
145
        t.commit('foo4')
154
153
    def test_disable_set_mode(self):
155
154
        # TODO: jam 20051215 Ultimately, this test should probably test that
156
155
        #                    extra chmod calls aren't being made
 
156
        import bzrlib.branch
157
157
        try:
158
 
            transport = get_transport(self.get_url())
159
 
            transport.put('my-lock', StringIO(''))
160
 
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
161
 
            self.assertNotEqual(None, lockable._dir_mode)
162
 
            self.assertNotEqual(None, lockable._file_mode)
163
 
 
164
 
            LockableFiles._set_dir_mode = False
165
 
            transport = get_transport('.')
166
 
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
167
 
            self.assertEqual(None, lockable._dir_mode)
168
 
            self.assertNotEqual(None, lockable._file_mode)
169
 
 
170
 
            LockableFiles._set_file_mode = False
171
 
            transport = get_transport('.')
172
 
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
173
 
            self.assertEqual(None, lockable._dir_mode)
174
 
            self.assertEqual(None, lockable._file_mode)
175
 
 
176
 
            LockableFiles._set_dir_mode = True
177
 
            transport = get_transport('.')
178
 
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
179
 
            self.assertNotEqual(None, lockable._dir_mode)
180
 
            self.assertEqual(None, lockable._file_mode)
181
 
 
182
 
            LockableFiles._set_file_mode = True
183
 
            transport = get_transport('.')
184
 
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
185
 
            self.assertNotEqual(None, lockable._dir_mode)
186
 
            self.assertNotEqual(None, lockable._file_mode)
 
158
            b = Branch.initialize(u'.')
 
159
            self.assertNotEqual(None, b._dir_mode)
 
160
            self.assertNotEqual(None, b._file_mode)
 
161
 
 
162
            bzrlib.branch.BzrBranch._set_dir_mode = False
 
163
            b = Branch.open(u'.')
 
164
            self.assertEqual(None, b._dir_mode)
 
165
            self.assertNotEqual(None, b._file_mode)
 
166
 
 
167
            bzrlib.branch.BzrBranch._set_file_mode = False
 
168
            b = Branch.open(u'.')
 
169
            self.assertEqual(None, b._dir_mode)
 
170
            self.assertEqual(None, b._file_mode)
 
171
 
 
172
            bzrlib.branch.BzrBranch._set_dir_mode = True
 
173
            b = Branch.open(u'.')
 
174
            self.assertNotEqual(None, b._dir_mode)
 
175
            self.assertEqual(None, b._file_mode)
 
176
 
 
177
            bzrlib.branch.BzrBranch._set_file_mode = True
 
178
            b = Branch.open(u'.')
 
179
            self.assertNotEqual(None, b._dir_mode)
 
180
            self.assertNotEqual(None, b._file_mode)
187
181
        finally:
188
 
            LockableFiles._set_dir_mode = True
189
 
            LockableFiles._set_file_mode = True
 
182
            bzrlib.branch.BzrBranch._set_dir_mode = True
 
183
            bzrlib.branch.BzrBranch._set_file_mode = True
 
184
 
 
185
    def test_new_branch(self):
 
186
        if sys.platform == 'win32':
 
187
            raise TestSkipped('chmod has no effect on win32')
 
188
 
 
189
        os.mkdir('a')
 
190
        mode = stat.S_IMODE(os.stat('a').st_mode)
 
191
        b = Branch.initialize('a')
 
192
        assertEqualMode(self, mode, b._dir_mode)
 
193
        assertEqualMode(self, mode & ~07111, b._file_mode)
 
194
 
 
195
        os.mkdir('b')
 
196
        os.chmod('b', 02777)
 
197
        b = Branch.initialize('b')
 
198
        assertEqualMode(self, 02777, b._dir_mode)
 
199
        assertEqualMode(self, 00666, b._file_mode)
 
200
        check_mode_r(self, 'b/.bzr', 00666, 02777)
 
201
 
 
202
        os.mkdir('c')
 
203
        os.chmod('c', 02750)
 
204
        b = Branch.initialize('c')
 
205
        assertEqualMode(self, 02750, b._dir_mode)
 
206
        assertEqualMode(self, 00640, b._file_mode)
 
207
        check_mode_r(self, 'c/.bzr', 00640, 02750)
 
208
 
 
209
        os.mkdir('d')
 
210
        os.chmod('d', 0700)
 
211
        b = Branch.initialize('d')
 
212
        assertEqualMode(self, 0700, b._dir_mode)
 
213
        assertEqualMode(self, 0600, b._file_mode)
 
214
        check_mode_r(self, 'd/.bzr', 00600, 0700)
190
215
 
191
216
 
192
217
class TestSftpPermissions(TestCaseWithSFTPServer):
197
222
        # Though it would be nice to test that SFTP to a server
198
223
        # which does support chmod has the right effect
199
224
 
200
 
        # bodge around for stubsftpserver not letting use connect
201
 
        # more than once
202
 
        _t = get_transport(self.get_url())
 
225
        from bzrlib.transport.sftp import SFTPTransport
 
226
 
 
227
        # We don't actually use it directly, we just want to
 
228
        # keep the connection open, since StubSFTPServer only
 
229
        # allows 1 connection
 
230
        self.delayed_setup()
 
231
        _transport = SFTPTransport(self._sftp_url)
203
232
 
204
233
        os.mkdir('local')
205
 
        t_local = self.make_branch_and_tree('local')
206
 
        b_local = t_local.branch
 
234
        b_local = Branch.initialize(u'local')
 
235
        t_local = b_local.working_tree()
207
236
        open('local/a', 'wb').write('foo\n')
208
237
        t_local.add('a')
209
238
        t_local.commit('foo')
210
239
 
211
240
        # Delete them because we are modifying the filesystem underneath them
 
241
        del b_local, t_local 
212
242
        chmod_r('local/.bzr', 0644, 0755)
213
243
        check_mode_r(self, 'local/.bzr', 0644, 0755)
214
244
 
215
 
        t = WorkingTree.open('local')
216
 
        b_local = t.branch
217
 
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
218
 
        self.assertEqualMode(0644, b_local.control_files._file_mode)
 
245
        b_local = Branch.open(u'local')
 
246
        t_local = b_local.working_tree()
 
247
        assertEqualMode(self, 0755, b_local._dir_mode)
 
248
        assertEqualMode(self, 0644, b_local._file_mode)
219
249
 
220
250
        os.mkdir('sftp')
221
 
        sftp_url = self.get_url('sftp')
222
 
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
251
        # Why does self._sftp_url end with a slash????
 
252
        sftp_url = self._sftp_url + 'sftp'
 
253
        b_sftp = Branch.initialize(sftp_url)
223
254
 
224
255
        b_sftp.pull(b_local)
225
256
        del b_sftp
227
258
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
228
259
 
229
260
        b_sftp = Branch.open(sftp_url)
230
 
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
231
 
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
 
261
        assertEqualMode(self, 0755, b_sftp._dir_mode)
 
262
        assertEqualMode(self, 0644, b_sftp._file_mode)
232
263
 
233
264
        open('local/a', 'wb').write('foo2\n')
234
265
        t_local.commit('foo2')
248
279
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
249
280
 
250
281
        b_sftp = Branch.open(sftp_url)
251
 
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
252
 
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
 
282
        assertEqualMode(self, 0775, b_sftp._dir_mode)
 
283
        assertEqualMode(self, 0664, b_sftp._file_mode)
253
284
 
254
285
        open('local/a', 'wb').write('foo3\n')
255
286
        t_local.commit('foo3')
270
301
        original_umask = os.umask(umask)
271
302
 
272
303
        try:
273
 
            t = get_transport(self.get_url())
 
304
            from bzrlib.transport.sftp import SFTPTransport
 
305
            self.delayed_setup()
 
306
            t = SFTPTransport(self._sftp_url)
274
307
            # Direct access should be masked by umask
275
308
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
276
 
            self.assertTransportMode(t, 'a', 0666 &~umask)
 
309
            check_mode(self, 'a', 0666 &~umask)
277
310
 
278
311
            # but Transport overrides umask
279
312
            t.put('b', 'txt', mode=0666)
280
 
            self.assertTransportMode(t, 'b', 0666)
 
313
            check_mode(self, 'b', 0666)
281
314
 
282
315
            t._sftp.mkdir('c', mode=0777)
283
 
            self.assertTransportMode(t, 'c', 0777 &~umask)
 
316
            check_mode(self, 'c', 0777 &~umask)
284
317
 
285
318
            t.mkdir('d', mode=0777)
286
 
            self.assertTransportMode(t, 'd', 0777)
 
319
            check_mode(self, 'd', 0777)
287
320
        finally:
288
321
            os.umask(original_umask)
 
322
 
 
323