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.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
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):
53
p = os.path.join(root, d)
56
p = os.path.join(root, f)
57
os.chmod(p, file_mode)
60
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
61
"""Check that all permissions match
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
69
assert os.path.isdir(base)
70
t = get_transport(".")
72
test.assertTransportMode(t, base, dir_mode)
73
for root, dirs, files in os.walk(base):
75
p = os.path.join(root, d)
76
test.assertTransportMode(t, p, dir_mode)
78
p = os.path.join(root, f)
79
test.assertTransportMode(t, p, file_mode)
82
def assertEqualMode(test, mode, mode_test):
83
test.assertEqual(mode, mode_test,
84
'mode mismatch %o != %o' % (mode, mode_test))
87
class TestPermissions(TestCaseWithTransport):
89
def test_new_files(self):
90
if sys.platform == 'win32':
91
raise TestSkipped('chmod has no effect on win32')
93
t = WorkingTree.create_standalone('.')
95
open('a', 'wb').write('foo\n')
99
chmod_r('.bzr', 0644, 0755)
100
check_mode_r(self, '.bzr', 0644, 0755)
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.
109
assertEqualMode(self, 0755, b.control_files._dir_mode)
110
assertEqualMode(self, 0644, b.control_files._file_mode)
112
# Modifying a file shouldn't break the permissions
113
open('a', 'wb').write('foo2\n')
115
# The mode should be maintained after commit
116
check_mode_r(self, '.bzr', 0644, 0755)
118
# Adding a new file should maintain the permissions
119
open('b', 'wb').write('new b\n')
122
check_mode_r(self, '.bzr', 0644, 0755)
124
# Recursively update the modes of all files
125
chmod_r('.bzr', 0664, 0775)
126
check_mode_r(self, '.bzr', 0664, 0775)
129
assertEqualMode(self, 0775, b.control_files._dir_mode)
130
assertEqualMode(self, 0664, b.control_files._file_mode)
132
open('a', 'wb').write('foo3\n')
134
check_mode_r(self, '.bzr', 0664, 0775)
136
open('c', 'wb').write('new c\n')
139
check_mode_r(self, '.bzr', 0664, 0775)
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)
147
assertEqualMode(self, 02775, b.control_files._dir_mode)
148
assertEqualMode(self, 0664, b.control_files._file_mode)
150
open('a', 'wb').write('foo4\n')
152
check_mode_r(self, '.bzr', 0664, 02775)
154
open('d', 'wb').write('new d\n')
157
check_mode_r(self, '.bzr', 0664, 02775)
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
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)
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)
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)
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)
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)
193
LockableFiles._set_dir_mode = True
194
LockableFiles._set_file_mode = True
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
201
# also, these are BzrBranch format specific things..
203
mode = stat.S_IMODE(os.stat('a').st_mode)
204
t = WorkingTree.create_standalone('.')
206
assertEqualMode(self, mode, b.control_files._dir_mode)
207
assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
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)
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)
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)
231
class TestSftpPermissions(TestCaseWithSFTPServer):
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
239
from bzrlib.transport.sftp import SFTPTransport
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)
247
t_local = WorkingTree.create_standalone('local')
248
b_local = t_local.branch
249
open('local/a', 'wb').write('foo\n')
251
t_local.commit('foo')
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)
257
t = WorkingTree('local')
259
assertEqualMode(self, 0755, b_local.control_files._dir_mode)
260
assertEqualMode(self, 0644, b_local.control_files._file_mode)
263
sftp_url = self.get_remote_url('sftp')
264
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
268
chmod_r('sftp/.bzr', 0644, 0755)
269
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
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)
275
open('local/a', 'wb').write('foo2\n')
276
t_local.commit('foo2')
278
# The mode should be maintained after commit
279
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
281
open('local/b', 'wb').write('new b\n')
283
t_local.commit('new b')
285
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
288
# Recursively update the modes of all files
289
chmod_r('sftp/.bzr', 0664, 0775)
290
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
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)
296
open('local/a', 'wb').write('foo3\n')
297
t_local.commit('foo3')
299
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
301
open('local/c', 'wb').write('new c\n')
303
t_local.commit('new c')
305
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
307
def test_sftp_server_modes(self):
308
if sys.platform == 'win32':
309
raise TestSkipped('chmod has no effect on win32')
312
original_umask = os.umask(umask)
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)
321
# but Transport overrides umask
322
t.put('b', 'txt', mode=0666)
323
self.assertTransportMode(t, 'b', 0666)
325
t._sftp.mkdir('c', mode=0777)
326
self.assertTransportMode(t, 'c', 0777 &~umask)
328
t.mkdir('d', mode=0777)
329
self.assertTransportMode(t, 'd', 0777)
331
os.umask(original_umask)