/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_permissions.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
"""Tests for bzr setting permissions.
 
20
 
 
21
Files which are created underneath .bzr/ should inherit its permissions.
 
22
So if the directory is group writable, the files and subdirs should be as well.
 
23
 
 
24
In the future, when we have Repository/Branch/Checkout information, the
 
25
permissions should be inherited individually, rather than all be the same.
 
26
"""
 
27
 
 
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
 
30
#                    defined by the local umask. This isn't terrible, is it
 
31
#                    the truly desired behavior?
 
32
 
 
33
import os
 
34
import sys
 
35
 
 
36
from breezy import urlutils
 
37
from breezy.branch import Branch
 
38
from breezy.controldir import ControlDir
 
39
from breezy.tests import TestCaseWithTransport, TestSkipped
 
40
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
 
41
from breezy.workingtree import WorkingTree
 
42
 
 
43
 
 
44
def chmod_r(base, file_mode, dir_mode):
 
45
    """Recursively chmod from a base directory"""
 
46
    os.chmod(base, dir_mode)
 
47
    for root, dirs, files in os.walk(base):
 
48
        for d in dirs:
 
49
            p = os.path.join(root, d)
 
50
            os.chmod(p, dir_mode)
 
51
        for f in files:
 
52
            p = os.path.join(root, f)
 
53
            os.chmod(p, file_mode)
 
54
 
 
55
 
 
56
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
 
57
    """Check that all permissions match
 
58
 
 
59
    :param test: The TestCase being run
 
60
    :param base: The path to the root directory to check
 
61
    :param file_mode: The mode for all files
 
62
    :param dir_mode: The mode for all directories
 
63
    :param include_base: If false, only check the subdirectories
 
64
    """
 
65
    t = test.get_transport()
 
66
    if include_base:
 
67
        test.assertTransportMode(t, base, dir_mode)
 
68
    for root, dirs, files in os.walk(base):
 
69
        for d in dirs:
 
70
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
 
71
            test.assertTransportMode(t, p, dir_mode)
 
72
        for f in files:
 
73
            p = os.path.join(root, f)
 
74
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
 
75
            test.assertTransportMode(t, p, file_mode)
 
76
 
 
77
 
 
78
class TestPermissions(TestCaseWithTransport):
 
79
 
 
80
    def test_new_files(self):
 
81
        if sys.platform == 'win32':
 
82
            raise TestSkipped('chmod has no effect on win32')
 
83
 
 
84
        t = self.make_branch_and_tree('.')
 
85
        b = t.branch
 
86
        with open('a', 'wb') as f:
 
87
            f.write(b'foo\n')
 
88
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
 
89
        t.add('a', b'CAPS-ID')
 
90
        t.commit('foo')
 
91
 
 
92
        chmod_r('.bzr', 0o644, 0o755)
 
93
        check_mode_r(self, '.bzr', 0o644, 0o755)
 
94
 
 
95
        # although we are modifying the filesystem
 
96
        # underneath the objects, they are not locked, and thus it must
 
97
        # be safe for most operations. But here we want to observe a
 
98
        # mode change in the control bits, which current do not refresh
 
99
        # when a new lock is taken out.
 
100
        t = WorkingTree.open('.')
 
101
        b = t.branch
 
102
        self.assertEqualMode(0o755, b.control_files._dir_mode)
 
103
        self.assertEqualMode(0o644, b.control_files._file_mode)
 
104
        self.assertEqualMode(0o755, b.controldir._get_dir_mode())
 
105
        self.assertEqualMode(0o644, b.controldir._get_file_mode())
 
106
 
 
107
        # Modifying a file shouldn't break the permissions
 
108
        with open('a', 'wb') as f:
 
109
            f.write(b'foo2\n')
 
110
        t.commit('foo2')
 
111
        # The mode should be maintained after commit
 
112
        check_mode_r(self, '.bzr', 0o644, 0o755)
 
113
 
 
114
        # Adding a new file should maintain the permissions
 
115
        with open('b', 'wb') as f:
 
116
            f.write(b'new b\n')
 
