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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 bzrlib import urlutils
39
37
from bzrlib.branch import Branch
40
from bzrlib.bzrdir import BzrDir
41
from bzrlib.lockable_files import LockableFiles, TransportLock
38
from bzrlib.controldir import ControlDir
42
39
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
from bzrlib.transport import get_transport
45
41
from bzrlib.workingtree import WorkingTree
48
44
def chmod_r(base, file_mode, dir_mode):
49
45
"""Recursively chmod from a base directory"""
50
assert os.path.isdir(base)
51
46
os.chmod(base, dir_mode)
52
47
for root, dirs, files in os.walk(base):
67
62
:param dir_mode: The mode for all directories
68
63
:param include_base: If false, only check the subdirectories
70
assert os.path.isdir(base)
71
t = get_transport(".")
65
t = test.get_transport()
73
67
test.assertTransportMode(t, base, dir_mode)
74
68
for root, dirs, files in os.walk(base):
76
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
70
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
77
71
test.assertTransportMode(t, p, dir_mode)
79
73
p = os.path.join(root, f)
80
p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
74
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
81
75
test.assertTransportMode(t, p, file_mode)
100
94
# although we are modifying the filesystem
101
95
# underneath the objects, they are not locked, and thus it must
102
# be safe for most operations. But here we want to observe a
96
# be safe for most operations. But here we want to observe a
103
97
# mode change in the control bits, which current do not refresh
104
98
# when a new lock is taken out.
105
99
t = WorkingTree.open('.')
107
101
self.assertEqualMode(0755, b.control_files._dir_mode)
108
102
self.assertEqualMode(0644, b.control_files._file_mode)
103
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
104
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
110
106
# Modifying a file shouldn't break the permissions
111
open('a', 'wb').write('foo2\n')
107
with open('a', 'wb') as f: f.write('foo2\n')
113
109
# The mode should be maintained after commit
114
110
check_mode_r(self, '.bzr', 0644, 0755)
116
112
# Adding a new file should maintain the permissions
117
open('b', 'wb').write('new b\n')
113
with open('b', 'wb') as f: f.write('new b\n')
119
115
t.commit('new b')
120
116
check_mode_r(self, '.bzr', 0644, 0755)
127
123
self.assertEqualMode(0775, b.control_files._dir_mode)
128
124
self.assertEqualMode(0664, b.control_files._file_mode)
125
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
126
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
130
open('a', 'wb').write('foo3\n')
128
with open('a', 'wb') as f: f.write('foo3\n')
132
130
check_mode_r(self, '.bzr', 0664, 0775)
134
open('c', 'wb').write('new c\n')
132
with open('c', 'wb') as f: f.write('new c\n')
136
134
t.commit('new c')
137
135
check_mode_r(self, '.bzr', 0664, 0775)
137
def test_new_files_group_sticky_bit(self):
138
if sys.platform == 'win32':
139
raise TestSkipped('chmod has no effect on win32')
140
elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
141
# FreeBSD-based platforms create temp dirs with the 'wheel' group,
142
# which users are not likely to be in, and this prevents us from
143
# setting the sgid bit
144
os.chown(self.test_dir, os.getuid(), os.getgid())
146
t = self.make_branch_and_tree('.')
139
149
# Test the group sticky bit
140
150
# Recursively update the modes of all files
141
151
chmod_r('.bzr', 0664, 02775)
145
155
self.assertEqualMode(02775, b.control_files._dir_mode)
146
156
self.assertEqualMode(0664, b.control_files._file_mode)
157
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
158
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
148
open('a', 'wb').write('foo4\n')
160
with open('a', 'wb') as f: f.write('foo4\n')
150
162
check_mode_r(self, '.bzr', 0664, 02775)
152
open('d', 'wb').write('new d\n')
164
with open('d', 'wb') as f: f.write('new d\n')
154
166
t.commit('new d')
155
167
check_mode_r(self, '.bzr', 0664, 02775)
157
def test_disable_set_mode(self):
158
# TODO: jam 20051215 Ultimately, this test should probably test that
159
# extra chmod calls aren't being made
161
transport = get_transport(self.get_url())
162
transport.put_bytes('my-lock', '')
163
lockable = LockableFiles(transport, 'my-lock', TransportLock)
164
self.assertNotEqual(None, lockable._dir_mode)
165
self.assertNotEqual(None, lockable._file_mode)
167
LockableFiles._set_dir_mode = False
168
transport = get_transport('.')
169
lockable = LockableFiles(transport, 'my-lock', TransportLock)
170
self.assertEqual(None, lockable._dir_mode)
171
self.assertNotEqual(None, lockable._file_mode)
173
LockableFiles._set_file_mode = False
174
transport = get_transport('.')
175
lockable = LockableFiles(transport, 'my-lock', TransportLock)
176
self.assertEqual(None, lockable._dir_mode)
177
self.assertEqual(None, lockable._file_mode)
179
LockableFiles._set_dir_mode = True
180
transport = get_transport('.')
181
lockable = LockableFiles(transport, 'my-lock', TransportLock)
182
self.assertNotEqual(None, lockable._dir_mode)
183
self.assertEqual(None, lockable._file_mode)
185
LockableFiles._set_file_mode = True
186
transport = get_transport('.')
187
lockable = LockableFiles(transport, 'my-lock', TransportLock)
188
self.assertNotEqual(None, lockable._dir_mode)
189
self.assertNotEqual(None, lockable._file_mode)
191
LockableFiles._set_dir_mode = True
192
LockableFiles._set_file_mode = True
195
170
class TestSftpPermissions(TestCaseWithSFTPServer):
219
194
b_local = t.branch
220
195
self.assertEqualMode(0755, b_local.control_files._dir_mode)
221
196
self.assertEqualMode(0644, b_local.control_files._file_mode)
197
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
198
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
224
201
sftp_url = self.get_url('sftp')
225
b_sftp = BzrDir.create_branch_and_repo(sftp_url)
202
b_sftp = ControlDir.create_branch_and_repo(sftp_url)
227
204
b_sftp.pull(b_local)
232
209
b_sftp = Branch.open(sftp_url)
233
210
self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
234
211
self.assertEqualMode(0644, b_sftp.control_files._file_mode)
212
self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
213
self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
236
open('local/a', 'wb').write('foo2\n')
215
with open('local/a', 'wb') as f: f.write('foo2\n')
237
216
t_local.commit('foo2')
238
217
b_sftp.pull(b_local)
239
218
# The mode should be maintained after commit
240
219
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
242
open('local/b', 'wb').write('new b\n')
221
with open('local/b', 'wb') as f: f.write('new b\n')
244
223
t_local.commit('new b')
245
224
b_sftp.pull(b_local)
253
232
b_sftp = Branch.open(sftp_url)
254
233
self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
255
234
self.assertEqualMode(0664, b_sftp.control_files._file_mode)
235
self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
236
self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
257
open('local/a', 'wb').write('foo3\n')
238
with open('local/a', 'wb') as f: f.write('foo3\n')
258
239
t_local.commit('foo3')
259
240
b_sftp.pull(b_local)
260
241
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
262
open('local/c', 'wb').write('new c\n')
243
with open('local/c', 'wb') as f: f.write('new c\n')
264
245
t_local.commit('new c')
265
246
b_sftp.pull(b_local)