Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 2 | # coding=utf-8 |
maruel | ea586f3 | 2016-04-05 11:11:33 -0700 | [diff] [blame] | 3 | # Copyright 2013 The LUCI Authors. All rights reserved. |
maruel | f1f5e2a | 2016-05-25 17:10:39 -0700 | [diff] [blame] | 4 | # 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 6 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 7 | import getpass |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 8 | import io |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 9 | import os |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 10 | import subprocess |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 11 | import sys |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 12 | import tempfile |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 13 | import time |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame] | 14 | import unittest |
| 15 | |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 16 | # Mutates sys.path. |
| 17 | import test_env |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 18 | |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 19 | # third_party/ |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 20 | from depot_tools import auto_stub |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 21 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 22 | from utils import file_path |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 23 | from utils import fs |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame] | 24 | from utils import subprocess42 |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 25 | |
| 26 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 27 | def write_content(filepath, content): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 28 | with fs.open(filepath, 'wb') as f: |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 29 | f.write(content) |
| 30 | |
| 31 | |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 32 | class FilePathTest(auto_stub.TestCase): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 33 | def setUp(self): |
| 34 | super(FilePathTest, self).setUp() |
| 35 | self._tempdir = None |
| 36 | |
| 37 | def tearDown(self): |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 38 | try: |
| 39 | if self._tempdir: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 40 | for dirpath, dirnames, filenames in fs.walk( |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 41 | 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 Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 49 | |
| 50 | @property |
| 51 | def tempdir(self): |
| 52 | if not self._tempdir: |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 53 | self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 54 | return self._tempdir |
| 55 | |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 56 | def test_atomic_replace_new_file(self): |
| 57 | path = os.path.join(self.tempdir, 'new_file') |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 58 | file_path.atomic_replace(path, b'blah') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 59 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 60 | self.assertEqual(b'blah', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 61 | 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 Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 66 | f.write(b'existing body') |
| 67 | file_path.atomic_replace(path, b'new body') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 68 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 69 | self.assertEqual(b'new body', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 70 | self.assertEqual([u'existing_file'], os.listdir(self.tempdir)) |
| 71 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 72 | def assertFileMode(self, filepath, mode, umask=None): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 73 | umask = test_env.umask() if umask is None else umask |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 74 | actual = fs.stat(filepath).st_mode |
Junji Watanabe | ab2102a | 2022-01-12 01:44:04 +0000 | [diff] [blame] | 75 | expected = mode & ~umask # pylint: disable=invalid-unary-operand-type |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 76 | 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 83 | self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 84 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 85 | def test_native_case_end_with_os_path_sep(self): |
| 86 | # Make sure the trailing os.path.sep is kept. |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 87 | path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 88 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 91 | path = file_path.get_native_path_case(test_env.CLIENT_DIR + os.path.sep) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 92 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 101 | path = file_path.get_native_path_case(test_env.CLIENT_DIR) + os.path.sep |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 102 | self.assertEqual(file_path.get_native_path_case(path), path) |
| 103 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 104 | 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 108 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 109 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 110 | file_path.set_read_only(dir_foo, False) |
| 111 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 112 | self.assertFileMode(dir_foo, 0o40777) |
| 113 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 114 | if sys.platform == 'win32': |
| 115 | # On Windows, a read-only file can't be deleted. |
| 116 | with self.assertRaises(OSError): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 117 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 118 | else: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 119 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 120 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 125 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 126 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 127 | file_path.set_read_only(dir_foo, True) |
| 128 | file_path.set_read_only(file_bar, False) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 129 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 130 | self.assertFileMode(file_bar, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 131 | 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. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 139 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 140 | else: |
| 141 | with self.assertRaises(OSError): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 142 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 143 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 148 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 149 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 150 | file_path.set_read_only(dir_foo, True) |
| 151 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 152 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 153 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 154 | with self.assertRaises(OSError): |
| 155 | # It fails for different reason depending on the OS. See the test cases |
| 156 | # above. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 157 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 158 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 165 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 166 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 167 | file_path.hardlink(file_bar, file_link) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 168 | self.assertFileMode(file_bar, 0o100666) |
| 169 | self.assertFileMode(file_link, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 170 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 171 | self.assertMaskedFileMode(file_bar, 0o100444) |
| 172 | self.assertMaskedFileMode(file_link, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 173 | # 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 Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 178 | def test_ensure_tree(self): |
| 179 | dir_foo = os.path.join(self.tempdir, 'foo') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 180 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 181 | |
| 182 | self.assertTrue(os.path.isdir(dir_foo)) |
| 183 | |
| 184 | # Do not raise OSError with errno.EEXIST |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 185 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 186 | |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame] | 187 | 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 Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 249 | def test_rmtree_unicode(self): |
| 250 | subdir = os.path.join(self.tempdir, 'hi') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 251 | fs.mkdir(subdir) |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 252 | filepath = os.path.join( |
| 253 | subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 254 | with fs.open(filepath, 'wb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 255 | f.write(b'hi') |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 256 | # In particular, it fails when the input argument is a str. |
| 257 | file_path.rmtree(str(subdir)) |
| 258 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 259 | if sys.platform == 'darwin': |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame] | 260 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 261 | def test_native_case_symlink_wrong_case(self): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 262 | base_dir = file_path.get_native_path_case(test_env.TESTS_DIR) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 263 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 284 | if sys.platform in ('darwin', 'win32'): |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame] | 285 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 286 | 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) |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 311 | self.assertFalse(fs.exists(path)) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 312 | 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 Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 319 | if sys.platform == 'win32': |
| 320 | def test_native_case_alternate_datastream(self): |
| 321 | # Create the file manually, since tempfile doesn't support ADS. |
Junji Watanabe | 458a227 | 2022-01-12 06:59:04 +0000 | [diff] [blame] | 322 | tempdir = tempfile.mkdtemp(prefix=u'trace_inputs') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 323 | 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? |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 341 | self.assertEqual([basename], fs.listdir(tempdir)) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 342 | finally: |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 343 | file_path.rmtree(tempdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 344 | |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame] | 345 | 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 Watanabe | d94a5ab | 2021-08-06 23:03:29 +0000 | [diff] [blame] | 364 | def test_rmtree_outliving_processes(self): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 365 | # Mock our sleep for faster test case execution. |
| 366 | sleeps = [] |
| 367 | self.mock(time, 'sleep', sleeps.append) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 368 | self.mock(sys, 'stderr', io.StringIO()) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 369 | |
| 370 | # Open a child process, so the file is locked. |
| 371 | subdir = os.path.join(self.tempdir, 'to_be_deleted') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 372 | fs.mkdir(subdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 373 | 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. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 377 | while not fs.isfile(os.path.join(subdir, 'a')): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 378 | self.assertEqual(None, proc.poll()) |
| 379 | file_path.rmtree(subdir) |
Junji Watanabe | d94a5ab | 2021-08-06 23:03:29 +0000 | [diff] [blame] | 380 | self.assertEqual([4, 2], sleeps) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 381 | # 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 Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 386 | def test_filter_processes_dir_win(self): |
| 387 | python_dir = os.path.dirname(sys.executable) |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 388 | processes = file_path._filter_processes_dir_win( |
| 389 | file_path._enum_processes_win(), python_dir) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 390 | 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 Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 416 | file_path._enum_processes_win()) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 417 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 425 | if sys.platform != 'win32': |
| 426 | def test_symlink(self): |
| 427 | # This test will fail if the checkout is in a symlink. |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 428 | actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR) |
| 429 | expected = (test_env.CLIENT_DIR, None, None) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 430 | self.assertEqual(expected, actual) |
| 431 | |
| 432 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 433 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 439 | expected = ( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 440 | os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 441 | self.assertEqual(expected, actual) |
| 442 | |
| 443 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 444 | test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 445 | expected = ( |
| 446 | os.path.join('tests', 'trace_inputs'), 'files2', '') |
| 447 | self.assertEqual(expected, actual) |
| 448 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 449 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 452 | self.assertEqual(expected, actual) |
| 453 | |
| 454 | def test_native_case_symlink_right_case(self): |
| 455 | actual = file_path.get_native_path_case( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 456 | os.path.join(test_env.TESTS_DIR, 'trace_inputs')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 457 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 461 | os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 462 | self.assertEqual('files2', os.path.basename(actual)) |
| 463 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 464 | else: |
Junji Watanabe | e1d6df2 | 2020-11-12 08:13:23 +0000 | [diff] [blame] | 465 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 466 | 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 Ikuta | 995da06 | 2021-03-17 05:01:59 +0000 | [diff] [blame] | 500 | 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 Watanabe | 9baeedc | 2021-09-21 04:52:09 +0000 | [diff] [blame] | 516 | # 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 Ikuta | 995da06 | 2021-03-17 05:01:59 +0000 | [diff] [blame] | 521 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 568 | |
| 569 | if __name__ == '__main__': |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 570 | test_env.main() |