~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-03-12 17:30:53 UTC
  • mfrom: (2338.3.1 hide-nested)
  • Revision ID: pqm@pqm.ubuntu.com-20070312173053-4cdb4cd14190d29e
Hide nested-tree commands and improve their docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 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
33
33
import os
34
34
import sys
35
35
import stat
36
 
from StringIO import StringIO
 
36
from cStringIO import StringIO
 
37
import urllib
37
38
 
38
39
from bzrlib.branch import Branch
39
 
from bzrlib.lockable_files import LockableFiles
40
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped
41
 
from bzrlib.transport import get_transport
42
 
 
 
40
from bzrlib.bzrdir import BzrDir
 
41
from bzrlib.lockable_files import LockableFiles, TransportLock
 
42
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
43
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
44
from bzrlib.transport import get_transport
 
45
from bzrlib.workingtree import WorkingTree
45
46
 
46
47
 
47
48
def chmod_r(base, file_mode, dir_mode):
72
73
        test.assertTransportMode(t, base, dir_mode)
73
74
    for root, dirs, files in os.walk(base):
74
75
        for d in dirs:
75
 
            p = os.path.join(root, d)
 
76
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
76
77
            test.assertTransportMode(t, p, dir_mode)
77
78
        for f in files:
78
79
            p = os.path.join(root, f)
 
80
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
79
81
            test.assertTransportMode(t, p, file_mode)
80
82
 
81
83
 
82
 
def assertEqualMode(test, mode, mode_test):
83
 
    test.assertEqual(mode, mode_test,
84
 
                     'mode mismatch %o != %o' % (mode, mode_test))
85
 
 
86
 
 
87
 
class TestPermissions(TestCaseInTempDir):
 
84
class TestPermissions(TestCaseWithTransport):
88
85
 
89
86
    def test_new_files(self):
90
87
        if sys.platform == 'win32':
91
88
            raise TestSkipped('chmod has no effect on win32')
92
89
 
93
 
        b = Branch.initialize(u'.')
94
 
        t = b.working_tree()
 
90
        t = self.make_branch_and_tree('.')
 
91
        b = t.branch
95
92
        open('a', 'wb').write('foo\n')
96
 
        t.add('a')
 
93
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
 
94
        t.add('a', 'CAPS-ID')
97
95
        t.commit('foo')
98
96
 
99
 
        # Delete them because we are modifying the filesystem underneath them
100
 
        del b, t 
101
97
        chmod_r('.bzr', 0644, 0755)
102
98
        check_mode_r(self, '.bzr', 0644, 0755)
103
99
 
104
 
        b = Branch.open('.')
105
 
        t = b.working_tree()
106
 
        assertEqualMode(self, 0755, b.control_files._dir_mode)
107
 
        assertEqualMode(self, 0644, b.control_files._file_mode)
 
100
        # although we are modifying the filesystem
 
101
        # underneath the objects, they are not locked, and thus it must
 
102
        # be safe for most operations. But here we want to observe a 
 
103
        # mode change in the control bits, which current do not refresh
 
104
        # when a new lock is taken out.
 
105
        t = WorkingTree.open('.')
 
106
        b = t.branch
 
107
        self.assertEqualMode(0755, b.control_files._dir_mode)
 
108
        self.assertEqualMode(0644, b.control_files._file_mode)
108
109
 
109
110
        # Modifying a file shouldn't break the permissions
110
111
        open('a', 'wb').write('foo2\n')
118
119
        t.commit('new b')
119
120
        check_mode_r(self, '.bzr', 0644, 0755)
120
121
 
121
 
        del b, t
122
122
        # Recursively update the modes of all files
123
123
        chmod_r('.bzr', 0664, 0775)
124
124
        check_mode_r(self, '.bzr', 0664, 0775)
125
 
        b = Branch.open('.')
126
 
        t = b.working_tree()
127
 
        assertEqualMode(self, 0775, b.control_files._dir_mode)
128
 
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
125
        t = WorkingTree.open('.')
 
126
        b = t.branch
 
127
        self.assertEqualMode(0775, b.control_files._dir_mode)
 
128
        self.assertEqualMode(0664, b.control_files._file_mode)
129
129
 
130
130
        open('a', 'wb').write('foo3\n')
