1
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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36
36
from StringIO import StringIO
38
38
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
39
from bzrlib.bzrdir import BzrDir
40
from bzrlib.lockable_files import LockableFiles, TransportLock
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
43
from bzrlib.transport import get_transport
44
from bzrlib.workingtree import WorkingTree
47
47
def chmod_r(base, file_mode, dir_mode):
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')
99
# Delete them because we are modifying the filesystem underneath them
101
94
chmod_r('.bzr', 0644, 0755)
102
95
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)
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)
109
107
# Modifying a file shouldn't break the permissions
110
108
open('a', 'wb').write('foo2\n')
118
116
t.commit('new b')
119
117
check_mode_r(self, '.bzr', 0644, 0755)
122
119
# Recursively update the modes of all files
123
120
chmod_r('.bzr', 0664, 0775)
124
121
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)
122
t = WorkingTree.open('.')
124
self.assertEqualMode(0775, b.control_files._dir_mode)
125
self.assertEqualMode(0664, b.control_files._file_mode)
130
127
open('a', 'wb').write('foo3\n')
137
134
check_mode_r(self, '.bzr', 0664, 0775)
139
136
# Test the group sticky bit
141
137
# Recursively update the modes of all files
142
138
chmod_r('.bzr', 0664, 02775)
143
139
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)
140
t = WorkingTree.open('.')
142
self.assertEqualMode(02775, b.control_files._dir_mode)
143
self.assertEqualMode(0664, b.control_files._file_mode)
149
145
open('a', 'wb').write('foo4\n')
159
155
# TODO: jam 20051215 Ultimately, this test should probably test that
160
156
# extra chmod calls aren't being made
162
transport = get_transport('.')
158
transport = get_transport(self.get_url())
163
159
transport.put('my-lock', StringIO(''))
164
lockable = LockableFiles(transport, 'my-lock')
160
lockable = LockableFiles(transport, 'my-lock', TransportLock)
165
161
self.assertNotEqual(None, lockable._dir_mode)
166
162
self.assertNotEqual(None, lockable._file_mode)
168
164
LockableFiles._set_dir_mode = False
169
165
transport = get_transport('.')
170
lockable = LockableFiles(transport, 'my-lock')
166
lockable = LockableFiles(transport, 'my-lock', TransportLock)
171
167
self.assertEqual(None, lockable._dir_mode)
172
168
self.assertNotEqual(None, lockable._file_mode)
174
170
LockableFiles._set_file_mode = False
175
171
transport = get_transport('.')
176
lockable = LockableFiles(transport, 'my-lock')
172
lockable = LockableFiles(transport, 'my-lock', TransportLock)
177
173
self.assertEqual(None, lockable._dir_mode)
178
174
self.assertEqual(None, lockable._file_mode)
180
176
LockableFiles._set_dir_mode = True
181
177
transport = get_transport('.')
182
lockable = LockableFiles(transport, 'my-lock')
178
lockable = LockableFiles(transport, 'my-lock', TransportLock)
183
179
self.assertNotEqual(None, lockable._dir_mode)
184
180
self.assertEqual(None, lockable._file_mode)
186
182
LockableFiles._set_file_mode = True
187
183
transport = get_transport('.')
188
lockable = LockableFiles(transport, 'my-lock')
184
lockable = LockableFiles(transport, 'my-lock', TransportLock)
189
185
self.assertNotEqual(None, lockable._dir_mode)
190
186
self.assertNotEqual(None, lockable._file_mode)
192
188
LockableFiles._set_dir_mode = True
193
189
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
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
b_local = Branch.initialize(u'local')
246
t_local = b_local.working_tree()
205
t_local = self.make_branch_and_tree('local')
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.initialize(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)