~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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
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
 
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
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
41
from bzrlib.lockable_files import LockableFiles, TransportLock
1534.4.28 by Robert Collins
first cut at merge from integration.
42
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.
43
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.
44
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.
45
from bzrlib.workingtree import WorkingTree
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
46
47
48
def chmod_r(base, file_mode, dir_mode):
49
    """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.
50
    assert os.path.isdir(base)
51
    os.chmod(base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
52
    for root, dirs, files in os.walk(base):
53
        for d in dirs:
54
            p = os.path.join(root, d)
55
            os.chmod(p, dir_mode)
56
        for f in files:
57
            p = os.path.join(root, f)
58
            os.chmod(p, file_mode)
59
60
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
61
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
62
    """Check that all permissions match
63
64
    :param test: The TestCase being run
65
    :param base: The path to the root directory to check
66
    :param file_mode: The mode for all files
67
    :param dir_mode: The mode for all directories
68
    :param include_base: If false, only check the subdirectories
69
    """
70
    assert os.path.isdir(base)
1530.1.17 by Robert Collins
Move check_mode to TestCase.assertMode to make it generally accessible.
71
    t = get_transport(".")
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
72
    if include_base:
1530.1.21 by Robert Collins
Review feedback fixes.
73
        test.assertTransportMode(t, base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
74
    for root, dirs, files in os.walk(base):
75
        for d in dirs:
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
76
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
1530.1.21 by Robert Collins
Review feedback fixes.
77
            test.assertTransportMode(t, p, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
78
        for f in files:
79
            p = os.path.join(root, f)
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
80
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
1530.1.21 by Robert Collins
Review feedback fixes.
81
            test.assertTransportMode(t, p, file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
82
1532 by Robert Collins
Merge in John Meinels integration branch.
83
1534.4.28 by Robert Collins
first cut at merge from integration.
84
class TestPermissions(TestCaseWithTransport):
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
85
86
    def test_new_files(self):
87
        if sys.platform == 'win32':
88
            raise TestSkipped('chmod has no effect on win32')
89
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.
90
        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.
91
        b = t.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
92
        open('a', 'wb').write('foo\n')
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
93
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
94
        t.add('a', 'CAPS-ID')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
95
        t.commit('foo')
96
97
        chmod_r('.bzr', 0644, 0755)
98
        check_mode_r(self, '.bzr', 0644, 0755)
99
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
100
        # although we are modifying the filesystem
101
        # underneath the objects, they are not locked, and thus it must
102
        # be safe for most operations. But here we want to observe a 
103
        # mode change in the control bits, which current do not refresh
104
        # when a new lock is taken out.
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
105
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
106
        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.
107
        self.assertEqualMode(0755, b.control_files._dir_mode)
108
        self.assertEqualMode(0644, b.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
109
110
        # Modifying a file shouldn't break the permissions
111
        open('a', 'wb').write('foo2\n')
112
        t.commit('foo2')
113
        # The mode should be maintained after commit
114
        check_mode_r(self, '.bzr', 0644, 0755)
115
116
        # Adding a new file should maintain the permissions
117
        open('b', 'wb').write('new b\n')
118
        t.add('b')
119
        t.commit('new b')
120
        check_mode_r(self, '.bzr', 0644, 0755)
121
122
        # Recursively update the modes of all files
123
        chmod_r('.bzr', 0664, 0775)
124
        check_mode_r(self, '.bzr', 0664, 0775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
125
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
126
        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.
127
        self.assertEqualMode(0775, b.control_files._dir_mode)
128
        self.assertEqualMode(0664, b.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
129
130
        open('a', 'wb').write('foo3\n')
131
        t.commit('foo3')
132
        check_mode_r(self, '.bzr', 0664, 0775)
133
134
        open('c', 'wb').write('new c\n')
135
        t.add('c')
136
        t.commit('new c')
137
        check_mode_r(self, '.bzr', 0664, 0775)
138
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
139
        # Test the group sticky bit
140
        # Recursively update the modes of all files
141
        chmod_r('.bzr', 0664, 02775)
142
        check_mode_r(self, '.bzr', 0664, 02775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
143
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
144
        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.
145
        self.assertEqualMode(02775, b.control_files._dir_mode)
146
        self.assertEqualMode(0664, b.control_files._file_mode)
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
147
148
        open('a', 'wb').write('foo4\n')
149
        t.commit('foo4')
150
        check_mode_r(self, '.bzr', 0664, 02775)
151
152
        open('d', 'wb').write('new d\n')
153
        t.add('d')
154
        t.commit('new d')
155
        check_mode_r(self, '.bzr', 0664, 02775)
156
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
157
    def test_disable_set_mode(self):
158
        # TODO: jam 20051215 Ultimately, this test should probably test that
159
        #                    extra chmod calls aren't being made
160
        try:
1534.4.28 by Robert Collins
first cut at merge from integration.
161
            transport = get_transport(self.get_url())
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
162
            transport.put_bytes('my-lock', '')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
163
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
164
            self.assertNotEqual(None, lockable._dir_mode)
165
            self.assertNotEqual(None, lockable._file_mode)
166
167
            LockableFiles._set_dir_mode = False
168
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
169
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
170
            self.assertEqual(None, lockable._dir_mode)
171
            self.assertNotEqual(None, lockable._file_mode)
172
173
            LockableFiles._set_file_mode = False
174
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
175
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
176
            self.assertEqual(None, lockable._dir_mode)
177
            self.assertEqual(None, lockable._file_mode)
178
179
            LockableFiles._set_dir_mode = True
180
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
181
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
182
            self.assertNotEqual(None, lockable._dir_mode)
183
            self.assertEqual(None, lockable._file_mode)
184
185
            LockableFiles._set_file_mode = True
186
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
187
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
188
            self.assertNotEqual(None, lockable._dir_mode)
189
            self.assertNotEqual(None, lockable._file_mode)
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
190
        finally:
1185.65.23 by Robert Collins
update tests somewhat
191
            LockableFiles._set_dir_mode = True
192
            LockableFiles._set_file_mode = True
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
193
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
194
195
class TestSftpPermissions(TestCaseWithSFTPServer):
196
197
    def test_new_files(self):
198
        if sys.platform == 'win32':
199
            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.
200
        # Though it would be nice to test that SFTP to a server
201
        # which does support chmod has the right effect
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
202
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.
203
        # bodge around for stubsftpserver not letting use connect
204
        # more than once
205
        _t = get_transport(self.get_url())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
206
207
        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.
208
        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.
209
        b_local = t_local.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
210
        open('local/a', 'wb').write('foo\n')
211
        t_local.add('a')
212
        t_local.commit('foo')
213
214
        # Delete them because we are modifying the filesystem underneath them
215
        chmod_r('local/.bzr', 0644, 0755)
216
        check_mode_r(self, 'local/.bzr', 0644, 0755)
217
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
218
        t = WorkingTree.open('local')
1185.50.76 by John Arbash Meinel
[merge] robertc's integration branch: add BzrDir, and checkouts
219
        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.
220
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
221
        self.assertEqualMode(0644, b_local.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
222
223
        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.
224
        sftp_url = self.get_url('sftp')
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
225
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
226
227
        b_sftp.pull(b_local)
228
        del b_sftp
229
        chmod_r('sftp/.bzr', 0644, 0755)
230
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
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(0755, b_sftp.control_files._dir_mode)
234
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
235
236
        open('local/a', 'wb').write('foo2\n')
237
        t_local.commit('foo2')
238
        b_sftp.pull(b_local)
239
        # The mode should be maintained after commit
240
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
241
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
242
        open('local/b', 'wb').write('new b\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
243
        t_local.add('b')
244
        t_local.commit('new b')
245
        b_sftp.pull(b_local)
246
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
247
248
        del b_sftp
249
        # Recursively update the modes of all files
250
        chmod_r('sftp/.bzr', 0664, 0775)
251
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
252
253
        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.
254
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
255
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
256
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
257
        open('local/a', 'wb').write('foo3\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
258
        t_local.commit('foo3')
259
        b_sftp.pull(b_local)
260
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
261
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
262
        open('local/c', 'wb').write('new c\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
263
        t_local.add('c')
264
        t_local.commit('new c')
265
        b_sftp.pull(b_local)
266
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
267
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
268
    def test_sftp_server_modes(self):
269
        if sys.platform == 'win32':
270
            raise TestSkipped('chmod has no effect on win32')
271
272
        umask = 0022
273
        original_umask = os.umask(umask)
274
275
        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.
276
            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)
277
            # Direct access should be masked by umask
278
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
1530.1.21 by Robert Collins
Review feedback fixes.
279
            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)
280
281
            # but Transport overrides umask
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
282
            t.put_bytes('b', 'txt', mode=0666)
1530.1.21 by Robert Collins
Review feedback fixes.
283
            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)
284
285
            t._sftp.mkdir('c', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
286
            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)
287
288
            t.mkdir('d', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
289
            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)
290
        finally:
291
            os.umask(original_umask)