131
131
        t.commit('foo3')
137
137
        check_mode_r(self, '.bzr', 0664, 0775)
138
138
 
139
139
        # Test the group sticky bit
140
 
        del b, t
141
140
        # Recursively update the modes of all files
142
141
        chmod_r('.bzr', 0664, 02775)
143
142
        check_mode_r(self, '.bzr', 0664, 02775)
144
 
        b = Branch.open('.')
145
 
        t = b.working_tree()
146
 
        assertEqualMode(self, 02775, b.control_files._dir_mode)
147
 
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
143
        t = WorkingTree.open('.')
 
144
        b = t.branch
 
145
        self.assertEqualMode(02775, b.control_files._dir_mode)
 
146
        self.assertEqualMode(0664, b.control_files._file_mode)
148
147
 
149
148
        open('a', 'wb').write('foo4\n')
150
149
        t.commit('foo4')
159
158
        # TODO: jam 20051215 Ultimately, this test should probably test that
160
159
        #                    extra chmod calls aren't being made
161
160
        try:
162
 
            transport = get_transport('.')
163
 
            transport.put('my-lock', StringIO(''))
164
 
            lockable = LockableFiles(transport, 'my-lock')
 
161
            transport = get_transport(self.get_url())
 
162
            transport.put_bytes('my-lock', '')
 
163
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
165
164
            self.assertNotEqual(None, lockable._dir_mode)
166
165
            self.assertNotEqual(None, lockable._file_mode)
167
166
 
168
167
            LockableFiles._set_dir_mode = False
169
168
            transport = get_transport('.')
170
 
            lockable = LockableFiles(transport, 'my-lock')
 
169
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
171
170
            self.assertEqual(None, lockable._dir_mode)
172
171
            self.assertNotEqual(None, lockable._file_mode)
173
172
 
174
173
            LockableFiles._set_file_mode = False
175
174
            transport = get_transport('.')
176
 
            lockable = LockableFiles(transport, 'my-lock')
 
175
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
177
176
            self.assertEqual(None, lockable._dir_mode)
178
177
            self.assertEqual(None, lockable._file_mode)
179
178
 
180
179
            LockableFiles._set_dir_mode = True
181
180
            transport = get_transport('.')
182
 
            lockable = LockableFiles(transport, 'my-lock')
 
181
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
183
182
            self.assertNotEqual(None, lockable._dir_mode)
184
183
            self.assertEqual(None, lockable._file_mode)
185
184
 
186
185
            LockableFiles._set_file_mode = True
187
186
            transport = get_transport('.')
188
 
            lockable = LockableFiles(transport, 'my-lock')
 
187
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
189
188
            self.assertNotEqual(None, lockable._dir_mode)
190
189
            self.assertNotEqual(None, lockable._file_mode)
191
190
        finally:
192
191
            LockableFiles._set_dir_mode = True
193
192
            LockableFiles._set_file_mode = True
194
193
 
195
 
    def test_new_branch(self):
196
 
        if sys.platform == 'win32':
197
 
            raise TestSkipped('chmod has no effect on win32')
198
 
        #FIXME RBC 20060105 should test branch and repository 
199
 
        # permissions ? 
200
 
        # also, these are BzrBranch format specific things..
201
 
        os.mkdir('a')
202
 
        mode = stat.S_IMODE(os.stat('a').st_mode)
203
 
        b = Branch.initialize('a')
204
 
        assertEqualMode(self, mode, b.control_files._dir_mode)
205
 
        assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
206
 
 
207
 
        os.mkdir('b')
208
 
        os.chmod('b', 02777)
209
 
        b = Branch.initialize('b')
210
 
        assertEqualMode(self, 02777, b.control_files._dir_mode)
211
 
        assertEqualMode(self, 00666, b.control_files._file_mode)
212
 
        check_mode_r(self, 'b/.bzr', 00666, 02777)
213
 
 
214
 
        os.mkdir('c')
215
 
        os.chmod('c', 02750)
216
 
        b = Branch.initialize('c')
217
 
        assertEqualMode(self, 02750, b.control_files._dir_mode)
218
 
        assertEqualMode(self, 00640, b.control_files._file_mode)
219
 
        check_mode_r(self, 'c/.bzr', 00640, 02750)
