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 |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 26 | |
| 27 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 28 | def write_content(filepath, content): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 29 | with fs.open(filepath, 'wb') as f: |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 30 | f.write(content) |
| 31 | |
| 32 | |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 33 | class FilePathTest(auto_stub.TestCase): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 34 | def setUp(self): |
| 35 | super(FilePathTest, self).setUp() |
| 36 | self._tempdir = None |
| 37 | |
| 38 | def tearDown(self): |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 39 | try: |
| 40 | if self._tempdir: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 41 | for dirpath, dirnames, filenames in fs.walk( |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 42 | self._tempdir, topdown=True): |
| 43 | for filename in filenames: |
| 44 | file_path.set_read_only(os.path.join(dirpath, filename), False) |
| 45 | for dirname in dirnames: |
| 46 | file_path.set_read_only(os.path.join(dirpath, dirname), False) |
| 47 | file_path.rmtree(self._tempdir) |
| 48 | finally: |
| 49 | super(FilePathTest, self).tearDown() |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 50 | |
| 51 | @property |
| 52 | def tempdir(self): |
| 53 | if not self._tempdir: |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 54 | self._tempdir = tempfile.mkdtemp(prefix=u'file_path_test') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 55 | return self._tempdir |
| 56 | |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 57 | def test_atomic_replace_new_file(self): |
| 58 | path = os.path.join(self.tempdir, 'new_file') |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 59 | file_path.atomic_replace(path, b'blah') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 60 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 61 | self.assertEqual(b'blah', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 62 | self.assertEqual([u'new_file'], os.listdir(self.tempdir)) |
| 63 | |
| 64 | def test_atomic_replace_existing_file(self): |
| 65 | path = os.path.join(self.tempdir, 'existing_file') |
| 66 | with open(path, 'wb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 67 | f.write(b'existing body') |
| 68 | file_path.atomic_replace(path, b'new body') |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 69 | with open(path, 'rb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 70 | self.assertEqual(b'new body', f.read()) |
vadimsh | e42aeba | 2016-06-03 12:32:21 -0700 | [diff] [blame] | 71 | self.assertEqual([u'existing_file'], os.listdir(self.tempdir)) |
| 72 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 73 | def assertFileMode(self, filepath, mode, umask=None): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 74 | umask = test_env.umask() if umask is None else umask |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 75 | actual = fs.stat(filepath).st_mode |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 76 | expected = mode & ~umask |
| 77 | self.assertEqual( |
| 78 | expected, |
| 79 | actual, |
| 80 | (filepath, oct(expected), oct(actual), oct(umask))) |
| 81 | |
| 82 | def assertMaskedFileMode(self, filepath, mode): |
| 83 | """It's usually when the file was first marked read only.""" |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 84 | self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 0o77) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 85 | |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 86 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 87 | 'TODO(crbug.com/1017545): ' |
| 88 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 89 | def test_native_case_end_with_os_path_sep(self): |
| 90 | # Make sure the trailing os.path.sep is kept. |
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(file_path.get_native_path_case(path), path) |
| 93 | |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 94 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 95 | 'TODO(crbug.com/1017545): ' |
| 96 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 97 | def test_native_case_end_with_dot_os_path_sep(self): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 98 | 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] | 99 | self.assertEqual( |
| 100 | file_path.get_native_path_case(path + '.' + os.path.sep), |
| 101 | path) |
| 102 | |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 103 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 104 | 'TODO(crbug.com/1017545): ' |
| 105 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 106 | def test_native_case_non_existing(self): |
| 107 | # Make sure it doesn't throw on non-existing files. |
| 108 | non_existing = 'trace_input_test_this_file_should_not_exist' |
| 109 | path = os.path.expanduser('~/' + non_existing) |
| 110 | self.assertFalse(os.path.exists(path)) |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 111 | 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] | 112 | self.assertEqual(file_path.get_native_path_case(path), path) |
| 113 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 114 | def test_delete_wd_rf(self): |
| 115 | # Confirms that a RO file in a RW directory can be deleted on non-Windows. |
| 116 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 117 | file_bar = os.path.join(dir_foo, 'bar') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 118 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 119 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 120 | file_path.set_read_only(dir_foo, False) |
| 121 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 122 | self.assertFileMode(dir_foo, 0o40777) |
| 123 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 124 | if sys.platform == 'win32': |
| 125 | # On Windows, a read-only file can't be deleted. |
| 126 | with self.assertRaises(OSError): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 127 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 128 | else: |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 129 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 130 | |
| 131 | def test_delete_rd_wf(self): |
| 132 | # Confirms that a Rw file in a RO directory can be deleted on Windows only. |
| 133 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 134 | file_bar = os.path.join(dir_foo, 'bar') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 135 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 136 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 137 | file_path.set_read_only(dir_foo, True) |
| 138 | file_path.set_read_only(file_bar, False) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 139 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 140 | self.assertFileMode(file_bar, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 141 | if sys.platform == 'win32': |
| 142 | # A read-only directory has a convoluted meaning on Windows, it means that |
| 143 | # the directory is "personalized". This is used as a signal by Windows |
| 144 | # Explorer to tell it to look into the directory for desktop.ini. |
| 145 | # See http://support.microsoft.com/kb/326549 for more details. |
| 146 | # As such, it is important to not try to set the read-only bit on |
| 147 | # directories on Windows since it has no effect other than trigger |
| 148 | # Windows Explorer to look for desktop.ini, which is unnecessary. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 149 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 150 | else: |
| 151 | with self.assertRaises(OSError): |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 152 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 153 | |
| 154 | def test_delete_rd_rf(self): |
| 155 | # Confirms that a RO file in a RO directory can't be deleted. |
| 156 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 157 | file_bar = os.path.join(dir_foo, 'bar') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 158 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 159 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 160 | file_path.set_read_only(dir_foo, True) |
| 161 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 162 | self.assertMaskedFileMode(dir_foo, 0o40555) |
| 163 | self.assertMaskedFileMode(file_bar, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 164 | with self.assertRaises(OSError): |
| 165 | # It fails for different reason depending on the OS. See the test cases |
| 166 | # above. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 167 | fs.remove(file_bar) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 168 | |
| 169 | def test_hard_link_mode(self): |
| 170 | # Creates a hard link, see if the file mode changed on the node or the |
| 171 | # directory entry. |
| 172 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 173 | file_bar = os.path.join(dir_foo, 'bar') |
| 174 | file_link = os.path.join(dir_foo, 'link') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 175 | fs.mkdir(dir_foo, 0o777) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 176 | write_content(file_bar, b'bar') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 177 | file_path.hardlink(file_bar, file_link) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 178 | self.assertFileMode(file_bar, 0o100666) |
| 179 | self.assertFileMode(file_link, 0o100666) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 180 | file_path.set_read_only(file_bar, True) |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 181 | self.assertMaskedFileMode(file_bar, 0o100444) |
| 182 | self.assertMaskedFileMode(file_link, 0o100444) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 183 | # This is bad news for Windows; on Windows, the file must be writeable to be |
| 184 | # deleted, but the file node is modified. This means that every hard links |
| 185 | # must be reset to be read-only after deleting one of the hard link |
| 186 | # directory entry. |
| 187 | |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 188 | def test_ensure_tree(self): |
| 189 | dir_foo = os.path.join(self.tempdir, 'foo') |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 190 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 191 | |
| 192 | self.assertTrue(os.path.isdir(dir_foo)) |
| 193 | |
| 194 | # Do not raise OSError with errno.EEXIST |
Takuto Ikuta | 7669fb5 | 2019-10-23 06:07:30 +0000 | [diff] [blame] | 195 | file_path.ensure_tree(dir_foo, 0o777) |
Takuto Ikuta | e3f7038 | 2019-04-03 14:52:06 +0000 | [diff] [blame] | 196 | |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 197 | def test_rmtree_unicode(self): |
| 198 | subdir = os.path.join(self.tempdir, 'hi') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 199 | fs.mkdir(subdir) |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 200 | filepath = os.path.join( |
| 201 | subdir, u'\u0627\u0644\u0635\u064A\u0646\u064A\u0629') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 202 | with fs.open(filepath, 'wb') as f: |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 203 | f.write(b'hi') |
Marc-Antoine Ruel | 0a795bd | 2015-01-16 20:32:10 -0500 | [diff] [blame] | 204 | # In particular, it fails when the input argument is a str. |
| 205 | file_path.rmtree(str(subdir)) |
| 206 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 207 | if sys.platform == 'darwin': |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 208 | |
| 209 | @unittest.skipIf(six.PY3, 'TODO(crbug.com/1017545): ' |
| 210 | 'MacOS and Carbon are not defined') |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 211 | def test_native_case_symlink_wrong_case(self): |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 212 | 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] | 213 | trace_inputs_dir = os.path.join(base_dir, 'trace_inputs') |
| 214 | actual = file_path.get_native_path_case(trace_inputs_dir) |
| 215 | self.assertEqual(trace_inputs_dir, actual) |
| 216 | |
| 217 | # Make sure the symlink is not resolved. |
| 218 | data = os.path.join(trace_inputs_dir, 'Files2') |
| 219 | actual = file_path.get_native_path_case(data) |
| 220 | self.assertEqual( |
| 221 | os.path.join(trace_inputs_dir, 'files2'), actual) |
| 222 | |
| 223 | data = os.path.join(trace_inputs_dir, 'Files2', '') |
| 224 | actual = file_path.get_native_path_case(data) |
| 225 | self.assertEqual( |
| 226 | os.path.join(trace_inputs_dir, 'files2', ''), actual) |
| 227 | |
| 228 | data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py') |
| 229 | actual = file_path.get_native_path_case(data) |
| 230 | # TODO(maruel): Should be child1.py. |
| 231 | self.assertEqual( |
| 232 | os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual) |
| 233 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 234 | if sys.platform in ('darwin', 'win32'): |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 235 | |
| 236 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 237 | 'TODO(crbug.com/1017545): ' |
| 238 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 239 | def test_native_case_not_sensitive(self): |
| 240 | # The home directory is almost guaranteed to have mixed upper/lower case |
| 241 | # letters on both Windows and OSX. |
| 242 | # This test also ensures that the output is independent on the input |
| 243 | # string case. |
| 244 | path = os.path.expanduser(u'~') |
| 245 | self.assertTrue(os.path.isdir(path)) |
| 246 | path = path.replace('/', os.path.sep) |
| 247 | if sys.platform == 'win32': |
| 248 | # Make sure the drive letter is upper case for consistency. |
| 249 | path = path[0].upper() + path[1:] |
| 250 | # This test assumes the variable is in the native path case on disk, this |
| 251 | # should be the case. Verify this assumption: |
| 252 | self.assertEqual(path, file_path.get_native_path_case(path)) |
| 253 | self.assertEqual( |
| 254 | file_path.get_native_path_case(path.lower()), |
| 255 | file_path.get_native_path_case(path.upper())) |
| 256 | |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 257 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 258 | 'TODO(crbug.com/1017545): ' |
| 259 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 260 | def test_native_case_not_sensitive_non_existent(self): |
| 261 | # This test also ensures that the output is independent on the input |
| 262 | # string case. |
| 263 | non_existing = os.path.join( |
| 264 | 'trace_input_test_this_dir_should_not_exist', 'really not', '') |
| 265 | path = os.path.expanduser(os.path.join(u'~', non_existing)) |
| 266 | path = path.replace('/', os.path.sep) |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 267 | self.assertFalse(fs.exists(path)) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 268 | lower = file_path.get_native_path_case(path.lower()) |
| 269 | upper = file_path.get_native_path_case(path.upper()) |
| 270 | # Make sure non-existing element is not modified: |
| 271 | self.assertTrue(lower.endswith(non_existing.lower())) |
| 272 | self.assertTrue(upper.endswith(non_existing.upper())) |
| 273 | self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)]) |
| 274 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 275 | if sys.platform == 'win32': |
| 276 | def test_native_case_alternate_datastream(self): |
| 277 | # Create the file manually, since tempfile doesn't support ADS. |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 278 | tempdir = six.text_type(tempfile.mkdtemp(prefix=u'trace_inputs')) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 279 | try: |
| 280 | tempdir = file_path.get_native_path_case(tempdir) |
| 281 | basename = 'foo.txt' |
| 282 | filename = basename + ':Zone.Identifier' |
| 283 | filepath = os.path.join(tempdir, filename) |
| 284 | open(filepath, 'w').close() |
| 285 | self.assertEqual(filepath, file_path.get_native_path_case(filepath)) |
| 286 | data_suffix = ':$DATA' |
| 287 | self.assertEqual( |
| 288 | filepath + data_suffix, |
| 289 | file_path.get_native_path_case(filepath + data_suffix)) |
| 290 | |
| 291 | open(filepath + '$DATA', 'w').close() |
| 292 | self.assertEqual( |
| 293 | filepath + data_suffix, |
| 294 | file_path.get_native_path_case(filepath + data_suffix)) |
| 295 | # Ensure the ADS weren't created as separate file. You love NTFS, don't |
| 296 | # you? |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 297 | self.assertEqual([basename], fs.listdir(tempdir)) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 298 | finally: |
maruel | 4b14f04 | 2015-10-06 12:08:08 -0700 | [diff] [blame] | 299 | file_path.rmtree(tempdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 300 | |
| 301 | def test_rmtree_win(self): |
| 302 | # Mock our sleep for faster test case execution. |
| 303 | sleeps = [] |
| 304 | self.mock(time, 'sleep', sleeps.append) |
Takuto Ikuta | f48103c | 2019-10-24 05:23:19 +0000 | [diff] [blame] | 305 | self.mock(sys, 'stderr', io.StringIO()) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 306 | |
| 307 | # Open a child process, so the file is locked. |
| 308 | subdir = os.path.join(self.tempdir, 'to_be_deleted') |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 309 | fs.mkdir(subdir) |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 310 | script = 'import time; open(\'a\', \'w\'); time.sleep(60)' |
| 311 | proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir) |
| 312 | try: |
| 313 | # Wait until the file exist. |
maruel | 4e73299 | 2015-10-16 10:17:21 -0700 | [diff] [blame] | 314 | while not fs.isfile(os.path.join(subdir, 'a')): |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 315 | self.assertEqual(None, proc.poll()) |
| 316 | file_path.rmtree(subdir) |
| 317 | self.assertEqual([2, 4, 2], sleeps) |
| 318 | # sys.stderr.getvalue() would return a fair amount of output but it is |
| 319 | # not completely deterministic so we're not testing it here. |
| 320 | finally: |
| 321 | proc.wait() |
| 322 | |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 323 | def test_filter_processes_dir_win(self): |
| 324 | python_dir = os.path.dirname(sys.executable) |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 325 | processes = file_path._filter_processes_dir_win( |
| 326 | file_path._enum_processes_win(), python_dir) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 327 | self.assertTrue(processes) |
| 328 | proc_names = [proc.ExecutablePath for proc in processes] |
| 329 | # Try to find at least one python process. |
| 330 | self.assertTrue( |
| 331 | any(proc == sys.executable for proc in proc_names), proc_names) |
| 332 | |
| 333 | def test_filter_processes_tree_win(self): |
| 334 | # Create a grand-child. |
| 335 | script = ( |
| 336 | 'import subprocess,sys;' |
| 337 | 'proc = subprocess.Popen(' |
| 338 | '[sys.executable, \'-u\', \'-c\', \'import time; print(1); ' |
| 339 | 'time.sleep(60)\'], stdout=subprocess.PIPE); ' |
| 340 | # Signal grand child is ready. |
| 341 | 'print(proc.stdout.read(1)); ' |
| 342 | # Wait for parent to have completed the test. |
| 343 | 'sys.stdin.read(1); ' |
| 344 | 'proc.kill()' |
| 345 | ) |
| 346 | proc = subprocess.Popen( |
| 347 | [sys.executable, '-u', '-c', script], |
| 348 | stdin=subprocess.PIPE, |
| 349 | stdout=subprocess.PIPE) |
| 350 | try: |
| 351 | proc.stdout.read(1) |
| 352 | processes = file_path.filter_processes_tree_win( |
Marc-Antoine Ruel | 0eb2eb2 | 2019-01-29 21:00:16 +0000 | [diff] [blame] | 353 | file_path._enum_processes_win()) |
Marc-Antoine Ruel | a275b29 | 2014-11-25 15:17:21 -0500 | [diff] [blame] | 354 | self.assertEqual(3, len(processes), processes) |
| 355 | proc.stdin.write('a') |
| 356 | proc.wait() |
| 357 | except Exception: |
| 358 | proc.kill() |
| 359 | finally: |
| 360 | proc.wait() |
| 361 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 362 | if sys.platform != 'win32': |
| 363 | def test_symlink(self): |
| 364 | # This test will fail if the checkout is in a symlink. |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 365 | actual = file_path.split_at_symlink(None, test_env.CLIENT_DIR) |
| 366 | expected = (test_env.CLIENT_DIR, None, None) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 367 | self.assertEqual(expected, actual) |
| 368 | |
| 369 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 370 | None, os.path.join(test_env.TESTS_DIR, 'trace_inputs')) |
| 371 | expected = (os.path.join(test_env.TESTS_DIR, 'trace_inputs'), None, None) |
| 372 | self.assertEqual(expected, actual) |
| 373 | |
| 374 | actual = file_path.split_at_symlink( |
| 375 | None, os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 376 | expected = ( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 377 | os.path.join(test_env.TESTS_DIR, 'trace_inputs'), 'files2', '') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 378 | self.assertEqual(expected, actual) |
| 379 | |
| 380 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 381 | test_env.CLIENT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 382 | expected = ( |
| 383 | os.path.join('tests', 'trace_inputs'), 'files2', '') |
| 384 | self.assertEqual(expected, actual) |
| 385 | actual = file_path.split_at_symlink( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 386 | test_env.CLIENT_DIR, |
| 387 | os.path.join('tests', 'trace_inputs', 'files2', 'bar')) |
| 388 | expected = (os.path.join('tests', 'trace_inputs'), 'files2', '/bar') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 389 | self.assertEqual(expected, actual) |
| 390 | |
Junji Watanabe | 16d5152 | 2020-07-01 01:59:07 +0000 | [diff] [blame^] | 391 | @unittest.skipIf(sys.platform == 'darwin' and six.PY3, |
| 392 | 'TODO(crbug.com/1017545): ' |
| 393 | 'MacOS and Carbon are not defined') |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 394 | def test_native_case_symlink_right_case(self): |
| 395 | actual = file_path.get_native_path_case( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 396 | os.path.join(test_env.TESTS_DIR, 'trace_inputs')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 397 | self.assertEqual('trace_inputs', os.path.basename(actual)) |
| 398 | |
| 399 | # Make sure the symlink is not resolved. |
| 400 | actual = file_path.get_native_path_case( |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 401 | os.path.join(test_env.TESTS_DIR, 'trace_inputs', 'files2')) |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 402 | self.assertEqual('files2', os.path.basename(actual)) |
| 403 | |
maruel | 9cdd761 | 2015-12-02 13:40:52 -0800 | [diff] [blame] | 404 | else: |
| 405 | def test_undeleteable_chmod(self): |
| 406 | # Create a file and a directory with an empty ACL. Then try to delete it. |
| 407 | dirpath = os.path.join(self.tempdir, 'd') |
| 408 | filepath = os.path.join(dirpath, 'f') |
| 409 | os.mkdir(dirpath) |
| 410 | with open(filepath, 'w') as f: |
| 411 | f.write('hi') |
| 412 | os.chmod(filepath, 0) |
| 413 | os.chmod(dirpath, 0) |
| 414 | file_path.rmtree(dirpath) |
| 415 | |
| 416 | def test_undeleteable_owner(self): |
| 417 | # Create a file and a directory with an empty ACL. Then try to delete it. |
| 418 | dirpath = os.path.join(self.tempdir, 'd') |
| 419 | filepath = os.path.join(dirpath, 'f') |
| 420 | os.mkdir(dirpath) |
| 421 | with open(filepath, 'w') as f: |
| 422 | f.write('hi') |
| 423 | import win32security |
| 424 | user, _domain, _type = win32security.LookupAccountName( |
| 425 | '', getpass.getuser()) |
| 426 | sd = win32security.SECURITY_DESCRIPTOR() |
| 427 | sd.Initialize() |
| 428 | sd.SetSecurityDescriptorOwner(user, False) |
| 429 | # Create an empty DACL, which removes all rights. |
| 430 | dacl = win32security.ACL() |
| 431 | dacl.Initialize() |
| 432 | sd.SetSecurityDescriptorDacl(1, dacl, 0) |
| 433 | win32security.SetFileSecurity( |
| 434 | fs.extend(filepath), win32security.DACL_SECURITY_INFORMATION, sd) |
| 435 | win32security.SetFileSecurity( |
| 436 | fs.extend(dirpath), win32security.DACL_SECURITY_INFORMATION, sd) |
| 437 | file_path.rmtree(dirpath) |
| 438 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 439 | |
| 440 | if __name__ == '__main__': |
Marc-Antoine Ruel | 76cfcee | 2019-04-01 23:16:36 +0000 | [diff] [blame] | 441 | test_env.main() |