~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
2
# -*- coding: utf-8 -*-
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
3
#
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
8
#
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
13
#
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
17
18
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
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.
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
26
"""
27
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
28
# TODO: jam 20051215 There are no tests for ftp yet, because we have no ftp server
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
29
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
30
#                    defined by the local umask. This isn't terrible, is it
31
#                    the truly desired behavior?
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
32
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
33
import os
34
import sys
35
import stat
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
36
from cStringIO import StringIO
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
37
import urllib
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
38
39
from bzrlib.branch import Branch
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
40
from bzrlib.bzrdir import BzrDir
1534.4.28 by Robert Collins
first cut at merge from integration.
41
from bzrlib.tests import TestCaseWithTransport, TestSkipped
1185.50.20 by John Arbash Meinel
merge permissions branch, also fixup tests so they are lined up with bzr.dev to help prevent conflicts.
42
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
1530.1.17 by Robert Collins
Move check_mode to TestCase.assertMode to make it generally accessible.
43
from bzrlib.transport import get_transport
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
44
from bzrlib.workingtree import WorkingTree
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
45
46
47
def chmod_r(base, file_mode, dir_mode):
48
    """Recursively chmod from a base directory"""
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
49
    os.chmod(base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
50
    for root, dirs, files in os.walk(base):
51
        for d in dirs:
52
            p = os.path.join(root, d)
53
            os.chmod(p, dir_mode)
54
        for f in files:
55
            p = os.path.join(root, f)
56
            os.chmod(p, file_mode)
57
58
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
59
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
60
    """Check that all permissions match
61
62
    :param test: The TestCase being run
63
    :param base: The path to the root directory to check
64
    :param file_mode: The mode for all files
65
    :param dir_mode: The mode for all directories
66
    :param include_base: If false, only check the subdirectories
