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