1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
2
2
# -*- coding: utf-8 -*-
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
6
6
# the Free Software Foundation; either version 2 of the License, or
7
7
# (at your option) any later version.
9
9
# This program is distributed in the hope that it will be useful,
10
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
12
# GNU General Public License for more details.
14
14
# You should have received a copy of the GNU General Public License
15
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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
19
"""Tests for bzr setting permissions.
28
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
29
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
30
30
# defined by the local umask. This isn't terrible, is it
31
31
# the truly desired behavior?
36
from cStringIO import StringIO
36
from StringIO import StringIO
39
from bzrlib import transport
40
38
from bzrlib.branch import Branch
41
39
from bzrlib.bzrdir import BzrDir
40
from bzrlib.lockable_files import LockableFiles, TransportLock
42
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
43
from bzrlib.transport import get_transport
44
44
from bzrlib.workingtree import WorkingTree
47
47
def chmod_r(base, file_mode, dir_mode):
48
48
"""Recursively chmod from a base directory"""
49
assert os.path.isdir(base)
49
50
os.chmod(base, dir_mode)
50
51
for root, dirs, files in os.walk(base):
65
66
:param dir_mode: The mode for all directories
66
67
:param include_base: If false, only check the subdirectories
68
t = transport.get_transport(".")
69
assert os.path.isdir(base)
70
t = get_transport(".")
70
72
test.assertTransportMode(t, base, dir_mode)
71
73
for root, dirs, files in os.walk(base):
73
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
75
p = os.path.join(root, d)
74
76
test.assertTransportMode(t, p, dir_mode)
76
78
p = os.path.join(root, f)
77
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
78
79
test.assertTransportMode(t, p, file_mode)
87
88
t = self.make_branch_and_tree('.')
89
90
open('a', 'wb').write('foo\n')
90
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
94
94
chmod_r('.bzr', 0644, 0755)
97
97
# although we are modifying the filesystem
98
98
# underneath the objects, they are not locked, and thus it must
99
# be safe for most operations. But here we want to observe a
99
# be safe for most operations. But here we want to observe a
100
100
# mode change in the control bits, which current do not refresh
101
101
# when a new lock is taken out.
102
102
t = WorkingTree.open('.')
104
104
self.assertEqualMode(0755, b.control_files._dir_mode)
105
105
self.assertEqualMode(0644, b.control_files._file_mode)
106
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
107
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
109
107
# Modifying a file shouldn't break the permissions
110
108
open('a', 'wb').write('foo2\n')
126
124
self.assertEqualMode(0775, b.control_files._dir_mode)
127
125
self.assertEqualMode(0664, b.control_files._file_mode)
128
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
129
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
131
127
open('a', 'wb').write('foo3\n')
137
133
t.commit('new c')
138
134
check_mode_r(self, '.bzr', 0664, 0775)
140
def test_new_files_group_sticky_bit(self):
141
if sys.platform == 'win32':
142
raise TestSkipped('chmod has no effect on win32')
143
elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
144
# OS X (and FreeBSD) create temp dirs with the 'wheel' group, which
145
# users are not likely to be in, and this prevents us from setting
147
os.chown(self.test_dir, os.getuid(), os.getgid())
149
t = self.make_branch_and_tree('.')
152
136
# Test the group sticky bit
153
137
# Recursively update the modes of all files
154
138
chmod_r('.bzr', 0664, 02775)
158
142
self.assertEqualMode(02775, b.control_files._dir_mode)
159
143
self.assertEqualMode(0664, b.control_files._file_mode)
160
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
161
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
163
145
open('a', 'wb').write('foo4\n')
169
151
t.commit('new d')
170
152
check_mode_r(self, '.bzr', 0664, 02775)
154
def test_disable_set_mode(self):
155
# TODO: jam 20051215 Ultimately, this test should probably test that
156
# extra chmod calls aren't being made
158
transport = get_transport(self.get_url())
159
transport.put('my-lock', StringIO(''))
160
lockable = LockableFiles(transport, 'my-lock', TransportLock)
161
self.assertNotEqual(None, lockable._dir_mode)
162
self.assertNotEqual(None, lockable._file_mode)
164
LockableFiles._set_dir_mode = False
165
transport = get_transport('.')
166
lockable = LockableFiles(transport, 'my-lock', TransportLock)
167
self.assertEqual(None, lockable._dir_mode)
168
self.assertNotEqual(None, lockable._file_mode)
170
LockableFiles._set_file_mode = False
171
transport = get_transport('.')
172
lockable = LockableFiles(transport, 'my-lock', TransportLock)
173
self.assertEqual(None, lockable._dir_mode)
174
self.assertEqual(None, lockable._file_mode)
176
LockableFiles._set_dir_mode = True
177
transport = get_transport('.')
178
lockable = LockableFiles(transport, 'my-lock', TransportLock)
179
self.assertNotEqual(None, lockable._dir_mode)
180
self.assertEqual(None, lockable._file_mode)
182
LockableFiles._set_file_mode = True
183
transport = get_transport('.')
184
lockable = LockableFiles(transport, 'my-lock', TransportLock)
185
self.assertNotEqual(None, lockable._dir_mode)
186
self.assertNotEqual(None, lockable._file_mode)
188
LockableFiles._set_dir_mode = True
189
LockableFiles._set_file_mode = True
173
192
class TestSftpPermissions(TestCaseWithSFTPServer):
197
216
b_local = t.branch
198
217
self.assertEqualMode(0755, b_local.control_files._dir_mode)
199
218
self.assertEqualMode(0644, b_local.control_files._file_mode)
200
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
201
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
204
221
sftp_url = self.get_url('sftp')
212
229
b_sftp = Branch.open(sftp_url)
213
230
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
214
231
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
215
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
216
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
218
233
open('local/a', 'wb').write('foo2\n')
219
234
t_local.commit('foo2')
235
250
b_sftp = Branch.open(sftp_url)
236
251
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
237
252
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
238
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
239
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
241
254
open('local/a', 'wb').write('foo3\n')
242
255
t_local.commit('foo3')
257
270
original_umask = os.umask(umask)
260
t = transport.get_transport(self.get_url())
273
t = get_transport(self.get_url())
261
274
# Direct access should be masked by umask
262
275
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
263
276
self.assertTransportMode(t, 'a', 0666 &~umask)
265
278
# but Transport overrides umask
266
t.put_bytes('b', 'txt', mode=0666)
279
t.put('b', 'txt', mode=0666)
267
280
self.assertTransportMode(t, 'b', 0666)
269
t._get_sftp().mkdir('c', mode=0777)
282
t._sftp.mkdir('c', mode=0777)
270
283
self.assertTransportMode(t, 'c', 0777 &~umask)
272
285
t.mkdir('d', mode=0777)