blob: d4c6c8445787ade592b8150d08e84fa4d02c0bd5 [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
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000016# Mutates sys.path.
17import test_env
maruel@chromium.org561d4b22013-09-26 21:08:08 +000018
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000019# third_party/
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -050020from depot_tools import auto_stub
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000021
maruel@chromium.org561d4b22013-09-26 21:08:08 +000022from utils import file_path
maruel4e732992015-10-16 10:17:21 -070023from utils import fs
Junji Watanabedff19e62021-08-10 02:22:11 +000024from utils import subprocess42
maruel@chromium.org561d4b22013-09-26 21:08:08 +000025
26
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040027def write_content(filepath, content):
maruel4e732992015-10-16 10:17:21 -070028 with fs.open(filepath, 'wb') as f:
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040029 f.write(content)
30
31
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -050032class FilePathTest(auto_stub.TestCase):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040033 def setUp(self):
34 super(FilePathTest, self).setUp()
35 self._tempdir = None
36
37 def tearDown(self):
maruel4b14f042015-10-06 12:08:08 -070038 try:
39 if self._tempdir:
maruel4e732992015-10-16 10:17:21 -070040 for dirpath, dirnames, filenames in fs.walk(
maruel4b14f042015-10-06 12:08:08 -070041 self._tempdir, topdown=True):
42 for filename in filenames:
43 file_path.set_read_only(os.path.join(dirpath, filename), False)
44 for dirname in dirnames:
45 file_path.set_read_only(os.path.join(dirpath, dirname), False)
46 file_path.rmtree(self._tempdir)
47 finally:
48 super(FilePathTest, self).tearDown()
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040049
50 @property
51 def tempdir(self):
52 if not self._tempdir:
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +000053 self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040054 return self._tempdir
55
vadimshe42aeba2016-06-03 12:32:21 -070056 def test_atomic_replace_new_file(self):
57 path = os.path.join(self.tempdir, 'new_file')
Takuto Ikutaf48103c2019-10-24 05:23:19 +000058 file_path.atomic_replace(path, b'blah')
vadimshe42aeba2016-06-03 12:32:21 -070059 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000060 self.assertEqual(b'blah', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070061 self.assertEqual([u'new_file'], os.listdir(self.tempdir))
62
63 def test_atomic_replace_existing_file(self):
64 path = os.path.join(self.tempdir, 'existing_file')
65 with open(path, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000066 f.write(b'existing body')
67 file_path.atomic_replace(path, b'new body')
vadimshe42aeba2016-06-03 12:32:21 -070068 with open(path, 'rb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +000069 self.assertEqual(b'new body', f.read())
vadimshe42aeba2016-06-03 12:32:21 -070070 self.assertEqual([u'existing_file'], os.listdir(self.tempdir))
71
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040072 def assertFileMode(self, filepath, mode, umask=None):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000073 umask = test_env.umask() if umask is None else umask
maruel4e732992015-10-16 10:17:21 -070074 actual = fs.stat(filepath).st_mode
Junji Watanabeab2102a2022-01-12 01:44:04 +000075 expected = mode & ~umask # pylint: disable=invalid-unary-operand-type
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040076 self.assertEqual(
77 expected,
78 actual,
79 (filepath, oct(expected), oct(actual), oct(umask)))
80
81 def assertMaskedFileMode(self, filepath, mode):
82 """It's usually when the file was first marked read only."""
Takuto Ikuta7669fb52019-10-23 06:07:30 +000083 self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040084
maruel@chromium.org561d4b22013-09-26 21:08:08 +000085 def test_native_case_end_with_os_path_sep(self):
86 # Make sure the trailing os.path.sep is kept.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +000087 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +000088 self.assertEqual(file_path.get_native_path_case(path), path)
89
90 def test_native_case_end_with_dot_os_path_sep(self):
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(
93 file_path.get_native_path_case(path + '.' + os.path.sep),
94 path)
95
96 def test_native_case_non_existing(self):
97 # Make sure it doesn't throw on non-existing files.
98 non_existing = 'trace_input_test_this_file_should_not_exist'
99 path = os.path.expanduser('~/' + non_existing)
100 self.assertFalse(os.path.exists(path))
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000101 path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000102 self.assertEqual(file_path.get_native_path_case(path), path)
103
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400104 def test_delete_wd_rf(self):
105 # Confirms that a RO file in a RW directory can be deleted on non-Windows.
106 dir_foo = os.path.join(self.tempdir, 'foo')
107 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000108 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000109 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400110 file_path.set_read_only(dir_foo, False)
111 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000112 self.assertFileMode(dir_foo, 0o40777)
113 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400114 if sys.platform == 'win32':
115 # On Windows, a read-only file can't be deleted.
116 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700117 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400118 else:
maruel4e732992015-10-16 10:17:21 -0700119 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400120
121 def test_delete_rd_wf(self):
122 # Confirms that a Rw file in a RO directory can be deleted on Windows only.
123 dir_foo = os.path.join(self.tempdir, 'foo')
124 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000125 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000126 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400127 file_path.set_read_only(dir_foo, True)
128 file_path.set_read_only(file_bar, False)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000129 self.assertMaskedFileMode(dir_foo, 0o40555)
130 self.assertFileMode(file_bar, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400131 if sys.platform == 'win32':
132 # A read-only directory has a convoluted meaning on Windows, it means that
133 # the directory is "personalized". This is used as a signal by Windows
134 # Explorer to tell it to look into the directory for desktop.ini.
135 # See http://support.microsoft.com/kb/326549 for more details.
136 # As such, it is important to not try to set the read-only bit on
137 # directories on Windows since it has no effect other than trigger
138 # Windows Explorer to look for desktop.ini, which is unnecessary.
maruel4e732992015-10-16 10:17:21 -0700139 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400140 else:
141 with self.assertRaises(OSError):
maruel4e732992015-10-16 10:17:21 -0700142 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400143
144 def test_delete_rd_rf(self):
145 # Confirms that a RO file in a RO directory can't be deleted.
146 dir_foo = os.path.join(self.tempdir, 'foo')
147 file_bar = os.path.join(dir_foo, 'bar')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000148 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000149 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400150 file_path.set_read_only(dir_foo, True)
151 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000152 self.assertMaskedFileMode(dir_foo, 0o40555)
153 self.assertMaskedFileMode(file_bar, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400154 with self.assertRaises(OSError):
155 # It fails for different reason depending on the OS. See the test cases
156 # above.
maruel4e732992015-10-16 10:17:21 -0700157 fs.remove(file_bar)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400158
159 def test_hard_link_mode(self):
160 # Creates a hard link, see if the file mode changed on the node or the
161 # directory entry.
162 dir_foo = os.path.join(self.tempdir, 'foo')
163 file_bar = os.path.join(dir_foo, 'bar')
164 file_link = os.path.join(dir_foo, 'link')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000165 fs.mkdir(dir_foo, 0o777)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000166 write_content(file_bar, b'bar')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400167 file_path.hardlink(file_bar, file_link)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000168 self.assertFileMode(file_bar, 0o100666)
169 self.assertFileMode(file_link, 0o100666)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400170 file_path.set_read_only(file_bar, True)
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000171 self.assertMaskedFileMode(file_bar, 0o100444)
172 self.assertMaskedFileMode(file_link, 0o100444)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400173 # This is bad news for Windows; on Windows, the file must be writeable to be
174 # deleted, but the file node is modified. This means that every hard links
175 # must be reset to be read-only after deleting one of the hard link
176 # directory entry.
177
Takuto Ikutae3f70382019-04-03 14:52:06 +0000178 def test_ensure_tree(self):
179 dir_foo = os.path.join(self.tempdir, 'foo')
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000180 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000181
182 self.assertTrue(os.path.isdir(dir_foo))
183
184 # Do not raise OSError with errno.EEXIST
Takuto Ikuta7669fb52019-10-23 06:07:30 +0000185 file_path.ensure_tree(dir_foo, 0o777)
Takuto Ikutae3f70382019-04-03 14:52:06 +0000186
Junji Watanabedff19e62021-08-10 02:22:11 +0000187 def _make_tree(self):
188 root = os.path.join(self.tempdir, 'root')
189 child_dir = os.path.join(root, 'child')
190 grand_child_dir = os.path.join(child_dir, 'grand_child')
191 dirs = [root, child_dir, grand_child_dir]
192 os.makedirs(grand_child_dir)
193 files = [
194 os.path.join(root, 'file1'),
195 os.path.join(child_dir, 'file2'),
196 os.path.join(grand_child_dir, 'file3'),
197 ]
198 for f in files:
199 open(f, 'w').close()
200 return root, dirs, files
201
202 @unittest.skipIf(sys.platform == 'win32', 'posix only')
203 def test_rmtree(self):
204 root, dirs, _ = self._make_tree()
205
206 # Emulate fs.rmtree() permission error.
207 can_delete = set()
208
209 def fs_rmtree_mock(_path, onerror):
210 for d in dirs:
211 if d not in can_delete:
212 onerror(None, None, (None, None, None))
213
214 def chmod_mock(path, _mode):
215 can_delete.add(path)
216
217 self.mock(fs, 'rmtree', fs_rmtree_mock)
218 if hasattr(os, 'lchmod'):
219 self.mock(fs, 'lchmod', chmod_mock)
220 else:
221 self.mock(fs, 'chmod', chmod_mock)
222
223 file_path.rmtree(root)
224
225 @unittest.skipIf(sys.platform == 'win32', 'posix only')
226 def test_rmtree_with_sudo_chmod(self):
227 root, dirs, _ = self._make_tree()
228
229 # Emulate fs.rmtree() permission error.
230 can_delete = set()
231
232 def fs_rmtree_mock(_path, onerror):
233 for d in dirs:
234 if d not in can_delete:
235 onerror(None, None, (None, None, None))
236
237 self.mock(fs, 'rmtree', fs_rmtree_mock)
238
239 # pylint: disable=unused-argument
240 def subprocess_mock(cmd, stdin=None):
241 path = cmd[4]
242 can_delete.add(path)
243
244 self.mock(file_path, 'set_read_only_swallow', lambda *_: OSError('error'))
245 self.mock(subprocess42, 'call', subprocess_mock)
246
247 file_path.rmtree(root)
248
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500249 def test_rmtree_unicode(self):
250 subdir = os.path.join(self.tempdir, 'hi')
maruel4e732992015-10-16 10:17:21 -0700251 fs.mkdir(subdir)
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500252 filepath = os.path.join(
253 subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629')
maruel4e732992015-10-16 10:17:21 -0700254 with fs.open(filepath, 'wb') as f:
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000255 f.write(b'hi')
Marc-Antoine Ruel0a795bd2015-01-16 20:32:10 -0500256 # In particular, it fails when the input argument is a str.
257 file_path.rmtree(str(subdir))
258
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400259 if sys.platform == 'darwin':
Junji Watanabe16d51522020-07-01 01:59:07 +0000260
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400261 def test_native_case_symlink_wrong_case(self):
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000262 base_dir = file_path.get_native_path_case(test_env.TESTS_DIR)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400263 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs')
264 actual = file_path.get_native_path_case(trace_inputs_dir)
265 self.assertEqual(trace_inputs_dir, actual)
266
267 # Make sure the symlink is not resolved.
268 data = os.path.join(trace_inputs_dir, 'Files2')
269 actual = file_path.get_native_path_case(data)
270 self.assertEqual(
271 os.path.join(trace_inputs_dir, 'files2'), actual)
272
273 data = os.path.join(trace_inputs_dir, 'Files2', '')
274 actual = file_path.get_native_path_case(data)
275 self.assertEqual(
276 os.path.join(trace_inputs_dir, 'files2', ''), actual)
277
278 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py')
279 actual = file_path.get_native_path_case(data)
280 # TODO(maruel): Should be child1.py.
281 self.assertEqual(
282 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual)
283
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000284 if sys.platform in ('darwin', 'win32'):
Junji Watanabe16d51522020-07-01 01:59:07 +0000285
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000286 def test_native_case_not_sensitive(self):
287 # The home directory is almost guaranteed to have mixed upper/lower case
288 # letters on both Windows and OSX.
289 # This test also ensures that the output is independent on the input
290 # string case.
291 path = os.path.expanduser(u'~')
292 self.assertTrue(os.path.isdir(path))
293 path = path.replace('/', os.path.sep)
294 if sys.platform == 'win32':
295 # Make sure the drive letter is upper case for consistency.
296 path = path[0].upper() + path[1:]
297 # This test assumes the variable is in the native path case on disk, this
298 # should be the case. Verify this assumption:
299 self.assertEqual(path, file_path.get_native_path_case(path))
300 self.assertEqual(
301 file_path.get_native_path_case(path.lower()),
302 file_path.get_native_path_case(path.upper()))
303
304 def test_native_case_not_sensitive_non_existent(self):
305 # This test also ensures that the output is independent on the input
306 # string case.
307 non_existing = os.path.join(
308 'trace_input_test_this_dir_should_not_exist', 'really not', '')
309 path = os.path.expanduser(os.path.join(u'~', non_existing))
310 path = path.replace('/', os.path.sep)
maruel4e732992015-10-16 10:17:21 -0700311 self.assertFalse(fs.exists(path))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000312 lower = file_path.get_native_path_case(path.lower())
313 upper = file_path.get_native_path_case(path.upper())
314 # Make sure non-existing element is not modified:
315 self.assertTrue(lower.endswith(non_existing.lower()))
316 self.assertTrue(upper.endswith(non_existing.upper()))
317 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)])
318
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400319 if sys.platform == 'win32':
320 def test_native_case_alternate_datastream(self):
321 # Create the file manually, since tempfile doesn't support ADS.
Junji Watanabe458a2272022-01-12 06:59:04 +0000322 tempdir = tempfile.mkdtemp(prefix=u'trace_inputs')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400323 try:
324 tempdir = file_path.get_native_path_case(tempdir)
325 basename = 'foo.txt'
326 filename = basename + ':Zone.Identifier'
327 filepath = os.path.join(tempdir, filename)
328 open(filepath, 'w').close()
329 self.assertEqual(filepath, file_path.get_native_path_case(filepath))
330 data_suffix = ':$DATA'
331 self.assertEqual(
332 filepath + data_suffix,
333 file_path.get_native_path_case(filepath + data_suffix))
334
335 open(filepath + '$DATA', 'w').close()
336 self.assertEqual(
337 filepath + data_suffix,
338 file_path.get_native_path_case(filepath + data_suffix))
339 # Ensure the ADS weren't created as separate file. You love NTFS, don't
340 # you?
maruel4e732992015-10-16 10:17:21 -0700341 self.assertEqual([basename], fs.listdir(tempdir))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400342 finally:
maruel4b14f042015-10-06 12:08:08 -0700343 file_path.rmtree(tempdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400344
Junji Watanabedff19e62021-08-10 02:22:11 +0000345 def test_rmtree_win(self):
346 root, _, files = self._make_tree()
347
348 # Emulate fs.rmtree() permission error.
349 can_delete = set()
350
351 def fs_rmtree_mock(_path, onerror):
352 for f in files:
353 if f not in can_delete:
354 onerror(None, None, (None, None, None))
355
356 def chmod_mock(path, _mode):
357 can_delete.add(path)
358
359 self.mock(fs, 'rmtree', fs_rmtree_mock)
360 self.mock(fs, 'chmod', chmod_mock)
361
362 file_path.rmtree(root)
363
Junji Watanabed94a5ab2021-08-06 23:03:29 +0000364 def test_rmtree_outliving_processes(self):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400365 # Mock our sleep for faster test case execution.
366 sleeps = []
367 self.mock(time, 'sleep', sleeps.append)
Takuto Ikutaf48103c2019-10-24 05:23:19 +0000368 self.mock(sys, 'stderr', io.StringIO())
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400369
370 # Open a child process, so the file is locked.
371 subdir = os.path.join(self.tempdir, 'to_be_deleted')
maruel4e732992015-10-16 10:17:21 -0700372 fs.mkdir(subdir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400373 script = 'import time; open(\'a\', \'w\'); time.sleep(60)'
374 proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir)
375 try:
376 # Wait until the file exist.
maruel4e732992015-10-16 10:17:21 -0700377 while not fs.isfile(os.path.join(subdir, 'a')):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400378 self.assertEqual(None, proc.poll())
379 file_path.rmtree(subdir)
Junji Watanabed94a5ab2021-08-06 23:03:29 +0000380 self.assertEqual([4, 2], sleeps)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400381 # sys.stderr.getvalue() would return a fair amount of output but it is
382 # not completely deterministic so we're not testing it here.
383 finally:
384 proc.wait()
385
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500386 def test_filter_processes_dir_win(self):
387 python_dir = os.path.dirname(sys.executable)
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000388 processes = file_path._filter_processes_dir_win(
389 file_path._enum_processes_win(), python_dir)
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500390 self.assertTrue(processes)
391 proc_names = [proc.ExecutablePath for proc in processes]
392 # Try to find at least one python process.
393 self.assertTrue(
394 any(proc == sys.executable for proc in proc_names), proc_names)
395
396 def test_filter_processes_tree_win(self):
397 # Create a grand-child.
398 script = (
399 'import subprocess,sys;'
400 'proc = subprocess.Popen('
401 '[sys.executable, \'-u\', \'-c\', \'import time; print(1); '
402 'time.sleep(60)\'], stdout=subprocess.PIPE); '
403 # Signal grand child is ready.
404 'print(proc.stdout.read(1)); '
405 # Wait for parent to have completed the test.
406 'sys.stdin.read(1); '
407 'proc.kill()'
408 )
409 proc = subprocess.Popen(
410 [sys.executable, '-u', '-c', script],
411 stdin=subprocess.PIPE,
412 stdout=subprocess.PIPE)
413 try:
414 proc.stdout.read(1)
415 processes = file_path.filter_processes_tree_win(
Marc-Antoine Ruel0eb2eb22019-01-29 21:00:16 +0000416 file_path._enum_processes_win())
Marc-Antoine Ruela275b292014-11-25 15:17:21 -0500417 self.assertEqual(3, len(processes), processes)
418 proc.stdin.write('a')
419 proc.wait()
420 except Exception:
421 proc.kill()
422 finally:
423 proc.wait()
424
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000425 if sys.platform != 'win32':
426 def test_symlink(self):
427 # This test will fail if the checkout is in a symlink.
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000428 actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR)
429 expected = (test_env.CLIENT_DIR, None, None)
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000430 self.assertEqual(expected, actual)
431
432 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000433 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
434 expected = (os.path.join(test_env.TESTS_DIR, 'trace_inputs'), None, None)
435 self.assertEqual(expected, actual)
436
437 actual = file_path.split_at_symlink(
438 None, os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000439 expected = (
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000440 os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000441 self.assertEqual(expected, actual)
442
443 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000444 test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000445 expected = (
446 os.path.join('tests', 'trace_inputs'), 'files2', '')
447 self.assertEqual(expected, actual)
448 actual = file_path.split_at_symlink(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000449 test_env.CLIENT_DIR,
450 os.path.join('tests', 'trace_inputs', 'files2', 'bar'))
451 expected = (os.path.join('tests', 'trace_inputs'), 'files2', '/bar')
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000452 self.assertEqual(expected, actual)
453
454 def test_native_case_symlink_right_case(self):
455 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000456 os.path.join(test_env.TESTS_DIR, 'trace_inputs'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000457 self.assertEqual('trace_inputs', os.path.basename(actual))
458
459 # Make sure the symlink is not resolved.
460 actual = file_path.get_native_path_case(
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000461 os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2'))
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000462 self.assertEqual('files2', os.path.basename(actual))
463
maruel9cdd7612015-12-02 13:40:52 -0800464 else:
Junji Watanabee1d6df22020-11-12 08:13:23 +0000465
maruel9cdd7612015-12-02 13:40:52 -0800466 def test_undeleteable_chmod(self):
467 # Create a file and a directory with an empty ACL. Then try to delete it.
468 dirpath = os.path.join(self.tempdir, 'd')
469 filepath = os.path.join(dirpath, 'f')
470 os.mkdir(dirpath)
471 with open(filepath, 'w') as f:
472 f.write('hi')
473 os.chmod(filepath, 0)
474 os.chmod(dirpath, 0)
475 file_path.rmtree(dirpath)
476
477 def test_undeleteable_owner(self):
478 # Create a file and a directory with an empty ACL. Then try to delete it.
479 dirpath = os.path.join(self.tempdir, 'd')
480 filepath = os.path.join(dirpath, 'f')
481 os.mkdir(dirpath)
482 with open(filepath, 'w') as f:
483 f.write('hi')
484 import win32security
485 user, _domain, _type = win32security.LookupAccountName(
486 '', getpass.getuser())
487 sd = win32security.SECURITY_DESCRIPTOR()
488 sd.Initialize()
489 sd.SetSecurityDescriptorOwner(user, False)
490 # Create an empty DACL, which removes all rights.
491 dacl = win32security.ACL()
492 dacl.Initialize()
493 sd.SetSecurityDescriptorDacl(1, dacl, 0)
494 win32security.SetFileSecurity(
495 fs.extend(filepath), win32security.DACL_SECURITY_INFORMATION, sd)
496 win32security.SetFileSecurity(
497 fs.extend(dirpath), win32security.DACL_SECURITY_INFORMATION, sd)
498 file_path.rmtree(dirpath)
499
Takuto Ikuta995da062021-03-17 05:01:59 +0000500 def _check_get_recursive_size(self, symlink='symlink'):
501 # Test that _get_recursive_size calculates file size recursively.
502 with open(os.path.join(self.tempdir, '1'), 'w') as f:
503 f.write('0')
504 self.assertEqual(file_path.get_recursive_size(self.tempdir), 1)
505
506 with open(os.path.join(self.tempdir, '2'), 'w') as f:
507 f.write('01')
508 self.assertEqual(file_path.get_recursive_size(self.tempdir), 3)
509
510 nested_dir = os.path.join(self.tempdir, 'dir1', 'dir2')
511 os.makedirs(nested_dir)
512 with open(os.path.join(nested_dir, '4'), 'w') as f:
513 f.write('0123')
514 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
515
Junji Watanabe9baeedc2021-09-21 04:52:09 +0000516 # Add an unreadable directory.
517 secure_dir = os.path.join(self.tempdir, 'dir_secure')
518 os.makedirs(secure_dir, mode=0o000)
519 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
520
Takuto Ikuta995da062021-03-17 05:01:59 +0000521 symlink_dir = os.path.join(self.tempdir, 'symlink_dir')
522 symlink_file = os.path.join(self.tempdir, 'symlink_file')
523 if symlink == 'symlink':
524
525 if sys.platform == 'win32':
526 subprocess.check_call('cmd /c mklink /d %s %s' %
527 (symlink_dir, nested_dir))
528 subprocess.check_call('cmd /c mklink %s %s' %
529 (symlink_file, os.path.join(self.tempdir, '1')))
530 else:
531 os.symlink(nested_dir, symlink_dir)
532 os.symlink(os.path.join(self.tempdir, '1'), symlink_file)
533
534 elif symlink == 'junction':
535 # junction should be ignored.
536 subprocess.check_call('cmd /c mklink /j %s %s' %
537 (symlink_dir, nested_dir))
538
539 # This is invalid junction, junction can be made only for directory.
540 subprocess.check_call('cmd /c mklink /j %s %s' %
541 (symlink_file, os.path.join(self.tempdir, '1')))
542 elif symlink == 'hardlink':
543 # hardlink can be made only for file.
544 subprocess.check_call('cmd /c mklink /h %s %s' %
545 (symlink_file, os.path.join(self.tempdir, '1')))
546 else:
547 assert False, ("symlink should be one of symlink, "
548 "junction or hardlink, but: %s" % symlink)
549
550 if symlink == 'hardlink':
551 # hardlinked file is double counted.
552 self.assertEqual(file_path.get_recursive_size(self.tempdir), 8)
553 else:
554 # symlink and junction should be ignored.
555 self.assertEqual(file_path.get_recursive_size(self.tempdir), 7)
556
557 def test_get_recursive_size(self):
558 self._check_get_recursive_size()
559
560 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
561 def test_get_recursive_size_win_junction(self):
562 self._check_get_recursive_size(symlink='junction')
563
564 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific')
565 def test_get_recursive_size_win_hardlink(self):
566 self._check_get_recursive_size(symlink='hardlink')
567
maruel@chromium.org561d4b22013-09-26 21:08:08 +0000568
569if __name__ == '__main__':
Marc-Antoine Ruel76cfcee2019-04-01 23:16:36 +0000570 test_env.main()