1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
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.
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.
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
19
"""Tests for bzr setting permissions.
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.
24
In the future, when we have Repository/Branch/Checkout information, the
25
permissions should be inherited individually, rather than all be the same.
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?
36
from StringIO import StringIO
38
from bzrlib.branch import Branch
39
from bzrlib.lockable_files import LockableFiles
40
from bzrlib.tests import TestCaseWithTransport, TestSkipped
41
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
42
from bzrlib.transport import get_transport
43
from bzrlib.workingtree import WorkingTree
46
def chmod_r(base, file_mode, dir_mode):
47
"""Recursively chmod from a base directory"""
48
assert os.path.isdir(base)
49
os.chmod(base, dir_mode)
50
for root, dirs, files in os.walk(base):
52
p = os.path.join(root, d)
55
p = os.path.join(root, f)
56
os.chmod(p, file_mode)
59
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
60
"""Check that all permissions match
62
:param test: The TestCase being run
63
:param base: The path to the root directory to check
64
:param file_mode: The mode for all files
65
:param dir_mode: The mode for all directories
66
:param include_base: If false, only check the subdirectories
68
assert os.path.isdir(base)
69
t = get_transport(".")
71
test.assertTransportMode(t, base, dir_mode)
72
for root, dirs, files in os.walk(base):
74
p = os.path.join(root, d)
75
test.assertTransportMode(t, p, dir_mode)
77
p = os.path.join(root, f)
78
test.assertTransportMode(t, p, file_mode)
81
def assertEqualMode(test, mode, mode_test):
82
test.assertEqual(mode, mode_test,
83
'mode mismatch %o != %o' % (mode, mode_test))
86
class TestPermissions(TestCaseWithTransport):
88
def test_new_files(self):
89
if sys.platform == 'win32':
90
raise TestSkipped('chmod has no effect on win32')
92
t = WorkingTree.create_standalone('.')
94
open('a', 'wb').write('foo\n')
98
# Delete them because we are modifying the filesystem underneath them
100
chmod_r('.bzr', 0644, 0755)
101
check_mode_r(self, '.bzr', 0644, 0755)
105
assertEqualMode(self, 0755, b.control_files._dir_mode)
106
assertEqualMode(self, 0644, b.control_files._file_mode)
108
# Modifying a file shouldn't break the permissions
109
open('a', 'wb').write('foo2\n')
111
# The mode should be maintained after commit
112
check_mode_r(self, '.bzr', 0644, 0755)
114
# Adding a new file should maintain the permissions
115
open('b', 'wb').write('new b\n')
118
check_mode_r(self, '.bzr', 0644, 0755)
121
# Recursively update the modes of all files
122
chmod_r('.bzr', 0664, 0775)
123
check_mode_r(self, '.bzr', 0664, 0775)
126
assertEqualMode(self, 0775, b.control_files._dir_mode)
127
assertEqualMode(self, 0664, b.control_files._file_mode)
129
open('a', 'wb').write('foo3\n')
131
check_mode_r(self, '.bzr', 0664, 0775)
133
open('c', 'wb').write('new c\n')
136
check_mode_r(self, '.bzr', 0664, 0775)
138
# Test the group sticky bit
140
# Recursively update the modes of all files
141
chmod_r('.bzr', 0664, 02775)
142
check_mode_r(self, '.bzr', 0664, 02775)
145
assertEqualMode(self, 02775, b.control_files._dir_mode)
146
assertEqualMode(self, 0664, b.control_files._file_mode)
148
open('a', 'wb').write('foo4\n')
150
check_mode_r(self, '.bzr', 0664, 02775)
152
open('d', 'wb').write('new d\n')
155
check_mode_r(self, '.bzr', 0664, 02775)
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
161
transport = get_transport(self.get_url())
162
transport.put('my-lock', StringIO(''))
163
lockable = LockableFiles(transport, 'my-lock')
164
self.assertNotEqual(None, lockable._dir_mode)
165
self.assertNotEqual(None, lockable._file_mode)
167
LockableFiles._set_dir_mode = False
168
transport = get_transport('.')
169
lockable = LockableFiles(transport, 'my-lock')
170
self.assertEqual(None, lockable._dir_mode)
171
self.assertNotEqual(None, lockable._file_mode)
173
LockableFiles._set_file_mode = False
174
transport = get_transport('.')
175
lockable = LockableFiles(transport, 'my-lock')
176
self.assertEqual(None, lockable._dir_mode)
177
self.assertEqual(None, lockable._file_mode)
179
LockableFiles._set_dir_mode = True
180
transport = get_transport('.')
181
lockable = LockableFiles(transport, 'my-lock')
182
self.assertNotEqual(None, lockable._dir_mode)
183
self.assertEqual(None, lockable._file_mode)
185
LockableFiles._set_file_mode = True
186
transport = get_transport('.')
187
lockable = LockableFiles(transport, 'my-lock')
188
self.assertNotEqual(None, lockable._dir_mode)
189
self.assertNotEqual(None, lockable._file_mode)
191
LockableFiles._set_dir_mode = True
192
LockableFiles._set_file_mode = True
194
def test_new_branch(self):
195
if sys.platform == 'win32':
196
raise TestSkipped('chmod has no effect on win32')
197
#FIXME RBC 20060105 should test branch and repository
199
# also, these are BzrBranch format specific things..
201
mode = stat.S_IMODE(os.stat('a').st_mode)
202
t = WorkingTree.create_standalone('.')
204
assertEqualMode(self, mode, b.control_files._dir_mode)
205
assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
209
b = Branch.create('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)
216
b = Branch.create('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)
223
b = Branch.create('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)
229
class TestSftpPermissions(TestCaseWithSFTPServer):
231
def test_new_files(self):
232
if sys.platform == 'win32':
233
raise TestSkipped('chmod has no effect on win32')
234
# Though it would be nice to test that SFTP to a server
235
# which does support chmod has the right effect
237
from bzrlib.transport.sftp import SFTPTransport
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)
245
t_local = WorkingTree.create_standalone('local')
246
b_local = t_local.branch
247
open('local/a', 'wb').write('foo\n')
249
t_local.commit('foo')
251
# Delete them because we are modifying the filesystem underneath them
253
chmod_r('local/.bzr', 0644, 0755)
254
check_mode_r(self, 'local/.bzr', 0644, 0755)
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)
262
sftp_url = self.get_remote_url('sftp')
263
b_sftp = Branch.create(sftp_url)
267
chmod_r('sftp/.bzr', 0644, 0755)
268
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
270
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)
274
open('local/a', 'wb').write('foo2\n')
275
t_local.commit('foo2')
277
# The mode should be maintained after commit
278
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
280
open('local/b', 'wb').write('new b\n')
282
t_local.commit('new b')
284
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
287
# Recursively update the modes of all files
288
chmod_r('sftp/.bzr', 0664, 0775)
289
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
291
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)
295
open('local/a', 'wb').write('foo3\n')
296
t_local.commit('foo3')
298
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
300
open('local/c', 'wb').write('new c\n')
302
t_local.commit('new c')
304
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
306
def test_sftp_server_modes(self):
307
if sys.platform == 'win32':
308
raise TestSkipped('chmod has no effect on win32')
311
original_umask = os.umask(umask)
314
from bzrlib.transport.sftp import SFTPTransport
315
t = SFTPTransport(self._sftp_url)
316
# Direct access should be masked by umask
317
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
318
self.assertTransportMode(t, 'a', 0666 &~umask)
320
# but Transport overrides umask
321
t.put('b', 'txt', mode=0666)
322
self.assertTransportMode(t, 'b', 0666)
324
t._sftp.mkdir('c', mode=0777)
325
self.assertTransportMode(t, 'c', 0777 &~umask)
327
t.mkdir('d', mode=0777)
328
self.assertTransportMode(t, 'd', 0777)
330
os.umask(original_umask)