~bzr-pqm/bzr/bzr.dev

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