~bzr-pqm/bzr/bzr.dev

1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
3
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.
8
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.
13
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
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
36
from StringIO import StringIO
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
37
38
from bzrlib.branch import Branch
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
39
from bzrlib.bzrdir import BzrDir
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
40
from bzrlib.lockable_files import LockableFiles, TransportLock
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
    assert os.path.isdir(base)
50
    os.chmod(base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
51
    for root, dirs, files in os.walk(base):
52
        for d in dirs:
53
            p = os.path.join(root, d)
54
            os.chmod(p, dir_mode)
55
        for f in files:
56
            p = os.path.join(root, f)
57
            os.chmod(p, file_mode)
58
59
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
60
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
61
    """Check that all permissions match
62
63
    :param test: The TestCase being run
64
    :param base: The path to the root directory to check
65
    :param file_mode: The mode for all files
66
    :param dir_mode: The mode for all directories
67
    :param include_base: If false, only check the subdirectories
68
    """
69
    assert os.path.isdir(base)
1530.1.17 by Robert Collins
Move check_mode to TestCase.assertMode to make it generally accessible.
70
    t = get_transport(".")
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
71
    if include_base:
1530.1.21 by Robert Collins
Review feedback fixes.
72
        test.assertTransportMode(t, base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
73
    for root, dirs, files in os.walk(base):
74
        for d in dirs:
75
            p = os.path.join(root, d)
1530.1.21 by Robert Collins
Review feedback fixes.
76
            test.assertTransportMode(t, p, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
77
        for f in files:
78
            p = os.path.join(root, f)
1530.1.21 by Robert Collins
Review feedback fixes.
79
            test.assertTransportMode(t, p, file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
80
1532 by Robert Collins
Merge in John Meinels integration branch.
81
1534.4.28 by Robert Collins
first cut at merge from integration.
82
class TestPermissions(TestCaseWithTransport):
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
83
84
    def test_new_files(self):
85
        if sys.platform == 'win32':
86
            raise TestSkipped('chmod has no effect on win32')
87
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.
88
        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.
89
        b = t.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
90
        open('a', 'wb').write('foo\n')
91
        t.add('a')
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
99
        # be safe for most operations. But here we want to observe a 
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)
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)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
126
127
        open('a', 'wb').write('foo3\n')
128
        t.commit('foo3')
129
        check_mode_r(self, '.bzr', 0664, 0775)
130
131
        open('c', 'wb').write('new c\n')
132
        t.add('c')
133
        t.commit('new c')
134
        check_mode_r(self, '.bzr', 0664, 0775)
135
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
136
        # Test the group sticky bit
137
        # Recursively update the modes of all files
138
        chmod_r('.bzr', 0664, 02775)
139
        check_mode_r(self, '.bzr', 0664, 02775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
140
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
141
        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.
142
        self.assertEqualMode(02775, b.control_files._dir_mode)
143
        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.
144
145
        open('a', 'wb').write('foo4\n')
146
        t.commit('foo4')
147
        check_mode_r(self, '.bzr', 0664, 02775)
148
149
        open('d', 'wb').write('new d\n')
150
        t.add('d')
151
        t.commit('new d')
152
        check_mode_r(self, '.bzr', 0664, 02775)
153
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
154
    def test_disable_set_mode(self):
155
        # TODO: jam 20051215 Ultimately, this test should probably test that
156
        #                    extra chmod calls aren't being made
157
        try:
1534.4.28 by Robert Collins
first cut at merge from integration.
158
            transport = get_transport(self.get_url())
1185.65.23 by Robert Collins
update tests somewhat
159
            transport.put('my-lock', StringIO(''))
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
160
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
161
            self.assertNotEqual(None, lockable._dir_mode)
162
            self.assertNotEqual(None, lockable._file_mode)
163
164
            LockableFiles._set_dir_mode = False
165
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
166
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
167
            self.assertEqual(None, lockable._dir_mode)
168
            self.assertNotEqual(None, lockable._file_mode)
169
170
            LockableFiles._set_file_mode = False
171
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
172
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
173
            self.assertEqual(None, lockable._dir_mode)
174
            self.assertEqual(None, lockable._file_mode)
175
176
            LockableFiles._set_dir_mode = True
177
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
178
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
179
            self.assertNotEqual(None, lockable._dir_mode)
180
            self.assertEqual(None, lockable._file_mode)
181
182
            LockableFiles._set_file_mode = True
183
            transport = get_transport('.')
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
184
            lockable = LockableFiles(transport, 'my-lock', TransportLock)
1185.65.23 by Robert Collins
update tests somewhat
185
            self.assertNotEqual(None, lockable._dir_mode)
186
            self.assertNotEqual(None, lockable._file_mode)
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
187
        finally:
1185.65.23 by Robert Collins
update tests somewhat
188
            LockableFiles._set_dir_mode = True
189
            LockableFiles._set_file_mode = True
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
190
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
191
192
class TestSftpPermissions(TestCaseWithSFTPServer):
193
194
    def test_new_files(self):
195
        if sys.platform == 'win32':
196
            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.
197
        # Though it would be nice to test that SFTP to a server
198
        # which does support chmod has the right effect
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
199
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.
200
        # bodge around for stubsftpserver not letting use connect
201
        # more than once
202
        _t = get_transport(self.get_url())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
203
204
        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.
205
        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.
206
        b_local = t_local.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
207
        open('local/a', 'wb').write('foo\n')
208
        t_local.add('a')
209
        t_local.commit('foo')
210
211
        # Delete them because we are modifying the filesystem underneath them
212
        chmod_r('local/.bzr', 0644, 0755)
213
        check_mode_r(self, 'local/.bzr', 0644, 0755)
214
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
215
        t = WorkingTree.open('local')
1185.50.76 by John Arbash Meinel
[merge] robertc's integration branch: add BzrDir, and checkouts
216
        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.
217
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
218
        self.assertEqualMode(0644, b_local.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
219
220
        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.
221
        sftp_url = self.get_url('sftp')
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
222
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
223
224
        b_sftp.pull(b_local)
225
        del b_sftp
226
        chmod_r('sftp/.bzr', 0644, 0755)
227
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
228
229
        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.
230
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
231
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
232
233
        open('local/a', 'wb').write('foo2\n')
234
        t_local.commit('foo2')
235
        b_sftp.pull(b_local)
236
        # The mode should be maintained after commit
237
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
238
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
239
        open('local/b', 'wb').write('new b\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
240
        t_local.add('b')
241
        t_local.commit('new b')
242
        b_sftp.pull(b_local)
243
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
244
245
        del b_sftp
246
        # Recursively update the modes of all files
247
        chmod_r('sftp/.bzr', 0664, 0775)
248
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
249
250
        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.
251
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
252
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
253
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
254
        open('local/a', 'wb').write('foo3\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
255
        t_local.commit('foo3')
256
        b_sftp.pull(b_local)
257
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
258
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
259
        open('local/c', 'wb').write('new c\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
260
        t_local.add('c')
261
        t_local.commit('new c')
262
        b_sftp.pull(b_local)
263
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
264
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
265
    def test_sftp_server_modes(self):
266
        if sys.platform == 'win32':
267
            raise TestSkipped('chmod has no effect on win32')
268
269
        umask = 0022
270
        original_umask = os.umask(umask)
271
272
        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.
273
            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)
274
            # Direct access should be masked by umask
275
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
1530.1.21 by Robert Collins
Review feedback fixes.
276
            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)
277
278
            # but Transport overrides umask
279
            t.put('b', 'txt', mode=0666)
1530.1.21 by Robert Collins
Review feedback fixes.
280
            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)
281
282
            t._sftp.mkdir('c', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
283
            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)
284
285
            t.mkdir('d', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
286
            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)
287
        finally:
288
            os.umask(original_umask)