/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

Branch now uses BzrDir reasonably sanely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
 
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
 
 
19
"""Tests for bzr setting permissions.
 
20
 
 
21
Files which are created underneath .bzr/ should inherit its permissions.
 
22
So if the directory is group writable, the files and subdirs should be as well.
 
23
 
 
24
In the future, when we have Repository/Branch/Checkout information, the
 
25
permissions should be inherited individually, rather than all be the same.
 
26
"""
 
27
 
 
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
import os
 
34
import sys
 
35
import stat
 
36
from StringIO import StringIO
 
37
 
 
38
from bzrlib.branch import Branch
 
39
from bzrlib.bzrdir import BzrDir
 
40
from bzrlib.lockable_files import LockableFiles
 
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
43
from bzrlib.transport import get_transport
 
44
from bzrlib.workingtree import WorkingTree
 
45
 
 
46
 
 
47
def chmod_r(base, file_mode, dir_mode):
 
48
    """Recursively chmod from a base directory"""
 
49
    assert os.path.isdir(base)
 
50
    os.chmod(base, dir_mode)
 
51
    for root, dirs, files in os.walk(base):
 
52
        for d in dirs:
 
53
            p = os.path.join(root, d)
 
54
            os.chmod(p, dir_mode)
 
55
        for f in files:
 
56
            p = os.path.join(root, f)
 
57
            os.chmod(p, file_mode)
 
58
 
 
59
 
 
60
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
 
61
    """Check that all permissions match
 
62
 
 
63
    :param test: The TestCase being run
 
64
    :param base: The path to the root directory to check
 
65
    :param file_mode: The mode for all files
 
66
    :param dir_mode: The mode for all directories
 
67
    :param include_base: If false, only check the subdirectories
 
68
    """
 
69
    assert os.path.isdir(base)
 
70
    t = get_transport(".")
 
71
    if include_base:
 
72
        test.assertTransportMode(t, base, dir_mode)
 
73
    for root, dirs, files in os.walk(base):
 
74
        for d in dirs:
 
75
            p = os.path.join(root, d)
 
76
            test.assertTransportMode(t, p, dir_mode)
 
77
        for f in files:
 
78
            p = os.path.join(root, f)
 
79
            test.assertTransportMode(t, p, file_mode)
 
80
 
 
81
 
 
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(TestCaseWithTransport):
 
88
 
 
89
    def test_new_files(self):
 
90
        if sys.platform == 'win32':
 
91
            raise TestSkipped('chmod has no effect on win32')
 
92
 
 
93
        t = WorkingTree.create_standalone('.')
 
94
        b = t.branch
 
95
        open('a', 'wb').write('foo\n')
 
96
        t.add('a')
 
97
        t.commit('foo')
 
98
 
 
99
        chmod_r('.bzr', 0644, 0755)
 
100
        check_mode_r(self, '.bzr', 0644, 0755)
 
101
 
 
102
        # although we are modifying the filesystem
 
103
        # underneath the objects, they are not locked, and thus it must
 
104
        # be safe for most operations. But here we want to observe a 
 
105
        # mode change in the control bits, which current do not refresh
 
106
        # when a new lock is taken out.
 
107
        t = WorkingTree('.')
 
108
        b = t.branch
 
109
        assertEqualMode(self, 0755, b.control_files._dir_mode)
 
110
        assertEqualMode(self, 0644, b.control_files._file_mode)
 
111
 
 
112
        # Modifying a file shouldn't break the permissions
 
113
        open('a', 'wb').write('foo2\n')
 
114
        t.commit('foo2')
 
115
        # The mode should be maintained after commit
 
116
        check_mode_r(self, '.bzr', 0644, 0755)
 
117
 
 
118
        # Adding a new file should maintain the permissions
 
119
        open('b', 'wb').write('new b\n')
 
120
        t.add('b')
 
121
        t.commit('new b')
 
122
        check_mode_r(self, '.bzr', 0644, 0755)
 
123
 
 
124
        # Recursively update the modes of all files
 
125
        chmod_r('.bzr', 0664, 0775)
 
126
        check_mode_r(self, '.bzr', 0664, 0775)
 
127
        t = WorkingTree('.')
 
128
        b = t.branch
 
129
        assertEqualMode(self, 0775, b.control_files._dir_mode)
 
130
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
131
 
 
132
        open('a', 'wb').write('foo3\n')
 
133
        t.commit('foo3')
 
134
        check_mode_r(self, '.bzr', 0664, 0775)
 
135
 
 
136
        open('c', 'wb').write('new c\n')
 
