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?
37
from bzrlib import transport
38
from bzrlib.branch import Branch
39
from bzrlib.bzrdir import BzrDir
40
from bzrlib.tests import TestCaseWithTransport, TestSkipped
41
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
42
from bzrlib.workingtree import WorkingTree
45
def chmod_r(base, file_mode, dir_mode):
46
"""Recursively chmod from a base directory"""
47
os.chmod(base, dir_mode)
48
for root, dirs, files in os.walk(base):
50
p = os.path.join(root, d)
53
p = os.path.join(root, f)
54
os.chmod(p, file_mode)
57
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
58
"""Check that all permissions match
60
:param test: The TestCase being run
61
:param base: The path to the root directory to check
62
:param file_mode: The mode for all files
63
:param dir_mode: The mode for all directories
64
:param include_base: If false, only check the subdirectories
66
t = test.get_transport()
68
test.assertTransportMode(t, base, dir_mode)
69
for root, dirs, files in os.walk(base):
71
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
72
test.assertTransportMode(t, p, dir_mode)
74
p = os.path.join(root, f)
75
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
76
test.assertTransportMode(t, p, file_mode)
79
class TestPermissions(TestCaseWithTransport):
81
def test_new_files(self):
82
if sys.platform == 'win32':
83
raise TestSkipped('chmod has no effect on win32')
85
t = self.make_branch_and_tree('.')
87
open('a', 'wb').write('foo\n')
88
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
92
chmod_r('.bzr', 0644, 0755)
93
check_mode_r(self, '.bzr', 0644, 0755)
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(0755, b.control_files._dir_mode)
103
self.assertEqualMode(0644, b.control_files._file_mode)
104
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
105
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
107
# Modifying a file shouldn't break the permissions
108
open('a', 'wb').write('foo2\n')
110
# The mode should be maintained after commit
111
check_mode_r(self, '.bzr', 0644, 0755)
113
# Adding a new file should maintain the permissions
114
open('b', 'wb').write('new b\n')
117
check_mode_r(self, '.bzr', 0644, 0755)
119
# Recursively update the modes of all files
120
chmod_r('.bzr', 0664, 0775)
121
check_mode_r(self, '.bzr', 0664, 0775)
122
t = WorkingTree.open('.')
124
self.assertEqualMode(0775, b.control_files._dir_mode)
125
self.assertEqualMode(0664, b.control_files._file_mode)
126
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
127
self.assertEqualMode(0664, b.bzrdir._get_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
def test_new_files_group_sticky_bit(self):
139
if sys.platform == 'win32':
140
raise TestSkipped('chmod has no effect on win32')
141
elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
142
# FreeBSD-based platforms create temp dirs with the 'wheel' group,
143
# which users are not likely to be in, and this prevents us from
144
# setting the sgid bit
145
os.chown(self.test_dir, os.getuid(), os.getgid())
147
t = self.make_branch_and_tree('.')
150
# Test the group sticky bit
151
# Recursively update the modes of all files
152
chmod_r('.bzr', 0664, 02775)
153
check_mode_r(self, '.bzr', 0664, 02775)
154
t = WorkingTree.open('.')
156
self.assertEqualMode(02775, b.control_files._dir_mode)
157
self.assertEqualMode(0664, b.control_files._file_mode)
158
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
159
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
161
open('a', 'wb').write('foo4\n')
163
check_mode_r(self, '.bzr', 0664, 02775)
165
open('d', 'wb').write('new d\n')
168
check_mode_r(self, '.bzr', 0664, 02775)
171
class TestSftpPermissions(TestCaseWithSFTPServer):
173
def test_new_files(self):
174
if sys.platform == 'win32':
175
raise TestSkipped('chmod has no effect on win32')
176
# Though it would be nice to test that SFTP to a server
177
# which does support chmod has the right effect
179
# bodge around for stubsftpserver not letting use connect
181
_t = self.get_transport()
184
t_local = self.make_branch_and_tree('local')
185
b_local = t_local.branch
186
open('local/a', 'wb').write('foo\n')
188
t_local.commit('foo')
190
# Delete them because we are modifying the filesystem underneath them
191
chmod_r('local/.bzr', 0644, 0755)
192
check_mode_r(self, 'local/.bzr', 0644, 0755)
194
t = WorkingTree.open('local')
196
self.assertEqualMode(0755, b_local.control_files._dir_mode)
197
self.assertEqualMode(0644, b_local.control_files._file_mode)
198
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
199
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
202
sftp_url = self.get_url('sftp')
203
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
207
chmod_r('sftp/.bzr', 0644, 0755)
208
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
210
b_sftp = Branch.open(sftp_url)
211
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
212
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
213
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
214
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
216
open('local/a', 'wb').write('foo2\n')
217
t_local.commit('foo2')
219
# The mode should be maintained after commit
220
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
222
open('local/b', 'wb').write('new b\n')
224
t_local.commit('new b')
226
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
229
# Recursively update the modes of all files
230
chmod_r('sftp/.bzr', 0664, 0775)
231
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
233
b_sftp = Branch.open(sftp_url)
234
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
235
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
236
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
237
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
239
open('local/a', 'wb').write('foo3\n')
240
t_local.commit('foo3')
242
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
244
open('local/c', 'wb').write('new c\n')
246
t_local.commit('new c')
248
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
250
def test_sftp_server_modes(self):
251
if sys.platform == 'win32':
252
raise TestSkipped('chmod has no effect on win32')
255
original_umask = os.umask(umask)
258
t = self.get_transport()
259
# Direct access should be masked by umask
260
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
261
self.assertTransportMode(t, 'a', 0666 &~umask)
263
# but Transport overrides umask
264
t.put_bytes('b', 'txt', mode=0666)
265
self.assertTransportMode(t, 'b', 0666)
267
t._get_sftp().mkdir('c', mode=0777)
268
self.assertTransportMode(t, 'c', 0777 &~umask)
270
t.mkdir('d', mode=0777)
271
self.assertTransportMode(t, 'd', 0777)
273
os.umask(original_umask)