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