1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005 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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
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 StringIO import StringIO
36
from cStringIO import StringIO
38
39
from bzrlib.branch import Branch
39
from bzrlib.lockable_files import LockableFiles
40
from bzrlib.tests import TestCaseInTempDir, TestSkipped
41
from bzrlib.transport import get_transport
40
from bzrlib.bzrdir import BzrDir
41
from bzrlib.lockable_files import LockableFiles, TransportLock
42
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
43
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
44
from bzrlib.transport import get_transport
45
from bzrlib.workingtree import WorkingTree
47
48
def chmod_r(base, file_mode, dir_mode):
48
49
"""Recursively chmod from a base directory"""
49
assert os.path.isdir(base)
50
50
os.chmod(base, dir_mode)
51
51
for root, dirs, files in os.walk(base):
66
66
:param dir_mode: The mode for all directories
67
67
:param include_base: If false, only check the subdirectories
69
assert os.path.isdir(base)
70
69
t = get_transport(".")
72
71
test.assertTransportMode(t, base, dir_mode)
73
72
for root, dirs, files in os.walk(base):
75
p = os.path.join(root, d)
74
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
76
75
test.assertTransportMode(t, p, dir_mode)
78
77
p = os.path.join(root, f)
78
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
79
79
test.assertTransportMode(t, p, file_mode)
82
def assertEqualMode(test, mode, mode_test):
83
test.assertEqual(mode, mode_test,
84
'mode mismatch %o != %o' % (mode, mode_test))
87
class TestPermissions(TestCaseInTempDir):
82
class TestPermissions(TestCaseWithTransport):
89
84
def test_new_files(self):
90
85
if sys.platform == 'win32':
91
86
raise TestSkipped('chmod has no effect on win32')
93
b = Branch.initialize(u'.')
88
t = self.make_branch_and_tree('.')
95
90
open('a', 'wb').write('foo\n')
91
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
99
# Delete them because we are modifying the filesystem underneath them
101
95
chmod_r('.bzr', 0644, 0755)
102
96
check_mode_r(self, '.bzr', 0644, 0755)
106
assertEqualMode(self, 0755, b.control_files._dir_mode)
107
assertEqualMode(self, 0644, b.control_files._file_mode)
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())
109
110
# Modifying a file shouldn't break the permissions
110
111
open('a', 'wb').write('foo2\n')
118
119
t.commit('new b')
119
120
check_mode_r(self, '.bzr', 0644, 0755)
122
122
# Recursively update the modes of all files
123
123
chmod_r('.bzr', 0664, 0775)
124
124
check_mode_r(self, '.bzr', 0664, 0775)
127
assertEqualMode(self, 0775, b.control_files._dir_mode)
128
assertEqualMode(self, 0664, b.control_files._file_mode)
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())
130
132
open('a', 'wb').write('foo3\n')
136
138
t.commit('new c')
137
139
check_mode_r(self, '.bzr', 0664, 0775)
141
def test_new_files_group_sticky_bit(self):
142
if sys.platform == 'win32':
143
raise TestSkipped('chmod has no effect on win32')
144
elif sys.platform == 'darwin':
145
# OS X creates temp dirs with the 'wheel' group, which users are
146
# not likely to be in, and this prevents us from setting the sgid
148
os.chown(self.test_dir, os.getuid(), os.getgid())
150
t = self.make_branch_and_tree('.')
139
153
# Test the group sticky bit
141
154
# Recursively update the modes of all files
142
155
chmod_r('.bzr', 0664, 02775)
143
156
check_mode_r(self, '.bzr', 0664, 02775)
146
assertEqualMode(self, 02775, b.control_files._dir_mode)
147
assertEqualMode(self, 0664, b.control_files._file_mode)
157
t = WorkingTree.open('.')
159
self.assertEqualMode(02775, b.control_files._dir_mode)
160
self.assertEqualMode(0664, b.control_files._file_mode)
161
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
162
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
149
164
open('a', 'wb').write('foo4\n')
155
170
t.commit('new d')
156
171
check_mode_r(self, '.bzr', 0664, 02775)
158
def test_disable_set_mode(self):
159
# TODO: jam 20051215 Ultimately, this test should probably test that
160
# extra chmod calls aren't being made
162
transport = get_transport('.')
163
transport.put('my-lock', StringIO(''))
164
lockable = LockableFiles(transport, 'my-lock')
165
self.assertNotEqual(None, lockable._dir_mode)
166
self.assertNotEqual(None, lockable._file_mode)
168
LockableFiles._set_dir_mode = False
169
transport = get_transport('.')
170
lockable = LockableFiles(transport, 'my-lock')
171
self.assertEqual(None, lockable._dir_mode)
172
self.assertNotEqual(None, lockable._file_mode)
174
LockableFiles._set_file_mode = False
175
transport = get_transport('.')
176
lockable = LockableFiles(transport, 'my-lock')
177
self.assertEqual(None, lockable._dir_mode)
178
self.assertEqual(None, lockable._file_mode)
180
LockableFiles._set_dir_mode = True
181
transport = get_transport('.')
182
lockable = LockableFiles(transport, 'my-lock')
183
self.assertNotEqual(None, lockable._dir_mode)
184
self.assertEqual(None, lockable._file_mode)
186
LockableFiles._set_file_mode = True
187
transport = get_transport('.')
188
lockable = LockableFiles(transport, 'my-lock')
189
self.assertNotEqual(None, lockable._dir_mode)
190
self.assertNotEqual(None, lockable._file_mode)
192
LockableFiles._set_dir_mode = True
193
LockableFiles._set_file_mode = True
195
def test_new_branch(self):
196
if sys.platform == 'win32':
197
raise TestSkipped('chmod has no effect on win32')
198
#FIXME RBC 20060105 should test branch and repository
200
# also, these are BzrBranch format specific things..
202
mode = stat.S_IMODE(os.stat('a').st_mode)
203
b = Branch.initialize('a')
204
assertEqualMode(self, mode, b.control_files._dir_mode)
205
assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
209
b = Branch.initialize('b')
210
assertEqualMode(self, 02777, b.control_files._dir_mode)
211
assertEqualMode(self, 00666, b.control_files._file_mode)
212
check_mode_r(self, 'b/.bzr', 00666, 02777)
216
b = Branch.initialize('c')
217
assertEqualMode(self, 02750, b.control_files._dir_mode)
218
assertEqualMode(self, 00640, b.control_files._file_mode)
219
check_mode_r(self, 'c/.bzr', 00640, 02750)
223
b = Branch.initialize('d')
224
assertEqualMode(self, 0700, b.control_files._dir_mode)
225
assertEqualMode(self, 0600, b.control_files._file_mode)
226
check_mode_r(self, 'd/.bzr', 00600, 0700)
229
174
class TestSftpPermissions(TestCaseWithSFTPServer):
234
179
# Though it would be nice to test that SFTP to a server
235
180
# which does support chmod has the right effect
237
from bzrlib.transport.sftp import SFTPTransport
239
# We don't actually use it directly, we just want to
240
# keep the connection open, since StubSFTPServer only
241
# allows 1 connection
242
_transport = SFTPTransport(self._sftp_url)
182
# bodge around for stubsftpserver not letting use connect
184
_t = get_transport(self.get_url())
244
186
os.mkdir('local')
245
b_local = Branch.initialize(u'local')
246
t_local = b_local.working_tree()
187
t_local = self.make_branch_and_tree('local')
188
b_local = t_local.branch
247
189
open('local/a', 'wb').write('foo\n')
249
191
t_local.commit('foo')
251
193
# Delete them because we are modifying the filesystem underneath them
253
194
chmod_r('local/.bzr', 0644, 0755)
254
195
check_mode_r(self, 'local/.bzr', 0644, 0755)
256
b_local = Branch.open(u'local')
257
t_local = b_local.working_tree()
258
assertEqualMode(self, 0755, b_local.control_files._dir_mode)
259
assertEqualMode(self, 0644, b_local.control_files._file_mode)
197
t = WorkingTree.open('local')
199
self.assertEqualMode(0755, b_local.control_files._dir_mode)
200
self.assertEqualMode(0644, b_local.control_files._file_mode)
201
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
202
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
262
sftp_url = self.get_remote_url('sftp')
263
b_sftp = Branch.initialize(sftp_url)
205
sftp_url = self.get_url('sftp')
206
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
265
208
b_sftp.pull(b_local)
268
211
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
270
213
b_sftp = Branch.open(sftp_url)
271
assertEqualMode(self, 0755, b_sftp.control_files._dir_mode)
272
assertEqualMode(self, 0644, b_sftp.control_files._file_mode)
214
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
215
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
216
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
217
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
274
219
open('local/a', 'wb').write('foo2\n')
275
220
t_local.commit('foo2')
289
234
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
291
236
b_sftp = Branch.open(sftp_url)
292
assertEqualMode(self, 0775, b_sftp.control_files._dir_mode)
293
assertEqualMode(self, 0664, b_sftp.control_files._file_mode)
237
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
238
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
239
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
240
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
295
242
open('local/a', 'wb').write('foo3\n')
296
243
t_local.commit('foo3')
311
258
original_umask = os.umask(umask)
314
from bzrlib.transport.sftp import SFTPTransport
315
t = SFTPTransport(self._sftp_url)
261
t = get_transport(self.get_url())
316
262
# Direct access should be masked by umask
317
263
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
318
264
self.assertTransportMode(t, 'a', 0666 &~umask)
320
266
# but Transport overrides umask
321
t.put('b', 'txt', mode=0666)
267
t.put_bytes('b', 'txt', mode=0666)
322
268
self.assertTransportMode(t, 'b', 0666)
324
t._sftp.mkdir('c', mode=0777)
270
t._get_sftp().mkdir('c', mode=0777)
325
271
self.assertTransportMode(t, 'c', 0777 &~umask)
327
273
t.mkdir('d', mode=0777)
328
274
self.assertTransportMode(t, 'd', 0777)
330
276
os.umask(original_umask)