1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
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.
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.
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
19
"""Tests for bzr setting permissions.
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.
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?
33
from StringIO import StringIO
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
43
# TODO RBC consolidate the helper methods here and in tests/test_permissions.py
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):
51
p = os.path.join(root, d)
54
p = os.path.join(root, f)
55
os.chmod(p, file_mode)
58
def check_mode_r(test, base, file_mode, dir_mode, include_base=True):
59
"""Check that all permissions match
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
67
assert os.path.isdir(base)
68
t = get_transport(".")
70
test.assertTransportMode(t, base, dir_mode)
71
for root, dirs, files in os.walk(base):
73
p = os.path.join(root, d)
74
test.assertTransportMode(t, p, dir_mode)
76
p = os.path.join(root, f)
77
test.assertTransportMode(t, p, file_mode)
80
class TestPermissions(TestCaseWithTransport):
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..
87
mode = stat.S_IMODE(os.stat('a').st_mode)
88
t = self.make_branch_and_tree('.')
90
self.assertEqualMode(mode, b.control_files._dir_mode)
91
self.assertEqualMode(mode & ~07111, b.control_files._file_mode)
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)
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)
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)