/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

  • Committer: John Arbash Meinel
  • Date: 2006-01-03 15:10:11 UTC
  • mto: (1185.50.36 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1536.
  • Revision ID: john@arbash-meinel.com-20060103151011-2ad890a2ff1475a4
Deprecating bzrlib.get_bzr_revision, in favor of bzrlib.builtins._get_bzr_branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
19
"""Tests for bzr setting permissions.
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 cStringIO import StringIO
37
 
import urllib
38
36
 
39
37
from bzrlib.branch import Branch
40
 
from bzrlib.bzrdir import BzrDir
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):
48
44
    """Recursively chmod from a base directory"""
 
45
    assert os.path.isdir(base)
49
46
    os.chmod(base, dir_mode)
50
47
    for root, dirs, files in os.walk(base):
51
48
        for d in dirs:
65
62
    :param dir_mode: The mode for all directories
66
63
    :param include_base: If false, only check the subdirectories
67
64
    """
68
 
    t = get_transport(".")
 
65
    assert os.path.isdir(base)
69
66
    if include_base:
70
 
        test.assertTransportMode(t, base, dir_mode)
 
67
        check_mode(test, base, dir_mode)
71
68
    for root, dirs, files in os.walk(base):
72
69
        for d in dirs:
73
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
74
 
            test.assertTransportMode(t, p, dir_mode)
 
70
            p = os.path.join(root, d)
 
71
            check_mode(test, p, dir_mode)
75
72
        for f in files:
76
73
            p = os.path.join(root, f)
77
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
78
 
            test.assertTransportMode(t, p, file_mode)
79
 
 
80
 
 
81
 
class TestPermissions(TestCaseWithTransport):
 
74
            check_mode(test, p, file_mode)
 
75
 
 
76
def assertEqualMode(test, mode, mode_test):
 
77
    test.assertEqual(mode, mode_test,
 
78
                     'mode mismatch %o != %o' % (mode, mode_test))
 
79
 
 
80
class TestPermissions(TestCaseInTempDir):
82
81
 
83
82
    def test_new_files(self):
84
83
        if sys.platform == 'win32':
85
84
            raise TestSkipped('chmod has no effect on win32')
86
85
 
87
 
        t = self.make_branch_and_tree('.')
88
 
        b = t.branch
 
86
        b = Branch.initialize(u'.')
 
87
        t = b.working_tree()
89
88
        open('a', 'wb').write('foo\n')
90
 
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
91
 
        t.add('a', 'CAPS-ID')
 
89
        t.add('a')
92
90
        t.commit('foo')
93
91
 
 
92
        # Delete them because we are modifying the filesystem underneath them
 
93
        del b, t 
94
94
        chmod_r('.bzr', 0644, 0755)
95
95
        check_mode_r(self, '.bzr', 0644, 0755)
96
96
 
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)
106
 
        self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
107
 
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())
 
97
        b = Branch.open('.')
 
98
        t = b.working_tree()
 
99
        assertEqualMode(self, 0755, b._dir_mode)
 
100
        assertEqualMode(self, 0644, b._file_mode)
108
101
 
109
102
        # Modifying a file shouldn't break the permissions
110
103
        open('a', 'wb').write('foo2\n')
118
111
        t.commit('new b')
119
112
        check_mode_r(self, '.bzr', 0644, 0755)
120
113
 
 
114
        del b, t
121
115
        # Recursively update the modes of all files
122
116
        chmod_r('.bzr', 0664, 0775)
123
117
        check_mode_r(self, '.bzr', 0664, 0775)
124
 
        t = WorkingTree.open('.')
125
 
        b = t.branch
126
 
        self.assertEqualMode(0775, b.control_files._dir_mode)
127
 
        self.assertEqualMode(0664, b.control_files._file_mode)
128
 
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
129
 
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
 
118
        b = Branch.open('.')
 
119
        t = b.working_tree()
 
120
        assertEqualMode(self, 0775, b._dir_mode)
 
121
        assertEqualMode(self, 0664, b._file_mode)
130
122
 
131
123
        open('a', 'wb').write('foo3\n')
132
124
        t.commit('foo3')
137
129
        t.commit('new c')
138
130
        check_mode_r(self, '.bzr', 0664, 0775)
139
131
 
140
 
    def test_new_files_group_sticky_bit(self):
141
 
        if sys.platform == 'win32':
142
 
            raise TestSkipped('chmod has no effect on win32')
143
 
        elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
144
 
            # OS X (and FreeBSD) create temp dirs with the 'wheel' group, which
145
 
            # users are not likely to be in, and this prevents us from setting
146
 
            # the sgid bit
147
 
            os.chown(self.test_dir, os.getuid(), os.getgid())
148
 
 
149
 
        t = self.make_branch_and_tree('.')
150
 
        b = t.branch
151
 
 
152
132
        # Test the group sticky bit
 
133
        del b, t
153
134
        # Recursively update the modes of all files
154
135
        chmod_r('.bzr', 0664, 02775)
155
136
        check_mode_r(self, '.bzr', 0664, 02775)
156
 
        t = WorkingTree.open('.')
157
 
        b = t.branch
