blob: 9afc2c6c506e31d19b6ea9f3f7021a93c074358f [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
maruel@chromium.org561d4b22013-09-26 21:08:08 +000086 def test_native_case_end_with_os_path_sep(self):
87 # Make sure the trailing os.path.sep is kept.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000088 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +000089 self.assertEqual(file_path.get_native_path_case(path), path)
90
91 def test_native_case_end_with_dot_os_path_sep(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000092 path = file_path.get_native_path_case(test_env.CLIENT_DIR + os.path.sep)
maruel@chromium.org561d4b22013-09-26 21:08:08 +000093 self.assertEqual(
94 file_path.get_native_path_case(path + '.' + os.path.sep),
95 path)
96
97 def test_native_case_non_existing(self):
98 # Make sure it doesn't throw on non-existing files.
99 non_existing = 'trace_input_test_this_file_should_not_exist'
100 path = os.path.expanduser('~/' + non_existing)
101 self.assertFalse(os.path.exists(path))
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000102 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000103 self.assertEqual(file_path.get_native_path_case(path), path)
104
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400105 def test_delete_wd_rf(self):
106 # Confirms that a RO file in a RW directory can be deleted on non-Windows.
107 dir_foo = os.path.join(self.tempdir, 'foo')
108 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000109 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000110 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400111 file_path.set_read_only(dir_foo, False)
112 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000113 self.assertFileMode(dir_foo, 0o40777)
114 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400115 if sys.platform == 'win32':
116 # On Windows, a read-only file can't be deleted.
117 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700118 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400119 else:
maruel4e732992015-10-16 10:17:21 -0700120 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400121
122 def test_delete_rd_wf(self):
123 # Confirms that a Rw file in a RO directory can be deleted on Windows only.
124 dir_foo = os.path.join(self.tempdir, 'foo')
125 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000126 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000127 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400128 file_path.set_read_only(dir_foo, True)
129 file_path.set_read_only(file_bar, False)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000130 self.assertMaskedFileMode(dir_foo, 0o40555)
131 self.assertFileMode(file_bar, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400132 if sys.platform == 'win32':
133 # A read-only directory has a convoluted meaning on Windows, it means that
134 # the directory is "personalized". This is used as a signal by Windows
135 # Explorer to tell it to look into the directory for desktop.ini.
136 # See http://support.microsoft.com/kb/326549 for more details.
137 # As such, it is important to not try to set the read-only bit on
138 # directories on Windows since it has no effect other than trigger
139 # Windows Explorer to look for desktop.ini, which is unnecessary.
maruel4e732992015-10-16 10:17:21 -0700140 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400141 else:
142 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700143 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400144
145 def test_delete_rd_rf(self):
146 # Confirms that a RO file in a RO directory can't be deleted.
147 dir_foo = os.path.join(self.tempdir, 'foo')
148 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000149 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000150 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400151 file_path.set_read_only(dir_foo, True)
152 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000153 self.assertMaskedFileMode(dir_foo, 0o40555)
154 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400155 with self.assertRaises(OSError):
156 # It fails for different reason depending on the OS. See the test cases
157 # above.
maruel4e732992015-10-16 10:17:21 -0700158 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400159
160 def test_hard_link_mode(self):
161 # Creates a hard link, see if the file mode changed on the node or the
162 # directory entry.
163 dir_foo = os.path.join(self.tempdir, 'foo')
164 file_bar = os.path.join(dir_foo, 'bar')
165 file_link = os.path.join(dir_foo, 'link')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000166 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000167 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400168 file_path.hardlink(file_bar, file_link)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000169 self.assertFileMode(file_bar, 0o100666)
170 self.assertFileMode(file_link, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400171 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000172 self.assertMaskedFileMode(file_bar, 0o100444)
173 self.assertMaskedFileMode(file_link, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400174 # This is bad news for Windows; on Windows, the file must be writeable to be
175 # deleted, but the file node is modified. This means that every hard links
176 # must be reset to be read-only after deleting one of the hard link
177 # directory entry.
178
Takuto Ikutae3f70382019-04-03 14:52:06 +0000179 def test_ensure_tree(self):
180 dir_foo = os.path.join(self.tempdir, 'foo')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000181 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000182
183 self.assertTrue(os.path.isdir(dir_foo))
184
185 # Do not raise OSError with errno.EEXIST
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000186 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000187
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500188 def test_rmtree_unicode(self):
189 subdir = os.path.join(self.tempdir, 'hi')
maruel4e732992015-10-16 10:17:21 -0700190 fs.mkdir(subdir)
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500191 filepath = os.path.join(
192 subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629')
maruel4e732992015-10-16 10:17:21 -0700193 with fs.open(filepath, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000194 f.write(b'hi')
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500195 # In particular, it fails when the input argument is a str.
196 file_path.rmtree(str(subdir))
197
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400198 if sys.platform == 'darwin':
Junji Watanabe16d51522020-07-01 01:59:07 +0000199
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400200 def test_native_case_symlink_wrong_case(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000201 base_dir = file_path.get_native_path_case(test_env.TESTS_DIR)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400202 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs')
203 actual = file_path.get_native_path_case(trace_inputs_dir)
204 self.assertEqual(trace_inputs_dir, actual)
205
206 # Make sure the symlink is not resolved.
207 data = os.path.join(trace_inputs_dir, 'Files2')
208 actual = file_path.get_native_path_case(data)
209 self.assertEqual(
210 os.path.join(trace_inputs_dir, 'files2'), actual)
211
212 data = os.path.join(trace_inputs_dir, 'Files2', '')
213 actual = file_path.get_native_path_case(data)
214 self.assertEqual(
215 os.path.join(trace_inputs_dir, 'files2', ''), actual)
216
217 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py')
218 actual = file_path.get_native_path_case(data)
219 # TODO(maruel): Should be child1.py.
220 self.assertEqual(
221 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual)
222
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000223 if sys.platform in ('darwin', 'win32'):
Junji Watanabe16d51522020-07-01 01:59:07 +0000224
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000225 def test_native_case_not_sensitive(self):
226 # The home directory is almost guaranteed to have mixed upper/lower case
227 # letters on both Windows and OSX.
228 # This test also ensures that the output is independent on the input
229 # string case.
230 path = os.path.expanduser(u'~')
231 self.assertTrue(os.path.isdir(path))
232 path = path.replace('/', os.path.sep)
233 if sys.platform == 'win32':
234 # Make sure the drive letter is upper case for consistency.
235 path = path[0].upper() + path[1:]
236 # This test assumes the variable is in the native path case on disk, this
237 # should be the case. Verify this assumption:
238 self.assertEqual(path, file_path.get_native_path_case(path))
239 self.assertEqual(
240 file_path.get_native_path_case(path.lower()),
241 file_path.get_native_path_case(path.upper()))
242
243 def test_native_case_not_sensitive_non_existent(self):
244 # This test also ensures that the output is independent on the input
245 # string case.
246 non_existing = os.path.join(
247 'trace_input_test_this_dir_should_not_exist', 'really not', '')
248 path = os.path.expanduser(os.path.join(u'~', non_existing))
249 path = path.replace('/', os.path.sep)
maruel4e732992015-10-16 10:17:21 -0700250 self.assertFalse(fs.exists(path))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000251 lower = file_path.get_native_path_case(path.lower())
252 upper = file_path.get_native_path_case(path.upper())
253 # Make sure non-existing element is not modified:
254 self.assertTrue(lower.endswith(non_existing.lower()))
255 self.assertTrue(upper.endswith(non_existing.upper()))
256 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)])
257
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400258 if sys.platform == 'win32':
259 def test_native_case_alternate_datastream(self):
260 # Create the file manually, since tempfile doesn't support ADS.
Takuto Ikuta6e2ff962019-10-29 12:35:27 +0000261 tempdir = six.text_type(tempfile.mkdtemp(prefix=u'trace_inputs'))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400262 try:
263 tempdir = file_path.get_native_path_case(tempdir)
264 basename = 'foo.txt'
265 filename = basename + ':Zone.Identifier'
266 filepath = os.path.join(tempdir, filename)
267 open(filepath, 'w').close()
268 self.assertEqual(filepath, file_path.get_native_path_case(filepath))
269 data_suffix = ':$DATA'
270 self.assertEqual(
271 filepath + data_suffix,
272 file_path.get_native_path_case(filepath + data_suffix))
273
274 open(filepath + '$DATA', 'w').close()
275 self.assertEqual(
276 filepath + data_suffix,
277 file_path.get_native_path_case(filepath + data_suffix))
278 # Ensure the ADS weren't created as separate file. You love NTFS, don't
279 # you?
maruel4e732992015-10-16 10:17:21 -0700280 self.assertEqual([basename], fs.listdir(tempdir))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400281 finally:
maruel4b14f042015-10-06 12:08:08 -0700282 file_path.rmtree(tempdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400283
Junji Watanabee1d6df22020-11-12 08:13:23 +0000284 @unittest.skipIf(sys.platform == 'win32', 'crbug.com/1148174')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400285 def test_rmtree_win(self):
286 # Mock our sleep for faster test case execution.
287 sleeps = []
288 self.mock(time, 'sleep', sleeps.append)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000289 self.mock(sys, 'stderr', io.StringIO())
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400290
291 # Open a child process, so the file is locked.
292 subdir = os.path.join(self.tempdir, 'to_be_deleted')
maruel4e732992015-10-16 10:17:21 -0700293 fs.mkdir(subdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400294 script = 'import time; open(\'a\', \'w\'); time.sleep(60)'
295 proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir)
296 try:
297 # Wait until the file exist.
maruel4e732992015-10-16 10:17:21 -0700298 while not fs.isfile(os.path.join(subdir, 'a')):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400299 self.assertEqual(None, proc.poll())
300 file_path.rmtree(subdir)
301 self.assertEqual([2, 4, 2], sleeps)
302 # sys.stderr.getvalue() would return a fair amount of output but it is
303 # not completely deterministic so we're not testing it here.
304 finally:
305 proc.wait()
306
Junji Watanabee1d6df22020-11-12 08:13:23 +0000307 @unittest.skipIf(sys.platform == 'win32', 'crbug.com/1148174')
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500308 def test_filter_processes_dir_win(self):
309 python_dir = os.path.dirname(sys.executable)
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000310 processes = file_path._filter_processes_dir_win(
311 file_path._enum_processes_win(), python_dir)
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500312 self.assertTrue(processes)
313 proc_names = [proc.ExecutablePath for proc in processes]
314 # Try to find at least one python process.
315 self.assertTrue(
316 any(proc == sys.executable for proc in proc_names), proc_names)
317
318 def test_filter_processes_tree_win(self):
319 # Create a grand-child.
320 script = (
321 'import subprocess,sys;'
322 'proc = subprocess.Popen('
323 '[sys.executable, \'-u\', \'-c\', \'import time; print(1); '
324 'time.sleep(60)\'], stdout=subprocess.PIPE); '
325 # Signal grand child is ready.
326 'print(proc.stdout.read(1)); '
327 # Wait for parent to have completed the test.
328 'sys.stdin.read(1); '
329 'proc.kill()'
330 )
331 proc = subprocess.Popen(
332 [sys.executable, '-u', '-c', script],
333 stdin=subprocess.PIPE,
334 stdout=subprocess.PIPE)
335 try:
336 proc.stdout.read(1)
337 processes = file_path.filter_processes_tree_win(
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000338 file_path._enum_processes_win())
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500339 self.assertEqual(3, len(processes), processes)
340 proc.stdin.write('a')
341 proc.wait()
342 except Exception:
343 proc.kill()
344 finally:
345 proc.wait()
346
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000347 if sys.platform != 'win32':
348 def test_symlink(self):
349 # This test will fail if the checkout is in a symlink.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000350 actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR)
351 expected = (test_env.CLIENT_DIR, None, None)
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000352 self.assertEqual(expected, actual)
353
354 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000355 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
356 expected = (os.path.join(test_env.TESTS_DIR, 'trace_inputs'), None, None)
357 self.assertEqual(expected, actual)
358
359 actual = file_path.split_at_symlink(
360 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000361 expected = (
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000362 os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000363 self.assertEqual(expected, actual)
364
365 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000366 test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000367 expected = (
368 os.path.join('tests', 'trace_inputs'), 'files2', '')
369 self.assertEqual(expected, actual)
370 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000371 test_env.CLIENT_DIR,
372 os.path.join('tests', 'trace_inputs', 'files2', 'bar'))
373 expected = (os.path.join('tests', 'trace_inputs'), 'files2', '/bar')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000374 self.assertEqual(expected, actual)
375
376 def test_native_case_symlink_right_case(self):
377 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000378 os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000379 self.assertEqual('trace_inputs', os.path.basename(actual))
380
381 # Make sure the symlink is not resolved.
382 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000383 os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000384 self.assertEqual('files2', os.path.basename(actual))
385
maruel9cdd7612015-12-02 13:40:52 -0800386 else:
Junji Watanabee1d6df22020-11-12 08:13:23 +0000387
388 @unittest.skipIf(sys.platform == 'win32', 'crbug.com/1148174')
maruel9cdd7612015-12-02 13:40:52 -0800389 def test_undeleteable_chmod(self):
390 # Create a file and a directory with an empty ACL. Then try to delete it.
391 dirpath = os.path.join(self.tempdir, 'd')
392 filepath = os.path.join(dirpath, 'f')
393 os.mkdir(dirpath)
394 with open(filepath, 'w') as f:
395 f.write('hi')
396 os.chmod(filepath, 0)
397 os.chmod(dirpath, 0)
398 file_path.rmtree(dirpath)
399
Junji Watanabee1d6df22020-11-12 08:13:23 +0000400 @unittest.skipIf(sys.platform == 'win32', 'crbug.com/1148174')
maruel9cdd7612015-12-02 13:40:52 -0800401 def test_undeleteable_owner(self):
402 # Create a file and a directory with an empty ACL. Then try to delete it.
403 dirpath = os.path.join(self.tempdir, 'd')
404 filepath = os.path.join(dirpath, 'f')
405 os.mkdir(dirpath)
406 with open(filepath, 'w') as f:
407 f.write('hi')
408 import win32security
409 user, _domain, _type = win32security.LookupAccountName(
410 '', getpass.getuser())
411 sd = win32security.SECURITY_DESCRIPTOR()
412 sd.Initialize()
413 sd.SetSecurityDescriptorOwner(user, False)
414 # Create an empty DACL, which removes all rights.
415 dacl = win32security.ACL()
416 dacl.Initialize()
417 sd.SetSecurityDescriptorDacl(1, dacl, 0)
418 win32security.SetFileSecurity(
419 fs.extend(filepath), win32security.DACL_SECURITY_INFORMATION, sd)
420 win32security.SetFileSecurity(
421 fs.extend(dirpath), win32security.DACL_SECURITY_INFORMATION, sd)
422 file_path.rmtree(dirpath)
423
Takuto Ikuta995da062021-03-17 05:01:59 +0000424 def _check_get_recursive_size(self, symlink='symlink'):
425 # Test that _get_recursive_size calculates file size recursively.
426 with open(os.path.join(self.tempdir, '1'), 'w') as f:
427 f.write('0')
428 self.assertEqual(file_path.get_recursive_size(self.tempdir), 1)
429
430 with open(os.path.join(self.tempdir, '2'), 'w') as f:
431 f.write('01')
432 self.assertEqual(file_path.get_recursive_size(self.tempdir), 3)
433
434 nested_dir = os.path.join(self.tempdir, 'dir1', 'dir2')
435 os.makedirs(nested_dir)
436 with open(os.path.join(nested_dir, '4'), 'w') as f:
437 f.write('0123')
438 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
439
440 symlink_dir = os.path.join(self.tempdir, 'symlink_dir')
441 symlink_file = os.path.join(self.tempdir, 'symlink_file')
442 if symlink == 'symlink':
443
444 if sys.platform == 'win32':
445 subprocess.check_call('cmd /c mklink /d %s %s' %
446 (symlink_dir, nested_dir))
447 subprocess.check_call('cmd /c mklink %s %s' %
448 (symlink_file, os.path.join(self.tempdir, '1')))
449 else:
450 os.symlink(nested_dir, symlink_dir)
451 os.symlink(os.path.join(self.tempdir, '1'), symlink_file)
452
453 elif symlink == 'junction':
454 # junction should be ignored.
455 subprocess.check_call('cmd /c mklink /j %s %s' %
456 (symlink_dir, nested_dir))
457
458 # This is invalid junction, junction can be made only for directory.
459 subprocess.check_call('cmd /c mklink /j %s %s' %
460 (symlink_file, os.path.join(self.tempdir, '1')))
461 elif symlink == 'hardlink':
462 # hardlink can be made only for file.
463 subprocess.check_call('cmd /c mklink /h %s %s' %
464 (symlink_file, os.path.join(self.tempdir, '1')))
465 else:
466 assert False, ("symlink should be one of symlink, "
467 "junction or hardlink, but: %s" % symlink)
468
469 if symlink == 'hardlink':
470 # hardlinked file is double counted.
471 self.assertEqual(file_path.get_recursive_size(self.tempdir), 8)
472 else:
473 # symlink and junction should be ignored.
474 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
475
476 def test_get_recursive_size(self):
477 self._check_get_recursive_size()
478
479 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
480 def test_get_recursive_size_win_junction(self):
481 self._check_get_recursive_size(symlink='junction')
482
483 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
484 def test_get_recursive_size_win_hardlink(self):
485 self._check_get_recursive_size(symlink='hardlink')
486
487 @unittest.skipIf(sys.platform == 'win32', 'non-Windows specific')
488 def test_get_recursive_size_scandir_on_non_win(self):
489 # Test scandir implementation on non-windows.
490 self.mock(file_path, '_use_scandir', lambda: True)
491 self._check_get_recursive_size()
492
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000493
494if __name__ == '__main__':
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000495 test_env.main()