~jelmer/ubuntu/maverick/bzr/2.2.5

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-03-10 14:11:59 UTC
  • mfrom: (1.2.1 upstream) (3.1.68 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090310141159-lwzzo5c1fwrtzgj4
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
# -*- coding: utf-8 -*-
 
1
# Copyright (C) 2005, 2008 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
23
22
So if the directory is group writable, the files and subdirs should be as well.
24
23
"""
25
24
 
26
 
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just 
 
25
# TODO: jam 20051215 Currently the default behavior for 'bzr branch' is just
27
26
#                    defined by the local umask. This isn't terrible, is it
28
27
#                    the truly desired behavior?
29
 
 
 
28
 
30
29
import os
31
30
import sys
32
31
import stat
33
32
from StringIO import StringIO
34
33
 
 
34
from bzrlib import tests
35
35
from bzrlib.branch import Branch
36
36
from bzrlib.bzrdir import BzrDir
37
37
from bzrlib.lockable_files import LockableFiles
38
38
from bzrlib.remote import RemoteBranchFormat
39
 
from bzrlib.tests import TestCaseWithTransport, TestSkipped
40
39
from bzrlib.tests.test_permissions import chmod_r, check_mode_r
41
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
42
41
from bzrlib.transport import get_transport
43
42
from bzrlib.workingtree import WorkingTree
44
43
 
45
44
 
46
 
class TestPermissions(TestCaseWithTransport):
 
45
class _NullPermsStat(object):
 
46
    """A class that proxy's a stat result and strips permissions."""
 
47
 
 
48
    def __init__(self, orig_stat):
 
49
        self._orig_stat = orig_stat
 
50
        # We strip all permission bits from st_mode
 
51
        self.st_mode = orig_stat.st_mode & ~07777
 
52
 
 
53
    def __getattr__(self, name):
 
54
        return getattr(self._orig_stat, name)
 
55
 
 
56
 
 
57
class TestPermissions(tests.TestCaseWithTransport):
47
58
 
48
59
    def test_new_branch(self):
49
60
        if isinstance(self.branch_format, RemoteBranchFormat):
50
61
            # Remote branch format have no permission logic in them; there's
51
62
            # nothing to test here.
52
 
            return
 
63
            raise tests.TestNotApplicable('Remote branches have no'
 
64
                                          ' permission logic')
53
65
        if sys.platform == 'win32':
54
 
            raise TestSkipped('chmod has no effect on win32')
 
66
            raise tests.TestNotApplicable('chmod has no effect on win32')
55
67
        # also, these are BzrBranch format specific things..
56
68
        os.mkdir('a')
57
69
        mode = stat.S_IMODE(os.stat('a').st_mode)
58
70
        t = self.make_branch_and_tree('.')
59
71
        b = t.branch
 
72
        self.assertEqualMode(mode, b.bzrdir._get_dir_mode())
 
73
        self.assertEqualMode(mode & ~07111, b.bzrdir._get_file_mode())
60
74
        self.assertEqualMode(mode, b.control_files._dir_mode)
61
75
        self.assertEqualMode(mode & ~07111, b.control_files._file_mode)
62
76
 
 
77
        os.mkdir('d')
 
78
        os.chmod('d', 0700)
 
79
        b = self.make_branch('d')
 
80
        self.assertEqualMode(0700, b.bzrdir._get_dir_mode())
 
81
        self.assertEqualMode(0600, b.bzrdir._get_file_mode())
 
82
        self.assertEqualMode(0700, b.control_files._dir_mode)
 
83
        self.assertEqualMode(0600, b.control_files._file_mode)
 
84
        check_mode_r(self, 'd/.bzr', 00600, 00700)
 
85
 
 
86
    def test_new_branch_group_sticky_bit(self):
 
87
        if isinstance(self.branch_format, RemoteBranchFormat):
 
88
            # Remote branch format have no permission logic in them; there's
 
89
            # nothing to test here.
 
90
            raise tests.TestNotApplicable('Remote branches have no'
 
91
                                          ' permission logic')
 
92
        if sys.platform == 'win32':
 
93
            raise tests.TestNotApplicable('chmod has no effect on win32')
 
94
        elif sys.platform == 'darwin':
 
95
            # OS X creates temp dirs with the 'wheel' group, which users are
 
96
            # not likely to be in, and this prevents us from setting the sgid
 
97
            # bit
 
98
            os.chown(self.test_dir, os.getuid(), os.getgid())
 
99
        # also, these are BzrBranch format specific things..
 
100
        t = self.make_branch_and_tree('.')
 
101
        b = t.branch
63
102
        os.mkdir('b')
64
103
        os.chmod('b', 02777)
65
104
        b = self.make_branch('b')
 
105
        self.assertEqualMode(02777, b.bzrdir._get_dir_mode())
 
106
        self.assertEqualMode(00666, b.bzrdir._get_file_mode())
66
107
        self.assertEqualMode(02777, b.control_files._dir_mode)
67
108
        self.assertEqualMode(00666, b.control_files._file_mode)
68
109
        check_mode_r(self, 'b/.bzr', 00666, 02777)
70
111
        os.mkdir('c')
71
112
        os.chmod('c', 02750)
72
113
        b = self.make_branch('c')
 
114
        self.assertEqualMode(02750, b.bzrdir._get_dir_mode())
 
115
        self.assertEqualMode(00640, b.bzrdir._get_file_mode())
73
116
        self.assertEqualMode(02750, b.control_files._dir_mode)
74
117
        self.assertEqualMode(00640, b.control_files._file_mode)
75
118
        check_mode_r(self, 'c/.bzr', 00640, 02750)
76
119
 
77
 
        os.mkdir('d')
78
 
        os.chmod('d', 0700)
79
 
        b = self.make_branch('d')
80
 
        self.assertEqualMode(0700, b.control_files._dir_mode)
81
 
        self.assertEqualMode(0600, b.control_files._file_mode)
82
 
        check_mode_r(self, 'd/.bzr', 00600, 00700)
 
120
    def test_mode_0(self):
 
121
        """Test when a transport returns null permissions for .bzr"""
 
122
        if isinstance(self.branch_format, RemoteBranchFormat):
 
123
            # Remote branch format have no permission logic in them; there's
 
124
            # nothing to test here.
 
125
            raise tests.TestNotApplicable('Remote branches have no'
 
126
                                          ' permission logic')
 
127
        self.make_branch_and_tree('.')
 
128
        bzrdir = BzrDir.open('.')
 
129
        # Monkey patch the transport
 
130
        _orig_stat = bzrdir.transport.stat
 
131
        def null_perms_stat(*args, **kwargs):
 
132
            result = _orig_stat(*args, **kwargs)
 
133
            return _NullPermsStat(result)
 
134
        bzrdir.transport.stat = null_perms_stat
 
135
        self.assertIs(None, bzrdir._get_dir_mode())
 
136
        self.assertIs(None, bzrdir._get_file_mode())