158
 
        self.assertEqualMode(02775, b.control_files._dir_mode)
159
 
        self.assertEqualMode(0664, b.control_files._file_mode)
160
 
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
161
 
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
 
137
        b = Branch.open('.')
 
138
        t = b.working_tree()
 
139
        assertEqualMode(self, 02775, b._dir_mode)
 
140
        assertEqualMode(self, 0664, b._file_mode)
162
141
 
163
142
        open('a', 'wb').write('foo4\n')
164
143
        t.commit('foo4')
169
148
        t.commit('new d')
170
149
        check_mode_r(self, '.bzr', 0664, 02775)
171
150
 
 
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
 
172
214
 
173
215
class TestSftpPermissions(TestCaseWithSFTPServer):
174
216
 
178
220
        # Though it would be nice to test that SFTP to a server
179
221
        # which does support chmod has the right effect
180
222
 
181
 
        # bodge around for stubsftpserver not letting use connect
182
 
        # more than once
183
 
        _t = get_transport(self.get_url())
 
223
        from bzrlib.transport.sftp import SFTPTransport
 
224
 
 
225
        # We don't actually use it directly, we just want to
 
226
        # keep the connection open, since StubSFTPServer only
 
227
        # allows 1 connection
 
228
        self.delayed_setup()
 
229
        _transport = SFTPTransport(self._sftp_url)
184
230
 
185
231
        os.mkdir('local')
186
 
        t_local = self.make_branch_and_tree('local')
187
 
        b_local = t_local.branch
 
232
        b_local = Branch.initialize(u'local')
 
233
        t_local = b_local.working_tree()
188
234
        open('local/a', 'wb').write('foo\n')
189
235
        t_local.add('a')
190
236
        t_local.commit('foo')
191
237
 
192
238
        # Delete them because we are modifying the filesystem underneath them
 
239
        del b_local, t_local 
193
240
        chmod_r('local/.bzr', 0644, 0755)
194
241
        check_mode_r(self, 'local/.bzr', 0644, 0755)
195
242
 
196
 
        t = WorkingTree.open('local')
197
 
        b_local = t.branch
198
 
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
199
 
        self.assertEqualMode(0644, b_local.control_files._file_mode)
200
 
        self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
201
 
        self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
 
243
        b_local = Branch.open(u'local')
 
244
        t_local = b_local.working_tree()
 
245
        assertEqualMode(self, 0755, b_local._dir_mode)
 
246
        assertEqualMode(self, 0644, b_local._file_mode)
202
247
 
203
248
        os.mkdir('sftp')
204
 
        sftp_url = self.get_url('sftp')
205
 
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
249
        # Why does self._sftp_url end with a slash????
 
250
        sftp_url = self._sftp_url + 'sftp'
 
251
        b_sftp = Branch.initialize(sftp_url)
206
252
 
207
253
        b_sftp.pull(b_local)
208
254
        del b_sftp
210
256
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
211
257
 
212
258
        b_sftp = Branch.open(sftp_url)
213
 
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
214
 
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
215
 
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
216
 
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
 
259
        assertEqualMode(self, 0755, b_sftp._dir_mode)
 
260
        assertEqualMode(self, 0644, b_sftp._file_mode)
217
261
 
218
262
        open('local/a', 'wb').write('foo2\n')
219
263
        t_local.commit('foo2')
233
277
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
234
278
 
235
279
        b_sftp = Branch.open(sftp_url)
236
 
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
237
 
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
238
 
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
239
 
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
 
280
        assertEqualMode(self, 0775, b_sftp._dir_mode)
 
281
        assertEqualMode(self, 0664, b_sftp._file_mode)
240
282
 
241
283
        open('local/a', 'wb').write('foo3\n')
242
284
        t_local.commit('foo3')
257
299
        original_umask = os.umask(umask)
258
300
 
259
301
        try:
260
 
            t = get_transport(self.get_url())
 
302
            from bzrlib.transport.sftp import SFTPTransport
 
303
            self.delayed_setup()
 
304
            t = SFTPTransport(self._sftp_url)
261
305
            # Direct access should be masked by umask
262
306
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
263
 
            self.assertTransportMode(t, 'a', 0666 &~umask)
 
307
            check_mode(self, 'a', 0666 &~umask)
264
308
 
265
309
            # but Transport overrides umask
266
 
            t.put_bytes('b', 'txt', mode=0666)
267
 
            self.assertTransportMode(t, 'b', 0666)
 
310
            t.put('b', 'txt', mode=0666)
 
311
            check_mode(self, 'b', 0666)
268
312
 
269
 
            t._get_sftp().mkdir('c', mode=0777)
270
 
            self.assertTransportMode(t, 'c', 0777 &~umask)
 
313
            t._sftp.mkdir('c', mode=0777)
 
314
            check_mode(self, 'c', 0777 &~umask)
271
315
 
272
316
            t.mkdir('d', mode=0777)
273
 
            self.assertTransportMode(t, 'd', 0777)
 
317
            check_mode(self, 'd', 0777)
274
318
        finally:
275
319
            os.umask(original_umask)
 
320
 
 
321