~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
from StringIO import StringIO
37
37
 
38
38
from bzrlib.branch import Branch
39
 
from bzrlib.lockable_files import LockableFiles
 
39
from bzrlib.bzrdir import BzrDir
 
40
from bzrlib.lockable_files import LockableFiles, TransportLock
40
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
41
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
42
43
from bzrlib.transport import get_transport
78
79
            test.assertTransportMode(t, p, file_mode)
79
80
 
80
81
 
81
 
def assertEqualMode(test, mode, mode_test):
82
 
    test.assertEqual(mode, mode_test,
83
 
                     'mode mismatch %o != %o' % (mode, mode_test))
84
 
 
85
 
 
86
82
class TestPermissions(TestCaseWithTransport):
87
83
 
88
84
    def test_new_files(self):
89
85
        if sys.platform == 'win32':
90
86
            raise TestSkipped('chmod has no effect on win32')
91
87
 
92
 
        t = WorkingTree.create_standalone('.')
 
88
        t = self.make_branch_and_tree('.')
93
89
        b = t.branch
94
90
        open('a', 'wb').write('foo\n')
95
91
        t.add('a')
96
92
        t.commit('foo')
97
93
 
98
 
        # Delete them because we are modifying the filesystem underneath them
99
 
        del b, t 
100
94
        chmod_r('.bzr', 0644, 0755)
101
95
        check_mode_r(self, '.bzr', 0644, 0755)
102
96
 
103
 
        b = Branch.open('.')
104
 
        t = b.working_tree()
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('.')
 
103
        b = t.branch
 
104
        self.assertEqualMode(0755, b.control_files._dir_mode)
 
105
        self.assertEqualMode(0644, b.control_files._file_mode)
107
106
 
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)
119
118
 
120
 
        del b, t
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)
124
 
        b = Branch.open('.')
125
 
        t = b.working_tree()
126
 
        assertEqualMode(self, 0775, b.control_files._dir_mode)
127
 
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
122
        t = WorkingTree.open('.')
 
123
        b = t.branch
 
124
        self.assertEqualMode(0775, b.control_files._dir_mode)
 
125
        self.assertEqualMode(0664, b.control_files._file_mode)
128
126
 
129
127
        open('a', 'wb').write('foo3\n')
130
128
        t.commit('foo3')
136
134
        check_mode_r(self, '.bzr', 0664, 0775)
137
135
 
138
136
        # Test the group sticky bit
139
 
        del b, t
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)
143
 
        b = Branch.open('.')
144
 
        t = b.working_tree()
145
 
        assertEqualMode(self, 02775, b.control_files._dir_mode)
146
 
        assertEqualMode(self, 0664, b.control_files._file_mode)
 
140
        t = WorkingTree.open('.')
 
141
        b = t.branch
 
142
        self.assertEqualMode(02775, b.control_files._dir_mode)
 
143
        self.assertEqualMode(0664, b.control_files._file_mode)
147
144
 
148
145
        open('a', 'wb').write('foo4\n')
149
146
        t.commit('foo4')
160
157
        try:
161
158
            transport = get_transport(self.get_url())
162
159
            transport.put('my-lock', StringIO(''))
163
 
            lockable = LockableFiles(transport, 'my-lock')
 
160
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
164
161
            self.assertNotEqual(None, lockable._dir_mode)
165
162
            self.assertNotEqual(None, lockable._file_mode)
166
163
 
167
164
            LockableFiles._set_dir_mode = False
168
165
            transport = get_transport('.')
169
 
            lockable = LockableFiles(transport, 'my-lock')
 
166
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
170
167
            self.assertEqual(None, lockable._dir_mode)
171
168
            self.assertNotEqual(None, lockable._file_mode)
172
169
 
173
170
            LockableFiles._set_file_mode = False
174
171
            transport = get_transport('.')
175
 
            lockable = LockableFiles(transport, 'my-lock')
 
172
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
176
173
            self.assertEqual(None, lockable._dir_mode)
177
174
            self.assertEqual(None, lockable._file_mode)
178
175
 
179
176
            LockableFiles._set_dir_mode = True
180
177
            transport = get_transport('.')
181
 
            lockable = LockableFiles(transport, 'my-lock')
 
178
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
182
179
            self.assertNotEqual(None, lockable._dir_mode)
183
180
            self.assertEqual(None, lockable._file_mode)
184
181
 
185
182
            LockableFiles._set_file_mode = True
186
183
            transport = get_transport('.')
187
 
            lockable = LockableFiles(transport, 'my-lock')
 
184
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
188
185
            self.assertNotEqual(None, lockable._dir_mode)
189
186
            self.assertNotEqual(None, lockable._file_mode)
190
187
        finally:
191
188
            LockableFiles._set_dir_mode = True
192
189
            LockableFiles._set_file_mode = True
193
190
 
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 
198
 
        # permissions ? 
199
 
        # also, these are BzrBranch format specific things..
200
 
        os.mkdir('a')
201
 
        mode = stat.S_IMODE(os.stat('a').st_mode)
202
 
        t = WorkingTree.create_standalone('.')
203
 
        b = t.branch
204
 
        assertEqualMode(self, mode, b.control_files._dir_mode)
205
 
        assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
206
 
 
207
 
        os.mkdir('b')
208
 
        os.chmod('b', 02777)
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)
213
 
 
214
 
        os.mkdir('c')
215
 
        os.chmod('c', 02750)
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)
220
 
 
221
 
        os.mkdir('d')
222
 
        os.chmod('d', 0700)
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)
227
 
 
228
191
 
229
192
class TestSftpPermissions(TestCaseWithSFTPServer):
230
193
 
234
197
        # Though it would be nice to test that SFTP to a server
235
198
        # which does support chmod has the right effect
236
199
 
237
 
        from bzrlib.transport.sftp import SFTPTransport
238
 
 
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
 
201
        # more than once
 
202
        _t = get_transport(self.get_url())
243
203
 
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')
248
208
        t_local.add('a')
249
209
        t_local.commit('foo')
250
210
 
251
211
        # Delete them because we are modifying the filesystem underneath them
252
 
        del b_local, t_local 
253
212
        chmod_r('local/.bzr', 0644, 0755)
254
213
        check_mode_r(self, 'local/.bzr', 0644, 0755)
255
214
 
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')
 
216
        b_local = t.branch
 
217
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
 
218
        self.assertEqualMode(0644, b_local.control_files._file_mode)
260
219
 
261
220
        os.mkdir('sftp')
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)
264
223
 
265
224
        b_sftp.pull(b_local)
266
225
        del b_sftp
268
227
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
269
228
 
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)
273
232
 
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)
290
249
 
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)
294
253
 
295
254
        open('local/a', 'wb').write('foo3\n')
296
255
        t_local.commit('foo3')
311
270
        original_umask = os.umask(umask)
312
271
 
313
272
        try:
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)