blob: f4e9ba8145a34deb22c052919d3bfc8c12ca3d83 [file] [log] [blame]
Takuto Ikutaf48103c2019-10-24 05:23:19 +00001#!/usr/bin/env vpython3
maruel@chromium.org561d4b22013-09-26 21:08:08 +00002# coding=utf-8
maruelea586f32016-04-05 11:11:33 -07003# Copyright 2013 The LUCI Authors. All rights reserved.
maruelf1f5e2a2016-05-25 17:10:39 -07004# Use of this source code is governed under the Apache License, Version 2.0
5# that can be found in the LICENSE file.
maruel@chromium.org561d4b22013-09-26 21:08:08 +00006
maruel9cdd7612015-12-02 13:40:52 -08007import getpass
Takuto Ikutaf48103c2019-10-24 05:23:19 +00008import io
maruel@chromium.org561d4b22013-09-26 21:08:08 +00009import os
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040010import subprocess
maruel@chromium.org561d4b22013-09-26 21:08:08 +000011import sys
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000012import tempfile
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040013import time
Junji Watanabe16d51522020-07-01 01:59:07 +000014import unittest
15
16import six
maruel@chromium.org561d4b22013-09-26 21:08:08 +000017
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000018# Mutates sys.path.
19import test_env
maruel@chromium.org561d4b22013-09-26 21:08:08 +000020
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000021# third_party/
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -050022from depot_tools import auto_stub
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000023
maruel@chromium.org561d4b22013-09-26 21:08:08 +000024from utils import file_path
maruel4e732992015-10-16 10:17:21 -070025from utils import fs
maruel@chromium.org561d4b22013-09-26 21:08:08 +000026
27
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040028def write_content(filepath, content):
maruel4e732992015-10-16 10:17:21 -070029 with fs.open(filepath, 'wb') as f:
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040030 f.write(content)
31
32
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -050033class FilePathTest(auto_stub.TestCase):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040034 def setUp(self):
35 super(FilePathTest, self).setUp()
36 self._tempdir = None
37
38 def tearDown(self):
maruel4b14f042015-10-06 12:08:08 -070039 try:
40 if self._tempdir:
maruel4e732992015-10-16 10:17:21 -070041 for dirpath, dirnames, filenames in fs.walk(
maruel4b14f042015-10-06 12:08:08 -070042 self._tempdir, topdown=True):
43 for filename in filenames:
44 file_path.set_read_only(os.path.join(dirpath, filename), False)
45 for dirname in dirnames:
46 file_path.set_read_only(os.path.join(dirpath, dirname), False)
47 file_path.rmtree(self._tempdir)
48 finally:
49 super(FilePathTest, self).tearDown()
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040050
51 @property
52 def tempdir(self):
53 if not self._tempdir:
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +000054 self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040055 return self._tempdir
56
vadimshe42aeba2016-06-03 12:32:21 -070057 def test_atomic_replace_new_file(self):
58 path = os.path.join(self.tempdir, 'new_file')
Takuto Ikutaf48103c2019-10-24 05:23:19 +000059 file_path.atomic_replace(path, b'blah')
vadimshe42aeba2016-06-03 12:32:21 -070060 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000061 self.assertEqual(b'blah', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070062 self.assertEqual([u'new_file'], os.listdir(self.tempdir))
63
64 def test_atomic_replace_existing_file(self):
65 path = os.path.join(self.tempdir, 'existing_file')
66 with open(path, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000067 f.write(b'existing body')
68 file_path.atomic_replace(path, b'new body')
vadimshe42aeba2016-06-03 12:32:21 -070069 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000070 self.assertEqual(b'new body', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070071 self.assertEqual([u'existing_file'], os.listdir(self.tempdir))
72
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040073 def assertFileMode(self, filepath, mode, umask=None):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000074 umask = test_env.umask() if umask is None else umask
maruel4e732992015-10-16 10:17:21 -070075 actual = fs.stat(filepath).st_mode
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040076 expected = mode & ~umask
77 self.assertEqual(
78 expected,
79 actual,
80 (filepath, oct(expected), oct(actual), oct(umask)))
81
82 def assertMaskedFileMode(self, filepath, mode):
83 """It's usually when the file was first marked read only."""
Takuto Ikuta7669fb52019-10-23 06:07:30 +000084 self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040085
Junji Watanabe16d51522020-07-01 01:59:07 +000086 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
87 'TODO(crbug.com/1017545): '
88 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +000089 def test_native_case_end_with_os_path_sep(self):
90 # Make sure the trailing os.path.sep is kept.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000091 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +000092 self.assertEqual(file_path.get_native_path_case(path), path)
93
Junji Watanabe16d51522020-07-01 01:59:07 +000094 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
95 'TODO(crbug.com/1017545): '
96 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +000097 def test_native_case_end_with_dot_os_path_sep(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000098 path = file_path.get_native_path_case(test_env.CLIENT_DIR + os.path.sep)
maruel@chromium.org561d4b22013-09-26 21:08:08 +000099 self.assertEqual(
100 file_path.get_native_path_case(path + '.' + os.path.sep),
101 path)
102
Junji Watanabe16d51522020-07-01 01:59:07 +0000103 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
104 'TODO(crbug.com/1017545): '
105 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000106 def test_native_case_non_existing(self):
107 # Make sure it doesn't throw on non-existing files.
108 non_existing = 'trace_input_test_this_file_should_not_exist'
109 path = os.path.expanduser('~/' + non_existing)
110 self.assertFalse(os.path.exists(path))
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000111 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000112 self.assertEqual(file_path.get_native_path_case(path), path)
113
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400114 def test_delete_wd_rf(self):
115 # Confirms that a RO file in a RW directory can be deleted on non-Windows.
116 dir_foo = os.path.join(self.tempdir, 'foo')
117 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000118 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000119 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400120 file_path.set_read_only(dir_foo, False)
121 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000122 self.assertFileMode(dir_foo, 0o40777)
123 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400124 if sys.platform == 'win32':
125 # On Windows, a read-only file can't be deleted.
126 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700127 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400128 else:
maruel4e732992015-10-16 10:17:21 -0700129 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400130
131 def test_delete_rd_wf(self):
132 # Confirms that a Rw file in a RO directory can be deleted on Windows only.
133 dir_foo = os.path.join(self.tempdir, 'foo')
134 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000135 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000136 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400137 file_path.set_read_only(dir_foo, True)
138 file_path.set_read_only(file_bar, False)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000139 self.assertMaskedFileMode(dir_foo, 0o40555)
140 self.assertFileMode(file_bar, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400141 if sys.platform == 'win32':
142 # A read-only directory has a convoluted meaning on Windows, it means that
143 # the directory is "personalized". This is used as a signal by Windows
144 # Explorer to tell it to look into the directory for desktop.ini.
145 # See http://support.microsoft.com/kb/326549 for more details.
146 # As such, it is important to not try to set the read-only bit on
147 # directories on Windows since it has no effect other than trigger
148 # Windows Explorer to look for desktop.ini, which is unnecessary.
maruel4e732992015-10-16 10:17:21 -0700149 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400150 else:
151 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700152 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400153
154 def test_delete_rd_rf(self):
155 # Confirms that a RO file in a RO directory can't be deleted.
156 dir_foo = os.path.join(self.tempdir, 'foo')
157 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000158 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000159 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400160 file_path.set_read_only(dir_foo, True)
161 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000162 self.assertMaskedFileMode(dir_foo, 0o40555)
163 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400164 with self.assertRaises(OSError):
165 # It fails for different reason depending on the OS. See the test cases
166 # above.
maruel4e732992015-10-16 10:17:21 -0700167 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400168
169 def test_hard_link_mode(self):
170 # Creates a hard link, see if the file mode changed on the node or the
171 # directory entry.
172 dir_foo = os.path.join(self.tempdir, 'foo')
173 file_bar = os.path.join(dir_foo, 'bar')
174 file_link = os.path.join(dir_foo, 'link')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000175 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000176 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400177 file_path.hardlink(file_bar, file_link)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000178 self.assertFileMode(file_bar, 0o100666)
179 self.assertFileMode(file_link, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400180 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000181 self.assertMaskedFileMode(file_bar, 0o100444)
182 self.assertMaskedFileMode(file_link, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400183 # This is bad news for Windows; on Windows, the file must be writeable to be
184 # deleted, but the file node is modified. This means that every hard links
185 # must be reset to be read-only after deleting one of the hard link
186 # directory entry.
187
Takuto Ikutae3f70382019-04-03 14:52:06 +0000188 def test_ensure_tree(self):
189 dir_foo = os.path.join(self.tempdir, 'foo')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000190 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000191
192 self.assertTrue(os.path.isdir(dir_foo))
193
194 # Do not raise OSError with errno.EEXIST
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000195 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000196
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500197 def test_rmtree_unicode(self):
198 subdir = os.path.join(self.tempdir, 'hi')
maruel4e732992015-10-16 10:17:21 -0700199 fs.mkdir(subdir)
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500200 filepath = os.path.join(
201 subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629')
maruel4e732992015-10-16 10:17:21 -0700202 with fs.open(filepath, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000203 f.write(b'hi')
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500204 # In particular, it fails when the input argument is a str.
205 file_path.rmtree(str(subdir))
206
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400207 if sys.platform == 'darwin':
Junji Watanabe16d51522020-07-01 01:59:07 +0000208
209 @unittest.skipIf(six.PY3, 'TODO(crbug.com/1017545): '
210 'MacOS and Carbon are not defined')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400211 def test_native_case_symlink_wrong_case(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000212 base_dir = file_path.get_native_path_case(test_env.TESTS_DIR)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400213 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs')
214 actual = file_path.get_native_path_case(trace_inputs_dir)
215 self.assertEqual(trace_inputs_dir, actual)
216
217 # Make sure the symlink is not resolved.
218 data = os.path.join(trace_inputs_dir, 'Files2')
219 actual = file_path.get_native_path_case(data)
220 self.assertEqual(
221 os.path.join(trace_inputs_dir, 'files2'), actual)
222
223 data = os.path.join(trace_inputs_dir, 'Files2', '')
224 actual = file_path.get_native_path_case(data)
225 self.assertEqual(
226 os.path.join(trace_inputs_dir, 'files2', ''), actual)
227
228 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py')
229 actual = file_path.get_native_path_case(data)
230 # TODO(maruel): Should be child1.py.
231 self.assertEqual(
232 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual)
233
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000234 if sys.platform in ('darwin', 'win32'):
Junji Watanabe16d51522020-07-01 01:59:07 +0000235
236 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
237 'TODO(crbug.com/1017545): '
238 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000239 def test_native_case_not_sensitive(self):
240 # The home directory is almost guaranteed to have mixed upper/lower case
241 # letters on both Windows and OSX.
242 # This test also ensures that the output is independent on the input
243 # string case.
244 path = os.path.expanduser(u'~')
245 self.assertTrue(os.path.isdir(path))
246 path = path.replace('/', os.path.sep)
247 if sys.platform == 'win32':
248 # Make sure the drive letter is upper case for consistency.
249 path = path[0].upper() + path[1:]
250 # This test assumes the variable is in the native path case on disk, this
251 # should be the case. Verify this assumption:
252 self.assertEqual(path, file_path.get_native_path_case(path))
253 self.assertEqual(
254 file_path.get_native_path_case(path.lower()),
255 file_path.get_native_path_case(path.upper()))
256
Junji Watanabe16d51522020-07-01 01:59:07 +0000257 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
258 'TODO(crbug.com/1017545): '
259 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000260 def test_native_case_not_sensitive_non_existent(self):
261 # This test also ensures that the output is independent on the input
262 # string case.
263 non_existing = os.path.join(
264 'trace_input_test_this_dir_should_not_exist', 'really not', '')
265 path = os.path.expanduser(os.path.join(u'~', non_existing))
266 path = path.replace('/', os.path.sep)
maruel4e732992015-10-16 10:17:21 -0700267 self.assertFalse(fs.exists(path))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000268 lower = file_path.get_native_path_case(path.lower())
269 upper = file_path.get_native_path_case(path.upper())
270 # Make sure non-existing element is not modified:
271 self.assertTrue(lower.endswith(non_existing.lower()))
272 self.assertTrue(upper.endswith(non_existing.upper()))
273 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)])
274
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400275 if sys.platform == 'win32':
276 def test_native_case_alternate_datastream(self):
277 # Create the file manually, since tempfile doesn't support ADS.
Takuto Ikuta6e2ff962019-10-29 12:35:27 +0000278 tempdir = six.text_type(tempfile.mkdtemp(prefix=u'trace_inputs'))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400279 try:
280 tempdir = file_path.get_native_path_case(tempdir)
281 basename = 'foo.txt'
282 filename = basename + ':Zone.Identifier'
283 filepath = os.path.join(tempdir, filename)
284 open(filepath, 'w').close()
285 self.assertEqual(filepath, file_path.get_native_path_case(filepath))
286 data_suffix = ':$DATA'
287 self.assertEqual(
288 filepath + data_suffix,
289 file_path.get_native_path_case(filepath + data_suffix))
290
291 open(filepath + '$DATA', 'w').close()
292 self.assertEqual(
293 filepath + data_suffix,
294 file_path.get_native_path_case(filepath + data_suffix))
295 # Ensure the ADS weren't created as separate file. You love NTFS, don't
296 # you?
maruel4e732992015-10-16 10:17:21 -0700297 self.assertEqual([basename], fs.listdir(tempdir))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400298 finally:
maruel4b14f042015-10-06 12:08:08 -0700299 file_path.rmtree(tempdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400300
301 def test_rmtree_win(self):
302 # Mock our sleep for faster test case execution.
303 sleeps = []
304 self.mock(time, 'sleep', sleeps.append)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000305 self.mock(sys, 'stderr', io.StringIO())
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400306
307 # Open a child process, so the file is locked.
308 subdir = os.path.join(self.tempdir, 'to_be_deleted')
maruel4e732992015-10-16 10:17:21 -0700309 fs.mkdir(subdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400310 script = 'import time; open(\'a\', \'w\'); time.sleep(60)'
311 proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir)
312 try:
313 # Wait until the file exist.
maruel4e732992015-10-16 10:17:21 -0700314 while not fs.isfile(os.path.join(subdir, 'a')):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400315 self.assertEqual(None, proc.poll())
316 file_path.rmtree(subdir)
317 self.assertEqual([2, 4, 2], sleeps)
318 # sys.stderr.getvalue() would return a fair amount of output but it is
319 # not completely deterministic so we're not testing it here.
320 finally:
321 proc.wait()
322
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500323 def test_filter_processes_dir_win(self):
324 python_dir = os.path.dirname(sys.executable)
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000325 processes = file_path._filter_processes_dir_win(
326 file_path._enum_processes_win(), python_dir)
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500327 self.assertTrue(processes)
328 proc_names = [proc.ExecutablePath for proc in processes]
329 # Try to find at least one python process.
330 self.assertTrue(
331 any(proc == sys.executable for proc in proc_names), proc_names)
332
333 def test_filter_processes_tree_win(self):
334 # Create a grand-child.
335 script = (
336 'import subprocess,sys;'
337 'proc = subprocess.Popen('
338 '[sys.executable, \'-u\', \'-c\', \'import time; print(1); '
339 'time.sleep(60)\'], stdout=subprocess.PIPE); '
340 # Signal grand child is ready.
341 'print(proc.stdout.read(1)); '
342 # Wait for parent to have completed the test.
343 'sys.stdin.read(1); '
344 'proc.kill()'
345 )
346 proc = subprocess.Popen(
347 [sys.executable, '-u', '-c', script],
348 stdin=subprocess.PIPE,
349 stdout=subprocess.PIPE)
350 try:
351 proc.stdout.read(1)
352 processes = file_path.filter_processes_tree_win(
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000353 file_path._enum_processes_win())
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500354 self.assertEqual(3, len(processes), processes)
355 proc.stdin.write('a')
356 proc.wait()
357 except Exception:
358 proc.kill()
359 finally:
360 proc.wait()
361
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000362 if sys.platform != 'win32':
363 def test_symlink(self):
364 # This test will fail if the checkout is in a symlink.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000365 actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR)
366 expected = (test_env.CLIENT_DIR, None, None)
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000367 self.assertEqual(expected, actual)
368
369 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000370 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
371 expected = (os.path.join(test_env.TESTS_DIR, 'trace_inputs'), None, None)
372 self.assertEqual(expected, actual)
373
374 actual = file_path.split_at_symlink(
375 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000376 expected = (
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000377 os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000378 self.assertEqual(expected, actual)
379
380 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000381 test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000382 expected = (
383 os.path.join('tests', 'trace_inputs'), 'files2', '')
384 self.assertEqual(expected, actual)
385 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000386 test_env.CLIENT_DIR,
387 os.path.join('tests', 'trace_inputs', 'files2', 'bar'))
388 expected = (os.path.join('tests', 'trace_inputs'), 'files2', '/bar')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000389 self.assertEqual(expected, actual)
390
Junji Watanabe16d51522020-07-01 01:59:07 +0000391 @unittest.skipIf(sys.platform == 'darwin' and six.PY3,
392 'TODO(crbug.com/1017545): '
393 'MacOS and Carbon are not defined')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000394 def test_native_case_symlink_right_case(self):
395 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000396 os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000397 self.assertEqual('trace_inputs', os.path.basename(actual))
398
399 # Make sure the symlink is not resolved.
400 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000401 os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000402 self.assertEqual('files2', os.path.basename(actual))
403
maruel9cdd7612015-12-02 13:40:52 -0800404 else:
405 def test_undeleteable_chmod(self):
406 # Create a file and a directory with an empty ACL. Then try to delete it.
407 dirpath = os.path.join(self.tempdir, 'd')
408 filepath = os.path.join(dirpath, 'f')
409 os.mkdir(dirpath)
410 with open(filepath, 'w') as f:
411 f.write('hi')
412 os.chmod(filepath, 0)
413 os.chmod(dirpath, 0)
414 file_path.rmtree(dirpath)
415
416 def test_undeleteable_owner(self):
417 # Create a file and a directory with an empty ACL. Then try to delete it.
418 dirpath = os.path.join(self.tempdir, 'd')
419 filepath = os.path.join(dirpath, 'f')
420 os.mkdir(dirpath)
421 with open(filepath, 'w') as f:
422 f.write('hi')
423 import win32security
424 user, _domain, _type = win32security.LookupAccountName(
425 '', getpass.getuser())
426 sd = win32security.SECURITY_DESCRIPTOR()
427 sd.Initialize()
428 sd.SetSecurityDescriptorOwner(user, False)
429 # Create an empty DACL, which removes all rights.
430 dacl = win32security.ACL()
431 dacl.Initialize()
432 sd.SetSecurityDescriptorDacl(1, dacl, 0)
433 win32security.SetFileSecurity(
434 fs.extend(filepath), win32security.DACL_SECURITY_INFORMATION, sd)
435 win32security.SetFileSecurity(
436 fs.extend(dirpath), win32security.DACL_SECURITY_INFORMATION, sd)
437 file_path.rmtree(dirpath)
438
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000439
440if __name__ == '__main__':
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000441 test_env.main()