~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

(vila) Make all transport put_bytes() raises TypeError when given unicode
 strings rather than bytes (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
# -*- coding: utf-8 -*-
3
 
 
 
3
#
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.
8
 
 
 
8
#
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.
13
 
 
 
13
#
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
17
17
 
18
18
 
19
19
"""Tests for bzr setting permissions.
26
26
"""
27
27
 
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?
32
 
 
 
32
 
33
33
import os
34
34
import sys
35
 
import stat
36
 
from StringIO import StringIO
37
35
 
 
36
from bzrlib import urlutils
38
37
from bzrlib.branch import Branch
39
 
from bzrlib.bzrdir import BzrDir
40
 
from bzrlib.lockable_files import LockableFiles, TransportLock
 
38
from bzrlib.controldir import ControlDir
41
39
from bzrlib.tests import TestCaseWithTransport, TestSkipped
42
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
43
 
from bzrlib.transport import get_transport
44
41
from bzrlib.workingtree import WorkingTree
45
42
 
46
43
 
47
44
def chmod_r(base, file_mode, dir_mode):
48
45
    """Recursively chmod from a base directory"""
49
 
    assert os.path.isdir(base)
50
46
    os.chmod(base, dir_mode)
51
47
    for root, dirs, files in os.walk(base):
52
48
        for d in dirs:
66
62
    :param dir_mode: The mode for all directories
67
63
    :param include_base: If false, only check the subdirectories
68
64
    """
69
 
    assert os.path.isdir(base)
70
 
    t = get_transport(".")
 
65
    t = test.get_transport()
71
66
    if include_base:
72
67
        test.assertTransportMode(t, base, dir_mode)
73
68
    for root, dirs, files in os.walk(base):
74
69
        for d in dirs:
75
 
            p = os.path.join(root, d)
 
70
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
76
71
            test.assertTransportMode(t, p, dir_mode)
77
72
        for f in files:
78
73
            p = os.path.join(root, f)
 
74
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
79
75
            test.assertTransportMode(t, p, file_mode)
80
76
 
81
77
 
87
83
 
88
84
        t = self.make_branch_and_tree('.')
89
85
        b = t.branch
90
 
        open('a', 'wb').write('foo\n')
91
 
        t.add('a')
 
86
        with open('a', 'wb') as f: f.write('foo\n')
 
87
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
 
88
        t.add('a', 'CAPS-ID')
92
89
        t.commit('foo')
93
90
 
94
91
        chmod_r('.bzr', 0644, 0755)
96
93
 
97
94
        # although we are modifying the filesystem
98
95
        # underneath the objects, they are not locked, and thus it must
99
 
        # 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
100
97
        # mode change in the control bits, which current do not refresh
101
98
        # when a new lock is taken out.
102
99
        t = WorkingTree.open('.')
103
100
        b = t.branch
104
101
        self.assertEqualMode(0755, b.control_files._dir_mode)
105
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())
106
105
 
107
106
        # Modifying a file shouldn't break the permissions
108
 
        open('a', 'wb').write('foo2\n')
 
107
        with open('a', 'wb') as f: f.write('foo2\n')
109
108
        t.commit('foo2')
110
109
        # The mode should be maintained after commit
111
110
        check_mode_r(self, '.bzr', 0644, 0755)
112
111
 
113
112
        # Adding a new file should maintain the permissions
114
 
        open('b', 'wb').write('new b\n')
 
113
        with open('b', 'wb') as f: f.write('new b\n')
115
114
        t.add('b')
116
115
        t.commit('new b')
117
116
        check_mode_r(self, '.bzr', 0644, 0755)
123
122
        b = t.branch
124
123
        self.assertEqualMode(0775, b.control_files._dir_mode)
125
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())
126
127
 
127
 
        open('a', 'wb').write('foo3\n')
 
128
        with open('a', 'wb') as f: f.write('foo3\n')
128
129
        t.commit('foo3')
129
130
        check_mode_r(self, '.bzr', 0664, 0775)
130
131
 
131
 
        open('c', 'wb').write('new c\n')
 
132
        with open('c', 'wb') as f: f.write('new c\n')
132
133
        t.add('c')
133
134
        t.commit('new c')
134
135
        check_mode_r(self, '.bzr', 0664, 0775)
135
136
 
 
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())
 
145
 
 
146
        t = self.make_branch_and_tree('.')
 
147
        b = t.branch
 
148
 
136
149
        # Test the group sticky bit
137
150
        # Recursively update the modes of all files
138
151
        chmod_r('.bzr', 0664, 02775)
141
154
        b = t.branch
142
155
        self.assertEqualMode(02775, b.control_files._dir_mode)
143
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())
144
159
 
145
 
        open('a', 'wb').write('foo4\n')
 
160
        with open('a', 'wb') as f: f.write('foo4\n')
146
161
        t.commit('foo4')
147
162
        check_mode_r(self, '.bzr', 0664, 02775)
148
163
 
149
 
        open('d', 'wb').write('new d\n')
 
164
        with open('d', 'wb') as f: f.write('new d\n')
150
165
        t.add('d')
151
166
        t.commit('new d')
152
167
        check_mode_r(self, '.bzr', 0664, 02775)
153
168
 
154
 
    def test_disable_set_mode(self):
155
 
        # TODO: jam 20051215 Ultimately, this test should probably test that
156
 
        #                    extra chmod calls aren't being made
157
 
        try:
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)
163
 
 
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)
169
 
 
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)
175
 
 
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)
181
 
 
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)
187
 
        finally:
188
 
            LockableFiles._set_dir_mode = True
189
 
            LockableFiles._set_file_mode = True
190
 
 
191
169
 
192
170
class TestSftpPermissions(TestCaseWithSFTPServer):
193
171
 
199
177
 
200
178
        # bodge around for stubsftpserver not letting use connect
201
179
        # more than once
202
 
        _t = get_transport(self.get_url())
 
180
        _t = self.get_transport()
203
181
 
204
182
        os.mkdir('local')
205
183
        t_local = self.make_branch_and_tree('local')
206
184
        b_local = t_local.branch
207
 
        open('local/a', 'wb').write('foo\n')
 
185
        with open('local/a', 'wb') as f: f.write('foo\n')
208
186
        t_local.add('a')
209
187
        t_local.commit('foo')
210
188
 
216
194
        b_local = t.branch
217
195
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
218
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())
219
199
 
220
200
        os.mkdir('sftp')
221
201
        sftp_url = self.get_url('sftp')
222
 
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
202
        b_sftp = ControlDir.create_branch_and_repo(sftp_url)
223
203
 
224
204
        b_sftp.pull(b_local)
225
205
        del b_sftp
229
209
        b_sftp = Branch.open(sftp_url)
230
210
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
231
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())
232
214
 
233
 
        open('local/a', 'wb').write('foo2\n')
 
215
        with open('local/a', 'wb') as f: f.write('foo2\n')
234
216
        t_local.commit('foo2')
235
217
        b_sftp.pull(b_local)
236
218
        # The mode should be maintained after commit
237
219
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
238
220
 
239
 
        open('local/b', 'wb').write('new b\n')
 
221
        with open('local/b', 'wb') as f: f.write('new b\n')
240
222
        t_local.add('b')
241
223
        t_local.commit('new b')
242
224
        b_sftp.pull(b_local)
250
232
        b_sftp = Branch.open(sftp_url)
251
233
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
252
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())
253
237
 
254
 
        open('local/a', 'wb').write('foo3\n')
 
238
        with open('local/a', 'wb') as f: f.write('foo3\n')
255
239
        t_local.commit('foo3')
256
240
        b_sftp.pull(b_local)
257
241
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
258
242
 
259
 
        open('local/c', 'wb').write('new c\n')
 
243
        with open('local/c', 'wb') as f: f.write('new c\n')
260
244
        t_local.add('c')
261
245
        t_local.commit('new c')
262
246
        b_sftp.pull(b_local)
270
254
        original_umask = os.umask(umask)
271
255
 
272
256
        try:
273
 
            t = get_transport(self.get_url())
 
257
            t = self.get_transport()
274
258
            # Direct access should be masked by umask
275
259
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
276
260
            self.assertTransportMode(t, 'a', 0666 &~umask)
277
261
 
278
262
            # but Transport overrides umask
279
 
            t.put('b', 'txt', mode=0666)
 
263
            t.put_bytes('b', 'txt', mode=0666)
280
264
            self.assertTransportMode(t, 'b', 0666)
281
265
 
282
 
            t._sftp.mkdir('c', mode=0777)
 
266
            t._get_sftp().mkdir('c', mode=0777)
283
267
            self.assertTransportMode(t, 'c', 0777 &~umask)
284
268
 
285
269
            t.mkdir('d', mode=0777)