~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 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
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 cStringIO import StringIO
37
 
import urllib
38
35
 
 
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
46
42
 
47
43
 
66
62
    :param dir_mode: The mode for all directories
67
63
    :param include_base: If false, only check the subdirectories
68
64
    """
69
 
    t = get_transport(".")
 
65
    t = test.get_transport()
70
66
    if include_base:
71
67
        test.assertTransportMode(t, base, dir_mode)
72
68
    for root, dirs, files in os.walk(base):
73
69
        for d in dirs:
74
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
 
70
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
75
71
            test.assertTransportMode(t, p, dir_mode)
76
72
        for f in files:
77
73
            p = os.path.join(root, f)
78
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [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')
 
86
        with open('a', 'wb') as f: f.write('foo\n')
91
87
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
92
88
        t.add('a', 'CAPS-ID')
93
89
        t.commit('foo')
97
93
 
98
94
        # although we are modifying the filesystem
99
95
        # underneath the objects, they are not locked, and thus it must
100
 
        # 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
101
97
        # mode change in the control bits, which current do not refresh
102
98
        # when a new lock is taken out.
103
99
        t = WorkingTree.open('.')
108
104
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())
109
105
 
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')
112
108
        t.commit('foo2')
113
109
        # The mode should be maintained after commit
114
110
        check_mode_r(self, '.bzr', 0644, 0755)
115
111
 
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')
118
114
        t.add('b')
119
115
        t.commit('new b')
120
116
        check_mode_r(self, '.bzr', 0644, 0755)
129
125
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
130
126
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
131
127
 
132
 
        open('a', 'wb').write('foo3\n')
 
128
        with open('a', 'wb') as f: f.write('foo3\n')
133
129
        t.commit('foo3')
134
130
        check_mode_r(self, '.bzr', 0664, 0775)
135
131
 
136
 
        open('c', 'wb').write('new c\n')
 
132
        with open('c', 'wb') as f: f.write('new c\n')
137
133
        t.add('c')
138
134
        t.commit('new c')
139
135
        check_mode_r(self, '.bzr', 0664, 0775)
141
137
    def test_new_files_group_sticky_bit(self):
142
138
        if sys.platform == 'win32':
143
139
            raise TestSkipped('chmod has no effect on win32')
144
 
        elif sys.platform == 'darwin':
145
 
            # OS X creates temp dirs with the 'wheel' group, which users are
146
 
            # not likely to be in, and this prevents us from setting the sgid
147
 
            # bit
 
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
148
144
            os.chown(self.test_dir, os.getuid(), os.getgid())
149
145
 
150
146
        t = self.make_branch_and_tree('.')
161
157
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
162
158
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
163
159
 
164
 
        open('a', 'wb').write('foo4\n')
 
160
        with open('a', 'wb') as f: f.write('foo4\n')
165
161
        t.commit('foo4')
166
162
        check_mode_r(self, '.bzr', 0664, 02775)
167
163
 
168
 
        open('d', 'wb').write('new d\n')
 
164
        with open('d', 'wb') as f: f.write('new d\n')
169
165
        t.add('d')
170
166
        t.commit('new d')
171
167
        check_mode_r(self, '.bzr', 0664, 02775)
181
177
 
182
178
        # bodge around for stubsftpserver not letting use connect
183
179
        # more than once
184
 
        _t = get_transport(self.get_url())
 
180
        _t = self.get_transport()
185
181
 
186
182
        os.mkdir('local')
187
183
        t_local = self.make_branch_and_tree('local')
188
184
        b_local = t_local.branch
189
 
        open('local/a', 'wb').write('foo\n')
 
185
        with open('local/a', 'wb') as f: f.write('foo\n')
190
186
        t_local.add('a')
191
187
        t_local.commit('foo')
192
188
 
203
199
 
204
200
        os.mkdir('sftp')
205
201
        sftp_url = self.get_url('sftp')
206
 
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
202
        b_sftp = ControlDir.create_branch_and_repo(sftp_url)
207
203
 
208
204
        b_sftp.pull(b_local)
209
205
        del b_sftp
216
212
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
217
213
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
218
214
 
219
 
        open('local/a', 'wb').write('foo2\n')
 
215
        with open('local/a', 'wb') as f: f.write('foo2\n')
220
216
        t_local.commit('foo2')
221
217
        b_sftp.pull(b_local)
222
218
        # The mode should be maintained after commit
223
219
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
224
220
 
225
 
        open('local/b', 'wb').write('new b\n')
 
221
        with open('local/b', 'wb') as f: f.write('new b\n')
226
222
        t_local.add('b')
227
223
        t_local.commit('new b')
228
224
        b_sftp.pull(b_local)
239
235
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
240
236
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
241
237
 
242
 
        open('local/a', 'wb').write('foo3\n')
 
238
        with open('local/a', 'wb') as f: f.write('foo3\n')
243
239
        t_local.commit('foo3')
244
240
        b_sftp.pull(b_local)
245
241
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
246
242
 
247
 
        open('local/c', 'wb').write('new c\n')
 
243
        with open('local/c', 'wb') as f: f.write('new c\n')
248
244
        t_local.add('c')
249
245
        t_local.commit('new c')
250
246
        b_sftp.pull(b_local)
258
254
        original_umask = os.umask(umask)
259
255
 
260
256
        try:
261
 
            t = get_transport(self.get_url())
 
257
            t = self.get_transport()
262
258
            # Direct access should be masked by umask
263
259
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
264
260
            self.assertTransportMode(t, 'a', 0666 &~umask)