~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Ian Clatworthy
  • Date: 2007-08-13 14:33:10 UTC
  • mto: (2733.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2734.
  • Revision ID: ian.clatworthy@internode.on.net-20070813143310-twhj4la0qnupvze8
Added Quick Start Summary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 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
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.
26
26
"""
27
27
 
28
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
 
29
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just 
30
30
#                    defined by the local umask. This isn't terrible, is it
31
31
#                    the truly desired behavior?
32
 
 
 
32
 
33
33
import os
34
34
import sys
 
35
import stat
 
36
from cStringIO import StringIO
35
37
import urllib
36
38
 
37
 
from bzrlib import transport
38
39
from bzrlib.branch import Branch
39
40
from bzrlib.bzrdir import BzrDir
 
41
from bzrlib.lockable_files import LockableFiles, TransportLock
40
42
from bzrlib.tests import TestCaseWithTransport, TestSkipped
41
43
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
44
from bzrlib.transport import get_transport
42
45
from bzrlib.workingtree import WorkingTree
43
46
 
44
47
 
45
48
def chmod_r(base, file_mode, dir_mode):
46
49
    """Recursively chmod from a base directory"""
 
50
    assert os.path.isdir(base)
47
51
    os.chmod(base, dir_mode)
48
52
    for root, dirs, files in os.walk(base):
49
53
        for d in dirs:
63
67
    :param dir_mode: The mode for all directories
64
68
    :param include_base: If false, only check the subdirectories
65
69
    """
66
 
    t = test.get_transport()
 
70
    assert os.path.isdir(base)
 
71
    t = get_transport(".")
67
72
    if include_base:
68
73
        test.assertTransportMode(t, base, dir_mode)
69
74
    for root, dirs, files in os.walk(base):
94
99
 
95
100
        # although we are modifying the filesystem
96
101
        # underneath the objects, they are not locked, and thus it must
97
 
        # be safe for most operations. But here we want to observe a
 
102
        # be safe for most operations. But here we want to observe a 
98
103
        # mode change in the control bits, which current do not refresh
99
104
        # when a new lock is taken out.
100
105
        t = WorkingTree.open('.')
101
106
        b = t.branch
102
107
        self.assertEqualMode(0755, b.control_files._dir_mode)
103
108
        self.assertEqualMode(0644, b.control_files._file_mode)
104
 
        self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
105
 
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())
106
109
 
107
110
        # Modifying a file shouldn't break the permissions
108
111
        open('a', 'wb').write('foo2\n')
123
126
        b = t.branch
124
127
        self.assertEqualMode(0775, b.control_files._dir_mode)
125
128
        self.assertEqualMode(0664, b.control_files._file_mode)
126
 
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
127
 
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
128
129
 
129
130
        open('a', 'wb').write('foo3\n')
130
131
        t.commit('foo3')
135
136
        t.commit('new c')
136
137
        check_mode_r(self, '.bzr', 0664, 0775)
137
138
 
138
 
    def test_new_files_group_sticky_bit(self):
139
 
        if sys.platform == 'win32':
140
 
            raise TestSkipped('chmod has no effect on win32')
141
 
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
142
 
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
143
 
            # which users are not likely to be in, and this prevents us from
144
 
            # setting the sgid bit
145
 
            os.chown(self.test_dir, os.getuid(), os.getgid())
146
 
 
147
 
        t = self.make_branch_and_tree('.')
148
 
        b = t.branch
149
 
 
150
139
        # Test the group sticky bit
151
140
        # Recursively update the modes of all files
152
141
        chmod_r('.bzr', 0664, 02775)
155
144
        b = t.branch
156
145
        self.assertEqualMode(02775, b.control_files._dir_mode)
157
146
        self.assertEqualMode(0664, b.control_files._file_mode)
158
 
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
159
 
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
160
147
 
161
148
        open('a', 'wb').write('foo4\n')
162
149
        t.commit('foo4')
167
154
        t.commit('new d')
168
155
        check_mode_r(self, '.bzr', 0664, 02775)
169
156
 
 
157
    def test_disable_set_mode(self):
 
158
        # TODO: jam 20051215 Ultimately, this test should probably test that
 
159
        #                    extra chmod calls aren't being made
 
160
        try:
 
161
            transport = get_transport(self.get_url())
 
162
            transport.put_bytes('my-lock', '')
 
163
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
 
164
            self.assertNotEqual(None, lockable._dir_mode)
 
165
            self.assertNotEqual(None, lockable._file_mode)
 
166
 
 
167
            LockableFiles._set_dir_mode = False
 
168
            transport = get_transport('.')
 
169
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
 
170
            self.assertEqual(None, lockable._dir_mode)
 
171
            self.assertNotEqual(None, lockable._file_mode)
 
172
 
 
173
            LockableFiles._set_file_mode = False
 
174
            transport = get_transport('.')
 
175
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
 
176
            self.assertEqual(None, lockable._dir_mode)
 
177
            self.assertEqual(None, lockable._file_mode)
 
178
 
 
179
            LockableFiles._set_dir_mode = True
 
180
            transport = get_transport('.')
 
181
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
 
182
            self.assertNotEqual(None, lockable._dir_mode)
 
183
            self.assertEqual(None, lockable._file_mode)
 
184
 
 
185
            LockableFiles._set_file_mode = True
 
186
            transport = get_transport('.')
 
187
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
 
188
            self.assertNotEqual(None, lockable._dir_mode)
 
189
            self.assertNotEqual(None, lockable._file_mode)
 
190
        finally:
 
191
            LockableFiles._set_dir_mode = True
 
192
            LockableFiles._set_file_mode = True
 
193
 
170
194
 
171
195
class TestSftpPermissions(TestCaseWithSFTPServer):
172
196
 
178
202
 
179
203
        # bodge around for stubsftpserver not letting use connect
180
204
        # more than once
181
 
        _t = self.get_transport()
 
205
        _t = get_transport(self.get_url())
182
206
 
183
207
        os.mkdir('local')
184
208
        t_local = self.make_branch_and_tree('local')
195
219
        b_local = t.branch
196
220
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
197
221
        self.assertEqualMode(0644, b_local.control_files._file_mode)
198
 
        self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
199
 
        self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
200
222
 
201
223
        os.mkdir('sftp')
202
224
        sftp_url = self.get_url('sftp')
210
232
        b_sftp = Branch.open(sftp_url)
211
233
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
212
234
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
213
 
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
214
 
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
215
235
 
216
236
        open('local/a', 'wb').write('foo2\n')
217
237
        t_local.commit('foo2')
233
253
        b_sftp = Branch.open(sftp_url)
234
254
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
235
255
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
236
 
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
237
 
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
238
256
 
239
257
        open('local/a', 'wb').write('foo3\n')
240
258
        t_local.commit('foo3')
255
273
        original_umask = os.umask(umask)
256
274
 
257
275
        try:
258
 
            t = self.get_transport()
 
276
            t = get_transport(self.get_url())
259
277
            # Direct access should be masked by umask
260
278
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
261
279
            self.assertTransportMode(t, 'a', 0666 &~umask)