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 bzrlib import urlutils
37
from bzrlib.branch import Branch
38
from bzrlib.bzrdir import BzrDir
39
from bzrlib.tests import TestCaseWithTransport, TestSkipped
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from bzrlib.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: f.write('foo\n')
87
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
91
chmod_r('.bzr', 0644, 0755)
92
check_mode_r(self, '.bzr', 0644, 0755)
94
# although we are modifying the filesystem
95
# underneath the objects, they are not locked, and thus it must
96
# be safe for most operations. But here we want to observe a
97
# mode change in the control bits, which current do not refresh
98
# when a new lock is taken out.
99
t = WorkingTree.open('.')
101
self.assertEqualMode(0755, b.control_files._dir_mode)
102
self.assertEqualMode(0644, b.control_files._file_mode)
103
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
104
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
106
# Modifying a file shouldn't break the permissions
107
with open('a', 'wb') as f: f.write('foo2\n')
109
# The mode should be maintained after commit
110
check_mode_r(self, '.bzr', 0644, 0755)
112
# Adding a new file should maintain the permissions
113
with open('b', 'wb') as f: f.write('new b\n')
116
check_mode_r(self, '.bzr', 0644, 0755)
118
# Recursively update the modes of all files
119
chmod_r('.bzr', 0664, 0775)
120
check_mode_r(self, '.bzr', 0664, 0775)
121
t = WorkingTree.open('.')
123
self.assertEqualMode(0775, b.control_files._dir_mode)
124
self.assertEqualMode(0664, b.control_files._file_mode)
125
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
126
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
128
with open('a', 'wb') as f: f.write('foo3\n')
130
check_mode_r(self, '.bzr', 0664, 0775)
132
with open('c', 'wb') as f: f.write('new c\n')
135
check_mode_r(self, '.bzr', 0664, 0775)
137
def test_new_files_group_sticky_bit(self):
138
if sys.platform == 'win32':
139
raise TestSkipped('chmod has no effect on win32')
140
elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
141
# FreeBSD-based platforms create temp dirs with the 'wheel' group,
142
# which users are not likely to be in, and this prevents us from
143
# setting the sgid bit
144
os.chown(self.test_dir, os.getuid(), os.getgid())
146
t = self.make_branch_and_tree('.')
149
# Test the group sticky bit
150
# Recursively update the modes of all files
151
chmod_r('.bzr', 0664, 02775)
152
check_mode_r(self, '.bzr', 0664, 02775)
153
t = WorkingTree.open('.')
155
self.assertEqualMode(02775, b.control_files._dir_mode)
156
self.assertEqualMode(0664, b.control_files._file_mode)
157
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
158
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
160
with open('a', 'wb') as f: f.write('foo4\n')
162
check_mode_r(self, '.bzr', 0664, 02775)
164
with open('d', 'wb') as f: f.write('new d\n')
167
check_mode_r(self, '.bzr', 0664, 02775)
170
class TestSftpPermissions(TestCaseWithSFTPServer):
172
def test_new_files(self):
173
if sys.platform == 'win32':
174
raise TestSkipped('chmod has no effect on win32')
175
# Though it would be nice to test that SFTP to a server
176
# which does support chmod has the right effect
178
# bodge around for stubsftpserver not letting use connect
180
_t = self.get_transport()
183
t_local = self.make_branch_and_tree('local')
184
b_local = t_local.branch
185
with open('local/a', 'wb') as f: f.write('foo\n')
187
t_local.commit('foo')
189
# Delete them because we are modifying the filesystem underneath them
190
chmod_r('local/.bzr', 0644, 0755)
191
check_mode_r(self, 'local/.bzr', 0644, 0755)
193
t = WorkingTree.open('local')
195
self.assertEqualMode(0755, b_local.control_files._dir_mode)
196
self.assertEqualMode(0644, b_local.control_files._file_mode)
197
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
198
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
201
sftp_url = self.get_url('sftp')
202
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
206
chmod_r('sftp/.bzr', 0644, 0755)
207
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
209
b_sftp = Branch.open(sftp_url)
210
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
211
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
212
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
213
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
215
with open('local/a', 'wb') as f: f.write('foo2\n')
216
t_local.commit('foo2')
218
# The mode should be maintained after commit
219
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
221
with open('local/b', 'wb') as f: f.write('new b\n')
223
t_local.commit('new b')
225
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
228
# Recursively update the modes of all files
229
chmod_r('sftp/.bzr', 0664, 0775)
230
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
232
b_sftp = Branch.open(sftp_url)
233
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
234
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
235
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
236
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
238
with open('local/a', 'wb') as f: f.write('foo3\n')
239
t_local.commit('foo3')
241
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
243
with open('local/c', 'wb') as f: f.write('new c\n')
245
t_local.commit('new c')
247
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
249
def test_sftp_server_modes(self):
250
if sys.platform == 'win32':
251
raise TestSkipped('chmod has no effect on win32')
254
original_umask = os.umask(umask)
257
t = self.get_transport()
258
# Direct access should be masked by umask
259
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
260
self.assertTransportMode(t, 'a', 0666 &~umask)
262
# but Transport overrides umask
263
t.put_bytes('b', 'txt', mode=0666)
264
self.assertTransportMode(t, 'b', 0666)
266
t._get_sftp().mkdir('c', mode=0777)
267
self.assertTransportMode(t, 'c', 0777 &~umask)
269
t.mkdir('d', mode=0777)
270
self.assertTransportMode(t, 'd', 0777)
272
os.umask(original_umask)