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.
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?
37
from bzrlib.branch import Branch
38
from bzrlib.tests import TestCaseInTempDir, TestSkipped
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
40
from bzrlib.tests.test_transport import check_mode
43
def chmod_r(base, file_mode, dir_mode):
44
"""Recursively chmod from a base directory"""
45
assert os.path.isdir(base)
46
os.chmod(base, dir_mode)
47
for root, dirs, files in os.walk(base):
49
p = os.path.join(root, d)
52
p = os.path.join(root, f)
53
os.chmod(p, file_mode)
56
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
57
"""Check that all permissions match
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
65
assert os.path.isdir(base)
67
check_mode(test, base, dir_mode)
68
for root, dirs, files in os.walk(base):
70
p = os.path.join(root, d)
71
check_mode(test, p, dir_mode)
73
p = os.path.join(root, f)
74
check_mode(test, p, file_mode)
76
def assertEqualMode(test, mode, mode_test):
77
test.assertEqual(mode, mode_test,
78
'mode mismatch %o != %o' % (mode, mode_test))
80
class TestPermissions(TestCaseInTempDir):
82
def test_new_files(self):
83
if sys.platform == 'win32':
84
raise TestSkipped('chmod has no effect on win32')
86
b = Branch.initialize(u'.')
88
open('a', 'wb').write('foo\n')
92
# Delete them because we are modifying the filesystem underneath them
94
chmod_r('.bzr', 0644, 0755)
95
check_mode_r(self, '.bzr', 0644, 0755)
99
assertEqualMode(self, 0755, b._dir_mode)
100
assertEqualMode(self, 0644, b._file_mode)
102
# Modifying a file shouldn't break the permissions
103
open('a', 'wb').write('foo2\n')
105
# The mode should be maintained after commit
106
check_mode_r(self, '.bzr', 0644, 0755)
108
# Adding a new file should maintain the permissions
109
open('b', 'wb').write('new b\n')
112
check_mode_r(self, '.bzr', 0644, 0755)
115
# Recursively update the modes of all files
116
chmod_r('.bzr', 0664, 0775)
117
check_mode_r(self, '.bzr', 0664, 0775)
120
assertEqualMode(self, 0775, b._dir_mode)
121
assertEqualMode(self, 0664, b._file_mode)
123
open('a', 'wb').write('foo3\n')
125
check_mode_r(self, '.bzr', 0664, 0775)
127
open('c', 'wb').write('new c\n')
130
check_mode_r(self, '.bzr', 0664, 0775)
132
# Test the group sticky bit
134
# Recursively update the modes of all files
135
chmod_r('.bzr', 0664, 02775)
136
check_mode_r(self, '.bzr', 0664, 02775)
139
assertEqualMode(self, 02775, b._dir_mode)
140
assertEqualMode(self, 0664, b._file_mode)
142
open('a', 'wb').write('foo4\n')
144
check_mode_r(self, '.bzr', 0664, 02775)
146
open('d', 'wb').write('new d\n')
149
check_mode_r(self, '.bzr', 0664, 02775)
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
156
b = Branch.initialize(u'.')
157
self.assertNotEqual(None, b._dir_mode)
158
self.assertNotEqual(None, b._file_mode)
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)
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)
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)
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)
180
bzrlib.branch.BzrBranch._set_dir_mode = True
181
bzrlib.branch.BzrBranch._set_file_mode = True
183
def test_new_branch(self):
184
if sys.platform == 'win32':
185
raise TestSkipped('chmod has no effect on win32')
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)
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)
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)
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)
215
class TestSftpPermissions(TestCaseWithSFTPServer):
217
def test_new_files(self):
218
if sys.platform == 'win32':
219
raise TestSkipped('chmod has no effect on win32')
220
# Though it would be nice to test that SFTP to a server
221
# which does support chmod has the right effect
223
from bzrlib.transport.sftp import SFTPTransport
225
# We don't actually use it directly, we just want to
226
# keep the connection open, since StubSFTPServer only
227
# allows 1 connection
229
_transport = SFTPTransport(self._sftp_url)
232
b_local = Branch.initialize(u'local')
233
t_local = b_local.working_tree()
234
open('local/a', 'wb').write('foo\n')
236
t_local.commit('foo')
238
# Delete them because we are modifying the filesystem underneath them
240
chmod_r('local/.bzr', 0644, 0755)
241
check_mode_r(self, 'local/.bzr', 0644, 0755)
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)
249
# Why does self._sftp_url end with a slash????
250
sftp_url = self._sftp_url + 'sftp'
251
b_sftp = Branch.initialize(sftp_url)
255
chmod_r('sftp/.bzr', 0644, 0755)
256
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
258
b_sftp = Branch.open(sftp_url)
259
assertEqualMode(self, 0755, b_sftp._dir_mode)
260
assertEqualMode(self, 0644, b_sftp._file_mode)
262
open('local/a', 'wb').write('foo2\n')
263
t_local.commit('foo2')
265
# The mode should be maintained after commit
266
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
268
open('local/b', 'wb').write('new b\n')
270
t_local.commit('new b')
272
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
275
# Recursively update the modes of all files
276
chmod_r('sftp/.bzr', 0664, 0775)
277
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
279
b_sftp = Branch.open(sftp_url)
280
assertEqualMode(self, 0775, b_sftp._dir_mode)
281
assertEqualMode(self, 0664, b_sftp._file_mode)
283
open('local/a', 'wb').write('foo3\n')
284
t_local.commit('foo3')
286
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
288
open('local/c', 'wb').write('new c\n')
290
t_local.commit('new c')
292
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
294
def test_sftp_server_modes(self):
295
if sys.platform == 'win32':
296
raise TestSkipped('chmod has no effect on win32')
299
original_umask = os.umask(umask)
302
from bzrlib.transport.sftp import SFTPTransport
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)
309
# but Transport overrides umask
310
t.put('b', 'txt', mode=0666)
311
check_mode(self, 'b', 0666)
313
t._sftp.mkdir('c', mode=0777)
314
check_mode(self, 'c', 0777 &~umask)
316
t.mkdir('d', mode=0777)
317
check_mode(self, 'd', 0777)
319
os.umask(original_umask)