36
36
from StringIO import StringIO
38
38
from bzrlib.branch import Branch
39
from bzrlib.bzrdir import BzrDir
39
40
from bzrlib.lockable_files import LockableFiles
40
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
41
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
78
79
test.assertTransportMode(t, p, file_mode)
81
def assertEqualMode(test, mode, mode_test):
82
test.assertEqual(mode, mode_test,
83
'mode mismatch %o != %o' % (mode, mode_test))
86
82
class TestPermissions(TestCaseWithTransport):
88
84
def test_new_files(self):
89
85
if sys.platform == 'win32':
90
86
raise TestSkipped('chmod has no effect on win32')
92
t = WorkingTree.create_standalone('.')
88
t = self.make_branch_and_tree('.')
94
90
open('a', 'wb').write('foo\n')
98
# Delete them because we are modifying the filesystem underneath them
100
94
chmod_r('.bzr', 0644, 0755)
101
95
check_mode_r(self, '.bzr', 0644, 0755)
105
assertEqualMode(self, 0755, b.control_files._dir_mode)
106
assertEqualMode(self, 0644, b.control_files._file_mode)
97
# although we are modifying the filesystem
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
100
# mode change in the control bits, which current do not refresh
101
# when a new lock is taken out.
102
t = WorkingTree.open('.')
104
self.assertEqualMode(0755, b.control_files._dir_mode)
105
self.assertEqualMode(0644, b.control_files._file_mode)
108
107
# Modifying a file shouldn't break the permissions
109
108
open('a', 'wb').write('foo2\n')
117
116
t.commit('new b')
118
117
check_mode_r(self, '.bzr', 0644, 0755)
121
119
# Recursively update the modes of all files
122
120
chmod_r('.bzr', 0664, 0775)
123
121
check_mode_r(self, '.bzr', 0664, 0775)
126
assertEqualMode(self, 0775, b.control_files._dir_mode)
127
assertEqualMode(self, 0664, b.control_files._file_mode)
122
t = WorkingTree.open('.')
124
self.assertEqualMode(0775, b.control_files._dir_mode)
125
self.assertEqualMode(0664, b.control_files._file_mode)
129
127
open('a', 'wb').write('foo3\n')
136
134
check_mode_r(self, '.bzr', 0664, 0775)
138
136
# Test the group sticky bit
140
137
# Recursively update the modes of all files
141
138
chmod_r('.bzr', 0664, 02775)
142
139
check_mode_r(self, '.bzr', 0664, 02775)
145
assertEqualMode(self, 02775, b.control_files._dir_mode)
146
assertEqualMode(self, 0664, b.control_files._file_mode)
140
t = WorkingTree.open('.')
142
self.assertEqualMode(02775, b.control_files._dir_mode)
143
self.assertEqualMode(0664, b.control_files._file_mode)
148
145
open('a', 'wb').write('foo4\n')
191
188
LockableFiles._set_dir_mode = True
192
189
LockableFiles._set_file_mode = True
194
def test_new_branch(self):
195
if sys.platform == 'win32':
196
raise TestSkipped('chmod has no effect on win32')
197
#FIXME RBC 20060105 should test branch and repository
199
# also, these are BzrBranch format specific things..
201
mode = stat.S_IMODE(os.stat('a').st_mode)
202
t = WorkingTree.create_standalone('.')
204
assertEqualMode(self, mode, b.control_files._dir_mode)
205
assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
209
b = Branch.create('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.create('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.create('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
192
class TestSftpPermissions(TestCaseWithSFTPServer):
234
197
# Though it would be nice to test that SFTP to a server
235
198
# 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)
200
# bodge around for stubsftpserver not letting use connect
202
_t = get_transport(self.get_url())
244
204
os.mkdir('local')
245
t_local = WorkingTree.create_standalone('local')
205
t_local = self.make_branch_and_tree('local')
246
206
b_local = t_local.branch
247
207
open('local/a', 'wb').write('foo\n')
249
209
t_local.commit('foo')
251
211
# Delete them because we are modifying the filesystem underneath them
253
212
chmod_r('local/.bzr', 0644, 0755)
254
213
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)
215
t = WorkingTree.open('local')
217
self.assertEqualMode(0755, b_local.control_files._dir_mode)
218
self.assertEqualMode(0644, b_local.control_files._file_mode)
262
sftp_url = self.get_remote_url('sftp')
263
b_sftp = Branch.create(sftp_url)
221
sftp_url = self.get_url('sftp')
222
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
265
224
b_sftp.pull(b_local)
268
227
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
270
229
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)
230
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
231
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
274
233
open('local/a', 'wb').write('foo2\n')
275
234
t_local.commit('foo2')
289
248
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
291
250
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)
251
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
252
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
295
254
open('local/a', 'wb').write('foo3\n')
296
255
t_local.commit('foo3')
311
270
original_umask = os.umask(umask)
314
from bzrlib.transport.sftp import SFTPTransport
315
t = SFTPTransport(self._sftp_url)
273
t = get_transport(self.get_url())
316
274
# Direct access should be masked by umask
317
275
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
318
276
self.assertTransportMode(t, 'a', 0666 &~umask)