blob: 3112b978a3ae9a4d4ba293334b8160cc02746c53 [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
Junji Watanabedff19e62021-08-10 02:22:11 +000026from utils import subprocess42
maruel@chromium.org561d4b22013-09-26 21:08:08 +000027
28
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040029def write_content(filepath, content):
maruel4e732992015-10-16 10:17:21 -070030 with fs.open(filepath, 'wb') as f:
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040031 f.write(content)
32
33
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -050034class FilePathTest(auto_stub.TestCase):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040035 def setUp(self):
36 super(FilePathTest, self).setUp()
37 self._tempdir = None
38
39 def tearDown(self):
maruel4b14f042015-10-06 12:08:08 -070040 try:
41 if self._tempdir:
maruel4e732992015-10-16 10:17:21 -070042 for dirpath, dirnames, filenames in fs.walk(
maruel4b14f042015-10-06 12:08:08 -070043 self._tempdir, topdown=True):
44 for filename in filenames:
45 file_path.set_read_only(os.path.join(dirpath, filename), False)
46 for dirname in dirnames:
47 file_path.set_read_only(os.path.join(dirpath, dirname), False)
48 file_path.rmtree(self._tempdir)
49 finally:
50 super(FilePathTest, self).tearDown()
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040051
52 @property
53 def tempdir(self):
54 if not self._tempdir:
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +000055 self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040056 return self._tempdir
57
vadimshe42aeba2016-06-03 12:32:21 -070058 def test_atomic_replace_new_file(self):
59 path = os.path.join(self.tempdir, 'new_file')
Takuto Ikutaf48103c2019-10-24 05:23:19 +000060 file_path.atomic_replace(path, b'blah')
vadimshe42aeba2016-06-03 12:32:21 -070061 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000062 self.assertEqual(b'blah', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070063 self.assertEqual([u'new_file'], os.listdir(self.tempdir))
64
65 def test_atomic_replace_existing_file(self):
66 path = os.path.join(self.tempdir, 'existing_file')
67 with open(path, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000068 f.write(b'existing body')
69 file_path.atomic_replace(path, b'new body')
vadimshe42aeba2016-06-03 12:32:21 -070070 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000071 self.assertEqual(b'new body', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070072 self.assertEqual([u'existing_file'], os.listdir(self.tempdir))
73
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040074 def assertFileMode(self, filepath, mode, umask=None):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000075 umask = test_env.umask() if umask is None else umask
maruel4e732992015-10-16 10:17:21 -070076 actual = fs.stat(filepath).st_mode
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040077 expected = mode & ~umask
78 self.assertEqual(
79 expected,
80 actual,
81 (filepath, oct(expected), oct(actual), oct(umask)))
82
83 def assertMaskedFileMode(self, filepath, mode):
84 """It's usually when the file was first marked read only."""
Takuto Ikuta7669fb52019-10-23 06:07:30 +000085 self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040086
maruel@chromium.org561d4b22013-09-26 21:08:08 +000087 def test_native_case_end_with_os_path_sep(self):
88 # Make sure the trailing os.path.sep is kept.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000089 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +000090 self.assertEqual(file_path.get_native_path_case(path), path)
91
92 def test_native_case_end_with_dot_os_path_sep(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000093 path = file_path.get_native_path_case(test_env.CLIENT_DIR + os.path.sep)
maruel@chromium.org561d4b22013-09-26 21:08:08 +000094 self.assertEqual(
95 file_path.get_native_path_case(path + '.' + os.path.sep),
96 path)
97
98 def test_native_case_non_existing(self):
99 # Make sure it doesn't throw on non-existing files.
100 non_existing = 'trace_input_test_this_file_should_not_exist'
101 path = os.path.expanduser('~/' + non_existing)
102 self.assertFalse(os.path.exists(path))
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000103 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000104 self.assertEqual(file_path.get_native_path_case(path), path)
105
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400106 def test_delete_wd_rf(self):
107 # Confirms that a RO file in a RW directory can be deleted on non-Windows.
108 dir_foo = os.path.join(self.tempdir, 'foo')
109 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000110 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000111 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400112 file_path.set_read_only(dir_foo, False)
113 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000114 self.assertFileMode(dir_foo, 0o40777)
115 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400116 if sys.platform == 'win32':
117 # On Windows, a read-only file can't be deleted.
118 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700119 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400120 else:
maruel4e732992015-10-16 10:17:21 -0700121 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400122
123 def test_delete_rd_wf(self):
124 # Confirms that a Rw file in a RO directory can be deleted on Windows only.
125 dir_foo = os.path.join(self.tempdir, 'foo')
126 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000127 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000128 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400129 file_path.set_read_only(dir_foo, True)
130 file_path.set_read_only(file_bar, False)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000131 self.assertMaskedFileMode(dir_foo, 0o40555)
132 self.assertFileMode(file_bar, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400133 if sys.platform == 'win32':
134 # A read-only directory has a convoluted meaning on Windows, it means that
135 # the directory is "personalized". This is used as a signal by Windows
136 # Explorer to tell it to look into the directory for desktop.ini.
137 # See http://support.microsoft.com/kb/326549 for more details.
138 # As such, it is important to not try to set the read-only bit on
139 # directories on Windows since it has no effect other than trigger
140 # Windows Explorer to look for desktop.ini, which is unnecessary.
maruel4e732992015-10-16 10:17:21 -0700141 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400142 else:
143 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700144 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400145
146 def test_delete_rd_rf(self):
147 # Confirms that a RO file in a RO directory can't be deleted.
148 dir_foo = os.path.join(self.tempdir, 'foo')
149 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000150 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000151 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400152 file_path.set_read_only(dir_foo, True)
153 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000154 self.assertMaskedFileMode(dir_foo, 0o40555)
155 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400156 with self.assertRaises(OSError):
157 # It fails for different reason depending on the OS. See the test cases
158 # above.
maruel4e732992015-10-16 10:17:21 -0700159 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400160
161 def test_hard_link_mode(self):
162 # Creates a hard link, see if the file mode changed on the node or the
163 # directory entry.
164 dir_foo = os.path.join(self.tempdir, 'foo')
165 file_bar = os.path.join(dir_foo, 'bar')
166 file_link = os.path.join(dir_foo, 'link')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000167 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000168 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400169 file_path.hardlink(file_bar, file_link)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000170 self.assertFileMode(file_bar, 0o100666)
171 self.assertFileMode(file_link, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400172 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000173 self.assertMaskedFileMode(file_bar, 0o100444)
174 self.assertMaskedFileMode(file_link, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400175 # This is bad news for Windows; on Windows, the file must be writeable to be
176 # deleted, but the file node is modified. This means that every hard links
177 # must be reset to be read-only after deleting one of the hard link
178 # directory entry.
179
Takuto Ikutae3f70382019-04-03 14:52:06 +0000180 def test_ensure_tree(self):
181 dir_foo = os.path.join(self.tempdir, 'foo')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000182 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000183
184 self.assertTrue(os.path.isdir(dir_foo))
185
186 # Do not raise OSError with errno.EEXIST
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000187 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000188
Junji Watanabedff19e62021-08-10 02:22:11 +0000189 def _make_tree(self):
190 root = os.path.join(self.tempdir, 'root')
191 child_dir = os.path.join(root, 'child')
192 grand_child_dir = os.path.join(child_dir, 'grand_child')
193 dirs = [root, child_dir, grand_child_dir]
194 os.makedirs(grand_child_dir)
195 files = [
196 os.path.join(root, 'file1'),
197 os.path.join(child_dir, 'file2'),
198 os.path.join(grand_child_dir, 'file3'),
199 ]
200 for f in files:
201 open(f, 'w').close()
202 return root, dirs, files
203
204 @unittest.skipIf(sys.platform == 'win32', 'posix only')
205 def test_rmtree(self):
206 root, dirs, _ = self._make_tree()
207
208 # Emulate fs.rmtree() permission error.
209 can_delete = set()
210
211 def fs_rmtree_mock(_path, onerror):
212 for d in dirs:
213 if d not in can_delete:
214 onerror(None, None, (None, None, None))
215
216 def chmod_mock(path, _mode):
217 can_delete.add(path)
218
219 self.mock(fs, 'rmtree', fs_rmtree_mock)
220 if hasattr(os, 'lchmod'):
221 self.mock(fs, 'lchmod', chmod_mock)
222 else:
223 self.mock(fs, 'chmod', chmod_mock)
224
225 file_path.rmtree(root)
226
227 @unittest.skipIf(sys.platform == 'win32', 'posix only')
228 def test_rmtree_with_sudo_chmod(self):
229 root, dirs, _ = self._make_tree()
230
231 # Emulate fs.rmtree() permission error.
232 can_delete = set()
233
234 def fs_rmtree_mock(_path, onerror):
235 for d in dirs:
236 if d not in can_delete:
237 onerror(None, None, (None, None, None))
238
239 self.mock(fs, 'rmtree', fs_rmtree_mock)
240
241 # pylint: disable=unused-argument
242 def subprocess_mock(cmd, stdin=None):
243 path = cmd[4]
244 can_delete.add(path)
245
246 self.mock(file_path, 'set_read_only_swallow', lambda *_: OSError('error'))
247 self.mock(subprocess42, 'call', subprocess_mock)
248
249 file_path.rmtree(root)
250
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500251 def test_rmtree_unicode(self):
252 subdir = os.path.join(self.tempdir, 'hi')
maruel4e732992015-10-16 10:17:21 -0700253 fs.mkdir(subdir)
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500254 filepath = os.path.join(
255 subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629')
maruel4e732992015-10-16 10:17:21 -0700256 with fs.open(filepath, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000257 f.write(b'hi')
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500258 # In particular, it fails when the input argument is a str.
259 file_path.rmtree(str(subdir))
260
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400261 if sys.platform == 'darwin':
Junji Watanabe16d51522020-07-01 01:59:07 +0000262
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400263 def test_native_case_symlink_wrong_case(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000264 base_dir = file_path.get_native_path_case(test_env.TESTS_DIR)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400265 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs')
266 actual = file_path.get_native_path_case(trace_inputs_dir)
267 self.assertEqual(trace_inputs_dir, actual)
268
269 # Make sure the symlink is not resolved.
270 data = os.path.join(trace_inputs_dir, 'Files2')
271 actual = file_path.get_native_path_case(data)
272 self.assertEqual(
273 os.path.join(trace_inputs_dir, 'files2'), actual)
274
275 data = os.path.join(trace_inputs_dir, 'Files2', '')
276 actual = file_path.get_native_path_case(data)
277 self.assertEqual(
278 os.path.join(trace_inputs_dir, 'files2', ''), actual)
279
280 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py')
281 actual = file_path.get_native_path_case(data)
282 # TODO(maruel): Should be child1.py.
283 self.assertEqual(
284 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual)
285
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000286 if sys.platform in ('darwin', 'win32'):
Junji Watanabe16d51522020-07-01 01:59:07 +0000287
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000288 def test_native_case_not_sensitive(self):
289 # The home directory is almost guaranteed to have mixed upper/lower case
290 # letters on both Windows and OSX.
291 # This test also ensures that the output is independent on the input
292 # string case.
293 path = os.path.expanduser(u'~')
294 self.assertTrue(os.path.isdir(path))
295 path = path.replace('/', os.path.sep)
296 if sys.platform == 'win32':
297 # Make sure the drive letter is upper case for consistency.
298 path = path[0].upper() + path[1:]
299 # This test assumes the variable is in the native path case on disk, this
300 # should be the case. Verify this assumption:
301 self.assertEqual(path, file_path.get_native_path_case(path))
302 self.assertEqual(
303 file_path.get_native_path_case(path.lower()),
304 file_path.get_native_path_case(path.upper()))
305
306 def test_native_case_not_sensitive_non_existent(self):
307 # This test also ensures that the output is independent on the input
308 # string case.
309 non_existing = os.path.join(
310 'trace_input_test_this_dir_should_not_exist', 'really not', '')
311 path = os.path.expanduser(os.path.join(u'~', non_existing))
312 path = path.replace('/', os.path.sep)
maruel4e732992015-10-16 10:17:21 -0700313 self.assertFalse(fs.exists(path))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000314 lower = file_path.get_native_path_case(path.lower())
315 upper = file_path.get_native_path_case(path.upper())
316 # Make sure non-existing element is not modified:
317 self.assertTrue(lower.endswith(non_existing.lower()))
318 self.assertTrue(upper.endswith(non_existing.upper()))
319 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)])
320
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400321 if sys.platform == 'win32':
322 def test_native_case_alternate_datastream(self):
323 # Create the file manually, since tempfile doesn't support ADS.
Takuto Ikuta6e2ff962019-10-29 12:35:27 +0000324 tempdir = six.text_type(tempfile.mkdtemp(prefix=u'trace_inputs'))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400325 try:
326 tempdir = file_path.get_native_path_case(tempdir)
327 basename = 'foo.txt'
328 filename = basename + ':Zone.Identifier'
329 filepath = os.path.join(tempdir, filename)
330 open(filepath, 'w').close()
331 self.assertEqual(filepath, file_path.get_native_path_case(filepath))
332 data_suffix = ':$DATA'
333 self.assertEqual(
334 filepath + data_suffix,
335 file_path.get_native_path_case(filepath + data_suffix))
336
337 open(filepath + '$DATA', 'w').close()
338 self.assertEqual(
339 filepath + data_suffix,
340 file_path.get_native_path_case(filepath + data_suffix))
341 # Ensure the ADS weren't created as separate file. You love NTFS, don't
342 # you?
maruel4e732992015-10-16 10:17:21 -0700343 self.assertEqual([basename], fs.listdir(tempdir))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400344 finally:
maruel4b14f042015-10-06 12:08:08 -0700345 file_path.rmtree(tempdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400346
Junji Watanabedff19e62021-08-10 02:22:11 +0000347 def test_rmtree_win(self):
348 root, _, files = self._make_tree()
349
350 # Emulate fs.rmtree() permission error.
351 can_delete = set()
352
353 def fs_rmtree_mock(_path, onerror):
354 for f in files:
355 if f not in can_delete:
356 onerror(None, None, (None, None, None))
357
358 def chmod_mock(path, _mode):
359 can_delete.add(path)
360
361 self.mock(fs, 'rmtree', fs_rmtree_mock)
362 self.mock(fs, 'chmod', chmod_mock)
363
364 file_path.rmtree(root)
365
Junji Watanabed94a5ab2021-08-06 23:03:29 +0000366 def test_rmtree_outliving_processes(self):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400367 # Mock our sleep for faster test case execution.
368 sleeps = []
369 self.mock(time, 'sleep', sleeps.append)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000370 self.mock(sys, 'stderr', io.StringIO())
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400371
372 # Open a child process, so the file is locked.
373 subdir = os.path.join(self.tempdir, 'to_be_deleted')
maruel4e732992015-10-16 10:17:21 -0700374 fs.mkdir(subdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400375 script = 'import time; open(\'a\', \'w\'); time.sleep(60)'
376 proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir)
377 try:
378 # Wait until the file exist.
maruel4e732992015-10-16 10:17:21 -0700379 while not fs.isfile(os.path.join(subdir, 'a')):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400380 self.assertEqual(None, proc.poll())
381 file_path.rmtree(subdir)
Junji Watanabed94a5ab2021-08-06 23:03:29 +0000382 self.assertEqual([4, 2], sleeps)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400383 # sys.stderr.getvalue() would return a fair amount of output but it is
384 # not completely deterministic so we're not testing it here.
385 finally:
386 proc.wait()
387
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500388 def test_filter_processes_dir_win(self):
389 python_dir = os.path.dirname(sys.executable)
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000390 processes = file_path._filter_processes_dir_win(
391 file_path._enum_processes_win(), python_dir)
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500392 self.assertTrue(processes)
393 proc_names = [proc.ExecutablePath for proc in processes]
394 # Try to find at least one python process.
395 self.assertTrue(
396 any(proc == sys.executable for proc in proc_names), proc_names)
397
398 def test_filter_processes_tree_win(self):
399 # Create a grand-child.
400 script = (
401 'import subprocess,sys;'
402 'proc = subprocess.Popen('
403 '[sys.executable, \'-u\', \'-c\', \'import time; print(1); '
404 'time.sleep(60)\'], stdout=subprocess.PIPE); '
405 # Signal grand child is ready.
406 'print(proc.stdout.read(1)); '
407 # Wait for parent to have completed the test.
408 'sys.stdin.read(1); '
409 'proc.kill()'
410 )
411 proc = subprocess.Popen(
412 [sys.executable, '-u', '-c', script],
413 stdin=subprocess.PIPE,
414 stdout=subprocess.PIPE)
415 try:
416 proc.stdout.read(1)
417 processes = file_path.filter_processes_tree_win(
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000418 file_path._enum_processes_win())
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500419 self.assertEqual(3, len(processes), processes)
420 proc.stdin.write('a')
421 proc.wait()
422 except Exception:
423 proc.kill()
424 finally:
425 proc.wait()
426
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000427 if sys.platform != 'win32':
428 def test_symlink(self):
429 # This test will fail if the checkout is in a symlink.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000430 actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR)
431 expected = (test_env.CLIENT_DIR, None, None)
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000432 self.assertEqual(expected, actual)
433
434 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000435 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
436 expected = (os.path.join(test_env.TESTS_DIR, 'trace_inputs'), None, None)
437 self.assertEqual(expected, actual)
438
439 actual = file_path.split_at_symlink(
440 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000441 expected = (
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000442 os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000443 self.assertEqual(expected, actual)
444
445 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000446 test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000447 expected = (
448 os.path.join('tests', 'trace_inputs'), 'files2', '')
449 self.assertEqual(expected, actual)
450 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000451 test_env.CLIENT_DIR,
452 os.path.join('tests', 'trace_inputs', 'files2', 'bar'))
453 expected = (os.path.join('tests', 'trace_inputs'), 'files2', '/bar')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000454 self.assertEqual(expected, actual)
455
456 def test_native_case_symlink_right_case(self):
457 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000458 os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000459 self.assertEqual('trace_inputs', os.path.basename(actual))
460
461 # Make sure the symlink is not resolved.
462 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000463 os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000464 self.assertEqual('files2', os.path.basename(actual))
465
maruel9cdd7612015-12-02 13:40:52 -0800466 else:
Junji Watanabee1d6df22020-11-12 08:13:23 +0000467
maruel9cdd7612015-12-02 13:40:52 -0800468 def test_undeleteable_chmod(self):
469 # Create a file and a directory with an empty ACL. Then try to delete it.
470 dirpath = os.path.join(self.tempdir, 'd')
471 filepath = os.path.join(dirpath, 'f')
472 os.mkdir(dirpath)
473 with open(filepath, 'w') as f:
474 f.write('hi')
475 os.chmod(filepath, 0)
476 os.chmod(dirpath, 0)
477 file_path.rmtree(dirpath)
478
479 def test_undeleteable_owner(self):
480 # Create a file and a directory with an empty ACL. Then try to delete it.
481 dirpath = os.path.join(self.tempdir, 'd')
482 filepath = os.path.join(dirpath, 'f')
483 os.mkdir(dirpath)
484 with open(filepath, 'w') as f:
485 f.write('hi')
486 import win32security
487 user, _domain, _type = win32security.LookupAccountName(
488 '', getpass.getuser())
489 sd = win32security.SECURITY_DESCRIPTOR()
490 sd.Initialize()
491 sd.SetSecurityDescriptorOwner(user, False)
492 # Create an empty DACL, which removes all rights.
493 dacl = win32security.ACL()
494 dacl.Initialize()
495 sd.SetSecurityDescriptorDacl(1, dacl, 0)
496 win32security.SetFileSecurity(
497 fs.extend(filepath), win32security.DACL_SECURITY_INFORMATION, sd)
498 win32security.SetFileSecurity(
499 fs.extend(dirpath), win32security.DACL_SECURITY_INFORMATION, sd)
500 file_path.rmtree(dirpath)
501
Takuto Ikuta995da062021-03-17 05:01:59 +0000502 def _check_get_recursive_size(self, symlink='symlink'):
503 # Test that _get_recursive_size calculates file size recursively.
504 with open(os.path.join(self.tempdir, '1'), 'w') as f:
505 f.write('0')
506 self.assertEqual(file_path.get_recursive_size(self.tempdir), 1)
507
508 with open(os.path.join(self.tempdir, '2'), 'w') as f:
509 f.write('01')
510 self.assertEqual(file_path.get_recursive_size(self.tempdir), 3)
511
512 nested_dir = os.path.join(self.tempdir, 'dir1', 'dir2')
513 os.makedirs(nested_dir)
514 with open(os.path.join(nested_dir, '4'), 'w') as f:
515 f.write('0123')
516 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
517
518 symlink_dir = os.path.join(self.tempdir, 'symlink_dir')
519 symlink_file = os.path.join(self.tempdir, 'symlink_file')
520 if symlink == 'symlink':
521
522 if sys.platform == 'win32':
523 subprocess.check_call('cmd /c mklink /d %s %s' %
524 (symlink_dir, nested_dir))
525 subprocess.check_call('cmd /c mklink %s %s' %
526 (symlink_file, os.path.join(self.tempdir, '1')))
527 else:
528 os.symlink(nested_dir, symlink_dir)
529 os.symlink(os.path.join(self.tempdir, '1'), symlink_file)
530
531 elif symlink == 'junction':
532 # junction should be ignored.
533 subprocess.check_call('cmd /c mklink /j %s %s' %
534 (symlink_dir, nested_dir))
535
536 # This is invalid junction, junction can be made only for directory.
537 subprocess.check_call('cmd /c mklink /j %s %s' %
538 (symlink_file, os.path.join(self.tempdir, '1')))
539 elif symlink == 'hardlink':
540 # hardlink can be made only for file.
541 subprocess.check_call('cmd /c mklink /h %s %s' %
542 (symlink_file, os.path.join(self.tempdir, '1')))
543 else:
544 assert False, ("symlink should be one of symlink, "
545 "junction or hardlink, but: %s" % symlink)
546
547 if symlink == 'hardlink':
548 # hardlinked file is double counted.
549 self.assertEqual(file_path.get_recursive_size(self.tempdir), 8)
550 else:
551 # symlink and junction should be ignored.
552 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
553
554 def test_get_recursive_size(self):
555 self._check_get_recursive_size()
556
557 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
558 def test_get_recursive_size_win_junction(self):
559 self._check_get_recursive_size(symlink='junction')
560
561 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
562 def test_get_recursive_size_win_hardlink(self):
563 self._check_get_recursive_size(symlink='hardlink')
564
565 @unittest.skipIf(sys.platform == 'win32', 'non-Windows specific')
566 def test_get_recursive_size_scandir_on_non_win(self):
567 # Test scandir implementation on non-windows.
568 self.mock(file_path, '_use_scandir', lambda: True)
569 self._check_get_recursive_size()
570
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000571
572if __name__ == '__main__':
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000573 test_env.main()