/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1
# Copyright (C) 2006-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
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.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
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.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
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.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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.
16
17
18
"""Tests for bzr setting permissions.
19
20
Files in the branch control directory (.bzr or .bzr/branch) should inherit
21
the .bzr directory permissions.
22
So if the directory is group writable, the files and subdirs should be as well.
23
"""
24
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
25
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
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.
26
#                    defined by the local umask. This isn't terrible, is it
27
#                    the truly desired behavior?
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
28
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.
29
import os
30
import sys
31
import stat
32
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
33
from breezy import tests
6670.4.3 by Jelmer Vernooij
Fix more imports.
34
from breezy.bzr.branch import BzrBranch
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
35
from breezy.controldir import ControlDir
6670.4.14 by Jelmer Vernooij
Move remote to breezy.bzr.
36
from breezy.bzr.remote import RemoteBranchFormat
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
37
from breezy.tests.test_permissions import check_mode_r
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.
38
39
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
40
class _NullPermsStat(object):
41
    """A class that proxy's a stat result and strips permissions."""
42
43
    def __init__(self, orig_stat):
44
        self._orig_stat = orig_stat
45
        # We strip all permission bits from st_mode
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
46
        self.st_mode = orig_stat.st_mode & ~0o7777
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
47
48
    def __getattr__(self, name):
49
        return getattr(self._orig_stat, name)
50
51
52
class TestPermissions(tests.TestCaseWithTransport):
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.
53
54
    def test_new_branch(self):
2018.5.99 by Andrew Bennetts
Don't test mode setting for remote branches because they don't do mode setting.
55
        if isinstance(self.branch_format, RemoteBranchFormat):
56
            # Remote branch format have no permission logic in them; there's
57
            # nothing to test here.
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
58
            raise tests.TestNotApplicable('Remote branches have no'
59
                                          ' permission logic')
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.
60
        if sys.platform == 'win32':
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
61
            raise tests.TestNotApplicable('chmod has no effect on win32')
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.
62
        os.mkdir('a')
63
        mode = stat.S_IMODE(os.stat('a').st_mode)
64
        t = self.make_branch_and_tree('.')
6123.9.9 by Jelmer Vernooij
Skip some permissions tests that are specific to bzr branches.
65
        # also, these are BzrBranch format specific things..
66
        if not isinstance(t.branch, BzrBranch):
67
            raise tests.TestNotApplicable(
68
                "Only applicable to bzr branches")
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.
69
        b = t.branch
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
70
        self.assertEqualMode(mode, b.controldir._get_dir_mode())
71
        self.assertEqualMode(mode & ~0o7111, b.controldir._get_file_mode())
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.
72
        self.assertEqualMode(mode, b.control_files._dir_mode)
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
73
        self.assertEqualMode(mode & ~0o7111, b.control_files._file_mode)
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.
74
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
75
        os.mkdir('d')
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
76
        os.chmod('d', 0o700)
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
77
        b = self.make_branch('d')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
78
        self.assertEqualMode(0o700, b.controldir._get_dir_mode())
79
        self.assertEqualMode(0o600, b.controldir._get_file_mode())
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
80
        self.assertEqualMode(0o700, b.control_files._dir_mode)
81
        self.assertEqualMode(0o600, b.control_files._file_mode)
82
        check_mode_r(self, 'd/.bzr', 0o0600, 0o0700)
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
83
84
    def test_new_branch_group_sticky_bit(self):
85
        if isinstance(self.branch_format, RemoteBranchFormat):
86
            # Remote branch format have no permission logic in them; there's
87
            # nothing to test here.
88
            raise tests.TestNotApplicable('Remote branches have no'
89
                                          ' permission logic')
90
        if sys.platform == 'win32':
91
            raise tests.TestNotApplicable('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.
92
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
93
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
94
            # which users are not likely to be in, and this prevents us
95
            # from setting the sgid bit
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
96
            os.chown(self.test_dir, os.getuid(), os.getgid())
97
        t = self.make_branch_and_tree('.')
98
        b = t.branch
6123.9.9 by Jelmer Vernooij
Skip some permissions tests that are specific to bzr branches.
99
        # also, these are BzrBranch format specific things..
100
        if not isinstance(b, BzrBranch):
101
            raise tests.TestNotApplicable(
102
                "Only applicable to bzr branches")
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.
103
        os.mkdir('b')
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
104
        os.chmod('b', 0o2777)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
105
        b = self.make_branch('b')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
106
        self.assertEqualMode(0o2777, b.controldir._get_dir_mode())
107
        self.assertEqualMode(0o0666, b.controldir._get_file_mode())
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
108
        self.assertEqualMode(0o2777, b.control_files._dir_mode)
109
        self.assertEqualMode(0o0666, b.control_files._file_mode)
110
        check_mode_r(self, 'b/.bzr', 0o0666, 0o2777)
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.
111
112
        os.mkdir('c')
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
113
        os.chmod('c', 0o2750)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
114
        b = self.make_branch('c')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
115
        self.assertEqualMode(0o2750, b.controldir._get_dir_mode())
116
        self.assertEqualMode(0o0640, b.controldir._get_file_mode())
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
117
        self.assertEqualMode(0o2750, b.control_files._dir_mode)
118
        self.assertEqualMode(0o0640, b.control_files._file_mode)
119
        check_mode_r(self, 'c/.bzr', 0o0640, 0o2750)
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.
120
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
121
    def test_mode_0(self):
122
        """Test when a transport returns null permissions for .bzr"""
123
        if isinstance(self.branch_format, RemoteBranchFormat):
124
            # Remote branch format have no permission logic in them; there's
125
            # nothing to test here.
126
            raise tests.TestNotApplicable('Remote branches have no'
127
                                          ' permission logic')
128
        self.make_branch_and_tree('.')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
129
        bzrdir = ControlDir.open('.')
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
130
        # Monkey patch the transport
131
        _orig_stat = bzrdir.transport.stat
7143.15.2 by Jelmer Vernooij
Run autopep8.
132
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
133
        def null_perms_stat(*args, **kwargs):
134
            result = _orig_stat(*args, **kwargs)
135
            return _NullPermsStat(result)
136
        bzrdir.transport.stat = null_perms_stat
137
        self.assertIs(None, bzrdir._get_dir_mode())
138
        self.assertIs(None, bzrdir._get_file_mode())