/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
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
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
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
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?
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
31
"""
32
33
import os
34
import sys
35
import stat
36
37
from bzrlib.branch import Branch
38
from bzrlib.tests import TestCaseInTempDir, TestSkipped
1185.50.20 by John Arbash Meinel
merge permissions branch, also fixup tests so they are lined up with bzr.dev to help prevent conflicts.
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
40
from bzrlib.tests.test_transport import check_mode
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
41
42
43
def chmod_r(base, file_mode, dir_mode):
44
    """Recursively chmod from a base directory"""
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
45
    assert os.path.isdir(base)
46
    os.chmod(base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
47
    for root, dirs, files in os.walk(base):
48
        for d in dirs:
49
            p = os.path.join(root, d)
50
            os.chmod(p, dir_mode)
51
        for f in files:
52
            p = os.path.join(root, f)
53
            os.chmod(p, file_mode)
54
55
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
56
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
57
    """Check that all permissions match
58
59
    :param test: The TestCase being run
60
    :param base: The path to the root directory to check
61
    :param file_mode: The mode for all files
62
    :param dir_mode: The mode for all directories
63
    :param include_base: If false, only check the subdirectories
64
    """
65
    assert os.path.isdir(base)
66
    if include_base:
67
        check_mode(test, base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
68
    for root, dirs, files in os.walk(base):
69
        for d in dirs:
70
            p = os.path.join(root, d)
71
            check_mode(test, p, dir_mode)
72
        for f in files:
73
            p = os.path.join(root, f)
74
            check_mode(test, p, file_mode)
75
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
76
def assertEqualMode(test, mode, mode_test):
77
    test.assertEqual(mode, mode_test,
78
                     'mode mismatch %o != %o' % (mode, mode_test))
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
79
80
class TestPermissions(TestCaseInTempDir):
81
82
    def test_new_files(self):
83
        if sys.platform == 'win32':
84
            raise TestSkipped('chmod has no effect on win32')
85
86
        b = Branch.initialize(u'.')
87
        t = b.working_tree()
88
        open('a', 'wb').write('foo\n')
89
        t.add('a')
90
        t.commit('foo')
91
92
        # Delete them because we are modifying the filesystem underneath them
93
        del b, t 
94
        chmod_r('.bzr', 0644, 0755)
95
        check_mode_r(self, '.bzr', 0644, 0755)
96
97
        b = Branch.open('.')
98
        t = b.working_tree()
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
99
        assertEqualMode(self, 0755, b._dir_mode)
100
        assertEqualMode(self, 0644, b._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
101
102
        # Modifying a file shouldn't break the permissions
103
        open('a', 'wb').write('foo2\n')
104
        t.commit('foo2')
105
        # The mode should be maintained after commit
106
        check_mode_r(self, '.bzr', 0644, 0755)
107
108
        # Adding a new file should maintain the permissions
109
        open('b', 'wb').write('new b\n')
110
        t.add('b')
111
        t.commit('new b')
112
        check_mode_r(self, '.bzr', 0644, 0755)
113
114
        del b, t
115
        # Recursively update the modes of all files
116
        chmod_r('.bzr', 0664, 0775)
117
        check_mode_r(self, '.bzr', 0664, 0775)
118
        b = Branch.open('.')
119
        t = b.working_tree()
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
120
        assertEqualMode(self, 0775, b._dir_mode)
121
        assertEqualMode(self, 0664, b._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
122
123
        open('a', 'wb').write('foo3\n')
124
        t.commit('foo3')
125
        check_mode_r(self, '.bzr', 0664, 0775)
126
127
        open('c', 'wb').write('new c\n')
128
        t.add('c')
129
        t.commit('new c')
130
        check_mode_r(self, '.bzr', 0664, 0775)
131
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
132
        # Test the group sticky bit
133
        del b, t
134
        # Recursively update the modes of all files
135
        chmod_r('.bzr', 0664, 02775)
136
        check_mode_r(self, '.bzr', 0664, 02775)
137
        b = Branch.open('.')
138
        t = b.working_tree()
139
        assertEqualMode(self, 02775, b._dir_mode)
140
        assertEqualMode(self, 0664, b._file_mode)
141
142
        open('a', 'wb').write('foo4\n')
143
        t.commit('foo4')
144
        check_mode_r(self, '.bzr', 0664, 02775)
145
146
        open('d', 'wb').write('new d\n')
147
        t.add('d')
148
        t.commit('new d')
149
        check_mode_r(self, '.bzr', 0664, 02775)
150
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
151
    def test_disable_set_mode(self):
152
        # TODO: jam 20051215 Ultimately, this test should probably test that
153
        #                    extra chmod calls aren't being made
154
        import bzrlib.branch
155
        try:
156
            b = Branch.initialize(u'.')
157
            self.assertNotEqual(None, b._dir_mode)
158
            self.assertNotEqual(None, b._file_mode)
159
160
            bzrlib.branch.BzrBranch._set_dir_mode = False
161
            b = Branch.open(u'.')
162
            self.assertEqual(None, b._dir_mode)
163
            self.assertNotEqual(None, b._file_mode)
164
165
            bzrlib.branch.BzrBranch._set_file_mode = False
166
            b = Branch.open(u'.')
167
            self.assertEqual(None, b._dir_mode)
168
            self.assertEqual(None, b._file_mode)
169
170
            bzrlib.branch.BzrBranch._set_dir_mode = True
171
            b = Branch.open(u'.')
172
            self.assertNotEqual(None, b._dir_mode)
173
            self.assertEqual(None, b._file_mode)
174
175
            bzrlib.branch.BzrBranch._set_file_mode = True
176
            b = Branch.open(u'.')
177
            self.assertNotEqual(None, b._dir_mode)
178
            self.assertNotEqual(None, b._file_mode)
179
        finally:
180
            bzrlib.branch.BzrBranch._set_dir_mode = True
181
            bzrlib.branch.BzrBranch._set_file_mode = True
182
183
    def test_new_branch(self):
184
        if sys.platform == 'win32':
185
            raise TestSkipped('chmod has no effect on win32')
186
187
        os.mkdir('a')
188
        mode = stat.S_IMODE(os.stat('a').st_mode)
189
        b = Branch.initialize('a')
190
        assertEqualMode(self, mode, b._dir_mode)
191
        assertEqualMode(self, mode & ~07111, b._file_mode)
192
193
        os.mkdir('b')
194
        os.chmod('b', 02777)
195
        b = Branch.initialize('b')
196
        assertEqualMode(self, 02777, b._dir_mode)
197
        assertEqualMode(self, 00666, b._file_mode)
198
        check_mode_r(self, 'b/.bzr', 00666, 02777)
199
200
        os.mkdir('c')
201
        os.chmod('c', 02750)
202
        b = Branch.initialize('c')
203
        assertEqualMode(self, 02750, b._dir_mode)
204
        assertEqualMode(self, 00640, b._file_mode)
205
        check_mode_r(self, 'c/.bzr', 00640, 02750)
206
207
        os.mkdir('d')
208
        os.chmod('d', 0700)
209
        b = Branch.initialize('d')
210
        assertEqualMode(self, 0700, b._dir_mode)
211
        assertEqualMode(self, 0600, b._file_mode)
212
        check_mode_r(self, 'd/.bzr', 00600, 0700)
213
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
214
215
class TestSftpPermissions(TestCaseWithSFTPServer):
216
217
    def test_new_files(self):
218
        if sys.platform == 'win32':
219
            raise TestSkipped('chmod has no effect on win32')
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
220
        # Though it would be nice to test that SFTP to a server
221
        # which does support chmod has the right effect
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
222
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
223
        from bzrlib.transport.sftp import SFTPTransport
224
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
225
        # We don't actually use it directly, we just want to
226
        # keep the connection open, since StubSFTPServer only
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
227
        # allows 1 connection
228
        self.delayed_setup()
229
        _transport = SFTPTransport(self._sftp_url)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
230
231
        os.mkdir('local')
232
        b_local = Branch.initialize(u'local')
233
        t_local = b_local.working_tree()
234
        open('local/a', 'wb').write('foo\n')
235
        t_local.add('a')
236
        t_local.commit('foo')
237
238
        # Delete them because we are modifying the filesystem underneath them
239
        del b_local, t_local 
240
        chmod_r('local/.bzr', 0644, 0755)
241
        check_mode_r(self, 'local/.bzr', 0644, 0755)
242
243
        b_local = Branch.open(u'local')
244
        t_local = b_local.working_tree()
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
245
        assertEqualMode(self, 0755, b_local._dir_mode)
246
        assertEqualMode(self, 0644, b_local._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
247
248
        os.mkdir('sftp')
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
249
        # Why does self._sftp_url end with a slash????
250
        sftp_url = self._sftp_url + 'sftp'
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
251
        b_sftp = Branch.initialize(sftp_url)
252
253
        b_sftp.pull(b_local)
254
        del b_sftp
255
        chmod_r('sftp/.bzr', 0644, 0755)
256
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
257
258
        b_sftp = Branch.open(sftp_url)
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
259
        assertEqualMode(self, 0755, b_sftp._dir_mode)
260
        assertEqualMode(self, 0644, b_sftp._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
261
262
        open('local/a', 'wb').write('foo2\n')
263
        t_local.commit('foo2')
264
        b_sftp.pull(b_local)
265
        # The mode should be maintained after commit
266
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
267
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
268
        open('local/b', 'wb').write('new b\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
269
        t_local.add('b')
270
        t_local.commit('new b')
271
        b_sftp.pull(b_local)
272
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
273
274
        del b_sftp
275
        # Recursively update the modes of all files
276
        chmod_r('sftp/.bzr', 0664, 0775)
277
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
278
279
        b_sftp = Branch.open(sftp_url)
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
280
        assertEqualMode(self, 0775, b_sftp._dir_mode)
281
        assertEqualMode(self, 0664, b_sftp._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
282
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
283
        open('local/a', 'wb').write('foo3\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
284
        t_local.commit('foo3')
285
        b_sftp.pull(b_local)
286
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
287
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
288
        open('local/c', 'wb').write('new c\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
289
        t_local.add('c')
290
        t_local.commit('new c')
291
        b_sftp.pull(b_local)
292
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
293
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
294
    def test_sftp_server_modes(self):
295
        if sys.platform == 'win32':
296
            raise TestSkipped('chmod has no effect on win32')
297
298
        umask = 0022
299
        original_umask = os.umask(umask)
300
301
        try:
302
            from bzrlib.transport.sftp import SFTPTransport
303
            self.delayed_setup()
304
            t = SFTPTransport(self._sftp_url)
305
            # Direct access should be masked by umask
306
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
307
            check_mode(self, 'a', 0666 &~umask)
308
309
            # but Transport overrides umask
310
            t.put('b', 'txt', mode=0666)
311
            check_mode(self, 'b', 0666)
312
313
            t._sftp.mkdir('c', mode=0777)
314
            check_mode(self, 'c', 0777 &~umask)
315
316
            t.mkdir('d', mode=0777)
317
            check_mode(self, 'd', 0777)
318
        finally:
319
            os.umask(original_umask)
320
321