137
        t.add('c')
 
138
        t.commit('new c')
 
139
        check_mode_r(self, '.bzr', 0664, 0775)
 
140
 
 
141
        # Test the group sticky bit
 
142
        # Recursively update the modes of all files
 
143
        chmod_r('.bzr', 0664, 02775)
 
144
        check_mode_r(self, '.bzr', 0664, 02775)
 
145
        t = WorkingTree('.')
 
146
        b = t.branch
 
147
        assertEqualMode(self, 02775, b.control_files._dir_mode)
 
148
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
149
 
 
150
        open('a', 'wb').write('foo4\n')
 
151
        t.commit('foo4')
 
152
        check_mode_r(self, '.bzr', 0664, 02775)
 
153
 
 
154
        open('d', 'wb').write('new d\n')
 
155
        t.add('d')
 
156
        t.commit('new d')
 
157
        check_mode_r(self, '.bzr', 0664, 02775)
 
158
 
 
159
    def test_disable_set_mode(self):
 
160
        # TODO: jam 20051215 Ultimately, this test should probably test that
 
161
        #                    extra chmod calls aren't being made
 
162
        try:
 
163
            transport = get_transport(self.get_url())
 
164
            transport.put('my-lock', StringIO(''))
 
165
            lockable = LockableFiles(transport, 'my-lock')
 
166
            self.assertNotEqual(None, lockable._dir_mode)
 
167
            self.assertNotEqual(None, lockable._file_mode)
 
168
 
 
169
            LockableFiles._set_dir_mode = False
 
170
            transport = get_transport('.')
 
171
            lockable = LockableFiles(transport, 'my-lock')
 
172
            self.assertEqual(None, lockable._dir_mode)
 
173
            self.assertNotEqual(None, lockable._file_mode)
 
174
 
 
175
            LockableFiles._set_file_mode = False
 
176
            transport = get_transport('.')
 
177
            lockable = LockableFiles(transport, 'my-lock')
 
178
            self.assertEqual(None, lockable._dir_mode)
 
179
            self.assertEqual(None, lockable._file_mode)
 
180
 
 
181
            LockableFiles._set_dir_mode = True
 
182
            transport = get_transport('.')
 
183
            lockable = LockableFiles(transport, 'my-lock')
 
184
            self.assertNotEqual(None, lockable._dir_mode)
 
185
            self.assertEqual(None, lockable._file_mode)
 
186
 
 
187
            LockableFiles._set_file_mode = True
 
188
            transport = get_transport('.')
 
189
            lockable = LockableFiles(transport, 'my-lock')
 
190
            self.assertNotEqual(None, lockable._dir_mode)
 
191
            self.assertNotEqual(None, lockable._file_mode)
 
192
        finally:
 
193
            LockableFiles._set_dir_mode = True
 
194
            LockableFiles._set_file_mode = True
 
195
 
 
196
    def test_new_branch(self):
 
197
        if sys.platform == 'win32':
 
198
            raise TestSkipped('chmod has no effect on win32')
 
199
        #FIXME RBC 20060105 should test branch and repository 
 
200
        # permissions ? 
 
201
        # also, these are BzrBranch format specific things..
 
202
        os.mkdir('a')
 
203
        mode = stat.S_IMODE(os.stat('a').st_mode)
 
204
        t = WorkingTree.create_standalone('.')
 
205
        b = t.branch
 
206
        assertEqualMode(self, mode, b.control_files._dir_mode)
 
207
        assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
 
208
 
 
209
        os.mkdir('b')
 
210
        os.chmod('b', 02777)
 
211
        b = BzrDir.create('b').create_branch()
 
212
        assertEqualMode(self, 02777, b.control_files._dir_mode)
 
213
        assertEqualMode(self, 00666, b.control_files._file_mode)
 
214
        check_mode_r(self, 'b/.bzr', 00666, 02777)
 
215
 
 
216
        os.mkdir('c')
 
217
        os.chmod('c', 02750)
 
218
        b = BzrDir.create('c').create_branch()
 
219
        assertEqualMode(self, 02750, b.control_files._dir_mode)
 
220
        assertEqualMode(self, 00640, b.control_files._file_mode)
 
221
        check_mode_r(self, 'c/.bzr', 00640, 02750)
 
222
 
 
223
        os.mkdir('d')
 
224
        os.chmod('d', 0700)
 
225
        b = BzrDir.create('d').create_branch()
 
226
        assertEqualMode(self, 0700, b.control_files._dir_mode)
 
