1
# Copyright (C) 2005-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 breezy import urlutils
37
from breezy.branch import Branch
38
from breezy.controldir import ControlDir
39
from breezy.tests import TestCaseWithTransport, TestSkipped
40
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from breezy.workingtree import WorkingTree
44
def chmod_r(base, file_mode, dir_mode):
45
"""Recursively chmod from a base directory"""
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
t = test.get_transport()
67
test.assertTransportMode(t, base, dir_mode)
68
for root, dirs, files in os.walk(base):
70
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
71
test.assertTransportMode(t, p, dir_mode)
73
p = os.path.join(root, f)
74
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
75
test.assertTransportMode(t, p, file_mode)
78
class TestPermissions(TestCaseWithTransport):
80
def test_new_files(self):
81
if sys.platform == 'win32':
82
raise TestSkipped('chmod has no effect on win32')
84
t = self.make_branch_and_tree('.')
86
with open('a', 'wb') as f:
88
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
89
t.add('a', b'CAPS-ID')
92
chmod_r('.bzr', 0o644, 0o755)
93
check_mode_r(self, '.bzr', 0o644, 0o755)
95
# although we are modifying the filesystem
96
# underneath the objects, they are not locked, and thus it must
97
# be safe for most operations. But here we want to observe a
98
# mode change in the control bits, which current do not refresh
99
# when a new lock is taken out.
100
t = WorkingTree.open('.')
102
self.assertEqualMode(0o755, b.control_files._dir_mode)
103
self.assertEqualMode(0o644, b.control_files._file_mode)
104
self.assertEqualMode(0o755, b.controldir._get_dir_mode())
105
self.assertEqualMode(0o644, b.controldir._get_file_mode())
107
# Modifying a file shouldn't break the permissions
108
with open('a', 'wb') as f:
111
# The mode should be maintained after commit
112
check_mode_r(self, '.bzr', 0o644, 0o755)
114
# Adding a new file should maintain the permissions
115
with open('b', 'wb') as f:
119
check_mode_r(self, '.bzr', 0o644, 0o755)
121
# Recursively update the modes of all files
122
chmod_r('.bzr', 0o664, 0o775)
123
check_mode_r(self, '.bzr', 0o664, 0o775)
124
t = WorkingTree.open('.')
126
self.assertEqualMode(0o775, b.control_files._dir_mode)
127
self.assertEqualMode(0o664, b.control_files._file_mode)
128
self.assertEqualMode(0o775, b.controldir._get_dir_mode())
129
self.assertEqualMode(0o664, b.controldir._get_file_mode())
131
with open('a', 'wb') as f:
134
check_mode_r(self, '.bzr', 0o664, 0o775)
136
with open('c', 'wb') as f:
140
check_mode_r(self, '.bzr', 0o664, 0o775)
142
def test_new_files_group_sticky_bit(self):
143
if sys.platform == 'win32':
144
raise TestSkipped('chmod has no effect on win32')
145
elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
146
# FreeBSD-based platforms create temp dirs with the 'wheel' group,
147
# which users are not likely to be in, and this prevents us from
148
# setting the sgid bit
149
os.chown(self.test_dir, os.getuid(), os.getgid())
151
t = self.make_branch_and_tree('.')
154
# Test the group sticky bit
155
# Recursively update the modes of all files
156
chmod_r('.bzr', 0o664, 0o2775)
157
check_mode_r(self, '.bzr', 0o664, 0o2775)
158
t = WorkingTree.open('.')
160
self.assertEqualMode(0o2775, b.control_files._dir_mode)
161
self.assertEqualMode(0o664, b.control_files._file_mode)
162
self.assertEqualMode(0o2775, b.controldir._get_dir_mode())
163
self.assertEqualMode(0o664, b.controldir._get_file_mode())
165
with open('a', 'wb') as f:
168
check_mode_r(self, '.bzr', 0o664, 0o2775)
170
with open('d', 'wb') as f:
174
check_mode_r(self, '.bzr', 0o664, 0o2775)
177
class TestSftpPermissions(TestCaseWithSFTPServer):
179
def test_new_files(self):
180
if sys.platform == 'win32':
181
raise TestSkipped('chmod has no effect on win32')
182
# Though it would be nice to test that SFTP to a server
183
# which does support chmod has the right effect
185
# bodge around for stubsftpserver not letting use connect
187
_t = self.get_transport()
190
t_local = self.make_branch_and_tree('local')
191
b_local = t_local.branch
192
with open('local/a', 'wb') as f:
195
t_local.commit('foo')
197
# Delete them because we are modifying the filesystem underneath them
198
chmod_r('local/.bzr', 0o644, 0o755)
199
check_mode_r(self, 'local/.bzr', 0o644, 0o755)
201
t = WorkingTree.open('local')
203
self.assertEqualMode(0o755, b_local.control_files._dir_mode)
204
self.assertEqualMode(0o644, b_local.control_files._file_mode)
205
self.assertEqualMode(0o755, b_local.controldir._get_dir_mode())
206
self.assertEqualMode(0o644, b_local.controldir._get_file_mode())
209
sftp_url = self.get_url('sftp')
210
b_sftp = ControlDir.create_branch_and_repo(sftp_url)
214
chmod_r('sftp/.bzr', 0o644, 0o755)
215
check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
217
b_sftp = Branch.open(sftp_url)
218
self.assertEqualMode(0o755, b_sftp.control_files._dir_mode)
219
self.assertEqualMode(0o644, b_sftp.control_files._file_mode)
220
self.assertEqualMode(0o755, b_sftp.controldir._get_dir_mode())
221
self.assertEqualMode(0o644, b_sftp.controldir._get_file_mode())
223
with open('local/a', 'wb') as f:
225
t_local.commit('foo2')
227
# The mode should be maintained after commit
228
check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
230
with open('local/b', 'wb') as f:
233
t_local.commit('new b')
235
check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
238
# Recursively update the modes of all files
239
chmod_r('sftp/.bzr', 0o664, 0o775)
240
check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
242
b_sftp = Branch.open(sftp_url)
243
self.assertEqualMode(0o775, b_sftp.control_files._dir_mode)
244
self.assertEqualMode(0o664, b_sftp.control_files._file_mode)
245
self.assertEqualMode(0o775, b_sftp.controldir._get_dir_mode())
246
self.assertEqualMode(0o664, b_sftp.controldir._get_file_mode())
248
with open('local/a', 'wb') as f:
250
t_local.commit('foo3')
252
check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
254
with open('local/c', 'wb') as f:
257
t_local.commit('new c')
259
check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
261
def test_sftp_server_modes(self):
262
if sys.platform == 'win32':
263
raise TestSkipped('chmod has no effect on win32')
266
original_umask = os.umask(umask)
269
t = self.get_transport()
270
# Direct access should be masked by umask
271
with t._sftp_open_exclusive('a', mode=0o666) as f:
273
self.assertTransportMode(t, 'a', 0o666 & ~umask)
275
# but Transport overrides umask
276
t.put_bytes('b', b'txt', mode=0o666)
277
self.assertTransportMode(t, 'b', 0o666)
279
t._get_sftp().mkdir('c', mode=0o777)
280
self.assertTransportMode(t, 'c', 0o777 & ~umask)
282
t.mkdir('d', mode=0o777)
283
self.assertTransportMode(t, 'd', 0o777)
285
os.umask(original_umask)