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 | |
| 16 | import six |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 17 | |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 18 | # Mutates sys.path. |
| 19 | import test_env |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 20 | |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 21 | # third_party/ |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 22 | from depot_tools import auto_stub |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 23 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 24 | from utils import file_path |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 25 | from utils import fs |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame^] | 26 | from utils import subprocess42 |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 27 | |
| 28 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 29 | def write_content(filepath, content): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 30 | with fs.open(filepath, 'wb') as f: |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 31 | f.write(content) |
| 32 | |
| 33 | |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 34 | class FilePathTest(auto_stub.TestCase): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 35 | def setUp(self): |
| 36 | super(FilePathTest, self).setUp() |
| 37 | self._tempdir = None |
| 38 | |
| 39 | def tearDown(self): |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 40 | try: |
| 41 | if self._tempdir: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 42 | for dirpath, dirnames, filenames in fs.walk( |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 43 | 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 Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 51 | |
| 52 | @property |
| 53 | def tempdir(self): |
| 54 | if not self._tempdir: |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 55 | self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 56 | return self._tempdir |
| 57 | |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 58 | def test_atomic_replace_new_file(self): |
| 59 | path = os.path.join(self.tempdir, 'new_file') |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 60 | file_path.atomic_replace(path, b'blah') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 61 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 62 | self.assertEqual(b'blah', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 63 | 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 Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 68 | f.write(b'existing body') |
| 69 | file_path.atomic_replace(path, b'new body') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 70 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 71 | self.assertEqual(b'new body', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 72 | self.assertEqual([u'existing_file'], os.listdir(self.tempdir)) |
| 73 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 74 | def assertFileMode(self, filepath, mode, umask=None): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 75 | umask = test_env.umask() if umask is None else umask |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 76 | actual = fs.stat(filepath).st_mode |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 77 | 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 85 | self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 86 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 87 | def test_native_case_end_with_os_path_sep(self): |
| 88 | # Make sure the trailing os.path.sep is kept. |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 89 | 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] | 90 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 93 | 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] | 94 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 103 | 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] | 104 | self.assertEqual(file_path.get_native_path_case(path), path) |
| 105 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 106 | 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 110 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 111 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 112 | file_path.set_read_only(dir_foo, False) |
| 113 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 114 | self.assertFileMode(dir_foo, 0o40777) |
| 115 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 116 | if sys.platform == 'win32': |
| 117 | # On Windows, a read-only file can't be deleted. |
| 118 | with self.assertRaises(OSError): |
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 | else: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 121 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 122 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 127 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 128 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 129 | file_path.set_read_only(dir_foo, True) |
| 130 | file_path.set_read_only(file_bar, False) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 131 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 132 | self.assertFileMode(file_bar, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 133 | 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. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 141 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 142 | else: |
| 143 | with self.assertRaises(OSError): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 144 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 145 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 150 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 151 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 152 | file_path.set_read_only(dir_foo, True) |
| 153 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 154 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 155 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 156 | with self.assertRaises(OSError): |
| 157 | # It fails for different reason depending on the OS. See the test cases |
| 158 | # above. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 159 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 160 | |
| 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 Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 167 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 168 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 169 | file_path.hardlink(file_bar, file_link) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 170 | self.assertFileMode(file_bar, 0o100666) |
| 171 | self.assertFileMode(file_link, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 172 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 173 | self.assertMaskedFileMode(file_bar, 0o100444) |
| 174 | self.assertMaskedFileMode(file_link, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 175 | # 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 Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 180 | def test_ensure_tree(self): |
| 181 | dir_foo = os.path.join(self.tempdir, 'foo') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 182 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 183 | |
| 184 | self.assertTrue(os.path.isdir(dir_foo)) |
| 185 | |
| 186 | # Do not raise OSError with errno.EEXIST |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 187 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 188 | |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame^] | 189 | 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 Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 251 | def test_rmtree_unicode(self): |
| 252 | subdir = os.path.join(self.tempdir, 'hi') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 253 | fs.mkdir(subdir) |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 254 | filepath = os.path.join( |
| 255 | subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 256 | with fs.open(filepath, 'wb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 257 | f.write(b'hi') |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 258 | # In particular, it fails when the input argument is a str. |
| 259 | file_path.rmtree(str(subdir)) |
| 260 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 261 | if sys.platform == 'darwin': |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame] | 262 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 263 | def test_native_case_symlink_wrong_case(self): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 264 | 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] | 265 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 286 | if sys.platform in ('darwin', 'win32'): |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame] | 287 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 288 | 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) |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 313 | self.assertFalse(fs.exists(path)) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 314 | 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 Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 321 | if sys.platform == 'win32': |
| 322 | def test_native_case_alternate_datastream(self): |
| 323 | # Create the file manually, since tempfile doesn't support ADS. |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 324 | tempdir = six.text_type(tempfile.mkdtemp(prefix=u'trace_inputs')) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 325 | 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? |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 343 | self.assertEqual([basename], fs.listdir(tempdir)) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 344 | finally: |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 345 | file_path.rmtree(tempdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 346 | |
Junji Watanabe | dff19e6 | 2021-08-10 02:22:11 +0000 | [diff] [blame^] | 347 | 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 Watanabe | d94a5ab | 2021-08-06 23:03:29 +0000 | [diff] [blame] | 366 | def test_rmtree_outliving_processes(self): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 367 | # Mock our sleep for faster test case execution. |
| 368 | sleeps = [] |
| 369 | self.mock(time, 'sleep', sleeps.append) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 370 | self.mock(sys, 'stderr', io.StringIO()) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 371 | |
| 372 | # Open a child process, so the file is locked. |
| 373 | subdir = os.path.join(self.tempdir, 'to_be_deleted') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 374 | fs.mkdir(subdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 375 | 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. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 379 | while not fs.isfile(os.path.join(subdir, 'a')): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 380 | self.assertEqual(None, proc.poll()) |
| 381 | file_path.rmtree(subdir) |
Junji Watanabe | d94a5ab | 2021-08-06 23:03:29 +0000 | [diff] [blame] | 382 | self.assertEqual([4, 2], sleeps) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 383 | # 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 Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 388 | def test_filter_processes_dir_win(self): |
| 389 | python_dir = os.path.dirname(sys.executable) |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 390 | processes = file_path._filter_processes_dir_win( |
| 391 | file_path._enum_processes_win(), python_dir) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 392 | 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 Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 418 | file_path._enum_processes_win()) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 419 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 427 | if sys.platform != 'win32': |
| 428 | def test_symlink(self): |
| 429 | # This test will fail if the checkout is in a symlink. |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 430 | actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR) |
| 431 | expected = (test_env.CLIENT_DIR, None, None) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 432 | self.assertEqual(expected, actual) |
| 433 | |
| 434 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 435 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 441 | expected = ( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 442 | os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 443 | self.assertEqual(expected, actual) |
| 444 | |
| 445 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 446 | test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 447 | expected = ( |
| 448 | os.path.join('tests', 'trace_inputs'), 'files2', '') |
| 449 | self.assertEqual(expected, actual) |
| 450 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 451 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 454 | self.assertEqual(expected, actual) |
| 455 | |
| 456 | def test_native_case_symlink_right_case(self): |
| 457 | actual = file_path.get_native_path_case( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 458 | os.path.join(test_env.TESTS_DIR, 'trace_inputs')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 459 | 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 Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 463 | os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 464 | self.assertEqual('files2', os.path.basename(actual)) |
| 465 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 466 | else: |
Junji Watanabe | e1d6df2 | 2020-11-12 08:13:23 +0000 | [diff] [blame] | 467 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 468 | 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 Ikuta | 995da06 | 2021-03-17 05:01:59 +0000 | [diff] [blame] | 502 | 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.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 571 | |
| 572 | if __name__ == '__main__': |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 573 | test_env.main() |