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
24
24
In the future, when we have Repository/Branch/Checkout information, the
25
25
permissions should be inherited individually, rather than all be the same.
27
TODO: jam 20051215 There are no tests for ftp yet, because we have no ftp server
28
TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
29
defined by the local umask. This isn't terrible, is it
30
the truly desired behavior?
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 StringIO import StringIO
38
37
from bzrlib.branch import Branch
39
from bzrlib.bzrdir import BzrDir
40
from bzrlib.lockable_files import LockableFiles, TransportLock
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
38
from bzrlib.tests import TestCaseInTempDir, TestSkipped
42
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
43
40
from bzrlib.transport import get_transport
44
from bzrlib.workingtree import WorkingTree
47
43
def chmod_r(base, file_mode, dir_mode):
79
75
test.assertTransportMode(t, p, file_mode)
82
class TestPermissions(TestCaseWithTransport):
78
def assertEqualMode(test, mode, mode_test):
79
test.assertEqual(mode, mode_test,
80
'mode mismatch %o != %o' % (mode, mode_test))
83
class TestPermissions(TestCaseInTempDir):
84
85
def test_new_files(self):
85
86
if sys.platform == 'win32':
86
87
raise TestSkipped('chmod has no effect on win32')
88
t = self.make_branch_and_tree('.')
89
b = Branch.initialize(u'.')
90
91
open('a', 'wb').write('foo\n')
95
# Delete them because we are modifying the filesystem underneath them
94
97
chmod_r('.bzr', 0644, 0755)
95
98
check_mode_r(self, '.bzr', 0644, 0755)
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)
102
assertEqualMode(self, 0755, b._dir_mode)
103
assertEqualMode(self, 0644, b._file_mode)
107
105
# Modifying a file shouldn't break the permissions
108
106
open('a', 'wb').write('foo2\n')
116
114
t.commit('new b')
117
115
check_mode_r(self, '.bzr', 0644, 0755)
119
118
# Recursively update the modes of all files
120
119
chmod_r('.bzr', 0664, 0775)
121
120
check_mode_r(self, '.bzr', 0664, 0775)
122
t = WorkingTree.open('.')
124
self.assertEqualMode(0775, b.control_files._dir_mode)
125
self.assertEqualMode(0664, b.control_files._file_mode)
123
assertEqualMode(self, 0775, b._dir_mode)
124
assertEqualMode(self, 0664, b._file_mode)
127
126
open('a', 'wb').write('foo3\n')
134
133
check_mode_r(self, '.bzr', 0664, 0775)
136
135
# Test the group sticky bit
137
137
# Recursively update the modes of all files
138
138
chmod_r('.bzr', 0664, 02775)
139
139
check_mode_r(self, '.bzr', 0664, 02775)
140
t = WorkingTree.open('.')
142
self.assertEqualMode(02775, b.control_files._dir_mode)
143
self.assertEqualMode(0664, b.control_files._file_mode)
142
assertEqualMode(self, 02775, b._dir_mode)
143
assertEqualMode(self, 0664, b._file_mode)
145
145
open('a', 'wb').write('foo4\n')
154
154
def test_disable_set_mode(self):
155
155
# TODO: jam 20051215 Ultimately, this test should probably test that
156
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)
159
b = Branch.initialize(u'.')
160
self.assertNotEqual(None, b._dir_mode)
161
self.assertNotEqual(None, b._file_mode)
163
bzrlib.branch.BzrBranch._set_dir_mode = False
164
b = Branch.open(u'.')
165
self.assertEqual(None, b._dir_mode)
166
self.assertNotEqual(None, b._file_mode)
168
bzrlib.branch.BzrBranch._set_file_mode = False
169
b = Branch.open(u'.')
170
self.assertEqual(None, b._dir_mode)
171
self.assertEqual(None, b._file_mode)
173
bzrlib.branch.BzrBranch._set_dir_mode = True
174
b = Branch.open(u'.')
175
self.assertNotEqual(None, b._dir_mode)
176
self.assertEqual(None, b._file_mode)
178
bzrlib.branch.BzrBranch._set_file_mode = True
179
b = Branch.open(u'.')
180
self.assertNotEqual(None, b._dir_mode)
181
self.assertNotEqual(None, b._file_mode)
188
LockableFiles._set_dir_mode = True
189
LockableFiles._set_file_mode = True
183
bzrlib.branch.BzrBranch._set_dir_mode = True
184
bzrlib.branch.BzrBranch._set_file_mode = True
186
def test_new_branch(self):
187
if sys.platform == 'win32':
188
raise TestSkipped('chmod has no effect on win32')
191
mode = stat.S_IMODE(os.stat('a').st_mode)
192
b = Branch.initialize('a')
193
assertEqualMode(self, mode, b._dir_mode)
194
assertEqualMode(self, mode & ~07111, b._file_mode)
198
b = Branch.initialize('b')
199
assertEqualMode(self, 02777, b._dir_mode)
200
assertEqualMode(self, 00666, b._file_mode)
201
check_mode_r(self, 'b/.bzr', 00666, 02777)
205
b = Branch.initialize('c')
206
assertEqualMode(self, 02750, b._dir_mode)
207
assertEqualMode(self, 00640, b._file_mode)
208
check_mode_r(self, 'c/.bzr', 00640, 02750)
212
b = Branch.initialize('d')
213
assertEqualMode(self, 0700, b._dir_mode)
214
assertEqualMode(self, 0600, b._file_mode)
215
check_mode_r(self, 'd/.bzr', 00600, 0700)
192
218
class TestSftpPermissions(TestCaseWithSFTPServer):
197
223
# Though it would be nice to test that SFTP to a server
198
224
# which does support chmod has the right effect
200
# bodge around for stubsftpserver not letting use connect
202
_t = get_transport(self.get_url())
226
from bzrlib.transport.sftp import SFTPTransport
228
# We don't actually use it directly, we just want to
229
# keep the connection open, since StubSFTPServer only
230
# allows 1 connection
231
_transport = SFTPTransport(self._sftp_url)
204
233
os.mkdir('local')
205
t_local = self.make_branch_and_tree('local')
206
b_local = t_local.branch
234
b_local = Branch.initialize(u'local')
235
t_local = b_local.working_tree()
207
236
open('local/a', 'wb').write('foo\n')
209
238
t_local.commit('foo')
211
240
# Delete them because we are modifying the filesystem underneath them
212
242
chmod_r('local/.bzr', 0644, 0755)
213
243
check_mode_r(self, 'local/.bzr', 0644, 0755)
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)
245
b_local = Branch.open(u'local')
246
t_local = b_local.working_tree()
247
assertEqualMode(self, 0755, b_local._dir_mode)
248
assertEqualMode(self, 0644, b_local._file_mode)
221
sftp_url = self.get_url('sftp')
222
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
251
sftp_url = self.get_remote_url('sftp')
252
b_sftp = Branch.initialize(sftp_url)
224
254
b_sftp.pull(b_local)