~bzr-pqm/bzr/bzr.dev

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
from StringIO import StringIO
33
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
34
from bzrlib import tests
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.
35
from bzrlib.branch import Branch
36
from bzrlib.bzrdir import BzrDir
37
from bzrlib.lockable_files import LockableFiles
2018.5.99 by Andrew Bennetts
Don't test mode setting for remote branches because they don't do mode setting.
38
from bzrlib.remote import RemoteBranchFormat
1685.1.17 by John Arbash Meinel
test_permissions.check_mode_r needs to pass a path that the '.' transport can handle
39
from bzrlib.tests.test_permissions import chmod_r, 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.
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from bzrlib.workingtree import WorkingTree
42
43
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
44
class _NullPermsStat(object):
45
    """A class that proxy's a stat result and strips permissions."""
46
47
    def __init__(self, orig_stat):
48
        self._orig_stat = orig_stat
49
        # We strip all permission bits from st_mode
50
        self.st_mode = orig_stat.st_mode & ~07777
51
52
    def __getattr__(self, name):
53
        return getattr(self._orig_stat, name)
54
55
56
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.
57
58
    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.
59
        if isinstance(self.branch_format, RemoteBranchFormat):
60
            # Remote branch format have no permission logic in them; there's
61
            # nothing to test here.
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
62
            raise tests.TestNotApplicable('Remote branches have no'
63
                                          ' 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.
64
        if sys.platform == 'win32':
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
65
            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.
66
        # also, these are BzrBranch format specific things..
67
        os.mkdir('a')
68
        mode = stat.S_IMODE(os.stat('a').st_mode)
69
        t = self.make_branch_and_tree('.')
70
        b = t.branch
3416.2.2 by Martin Pool
Change some callers to get file and directory permissions from bzrdir not LockableFiles
71
        self.assertEqualMode(mode, b.bzrdir._get_dir_mode())
72
        self.assertEqualMode(mode & ~07111, b.bzrdir._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.
73
        self.assertEqualMode(mode, b.control_files._dir_mode)
74
        self.assertEqualMode(mode & ~07111, b.control_files._file_mode)
75
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
76
        os.mkdir('d')
77
        os.chmod('d', 0700)
78
        b = self.make_branch('d')
79
        self.assertEqualMode(0700, b.bzrdir._get_dir_mode())
80
        self.assertEqualMode(0600, b.bzrdir._get_file_mode())
81
        self.assertEqualMode(0700, b.control_files._dir_mode)
82
        self.assertEqualMode(0600, b.control_files._file_mode)
83
        check_mode_r(self, 'd/.bzr', 00600, 00700)
84
85
    def test_new_branch_group_sticky_bit(self):
86
        if isinstance(self.branch_format, RemoteBranchFormat):
87
            # Remote branch format have no permission logic in them; there's
88
            # nothing to test here.
89
            raise tests.TestNotApplicable('Remote branches have no'
90
                                          ' permission logic')
91
        if sys.platform == 'win32':
92
            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.
93
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
94
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
95
            # which users are not likely to be in, and this prevents us
96
            # from setting the sgid bit
3638.3.6 by Vincent Ladeuil
Isolate group sticky bit related tests.
97
            os.chown(self.test_dir, os.getuid(), os.getgid())
98
        # also, these are BzrBranch format specific things..
99
        t = self.make_branch_and_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
        os.mkdir('b')
102
        os.chmod('b', 02777)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
103
        b = self.make_branch('b')
3416.2.2 by Martin Pool
Change some callers to get file and directory permissions from bzrdir not LockableFiles
104
        self.assertEqualMode(02777, b.bzrdir._get_dir_mode())
105
        self.assertEqualMode(00666, b.bzrdir._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.
106
        self.assertEqualMode(02777, b.control_files._dir_mode)
107
        self.assertEqualMode(00666, b.control_files._file_mode)
1685.1.17 by John Arbash Meinel
test_permissions.check_mode_r needs to pass a path that the '.' transport can handle
108
        check_mode_r(self, 'b/.bzr', 00666, 02777)
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.
109
110
        os.mkdir('c')
111
        os.chmod('c', 02750)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
112
        b = self.make_branch('c')
3416.2.2 by Martin Pool
Change some callers to get file and directory permissions from bzrdir not LockableFiles
113
        self.assertEqualMode(02750, b.bzrdir._get_dir_mode())
3416.2.5 by Martin Pool
Correction to branch permissions test
114
        self.assertEqualMode(00640, b.bzrdir._get_file_mode())
115
        self.assertEqualMode(02750, b.control_files._dir_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.
116
        self.assertEqualMode(00640, b.control_files._file_mode)
1685.1.17 by John Arbash Meinel
test_permissions.check_mode_r needs to pass a path that the '.' transport can handle
117
        check_mode_r(self, 'c/.bzr', 00640, 02750)
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.
118
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
119
    def test_mode_0(self):
120
        """Test when a transport returns null permissions for .bzr"""
121
        if isinstance(self.branch_format, RemoteBranchFormat):
122
            # Remote branch format have no permission logic in them; there's
123
            # nothing to test here.
124
            raise tests.TestNotApplicable('Remote branches have no'
125
                                          ' permission logic')
126
        self.make_branch_and_tree('.')
127
        bzrdir = BzrDir.open('.')
128
        # Monkey patch the transport
129
        _orig_stat = bzrdir.transport.stat
130
        def null_perms_stat(*args, **kwargs):
131
            result = _orig_stat(*args, **kwargs)
132
            return _NullPermsStat(result)
133
        bzrdir.transport.stat = null_perms_stat
134
        self.assertIs(None, bzrdir._get_dir_mode())
135
        self.assertIs(None, bzrdir._get_file_mode())