~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_permissions.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:56:05 UTC
  • mfrom: (6615.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201195605-o7rl92wf6uyum3fk
(vila) Open trunk again as 2.8b1 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
# -*- coding: utf-8 -*-
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
32
32
 
33
33
import os
34
34
import sys
35
 
import stat
36
 
from cStringIO import StringIO
37
 
import urllib
38
35
 
39
 
from bzrlib import transport
 
36
from bzrlib import urlutils
40
37
from bzrlib.branch import Branch
41
 
from bzrlib.bzrdir import BzrDir
 
38
from bzrlib.controldir import ControlDir
42
39
from bzrlib.tests import TestCaseWithTransport, TestSkipped
43
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
44
41
from bzrlib.workingtree import WorkingTree
65
62
    :param dir_mode: The mode for all directories
66
63
    :param include_base: If false, only check the subdirectories
67
64
    """
68
 
    t = transport.get_transport(".")
 
65
    t = test.get_transport()
69
66
    if include_base:
70
67
        test.assertTransportMode(t, base, dir_mode)
71
68
    for root, dirs, files in os.walk(base):
72
69
        for d in dirs:
73
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [d]])
 
70
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
74
71
            test.assertTransportMode(t, p, dir_mode)
75
72
        for f in files:
76
73
            p = os.path.join(root, f)
77
 
            p = '/'.join([urllib.quote(x) for x in root.split('/\\') + [f]])
 
74
            p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
78
75
            test.assertTransportMode(t, p, file_mode)
79
76
 
80
77
 
86
83
 
87
84
        t = self.make_branch_and_tree('.')
88
85
        b = t.branch
89
 
        open('a', 'wb').write('foo\n')
 
86
        with open('a', 'wb') as f: f.write('foo\n')
90
87
        # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
91
88
        t.add('a', 'CAPS-ID')
92
89
        t.commit('foo')
107
104
        self.assertEqualMode(0644, b.bzrdir._get_file_mode())
108
105
 
109
106
        # Modifying a file shouldn't break the permissions
110
 
        open('a', 'wb').write('foo2\n')
 
107
        with open('a', 'wb') as f: f.write('foo2\n')
111
108
        t.commit('foo2')
112
109
        # The mode should be maintained after commit
113
110
        check_mode_r(self, '.bzr', 0644, 0755)
114
111
 
115
112
        # Adding a new file should maintain the permissions
116
 
        open('b', 'wb').write('new b\n')
 
113
        with open('b', 'wb') as f: f.write('new b\n')
117
114
        t.add('b')
118
115
        t.commit('new b')
119
116
        check_mode_r(self, '.bzr', 0644, 0755)
128
125
        self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
129
126
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
130
127
 
131
 
        open('a', 'wb').write('foo3\n')
 
128
        with open('a', 'wb') as f: f.write('foo3\n')
132
129
        t.commit('foo3')
133
130
        check_mode_r(self, '.bzr', 0664, 0775)
134
131
 
135
 
        open('c', 'wb').write('new c\n')
 
132
        with open('c', 'wb') as f: f.write('new c\n')
136
133
        t.add('c')
137
134
        t.commit('new c')
138
135
        check_mode_r(self, '.bzr', 0664, 0775)
140
137
    def test_new_files_group_sticky_bit(self):
141
138
        if sys.platform == 'win32':
142
139
            raise TestSkipped('chmod has no effect on win32')
143
 
        elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
144
 
            # OS X (and FreeBSD) create temp dirs with the 'wheel' group, which
145
 
            # users are not likely to be in, and this prevents us from setting
146
 
            # the sgid bit
 
140
        elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
 
141
            # FreeBSD-based platforms create temp dirs with the 'wheel' group,
 
142
            # which users are not likely to be in, and this prevents us from
 
143
            # setting the sgid bit
147
144
            os.chown(self.test_dir, os.getuid(), os.getgid())
148
145
 
149
146
        t = self.make_branch_and_tree('.')
160
157
        self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
161
158
        self.assertEqualMode(0664, b.bzrdir._get_file_mode())
162
159
 
163
 
        open('a', 'wb').write('foo4\n')
 
160
        with open('a', 'wb') as f: f.write('foo4\n')
164
161
        t.commit('foo4')
165
162
        check_mode_r(self, '.bzr', 0664, 02775)
166
163
 
167
 
        open('d', 'wb').write('new d\n')
 
164
        with open('d', 'wb') as f: f.write('new d\n')
168
165
        t.add('d')
169
166
        t.commit('new d')
170
167
        check_mode_r(self, '.bzr', 0664, 02775)
180
177
 
181
178
        # bodge around for stubsftpserver not letting use connect
182
179
        # more than once
183
 
        _t = transport.get_transport(self.get_url())
 
180
        _t = self.get_transport()
184
181
 
185
182
        os.mkdir('local')
186
183
        t_local = self.make_branch_and_tree('local')
187
184
        b_local = t_local.branch
188
 
        open('local/a', 'wb').write('foo\n')
 
185
        with open('local/a', 'wb') as f: f.write('foo\n')
189
186
        t_local.add('a')
190
187
        t_local.commit('foo')
191
188
 
202
199
 
203
200
        os.mkdir('sftp')
204
201
        sftp_url = self.get_url('sftp')
205
 
        b_sftp = BzrDir.create_branch_and_repo(sftp_url)
 
202
        b_sftp = ControlDir.create_branch_and_repo(sftp_url)
206
203
 
207
204
        b_sftp.pull(b_local)
208
205
        del b_sftp
215
212
        self.assertEqualMode(0755, b_sftp.bzrdir._get_dir_mode())
216
213
        self.assertEqualMode(0644, b_sftp.bzrdir._get_file_mode())
217
214
 
218
 
        open('local/a', 'wb').write('foo2\n')
 
215
        with open('local/a', 'wb') as f: f.write('foo2\n')
219
216
        t_local.commit('foo2')
220
217
        b_sftp.pull(b_local)
221
218
        # The mode should be maintained after commit
222
219
        check_mode_r(self, 'sftp/.bzr', 0644, 0755)
223
220
 
224
 
        open('local/b', 'wb').write('new b\n')
 
221
        with open('local/b', 'wb') as f: f.write('new b\n')
225
222
        t_local.add('b')
226
223
        t_local.commit('new b')
227
224
        b_sftp.pull(b_local)
238
235
        self.assertEqualMode(0775, b_sftp.bzrdir._get_dir_mode())
239
236
        self.assertEqualMode(0664, b_sftp.bzrdir._get_file_mode())
240
237
 
241
 
        open('local/a', 'wb').write('foo3\n')
 
238
        with open('local/a', 'wb') as f: f.write('foo3\n')
242
239
        t_local.commit('foo3')
243
240
        b_sftp.pull(b_local)
244
241
        check_mode_r(self, 'sftp/.bzr', 0664, 0775)
245
242
 
246
 
        open('local/c', 'wb').write('new c\n')
 
243
        with open('local/c', 'wb') as f: f.write('new c\n')
247
244
        t_local.add('c')
248
245
        t_local.commit('new c')
249
246
        b_sftp.pull(b_local)
257
254
        original_umask = os.umask(umask)
258
255
 
259
256
        try:
260
 
            t = transport.get_transport(self.get_url())
 
257
            t = self.get_transport()
261
258
            # Direct access should be masked by umask
262
259
            t._sftp_open_exclusive('a', mode=0666).write('foo\n')
263
260
            self.assertTransportMode(t, 'a', 0666 &~umask)