/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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?
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
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
    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
    """
1530.1.17 by Robert Collins
Move check_mode to TestCase.assertMode to make it generally accessible.
69
    t = get_transport(".")
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
70
    if include_base:
1530.1.21 by Robert Collins
Review feedback fixes.
71
        test.assertTransportMode(t, base, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
72
    for root, dirs, files in os.walk(base):
73
        for d in dirs:
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
74
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
1530.1.21 by Robert Collins
Review feedback fixes.
75
            test.assertTransportMode(t, p, dir_mode)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
76
        for f in files:
77
            p = os.path.join(root, f)
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
78
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [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')
1910.2.32 by Aaron Bentley
Handle capital-letter file-ids
91
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
92
        t.add('a', 'CAPS-ID')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
93
        t.commit('foo')
94
95
        chmod_r('.bzr', 0644, 0755)
96
        check_mode_r(self, '.bzr', 0644, 0755)
97
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
98
        # although we are modifying the filesystem
99
        # underneath the objects, they are not locked, and thus it must
100
        # be safe for most operations. But here we want to observe a 
101
        # mode change in the control bits, which current do not refresh
102
        # when a new lock is taken out.
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
103
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
104
        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.
105
        self.assertEqualMode(0755, b.control_files._dir_mode)
106
        self.assertEqualMode(0644, b.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
107
        self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
108
        self.assertEqualMode(0644, b.bzrdir._get_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)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
129
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
130
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
131
132
        open('a', 'wb').write('foo3\n')
133
        t.commit('foo3')
134
        check_mode_r(self, '.bzr', 0664, 0775)
135
136
        open('c', 'wb').write('new c\n')
137
        t.add('c')
138
        t.commit('new c')
139
        check_mode_r(self, '.bzr', 0664, 0775)
140
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
141
    def test_new_files_group_sticky_bit(self):
142
        if sys.platform == 'win32':
143
            raise TestSkipped('chmod has no effect on win32')
144
        elif sys.platform == 'darwin':
145
            # OS X creates temp dirs with the 'wheel' group, which users are
146
            # not likely to be in, and this prevents us from setting the sgid
147
            # bit
148
            os.chown(self.test_dir, os.getuid(), os.getgid())
149
150
        t = self.make_branch_and_tree('.')
151
        b = t.branch
152
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
153
        # Test the group sticky bit
154
        # Recursively update the modes of all files
155
        chmod_r('.bzr', 0664, 02775)
156
        check_mode_r(self, '.bzr', 0664, 02775)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
157
        t = WorkingTree.open('.')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
158
        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.
159
        self.assertEqualMode(02775, b.control_files._dir_mode)
160
        self.assertEqualMode(0664, b.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
161
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
162
        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.
163
164
        open('a', 'wb').write('foo4\n')
165
        t.commit('foo4')
166
        check_mode_r(self, '.bzr', 0664, 02775)
167
168
        open('d', 'wb').write('new d\n')
169
        t.add('d')
170
        t.commit('new d')
171
        check_mode_r(self, '.bzr', 0664, 02775)
172
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
173
174
class TestSftpPermissions(TestCaseWithSFTPServer):
175
176
    def test_new_files(self):
177
        if sys.platform == 'win32':
178
            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.
179
        # Though it would be nice to test that SFTP to a server
180
        # which does support chmod has the right effect
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
181
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.
182
        # bodge around for stubsftpserver not letting use connect
183
        # more than once
184
        _t = get_transport(self.get_url())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
185
186
        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.
187
        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.
188
        b_local = t_local.branch
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
189
        open('local/a', 'wb').write('foo\n')
190
        t_local.add('a')
191
        t_local.commit('foo')
192
193
        # Delete them because we are modifying the filesystem underneath them
194
        chmod_r('local/.bzr', 0644, 0755)
195
        check_mode_r(self, 'local/.bzr', 0644, 0755)
196
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
197
        t = WorkingTree.open('local')
1185.50.76 by John Arbash Meinel
[merge] robertc's integration branch: add BzrDir, and checkouts
198
        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.
199
        self.assertEqualMode(0755, b_local.control_files._dir_mode)
200
        self.assertEqualMode(0644, b_local.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
201
        self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
202
        self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
203
204
        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.
205
        sftp_url = self.get_url('sftp')
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
206
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
207
208
        b_sftp.pull(b_local)
209
        del b_sftp
210
        chmod_r('sftp/.bzr', 0644, 0755)
211
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
212
213
        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.
214
        self.assertEqualMode(0755, b_sftp.control_files._dir_mode)
215
        self.assertEqualMode(0644, b_sftp.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
216
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
217
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
218
219
        open('local/a', 'wb').write('foo2\n')
220
        t_local.commit('foo2')
221
        b_sftp.pull(b_local)
222
        # The mode should be maintained after commit
223
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
224
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
225
        open('local/b', 'wb').write('new b\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
226
        t_local.add('b')
227
        t_local.commit('new b')
228
        b_sftp.pull(b_local)
229
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
230
231
        del b_sftp
232
        # Recursively update the modes of all files
233
        chmod_r('sftp/.bzr', 0664, 0775)
234
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
235
236
        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.
237
        self.assertEqualMode(0775, b_sftp.control_files._dir_mode)
238
        self.assertEqualMode(0664, b_sftp.control_files._file_mode)
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
239
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
240
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
241
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
242
        open('local/a', 'wb').write('foo3\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
243
        t_local.commit('foo3')
244
        b_sftp.pull(b_local)
245
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
246
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
247
        open('local/c', 'wb').write('new c\n')
1185.58.1 by John Arbash Meinel
Added new permissions test (currently don't pass)
248
        t_local.add('c')
249
        t_local.commit('new c')
250
        b_sftp.pull(b_local)
251
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
252
1185.58.10 by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories)
253
    def test_sftp_server_modes(self):
254
        if sys.platform == 'win32':
255
            raise TestSkipped('chmod has no effect on win32')
256
257
        umask = 0022
258
        original_umask = os.umask(umask)
259
260
        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.
261
            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)
262
            # Direct access should be masked by umask
263
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
1530.1.21 by Robert Collins
Review feedback fixes.
264
            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)
265
266
            # but Transport overrides umask
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
267
            t.put_bytes('b', 'txt', mode=0666)
1530.1.21 by Robert Collins
Review feedback fixes.
268
            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)
269
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
270
            t._get_sftp().mkdir('c', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
271
            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)
272
273
            t.mkdir('d', mode=0777)
1530.1.21 by Robert Collins
Review feedback fixes.
274
            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)
275
        finally:
276
            os.umask(original_umask)