~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_permissions.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-02-18 02:33:47 UTC
  • mfrom: (1534.1.24 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060218023347-0952c65f668bfd68
Merge Robert Collins integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
"""Tests for bzr setting permissions.
 
20
 
 
21
Files in the branch control directory (.bzr or .bzr/branch) should inherit
 
22
the .bzr directory permissions.
 
23
So if the directory is group writable, the files and subdirs should be as well.
 
24
"""
 
25
 
 
26
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just 
 
27
#                    defined by the local umask. This isn't terrible, is it
 
28
#                    the truly desired behavior?
 
29
 
 
30
import os
 
31
import sys
 
32
import stat
 
33
from StringIO import StringIO
 
34
 
 
35
from bzrlib.branch import Branch
 
36
from bzrlib.bzrdir import BzrDir
 
37
from bzrlib.lockable_files import LockableFiles
 
38
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
40
from bzrlib.transport import get_transport
 
41
from bzrlib.workingtree import WorkingTree
 
42
 
 
43
# TODO RBC consolidate the helper methods here and in tests/test_permissions.py
 
44
 
 
45
def chmod_r(base, file_mode, dir_mode):
 
46
    """Recursively chmod from a base directory"""
 
47
    assert os.path.isdir(base)
 
48
    os.chmod(base, dir_mode)
 
49
    for root, dirs, files in os.walk(base):
 
50
        for d in dirs:
 
51
            p = os.path.join(root, d)
 
52
            os.chmod(p, dir_mode)
 
53
        for f in files:
 
54
            p = os.path.join(root, f)
 
55
            os.chmod(p, file_mode)
 
56
 
 
57
 
 
58
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
 
59
    """Check that all permissions match
 
60
 
 
61
    :param test: The TestCase being run
 
62
    :param base: The path to the root directory to check
 
63
    :param file_mode: The mode for all files
 
64
    :param dir_mode: The mode for all directories
 
65
    :param include_base: If false, only check the subdirectories
 
66
    """
 
67
    assert os.path.isdir(base)
 
68
    t = get_transport(".")
 
69
    if include_base:
 
70
        test.assertTransportMode(t, base, dir_mode)
 
71
    for root, dirs, files in os.walk(base):
 
72
        for d in dirs:
 
73
            p = os.path.join(root, d)
 
74
            test.assertTransportMode(t, p, dir_mode)
 
75
        for f in files:
 
76
            p = os.path.join(root, f)
 
77
            test.assertTransportMode(t, p, file_mode)
 
78
 
 
79
 
 
80
class TestPermissions(TestCaseWithTransport):
 
81
 
 
82
    def test_new_branch(self):
 
83
        if sys.platform == 'win32':
 
84
            raise TestSkipped('chmod has no effect on win32')
 
85
        # also, these are BzrBranch format specific things..
 
86
        os.mkdir('a')
 
87
        mode = stat.S_IMODE(os.stat('a').st_mode)
 
88
        t = self.make_branch_and_tree('.')
 
89
        b = t.branch
 
90
        self.assertEqualMode(mode, b.control_files._dir_mode)
 
91
        self.assertEqualMode(mode & ~07111, b.control_files._file_mode)
 
92
 
 
93
        os.mkdir('b')
 
94
        os.chmod('b', 02777)
 
95
        b = BzrDir.create('b').create_branch()
 
96
        self.assertEqualMode(02777, b.control_files._dir_mode)
 
97
        self.assertEqualMode(00666, b.control_files._file_mode)
 
98
        check_mode_r(self, b.control_files._transport.base, 00666, 02777)
 
99
 
 
100
        os.mkdir('c')
 
101
        os.chmod('c', 02750)
 
102
        b = BzrDir.create('c').create_branch()
 
103
        self.assertEqualMode(02750, b.control_files._dir_mode)
 
104
        self.assertEqualMode(00640, b.control_files._file_mode)
 
105
        check_mode_r(self, b.control_files._transport.base, 00640, 02750)
 
106
 
 
107
        os.mkdir('d')
 
108
        os.chmod('d', 0700)
 
109
        b = BzrDir.create('d').create_branch()
 
110
        self.assertEqualMode(0700, b.control_files._dir_mode)
 
111
        self.assertEqualMode(0600, b.control_files._file_mode)
 
112
        check_mode_r(self, b.control_files._transport.base, 00600, 00700)