1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# Copyright (C) 2005 by Canonical Ltd
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for bzr setting permissions.
Files which are created underneath .bzr/ should inherit its permissions.
So if the directory is group writable, the files and subdirs should be as well.
In the future, when we have Repository/Branch/Checkout information, the
permissions should be inherited individually, rather than all be the same.
TODO: jam 20051215 There are no tests for ftp yet, because we have no ftp server
TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
defined by the local umask. This isn't terrible, is it
the truly desired behavior?
"""
import os
import sys
import stat
from bzrlib.branch import Branch
from bzrlib.tests import TestCaseInTempDir, TestSkipped
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
from bzrlib.tests.test_transport import check_mode
def chmod_r(base, file_mode, dir_mode):
"""Recursively chmod from a base directory"""
assert os.path.isdir(base)
os.chmod(base, dir_mode)
for root, dirs, files in os.walk(base):
for d in dirs:
p = os.path.join(root, d)
os.chmod(p, dir_mode)
for f in files:
p = os.path.join(root, f)
os.chmod(p, file_mode)
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
"""Check that all permissions match
:param test: The TestCase being run
:param base: The path to the root directory to check
:param file_mode: The mode for all files
:param dir_mode: The mode for all directories
:param include_base: If false, only check the subdirectories
"""
assert os.path.isdir(base)
if include_base:
check_mode(test, base, dir_mode)
for root, dirs, files in os.walk(base):
for d in dirs:
p = os.path.join(root, d)
check_mode(test, p, dir_mode)
for f in files:
p = os.path.join(root, f)
check_mode(test, p, file_mode)
def assertEqualMode(test, mode, mode_test):
test.assertEqual(mode, mode_test,
'mode mismatch %o != %o' % (mode, mode_test))
class TestPermissions(TestCaseInTempDir):
def test_new_files(self):
if sys.platform == 'win32':
raise TestSkipped('chmod has no effect on win32')
b = Branch.initialize(u'.')
t = b.working_tree()
open('a', 'wb').write('foo\n')
t.add('a')
t.commit('foo')
# Delete them because we are modifying the filesystem underneath them
del b, t
chmod_r('.bzr', 0644, 0755)
check_mode_r(self, '.bzr', 0644, 0755)
b = Branch.open('.')
t = b.working_tree()
assertEqualMode(self, 0755, b._dir_mode)
assertEqualMode(self, 0644, b._file_mode)
# Modifying a file shouldn't break the permissions
open('a', 'wb').write('foo2\n')
t.commit('foo2')
# The mode should be maintained after commit
check_mode_r(self, '.bzr', 0644, 0755)
# Adding a new file should maintain the permissions
open('b', 'wb').write('new b\n')
t.add('b')
t.commit('new b')
check_mode_r(self, '.bzr', 0644, 0755)
del b, t
# Recursively update the modes of all files
chmod_r('.bzr', 0664, 0775)
check_mode_r(self, '.bzr', 0664, 0775)
b = Branch.open('.')
t = b.working_tree()
assertEqualMode(self, 0775, b._dir_mode)
assertEqualMode(self, 0664, b._file_mode)
open('a', 'wb').write('foo3\n')
t.commit('foo3')
check_mode_r(self, '.bzr', 0664, 0775)
open('c', 'wb').write('new c\n')
t.add('c')
t.commit('new c')
check_mode_r(self, '.bzr', 0664, 0775)
# Test the group sticky bit
del b, t
# Recursively update the modes of all files
chmod_r('.bzr', 0664, 02775)
check_mode_r(self, '.bzr', 0664, 02775)
b = Branch.open('.')
t = b.working_tree()
assertEqualMode(self, 02775, b._dir_mode)
assertEqualMode(self, 0664, b._file_mode)
open('a', 'wb').write('foo4\n')
t.commit('foo4')
check_mode_r(self, '.bzr', 0664, 02775)
open('d', 'wb').write('new d\n')
t.add('d')
t.commit('new d')
check_mode_r(self, '.bzr', 0664, 02775)
def test_disable_set_mode(self):
# TODO: jam 20051215 Ultimately, this test should probably test that
# extra chmod calls aren't being made
import bzrlib.branch
try:
b = Branch.initialize(u'.')
self.assertNotEqual(None, b._dir_mode)
self.assertNotEqual(None, b._file_mode)
bzrlib.branch.BzrBranch._set_dir_mode = False
b = Branch.open(u'.')
self.assertEqual(None, b._dir_mode)
self.assertNotEqual(None, b._file_mode)
bzrlib.branch.BzrBranch._set_file_mode = False
b = Branch.open(u'.')
self.assertEqual(None, b._dir_mode)
self.assertEqual(None, b._file_mode)
bzrlib.branch.BzrBranch._set_dir_mode = True
b = Branch.open(u'.')
self.assertNotEqual(None, b._dir_mode)
self.assertEqual(None, b._file_mode)
bzrlib.branch.BzrBranch._set_file_mode = True
b = Branch.open(u'.')
self.assertNotEqual(None, b._dir_mode)
self.assertNotEqual(None, b._file_mode)
finally:
bzrlib.branch.BzrBranch._set_dir_mode = True
bzrlib.branch.BzrBranch._set_file_mode = True
def test_new_branch(self):
if sys.platform == 'win32':
raise TestSkipped('chmod has no effect on win32')
os.mkdir('a')
mode = stat.S_IMODE(os.stat('a').st_mode)
b = Branch.initialize('a')
assertEqualMode(self, mode, b._dir_mode)
assertEqualMode(self, mode & ~07111, b._file_mode)
os.mkdir('b')
os.chmod('b', 02777)
b = Branch.initialize('b')
assertEqualMode(self, 02777, b._dir_mode)
assertEqualMode(self, 00666, b._file_mode)
check_mode_r(self, 'b/.bzr', 00666, 02777)
os.mkdir('c')
os.chmod('c', 02750)
b = Branch.initialize('c')
assertEqualMode(self, 02750, b._dir_mode)
assertEqualMode(self, 00640, b._file_mode)
check_mode_r(self, 'c/.bzr', 00640, 02750)
os.mkdir('d')
os.chmod('d', 0700)
b = Branch.initialize('d')
assertEqualMode(self, 0700, b._dir_mode)
assertEqualMode(self, 0600, b._file_mode)
check_mode_r(self, 'd/.bzr', 00600, 0700)
class TestSftpPermissions(TestCaseWithSFTPServer):
def test_new_files(self):
if sys.platform == 'win32':
raise TestSkipped('chmod has no effect on win32')
# Though it would be nice to test that SFTP to a server
# which does support chmod has the right effect
from bzrlib.transport.sftp import SFTPTransport
# We don't actually use it directly, we just want to
# keep the connection open, since StubSFTPServer only
# allows 1 connection
self.delayed_setup()
_transport = SFTPTransport(self._sftp_url)
os.mkdir('local')
b_local = Branch.initialize(u'local')
t_local = b_local.working_tree()
open('local/a', 'wb').write('foo\n')
t_local.add('a')
t_local.commit('foo')
# Delete them because we are modifying the filesystem underneath them
del b_local, t_local
chmod_r('local/.bzr', 0644, 0755)
check_mode_r(self, 'local/.bzr', 0644, 0755)
b_local = Branch.open(u'local')
t_local = b_local.working_tree()
assertEqualMode(self, 0755, b_local._dir_mode)
assertEqualMode(self, 0644, b_local._file_mode)
os.mkdir('sftp')
# Why does self._sftp_url end with a slash????
sftp_url = self._sftp_url + 'sftp'
b_sftp = Branch.initialize(sftp_url)
b_sftp.pull(b_local)
del b_sftp
chmod_r('sftp/.bzr', 0644, 0755)
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
b_sftp = Branch.open(sftp_url)
assertEqualMode(self, 0755, b_sftp._dir_mode)
assertEqualMode(self, 0644, b_sftp._file_mode)
open('local/a', 'wb').write('foo2\n')
t_local.commit('foo2')
b_sftp.pull(b_local)
# The mode should be maintained after commit
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
open('local/b', 'wb').write('new b\n')
t_local.add('b')
t_local.commit('new b')
b_sftp.pull(b_local)
check_mode_r(self, 'sftp/.bzr', 0644, 0755)
del b_sftp
# Recursively update the modes of all files
chmod_r('sftp/.bzr', 0664, 0775)
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
b_sftp = Branch.open(sftp_url)
assertEqualMode(self, 0775, b_sftp._dir_mode)
assertEqualMode(self, 0664, b_sftp._file_mode)
open('local/a', 'wb').write('foo3\n')
t_local.commit('foo3')
b_sftp.pull(b_local)
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
open('local/c', 'wb').write('new c\n')
t_local.add('c')
t_local.commit('new c')
b_sftp.pull(b_local)
check_mode_r(self, 'sftp/.bzr', 0664, 0775)
def test_sftp_server_modes(self):
if sys.platform == 'win32':
raise TestSkipped('chmod has no effect on win32')
umask = 0022
original_umask = os.umask(umask)
try:
from bzrlib.transport.sftp import SFTPTransport
self.delayed_setup()
t = SFTPTransport(self._sftp_url)
# Direct access should be masked by umask
t._sftp_open_exclusive('a', mode=0666).write('foo\n')
check_mode(self, 'a', 0666 &~umask)
# but Transport overrides umask
t.put('b', 'txt', mode=0666)
check_mode(self, 'b', 0666)
t._sftp.mkdir('c', mode=0777)
check_mode(self, 'c', 0777 &~umask)
t.mkdir('d', mode=0777)
check_mode(self, 'd', 0777)
finally:
os.umask(original_umask)
|