~bzr-pqm/bzr/bzr.dev

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
# Copyright (C) 2005-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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

from bzrlib import urlutils
from bzrlib.branch import Branch
from bzrlib.bzrdir import BzrDir
from bzrlib.tests import TestCaseWithTransport, TestSkipped
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
from bzrlib.workingtree import WorkingTree


def chmod_r(base, file_mode, dir_mode):
    """Recursively chmod from a base directory"""
    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
    """
    t = test.get_transport()
    if include_base:
        test.assertTransportMode(t, base, dir_mode)
    for root, dirs, files in os.walk(base):
        for d in dirs:
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
            test.assertTransportMode(t, p, dir_mode)
        for f in files:
            p = os.path.join(root, f)
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
            test.assertTransportMode(t, p, file_mode)


class TestPermissions(TestCaseWithTransport):

    def test_new_files(self):
        if sys.platform == 'win32':
            raise TestSkipped('chmod has no effect on win32')

        t = self.make_branch_and_tree('.')
        b = t.branch
        with open('a', 'wb') as f: f.write('foo\n')
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
        t.add('a', 'CAPS-ID')
        t.commit('foo')

        chmod_r('.bzr', 0644, 0755)
        check_mode_r(self, '.bzr', 0644, 0755)

        # although we are modifying the filesystem
        # underneath the objects, they are not locked, and thus it must
        # be safe for most operations. But here we want to observe a
        # mode change in the control bits, which current do not refresh
        # when a new lock is taken out.
        t = WorkingTree.open('.')
        b = t.branch
        self.assertEqualMode(0755, b.control_files._dir_mode)
        self.assertEqualMode(0644, b.control_files._file_mode)
        self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())

        # Modifying a file shouldn't break the permissions
        with open('a', 'wb') as f: f.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
        with open('b', 'wb') as f: f.write('new b\n')
        t.add('b')
        t.commit('new b')
        check_mode_r(self, '.bzr', 0644, 0755)

        # Recursively update the modes of all files
        chmod_r('.bzr', 0664, 0775)
        check_mode_r(self, '.bzr', 0664, 0775)
        t = WorkingTree.open('.')
        b = t.branch
        self.assertEqualMode(0775, b.control_files._dir_mode)
        self.assertEqualMode(0664, b.control_files._file_mode)
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())

        with open('a', 'wb') as f: f.write('foo3\n')
        t.commit('foo3')
        check_mode_r(self, '.bzr', 0664, 0775)

        with open('c', 'wb') as f: f.write('new c\n')
        t.add('c')
        t.commit('new c')
        check_mode_r(self, '.bzr', 0664, 0775)

    def test_new_files_group_sticky_bit(self):
        if sys.platform == 'win32':
            raise TestSkipped('chmod has no effect on win32')
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
            # which users are not likely to be in, and this prevents us from
            # setting the sgid bit
            os.chown(self.test_dir, os.getuid(), os.getgid())

        t = self.make_branch_and_tree('.')
        b = t.branch

        # Test the group sticky bit
        # Recursively update the modes of all files
        chmod_r('.bzr', 0664, 02775)
        check_mode_r(self, '.bzr', 0664, 02775)
        t = WorkingTree.open('.')
        b = t.branch
        self.assertEqualMode(02775, b.control_files._dir_mode)
        self.assertEqualMode(0664, b.control_files._file_mode)
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())

        with open('a', 'wb') as f: f.write('foo4\n')
        t.commit('foo4')
        check_mode_r(self, '.bzr', 0664, 02775)

        with open('d', 'wb') as f: f.write('new d\n')
        t.add('d')
        t.commit('new d')
        check_mode_r(self, '.bzr', 0664, 02775)


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

        # bodge around for stubsftpserver not letting use connect
        # more than once
        _t = self.get_transport()

        os.mkdir('local')
        t_local = self.make_branch_and_tree('local')
        b_local = t_local.branch
        with open('local/a', 'wb') as f: f.write('foo\n')
        t_local.add('a')
        t_local.commit('foo')

        # Delete them because we are modifying the filesystem underneath them
        chmod_r('local/.bzr', 0644, 0755)
        check_mode_r(self, 'local/.bzr', 0644, 0755)

        t = WorkingTree.open('local')
        b_local = t.branch
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
        self.assertEqualMode(0644, b_local.control_files._file_mode)
        self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
        self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())

        os.mkdir('sftp')
        sftp_url = self.get_url('sftp')
        b_sftp = BzrDir.create_branch_and_repo(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)
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())

        with open('local/a', 'wb') as f: f.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)

        with open('local/b', 'wb') as f: f.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)
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())

        with open('local/a', 'wb') as f: f.write('foo3\n')
        t_local.commit('foo3')
        b_sftp.pull(b_local)
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)

        with open('local/c', 'wb') as f: f.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:
            t = self.get_transport()
            # Direct access should be masked by umask
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
            self.assertTransportMode(t, 'a', 0666 &~umask)

            # but Transport overrides umask
            t.put_bytes('b', 'txt', mode=0666)
            self.assertTransportMode(t, 'b', 0666)

            t._get_sftp().mkdir('c', mode=0777)
            self.assertTransportMode(t, 'c', 0777 &~umask)

            t.mkdir('d', mode=0777)
            self.assertTransportMode(t, 'd', 0777)
        finally:
            os.umask(original_umask)