227
        assertEqualMode(self, 0600, b.control_files._file_mode)
 
228
        check_mode_r(self, 'd/.bzr', 00600, 0700)
 
229
 
 
230
 
 
231
class TestSftpPermissions(TestCaseWithSFTPServer):
 
232
 
 
233
    def test_new_files(self):
 
234
        if sys.platform == 'win32':
 
235
            raise TestSkipped('chmod has no effect on win32')
 
236
        # Though it would be nice to test that SFTP to a server
 
237
        # which does support chmod has the right effect
 
238
 
 
239
        from bzrlib.transport.sftp import SFTPTransport
 
240
 
 
241
        # We don't actually use it directly, we just want to
 
242
        # keep the connection open, since StubSFTPServer only
 
243
        # allows 1 connection
 
244
        _transport = SFTPTransport(self._sftp_url)
 
245
 
 
246
        os.mkdir('local')
 
247
        t_local = WorkingTree.create_standalone('local')
 
248
        b_local = t_local.branch
 
249
        open('local/a', 'wb').write('foo\n')
 
250
        t_local.add('a')
 
251
        t_local.commit('foo')
 
252
 
 
253
        # Delete them because we are modifying the filesystem underneath them
 
254
        chmod_r('local/.bzr', 0644, 0755)
 
255
        check_mode_r(self, 'local/.bzr', 0644, 0755)
 
256
 
 
257
        t = WorkingTree('local')
 
258
        b = t.branch
 
259
        assertEqualMode(self, 0755, b_local.control_files._dir_mode)
 
260
        assertEqualMode(self, 0644, b_local.control_files._file_mode)
 
261
 
 
262
        os.mkdir('sftp')
 
263
        sftp_url = self.get_remote_url('sftp')
 
264
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
265
 
 
266
        b_sftp.pull(b_local)
 
267
        del b_sftp
 
268
        chmod_r('sftp/.bzr', 0644, 0755)
 
269
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
 
270
 
 
271
        b_sftp = Branch.open(sftp_url)
 
272
        assertEqualMode(self, 0755, b_sftp.control_files._dir_mode)
 
273
        assertEqualMode(self, 0644, b_sftp.control_files._file_mode)
 
274
 
 
275
        open('local/a', 'wb').write('foo2\n')
 
276
        t_local.commit('foo2')
 
277
        b_sftp.pull(b_local)
 
278
        # The mode should be maintained after commit
 
279
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
 
280
 
 
281
        open('local/b', 'wb').write('new b\n')
 
282
        t_local.add('b')
 
283
        t_local.commit('new b')
 
284
        b_sftp.pull(b_local)
 
285
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
 
286
 
 
287
        del b_sftp
 
288
        # Recursively update the modes of all files
 
289
        chmod_r('sftp/.bzr', 0664, 0775)
 
290
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
 
291
 
 
292
        b_sftp = Branch.open(sftp_url)
 
293
        assertEqualMode(self, 0775, b_sftp.control_files._dir_mode)
 
294
        assertEqualMode(self, 0664, b_sftp.control_files._file_mode)
 
295
 
 
296
        open('local/a', 'wb').write('foo3\n')
 
297
        t_local.commit('foo3')
 
298
        b_sftp.pull(b_local)
 
299
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
 
300
 
 
301
        open('local/c', 'wb').write('new c\n')
 
302
        t_local.add('c')
 
303
        t_local.commit('new c')
 
304
        b_sftp.pull(b_local)
 
305
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
 
306
 
 
307
    def test_sftp_server_modes(self):
 
308
        if sys.platform == 'win32':
 
309
            raise TestSkipped('chmod has no effect on win32')
 
310
 
 
311
        umask = 0022
 
312
        original_umask = os.umask(umask)
 
313
 
 
314
        try:
 
315
            from bzrlib.transport.sftp import SFTPTransport
 
316
            t = SFTPTransport(self._sftp_url)
 
317
            # Direct access should be masked by umask
 
318
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
 
319
            self.assertTransportMode(t, 'a', 0666 &~umask)
 
320
 
 
321
            # but Transport overrides umask
 
322
            t.put('b', 'txt', mode=0666)
 
323
            self.assertTransportMode(t, 'b', 0666)
 
324
 
 
325
            t._sftp.mkdir('c', mode=0777)
 
326
            self.assertTransportMode(t, 'c', 0777 &~umask)
 
327
 
 
328
            t.mkdir('d', mode=0777)
 
329
            self.assertTransportMode(t, 'd', 0777)
 
330
        finally:
 
331
            os.umask(original_umask)