117
        t.add('b')
 
118
        t.commit('new b')
 
119
        check_mode_r(self, '.bzr', 0o644, 0o755)
 
120
 
 
121
        # Recursively update the modes of all files
 
122
        chmod_r('.bzr', 0o664, 0o775)
 
123
        check_mode_r(self, '.bzr', 0o664, 0o775)
 
124
        t = WorkingTree.open('.')
 
125
        b = t.branch
 
126
        self.assertEqualMode(0o775, b.control_files._dir_mode)
 
127
        self.assertEqualMode(0o664, b.control_files._file_mode)
 
128
        self.assertEqualMode(0o775, b.controldir._get_dir_mode())
 
129
        self.assertEqualMode(0o664, b.controldir._get_file_mode())
 
130
 
 
131
        with open('a', 'wb') as f:
 
132
            f.write(b'foo3\n')
 
133
        t.commit('foo3')
 
134
        check_mode_r(self, '.bzr', 0o664, 0o775)
 
135
 
 
136
        with open('c', 'wb') as f:
 
137
            f.write(b'new c\n')
 
138
        t.add('c')
 
139
        t.commit('new c')
 
140
        check_mode_r(self, '.bzr', 0o664, 0o775)
 
141
 
 
142
    def test_new_files_group_sticky_bit(self):
 
143
        if sys.platform == 'win32':
 
144
            raise TestSkipped('chmod has no effect on win32')
 
145
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
 
146
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
 
147
            # which users are not likely to be in, and this prevents us from
 
148
            # setting the sgid bit
 
149
            os.chown(self.test_dir, os.getuid(), os.getgid())
 
150
 
 
151
        t = self.make_branch_and_tree('.')
 
152
        b = t.branch
 
153
 
 
154
        # Test the group sticky bit
 
155
        # Recursively update the modes of all files
 
156
        chmod_r('.bzr', 0o664, 0o2775)
 
157
        check_mode_r(self, '.bzr', 0o664, 0o2775)
 
158
        t = WorkingTree.open('.')
 
159
        b = t.branch
 
160
        self.assertEqualMode(0o2775, b.control_files._dir_mode)
 
161
        self.assertEqualMode(0o664, b.control_files._file_mode)
 
162
        self.assertEqualMode(0o2775, b.controldir._get_dir_mode())
 
163
        self.assertEqualMode(0o664, b.controldir._get_file_mode())
 
164
 
 
165
        with open('a', 'wb') as f:
 
166
            f.write(b'foo4\n')
 
167
        t.commit('foo4')
 
168
        check_mode_r(self, '.bzr', 0o664, 0o2775)
 
169
 
 
170
        with open('d', 'wb') as f:
 
171
            f.write(b'new d\n')
 
172
        t.add('d')
 
173
        t.commit('new d')
 
174
        check_mode_r(self, '.bzr', 0o664, 0o2775)
 
175
 
 
176
 
 
177
class TestSftpPermissions(TestCaseWithSFTPServer):
 
178
 
 
179
    def test_new_files(self):
 
180
        if sys.platform == 'win32':
 
181
            raise TestSkipped('chmod has no effect on win32')
 
182
        # Though it would be nice to test that SFTP to a server
 
183
        # which does support chmod has the right effect
 
184
 
 
185
        # bodge around for stubsftpserver not letting use connect
 
186
        # more than once
 
187
        _t = self.get_transport()
 
188
 
 
189
        os.mkdir('local')
 
190
        t_local = self.make_branch_and_tree('local')
 
191
        b_local = t_local.branch
 
192
        with open('local/a', 'wb') as f:
 
193
            f.write(b'foo\n')
 
194
        t_local.add('a')
 
195
        t_local.commit('foo')
 
196
 
 
197
        # Delete them because we are modifying the filesystem underneath them
 
198
        chmod_r('local/.bzr', 0o644, 0o755)
 
199
        check_mode_r(self, 'local/.bzr', 0o644, 0o755)
 
200
 
 
201
        t = WorkingTree.open('local')
 
202
        b_local = t.branch
 
