1
# Copyright (C) 2005 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 cStringIO import StringIO
39
from bzrlib.branch import Branch
40
from bzrlib.bzrdir import BzrDir
41
from bzrlib.lockable_files import LockableFiles, TransportLock
42
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
from bzrlib.transport import get_transport
45
from bzrlib.workingtree import WorkingTree
48
def chmod_r(base, file_mode, dir_mode):
49
"""Recursively chmod from a base directory"""
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
t = get_transport(".")
71
test.assertTransportMode(t, base, dir_mode)
72
for root, dirs, files in os.walk(base):
74
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
75
test.assertTransportMode(t, p, dir_mode)
77
p = os.path.join(root, f)
78
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
79
test.assertTransportMode(t, p, file_mode)
82
class TestPermissions(TestCaseWithTransport):
84
def test_new_files(self):
85
if sys.platform == 'win32':
86
raise TestSkipped('chmod has no effect on win32')
88
t = self.make_branch_and_tree('.')
90
open('a', 'wb').write('foo\n')
91
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
95
chmod_r('.bzr', 0644, 0755)
96
check_mode_r(self, '.bzr', 0644, 0755)
98
# although we are modifying the filesystem
99
# underneath the objects, they are not locked, and thus it must
100
# be safe for most operations. But here we want to observe a
101
# mode change in the control bits, which current do not refresh
102
# when a new lock is taken out.
103
t = WorkingTree.open('.')
105
self.assertEqualMode(0755, b.control_files._dir_mode)
106
self.assertEqualMode(0644, b.control_files._file_mode)
107
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
108
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
110
# Modifying a file shouldn't break the permissions
111
open('a', 'wb').write('foo2\n')
113
# The mode should be maintained after commit
114
check_mode_r(self, '.bzr', 0644, 0755)
116
# Adding a new file should maintain the permissions
117
open('b', 'wb').write('new b\n')
120
check_mode_r(self, '.bzr', 0644, 0755)
122
# Recursively update the modes of all files
123
chmod_r('.bzr', 0664, 0775)
124
check_mode_r(self, '.bzr', 0664, 0775)
125
t = WorkingTree.open('.')
127
self.assertEqualMode(0775, b.control_files._dir_mode)
128
self.assertEqualMode(0664, b.control_files._file_mode)
129
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
130
self.assertEqualMode(0664, b.bzrdir._get_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)
145
t = WorkingTree.open('.')
147
self.assertEqualMode(02775, b.control_files._dir_mode)
148
self.assertEqualMode(0664, b.control_files._file_mode)
149
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
150
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
152
open('a', 'wb').write('foo4\n')
154
check_mode_r(self, '.bzr', 0664, 02775)
156
open('d', 'wb').write('new d\n')
159
check_mode_r(self, '.bzr', 0664, 02775)
162
class TestSftpPermissions(TestCaseWithSFTPServer):
164
def test_new_files(self):
165
if sys.platform == 'win32':
166
raise TestSkipped('chmod has no effect on win32')
167
# Though it would be nice to test that SFTP to a server
168
# which does support chmod has the right effect
170
# bodge around for stubsftpserver not letting use connect
172
_t = get_transport(self.get_url())
175
t_local = self.make_branch_and_tree('local')
176
b_local = t_local.branch
177
open('local/a', 'wb').write('foo\n')
179
t_local.commit('foo')
181
# Delete them because we are modifying the filesystem underneath them
182
chmod_r('local/.bzr', 0644, 0755)
183
check_mode_r(self, 'local/.bzr', 0644, 0755)
185
t = WorkingTree.open('local')
187
self.assertEqualMode(0755, b_local.control_files._dir_mode)
188
self.assertEqualMode(0644, b_local.control_files._file_mode)
189
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
190
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
193
sftp_url = self.get_url('sftp')
194
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
198
chmod_r('sftp/.bzr', 0644, 0755)
199
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
201
b_sftp = Branch.open(sftp_url)
202
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
203
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
204
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
205
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
207
open('local/a', 'wb').write('foo2\n')
208
t_local.commit('foo2')
210
# The mode should be maintained after commit
211
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
213
open('local/b', 'wb').write('new b\n')
215
t_local.commit('new b')
217
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
220
# Recursively update the modes of all files
221
chmod_r('sftp/.bzr', 0664, 0775)
222
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
224
b_sftp = Branch.open(sftp_url)
225
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
226
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
227
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
228
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
230
open('local/a', 'wb').write('foo3\n')
231
t_local.commit('foo3')
233
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
235
open('local/c', 'wb').write('new c\n')
237
t_local.commit('new c')
239
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
241
def test_sftp_server_modes(self):
242
if sys.platform == 'win32':
243
raise TestSkipped('chmod has no effect on win32')
246
original_umask = os.umask(umask)
249
t = get_transport(self.get_url())
250
# Direct access should be masked by umask
251
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
252
self.assertTransportMode(t, 'a', 0666 &~umask)
254
# but Transport overrides umask
255
t.put_bytes('b', 'txt', mode=0666)
256
self.assertTransportMode(t, 'b', 0666)
258
t._get_sftp().mkdir('c', mode=0777)
259
self.assertTransportMode(t, 'c', 0777 &~umask)
261
t.mkdir('d', mode=0777)
262
self.assertTransportMode(t, 'd', 0777)
264
os.umask(original_umask)