67
    """
1530.1.17 by Robert Collins
Move check_mode to TestCase.assertMode to make it generally accessible.
68
    t = get_transport(".")
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
69
    if include_base:
1530.1.21 by Robert Collins
Review feedback fixes.
70
        test.assertTransportMode(t, base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
71
    for root, dirs, files in os.walk(base):
72
        for d in dirs:
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
73
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
1530.1.21 by Robert Collins
Review feedback fixes.
74
            test.assertTransportMode(t, p, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
75
        for f in files:
76
            p = os.path.join(root, f)
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
77
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
1530.1.21 by Robert Collins
Review feedback fixes.
78
            test.assertTransportMode(t, p, file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
79
1532 by Robert Collins
Merge in John Meinels integration branch.
80
1534.4.28 by Robert Collins
first cut at merge from integration.
81
class TestPermissions(TestCaseWithTransport):
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
82
83
    def test_new_files(self):
84
        if sys.platform == 'win32':
85
            raise TestSkipped('chmod has no effect on win32')
86
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
87
        t = self.make_branch_and_tree('.')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
88
        b = t.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
89
        open('a', 'wb').write('foo\n')
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
90
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
91
        t.add('a', 'CAPS-ID')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
92
        t.commit('foo')
93
94
        chmod_r('.bzr', 0644, 0755)
95
        check_mode_r(self, '.bzr', 0644, 0755)
96
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
97
        # although we are modifying the filesystem
98
        # underneath the objects, they are not locked, and thus it must
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
99
        # be safe for most operations. But here we want to observe a
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
100
        # mode change in the control bits, which current do not refresh
101
        # when a new lock is taken out.
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
102
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
103
        b = t.branch
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
104
        self.assertEqualMode(0755, b.control_files._dir_mode)
105
        self.assertEqualMode(0644, b.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
106
        self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
107
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
108
109
        # Modifying a file shouldn't break the permissions
110
        open('a', 'wb').write('foo2\n')
111
        t.commit('foo2')
112
        # The mode should be maintained after commit
113
        check_mode_r(self, '.bzr', 0644, 0755)
114
115
        # Adding a new file should maintain the permissions
116
        open('b', 'wb').write('new b\n')
117
        t.add('b')
118
        t.commit('new b')
119
        check_mode_r(self, '.bzr', 0644, 0755)
120
121
        # Recursively update the modes of all files
122
        chmod_r('.bzr', 0664, 0775)
123
        check_mode_r(self, '.bzr', 0664, 0775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
124
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
125
        b = t.branch
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
126
        self.assertEqualMode(0775, b.control_files._dir_mode)
127
        self.assertEqualMode(0664, b.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
128
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
129
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
130
131
        open('a', 'wb').write('foo3\n')
132
        t.commit('foo3')
133
        check_mode_r(self, '.bzr', 0664, 0775)
134
135
        open('c', 'wb').write('new c\n')
136
        t.add('c')
137
        t.commit('new c')
138
        check_mode_r(self, '.bzr', 0664, 0775)
139
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
140
    def test_new_files_group_sticky_bit(self):
141
        if sys.platform == 'win32':
142
            raise TestSkipped('chmod has no effect on win32')
4676.2.2 by Vincent Ladeuil
FreeBSD and OSX create temp dirs with the 'wheel' group.
143
        elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
144
            # OS X (and FreeBSD) create temp dirs with the 'wheel' group, which
145
            # users are not likely to be in, and this prevents us from setting
146
            # the sgid bit
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
147
            os.chown(self.test_dir, os.getuid(), os.getgid())
148
149
        t = self.make_branch_and_tree('.')
150
        b = t.branch
151
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
152
        # Test the group sticky bit
153
        # Recursively update the modes of all files
154
        chmod_r('.bzr', 0664, 02775)
155
        check_mode_r(self, '.bzr', 0664, 02775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
156
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
157
        b = t.branch
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
158
        self.assertEqualMode(02775, b.control_files._dir_mode)
159
        self.assertEqualMode(0664, b.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
160
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
161
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
162
163
        open('a', 'wb').write('foo4\n')
164
        t.commit('foo4')
165
        check_mode_r(self, '.bzr', 0664, 02775)
166
167
        open('d', 'wb').write('new d\n')
168
        t.add('d')
169
        t.commit('new d')
170
        check_mode_r(self, '.bzr', 0664, 02775)
171
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
172
173
class TestSftpPermissions(TestCaseWithSFTPServer):
174
175
    def test_new_files(self):
176
        if sys.platform == 'win32':
177
            raise TestSkipped('chmod has no effect on win32')
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
178
        # Though it would be nice to test that SFTP to a server
179
        # which does support chmod has the right effect
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
180
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
181
        # bodge around for stubsftpserver not letting use connect
182
        # more than once
183
        _t = get_transport(self.get_url())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
184
185
        os.mkdir('local')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
186
        t_local = self.make_branch_and_tree('local')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
187
        b_local = t_local.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
188
        open('local/a', 'wb').write('foo\n')
189
        t_local.add('a')
190
        t_local.commit('foo')
191
192
        # Delete them because we are modifying the filesystem underneath them
193
        chmod_r('local/.bzr', 0644, 0755)
194
        check_mode_r(self, 'local/.bzr', 0644, 0755)
195
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
196
        t = WorkingTree.open('local')
1185.50.76 by John Arbash Meinel
[merge] robertc's integration branch: add BzrDir, and checkouts
197
        b_local = t.branch
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
198
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
199
        self.assertEqualMode(0644, b_local.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
200
        self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
201
        self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
202
203
        os.mkdir('sftp')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
204
        sftp_url = self.get_url('sftp')
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
205
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
206
207
        b_sftp.pull(b_local)
208
        del b_sftp
209
        chmod_r('sftp/.bzr', 0644, 0755)
210
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
211
212
        b_sftp = Branch.open(sftp_url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
213
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
214
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
215
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
216
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
217
218
        open('local/a', 'wb').write('foo2\n')
219
        t_local.commit('foo2')
220
        b_sftp.pull(b_local)
221
        # The mode should be maintained after commit
222
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
223
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
224
        open('local/b', 'wb').write('new b\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
225
        t_local.add('b')
226
        t_local.commit('new b')
227
        b_sftp.pull(b_local)
228
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
229
230
        del b_sftp
231
        # Recursively update the modes of all files
232
        chmod_r('sftp/.bzr', 0664, 0775)
233
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
234
235
        b_sftp = Branch.open(sftp_url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
236
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
237
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
238
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
239
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
240
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
241
        open('local/a', 'wb').write('foo3\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
242
        t_local.commit('foo3')
243
        b_sftp.pull(b_local)
244
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
245
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
246
        open('local/c', 'wb').write('new c\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
247
        t_local.add('c')
248
        t_local.commit('new c')
249
        b_sftp.pull(b_local)
250
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
251
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
252
    def test_sftp_server_modes(self):
253
        if sys.platform == 'win32':
254
            raise TestSkipped('chmod has no effect on win32')
255
256
        umask = 0022
257
        original_umask = os.umask(umask)
258
259
        try:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
260
            t = get_transport(self.get_url())
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
261
            # Direct access should be masked by umask
262
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
1530.1.21 by Robert Collins
Review feedback fixes.
263
            self.assertTransportMode(t, 'a', 0666 &~umask)
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
264
265
            # but Transport overrides umask
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
266
            t.put_bytes('b', 'txt', mode=0666)
1530.1.21 by Robert Collins
Review feedback fixes.
267
            self.assertTransportMode(t, 'b', 0666)
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
268
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
269
            t._get_sftp().mkdir('c', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
270
            self.assertTransportMode(t, 'c', 0777 &~umask)
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
271
272
            t.mkdir('d', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
273
            self.assertTransportMode(t, 'd', 0777)
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
274
        finally:
275
            os.umask(original_umask)