203
        self.assertEqualMode(0o755, b_local.control_files._dir_mode)
 
204
        self.assertEqualMode(0o644, b_local.control_files._file_mode)
 
205
        self.assertEqualMode(0o755, b_local.controldir._get_dir_mode())
 
206
        self.assertEqualMode(0o644, b_local.controldir._get_file_mode())
 
207
 
 
208
        os.mkdir('sftp')
 
209
        sftp_url = self.get_url('sftp')
 
210
        b_sftp = ControlDir.create_branch_and_repo(sftp_url)
 
211
 
 
212
        b_sftp.pull(b_local)
 
213
        del b_sftp
 
214
        chmod_r('sftp/.bzr', 0o644, 0o755)
 
215
        check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
 
216
 
 
217
        b_sftp = Branch.open(sftp_url)
 
218
        self.assertEqualMode(0o755, b_sftp.control_files._dir_mode)
 
219
        self.assertEqualMode(0o644, b_sftp.control_files._file_mode)
 
220
        self.assertEqualMode(0o755, b_sftp.controldir._get_dir_mode())
 
221
        self.assertEqualMode(0o644, b_sftp.controldir._get_file_mode())
 
222
 
 
223
        with open('local/a', 'wb') as f:
 
224
            f.write(b'foo2\n')
 
225
        t_local.commit('foo2')
 
226
        b_sftp.pull(b_local)
 
227
        # The mode should be maintained after commit
 
228
        check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
 
229
 
 
230
        with open('local/b', 'wb') as f:
 
231
            f.write(b'new b\n')
 
232
        t_local.add('b')
 
233
        t_local.commit('new b')
 
234
        b_sftp.pull(b_local)
 
235
        check_mode_r(self, 'sftp/.bzr', 0o644, 0o755)
 
236
 
 
237
        del b_sftp
 
238
        # Recursively update the modes of all files
 
239
        chmod_r('sftp/.bzr', 0o664, 0o775)
 
240
        check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
 
241
 
 
242
        b_sftp = Branch.open(sftp_url)
 
243
        self.assertEqualMode(0o775, b_sftp.control_files._dir_mode)
 
244
        self.assertEqualMode(0o664, b_sftp.control_files._file_mode)
 
245
        self.assertEqualMode(0o775, b_sftp.controldir._get_dir_mode())
 
246
        self.assertEqualMode(0o664, b_sftp.controldir._get_file_mode())
 
247
 
 
248
        with open('local/a', 'wb') as f:
 
249
            f.write(b'foo3\n')
 
250
        t_local.commit('foo3')
 
251
        b_sftp.pull(b_local)
 
252
        check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
 
253
 
 
254
        with open('local/c', 'wb') as f:
 
255
            f.write(b'new c\n')
 
256
        t_local.add('c')
 
257
        t_local.commit('new c')
 
258
        b_sftp.pull(b_local)
 
259
        check_mode_r(self, 'sftp/.bzr', 0o664, 0o775)
 
260
 
 
261
    def test_sftp_server_modes(self):
 
262
        if sys.platform == 'win32':
 
263
            raise TestSkipped('chmod has no effect on win32')
 
264
 
 
265
        umask = 0o022
 
266
        original_umask = os.umask(umask)
 
267
 
 
268
        try:
 
269
            t = self.get_transport()
 
270
            # Direct access should be masked by umask
 
271
            with t._sftp_open_exclusive('a', mode=0o666) as f:
 
272
                f.write(b'foo\n')
 
273
            self.assertTransportMode(t, 'a', 0o666 & ~umask)
 
274
 
 
275
            # but Transport overrides umask
 
276
            t.put_bytes('b', b'txt', mode=0o666)
 
277
            self.assertTransportMode(t, 'b', 0o666)
 
278
 
 
279
            t._get_sftp().mkdir('c', mode=0o777)
 
280
            self.assertTransportMode(t, 'c', 0o777 & ~umask)
 
281
 
 
282
            t.mkdir('d', mode=0o777)
 
283
            self.assertTransportMode(t, 'd', 0o777)
 
284
        finally:
 
285
            os.umask(original_umask)