220
 
 
221
 
        os.mkdir('d')
222
 
        os.chmod('d', 0700)
223
 
        b = Branch.initialize('d')
224
 
        assertEqualMode(self, 0700, b.control_files._dir_mode)
225
 
        assertEqualMode(self, 0600, b.control_files._file_mode)
226
 
        check_mode_r(self, 'd/.bzr', 00600, 0700)
227
 
 
228
194
 
229
195
class TestSftpPermissions(TestCaseWithSFTPServer):
230
196
 
234
200
        # Though it would be nice to test that SFTP to a server
235
201
        # which does support chmod has the right effect
236
202
 
237
 
        from bzrlib.transport.sftp import SFTPTransport
238
 
 
239
 
        # We don't actually use it directly, we just want to
240
 
        # keep the connection open, since StubSFTPServer only
241
 
        # allows 1 connection
242
 
        _transport = SFTPTransport(self._sftp_url)
 
203
        # bodge around for stubsftpserver not letting use connect
 
204
        # more than once
 
205
        _t = get_transport(self.get_url())
243
206
 
244
207
        os.mkdir('local')
245
 
        b_local = Branch.initialize(u'local')
246
 
        t_local = b_local.working_tree()
 
208
        t_local = self.make_branch_and_tree('local')
 
209
        b_local = t_local.branch
247
210
        open('local/a', 'wb').write('foo\n')
248
211
        t_local.add('a')
249
212
        t_local.commit('foo')
250
213
 
251
214
        # Delete them because we are modifying the filesystem underneath them
252
 
        del b_local, t_local 
253
215
        chmod_r('local/.bzr', 0644, 0755)
254
216
        check_mode_r(self, 'local/.bzr', 0644, 0755)
255
217
 
256
 
        b_local = Branch.open(u'local')
257
 
        t_local = b_local.working_tree()
258
 
        assertEqualMode(self, 0755, b_local.control_files._dir_mode)
259
 
        assertEqualMode(self, 0644, b_local.control_files._file_mode)
 
218
        t = WorkingTree.open('local')
 
219
        b_local = t.branch
 
220
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
 
221
        self.assertEqualMode(0644, b_local.control_files._file_mode)
260
222
 
261
223
        os.mkdir('sftp')
262
 
        sftp_url = self.get_remote_url('sftp')
263
 
        b_sftp = Branch.initialize(sftp_url)
 
224
        sftp_url = self.get_url('sftp')
 
225
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
264
226
 
265
227
        b_sftp.pull(b_local)
266
228
        del b_sftp
268
230
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
269
231
 
270
232
        b_sftp = Branch.open(sftp_url)
271
 
        assertEqualMode(self, 0755, b_sftp.control_files._dir_mode)
272
 
        assertEqualMode(self, 0644, b_sftp.control_files._file_mode)
 
233
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
 
234
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
273
235
 
274
236
        open('local/a', 'wb').write('foo2\n')
275
237
        t_local.commit('foo2')
289
251
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
290
252
 
291
253
        b_sftp = Branch.open(sftp_url)
292
 
        assertEqualMode(self, 0775, b_sftp.control_files._dir_mode)
293
 
        assertEqualMode(self, 0664, b_sftp.control_files._file_mode)
 
254
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
 
255
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
294
256
 
295
257
        open('local/a', 'wb').write('foo3\n')
296
258
        t_local.commit('foo3')
311
273
        original_umask = os.umask(umask)
312
274
 
313
275
        try:
314
 
            from bzrlib.transport.sftp import SFTPTransport
315
 
            t = SFTPTransport(self._sftp_url)
 
276
            t = get_transport(self.get_url())
316
277
            # Direct access should be masked by umask
317
278
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
318
279
            self.assertTransportMode(t, 'a', 0666 &~umask)
319
280
 
320
281
            # but Transport overrides umask
321
 
            t.put('b', 'txt', mode=0666)
 
282
            t.put_bytes('b', 'txt', mode=0666)
322
283
            self.assertTransportMode(t, 'b', 0666)
323
284
 
324
285
            t._sftp.mkdir('c', mode=0777)
328
289
            self.assertTransportMode(t, 'd', 0777)
329
290
        finally:
330
291
            os.umask(original_umask)
331
 
 
332