| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # coding=utf-8 | |
| 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 import StringIO | |
| 8 import logging | |
| 9 import os | |
| 10 import sys | |
| 11 import unittest | |
| 12 | |
| 13 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) | |
| 14 ROOT_DIR = os.path.dirname(BASE_DIR) | |
| 15 sys.path.insert(0, ROOT_DIR) | |
| 16 | |
| 17 FILE_PATH = unicode(os.path.abspath(__file__)) | |
| 18 | |
| 19 import trace_inputs | |
| 20 | |
| 21 # Access to a protected member _FOO of a client class | |
| 22 # pylint: disable=W0212 | |
| 23 | |
| 24 | |
| 25 def join_norm(*args): | |
| 26 """Joins and normalizes path in a single step.""" | |
| 27 return unicode(os.path.normpath(os.path.join(*args))) | |
| 28 | |
| 29 | |
| 30 class TraceInputs(unittest.TestCase): | |
| 31 def test_process_quoted_arguments(self): | |
| 32 test_cases = ( | |
| 33 ('"foo"', ['foo']), | |
| 34 ('"foo", "bar"', ['foo', 'bar']), | |
| 35 ('"foo"..., "bar"', ['foo', 'bar']), | |
| 36 ('"foo", "bar"...', ['foo', 'bar']), | |
| 37 ( | |
| 38 '"/browser_tests", "--type=use,comma"', | |
| 39 ['/browser_tests', '--type=use,comma'] | |
| 40 ), | |
| 41 ( | |
| 42 '"/browser_tests", "--ignored=\\" --type=renderer \\""', | |
| 43 ['/browser_tests', '--ignored=" --type=renderer "'] | |
| 44 ), | |
| 45 ( | |
| 46 '"/Release+Asserts/bin/clang", "-cc1", ...', | |
| 47 ['/Release+Asserts/bin/clang', '-cc1'], | |
| 48 ), | |
| 49 ) | |
| 50 for actual, expected in test_cases: | |
| 51 self.assertEqual( | |
| 52 expected, trace_inputs.strace_process_quoted_arguments(actual)) | |
| 53 | |
| 54 def test_process_escaped_arguments(self): | |
| 55 test_cases = ( | |
| 56 ('foo\\0', ['foo']), | |
| 57 ('foo\\001bar\\0', ['foo', 'bar']), | |
| 58 ('\\"foo\\"\\0', ['"foo"']), | |
| 59 ) | |
| 60 for actual, expected in test_cases: | |
| 61 self.assertEqual( | |
| 62 expected, | |
| 63 trace_inputs.Dtrace.Context.process_escaped_arguments(actual)) | |
| 64 | |
| 65 def test_variable_abs(self): | |
| 66 value = trace_inputs.Results.File( | |
| 67 None, u'/foo/bar', None, None, trace_inputs.Results.File.READ) | |
| 68 actual = value.replace_variables({'$FOO': '/foo'}) | |
| 69 self.assertEqual('$FOO/bar', actual.path) | |
| 70 self.assertEqual('$FOO/bar', actual.full_path) | |
| 71 self.assertEqual(True, actual.tainted) | |
| 72 | |
| 73 def test_variable_rel(self): | |
| 74 value = trace_inputs.Results.File( | |
| 75 u'/usr', u'foo/bar', None, None, trace_inputs.Results.File.READ) | |
| 76 actual = value.replace_variables({'$FOO': 'foo'}) | |
| 77 self.assertEqual('$FOO/bar', actual.path) | |
| 78 self.assertEqual(os.path.join('/usr', '$FOO/bar'), actual.full_path) | |
| 79 self.assertEqual(True, actual.tainted) | |
| 80 | |
| 81 def test_strace_filename(self): | |
| 82 filename = u'foo, bar, ~p#o,,ué^t%t.txt' | |
| 83 data = 'foo, bar, ~p#o,,u\\303\\251^t%t.txt' | |
| 84 self.assertEqual(filename, trace_inputs.Strace.load_filename(data)) | |
| 85 | |
| 86 def test_CsvReader(self): | |
| 87 test_cases = { | |
| 88 u' Next is empty, , {00000000-0000}': | |
| 89 [u'Next is empty', u'', u'{00000000-0000}'], | |
| 90 | |
| 91 u' Foo, , "\\\\NT AUTHORITY\\SYSTEM", "Idle", ""': | |
| 92 [u'Foo', u'', u'\\\\NT AUTHORITY\\SYSTEM', u'Idle', u''], | |
| 93 | |
| 94 u' Foo, ""Who the hell thought delimiters are great as escape too""': | |
| 95 [u'Foo', u'"Who the hell thought delimiters are great as escape too"'], | |
| 96 | |
| 97 ( | |
| 98 u' "remoting.exe", ""C:\\Program Files\\remoting.exe" ' | |
| 99 u'--host="C:\\ProgramData\\host.json""' | |
| 100 ): | |
| 101 [ | |
| 102 u'remoting.exe', | |
| 103 u'"C:\\Program Files\\remoting.exe" ' | |
| 104 u'--host="C:\\ProgramData\\host.json"' | |
| 105 ], | |
| 106 | |
| 107 u'"MONSTRE", "", 0x0': [u'MONSTRE', u'', u'0x0'], | |
| 108 | |
| 109 # To whoever wrote this code at Microsoft: You did it wrong. | |
| 110 u'"cmd.exe", ""C:\\\\Winz\\\\cmd.exe" /k ""C:\\\\MSVS\\\\vc.bat"" x86"': | |
| 111 [u'cmd.exe', u'"C:\\\\Winz\\\\cmd.exe" /k "C:\\\\MSVS\\\\vc.bat" x86'], | |
| 112 } | |
| 113 for data, expected in test_cases.iteritems(): | |
| 114 csv = trace_inputs.LogmanTrace.Tracer.CsvReader(StringIO.StringIO(data)) | |
| 115 actual = [i for i in csv] | |
| 116 self.assertEqual(1, len(actual)) | |
| 117 self.assertEqual(expected, actual[0]) | |
| 118 | |
| 119 | |
| 120 if sys.platform != 'win32': | |
| 121 class StraceInputs(unittest.TestCase): | |
| 122 # Represents the root process pid (an arbitrary number). | |
| 123 _ROOT_PID = 27 | |
| 124 _CHILD_PID = 14 | |
| 125 _GRAND_CHILD_PID = 70 | |
| 126 | |
| 127 @staticmethod | |
| 128 def _load_context(lines, initial_cwd): | |
| 129 context = trace_inputs.Strace.Context(lambda _: False, None, initial_cwd) | |
| 130 for line in lines: | |
| 131 context.on_line(*line) | |
| 132 done = any(p._done for p in context._process_lookup.itervalues()) | |
| 133 return context.to_results().flatten(), done | |
| 134 | |
| 135 def assertContext(self, lines, initial_cwd, expected, expected_done): | |
| 136 actual, actual_done = self._load_context(lines, initial_cwd) | |
| 137 self.assertEqual(expected, actual) | |
| 138 # If actual_done is True, this means the log was cut off abruptly. | |
| 139 self.assertEqual(expected_done, actual_done) | |
| 140 | |
| 141 def _test_lines(self, lines, initial_cwd, files, command=None): | |
| 142 filepath = join_norm(initial_cwd, '../out/unittests') | |
| 143 command = command or ['../out/unittests'] | |
| 144 expected = { | |
| 145 'root': { | |
| 146 'children': [], | |
| 147 'command': command, | |
| 148 'executable': filepath, | |
| 149 'files': files, | |
| 150 'initial_cwd': initial_cwd, | |
| 151 'pid': self._ROOT_PID, | |
| 152 } | |
| 153 } | |
| 154 if not files: | |
| 155 expected['root']['command'] = None | |
| 156 expected['root']['executable'] = None | |
| 157 self.assertContext(lines, initial_cwd, expected, False) | |
| 158 | |
| 159 def test_execve(self): | |
| 160 lines = [ | |
| 161 (self._ROOT_PID, | |
| 162 'execve("/home/foo_bar_user/out/unittests", ' | |
| 163 '["/home/foo_bar_user/out/unittests", ' | |
| 164 '"--random-flag"], [/* 44 vars */]) = 0'), | |
| 165 (self._ROOT_PID, | |
| 166 'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'), | |
| 167 ] | |
| 168 files = [ | |
| 169 { | |
| 170 'mode': trace_inputs.Results.File.READ, | |
| 171 'path': u'/home/foo_bar_user/out/unittests', | |
| 172 'size': -1, | |
| 173 }, | |
| 174 { | |
| 175 'mode': trace_inputs.Results.File.WRITE, | |
| 176 'path': u'/home/foo_bar_user/src/out/unittests.log', | |
| 177 'size': -1, | |
| 178 }, | |
| 179 ] | |
| 180 command = [ | |
| 181 '/home/foo_bar_user/out/unittests', '--random-flag', | |
| 182 ] | |
| 183 self._test_lines(lines, u'/home/foo_bar_user/src', files, command) | |
| 184 | |
| 185 def test_empty(self): | |
| 186 try: | |
| 187 self._load_context([], None) | |
| 188 self.fail() | |
| 189 except trace_inputs.TracingFailure, e: | |
| 190 expected = ( | |
| 191 'Found internal inconsitency in process lifetime detection ' | |
| 192 'while finding the root process', | |
| 193 None, | |
| 194 None, | |
| 195 None, | |
| 196 None, | |
| 197 []) | |
| 198 self.assertEqual(expected, e.args) | |
| 199 | |
| 200 def test_chmod(self): | |
| 201 lines = [ | |
| 202 (self._ROOT_PID, 'chmod("temp/file", 0100644) = 0'), | |
| 203 ] | |
| 204 expected = { | |
| 205 'root': { | |
| 206 'children': [], | |
| 207 'command': None, | |
| 208 'executable': None, | |
| 209 'files': [ | |
| 210 { | |
| 211 'mode': trace_inputs.Results.File.WRITE, | |
| 212 'path': u'/home/foo_bar_user/src/temp/file', | |
| 213 'size': -1, | |
| 214 }, | |
| 215 ], | |
| 216 'initial_cwd': u'/home/foo_bar_user/src', | |
| 217 'pid': self._ROOT_PID, | |
| 218 } | |
| 219 } | |
| 220 self.assertContext(lines, u'/home/foo_bar_user/src', expected, False) | |
| 221 | |
| 222 def test_close(self): | |
| 223 lines = [ | |
| 224 (self._ROOT_PID, 'close(7) = 0'), | |
| 225 ] | |
| 226 self._test_lines(lines, u'/home/foo_bar_user/src', []) | |
| 227 | |
| 228 def test_clone(self): | |
| 229 # Grand-child with relative directory. | |
| 230 lines = [ | |
| 231 (self._ROOT_PID, | |
| 232 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | |
| 233 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | |
| 234 self._CHILD_PID), | |
| 235 (self._CHILD_PID, | |
| 236 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | |
| 237 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | |
| 238 self._GRAND_CHILD_PID), | |
| 239 (self._GRAND_CHILD_PID, | |
| 240 'open("%s", O_RDONLY) = 76' % os.path.basename(str(FILE_PATH))), | |
| 241 ] | |
| 242 size = os.stat(FILE_PATH).st_size | |
| 243 expected = { | |
| 244 'root': { | |
| 245 'children': [ | |
| 246 { | |
| 247 'children': [ | |
| 248 { | |
| 249 'children': [], | |
| 250 'command': None, | |
| 251 'executable': None, | |
| 252 'files': [ | |
| 253 { | |
| 254 'mode': trace_inputs.Results.File.READ, | |
| 255 'path': FILE_PATH, | |
| 256 'size': size, | |
| 257 }, | |
| 258 ], | |
| 259 'initial_cwd': BASE_DIR, | |
| 260 'pid': self._GRAND_CHILD_PID, | |
| 261 }, | |
| 262 ], | |
| 263 'command': None, | |
| 264 'executable': None, | |
| 265 'files': [], | |
| 266 'initial_cwd': BASE_DIR, | |
| 267 'pid': self._CHILD_PID, | |
| 268 }, | |
| 269 ], | |
| 270 'command': None, | |
| 271 'executable': None, | |
| 272 'files': [], | |
| 273 'initial_cwd': BASE_DIR, | |
| 274 'pid': self._ROOT_PID, | |
| 275 }, | |
| 276 } | |
| 277 self.assertContext(lines, BASE_DIR, expected, False) | |
| 278 | |
| 279 def test_clone_chdir(self): | |
| 280 # Grand-child with relative directory. | |
| 281 lines = [ | |
| 282 (self._ROOT_PID, | |
| 283 'execve("../out/unittests", ' | |
| 284 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 285 (self._ROOT_PID, | |
| 286 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | |
| 287 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | |
| 288 self._CHILD_PID), | |
| 289 (self._CHILD_PID, | |
| 290 'chdir("/home_foo_bar_user/path1") = 0'), | |
| 291 (self._CHILD_PID, | |
| 292 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | |
| 293 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | |
| 294 self._GRAND_CHILD_PID), | |
| 295 (self._GRAND_CHILD_PID, | |
| 296 'execve("../out/unittests", ' | |
| 297 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 298 (self._ROOT_PID, 'chdir("/home_foo_bar_user/path2") = 0'), | |
| 299 (self._GRAND_CHILD_PID, | |
| 300 'open("random.txt", O_RDONLY) = 76'), | |
| 301 ] | |
| 302 expected = { | |
| 303 'root': { | |
| 304 'children': [ | |
| 305 { | |
| 306 'children': [ | |
| 307 { | |
| 308 'children': [], | |
| 309 'command': ['../out/unittests'], | |
| 310 'executable': '/home_foo_bar_user/out/unittests', | |
| 311 'files': [ | |
| 312 { | |
| 313 'mode': trace_inputs.Results.File.READ, | |
| 314 'path': u'/home_foo_bar_user/out/unittests', | |
| 315 'size': -1, | |
| 316 }, | |
| 317 { | |
| 318 'mode': trace_inputs.Results.File.READ, | |
| 319 'path': u'/home_foo_bar_user/path1/random.txt', | |
| 320 'size': -1, | |
| 321 }, | |
| 322 ], | |
| 323 'initial_cwd': u'/home_foo_bar_user/path1', | |
| 324 'pid': self._GRAND_CHILD_PID, | |
| 325 }, | |
| 326 ], | |
| 327 # clone does not carry over the command and executable so it is | |
| 328 # clear if an execve() call was done or not. | |
| 329 'command': None, | |
| 330 'executable': None, | |
| 331 # This is important, since no execve call was done, it didn't | |
| 332 # touch the executable file. | |
| 333 'files': [], | |
| 334 'initial_cwd': unicode(ROOT_DIR), | |
| 335 'pid': self._CHILD_PID, | |
| 336 }, | |
| 337 ], | |
| 338 'command': ['../out/unittests'], | |
| 339 'executable': join_norm(ROOT_DIR, '../out/unittests'), | |
| 340 'files': [ | |
| 341 { | |
| 342 'mode': trace_inputs.Results.File.READ, | |
| 343 'path': join_norm(ROOT_DIR, '../out/unittests'), | |
| 344 'size': -1, | |
| 345 }, | |
| 346 ], | |
| 347 'initial_cwd': unicode(ROOT_DIR), | |
| 348 'pid': self._ROOT_PID, | |
| 349 }, | |
| 350 } | |
| 351 self.assertContext(lines, ROOT_DIR, expected, False) | |
| 352 | |
| 353 def test_faccess(self): | |
| 354 lines = [ | |
| 355 (self._ROOT_PID, | |
| 356 'faccessat(AT_FDCWD, "/home_foo_bar_user/file", W_OK) = 0'), | |
| 357 ] | |
| 358 expected = { | |
| 359 'root': { | |
| 360 'children': [], | |
| 361 'command': None, | |
| 362 'executable': None, | |
| 363 'files': [ | |
| 364 { | |
| 365 'mode': trace_inputs.Results.File.TOUCHED, | |
| 366 'path': u'/home_foo_bar_user/file', | |
| 367 'size': -1, | |
| 368 }, | |
| 369 ], | |
| 370 'initial_cwd': unicode(ROOT_DIR), | |
| 371 'pid': self._ROOT_PID, | |
| 372 }, | |
| 373 } | |
| 374 self.assertContext(lines, ROOT_DIR, expected, False) | |
| 375 | |
| 376 def test_futex_died(self): | |
| 377 # That's a pretty bad fork, copy-pasted from a real log. | |
| 378 lines = [ | |
| 379 (self._ROOT_PID, 'close(9) = 0'), | |
| 380 (self._ROOT_PID, 'futex( <unfinished ... exit status 0>'), | |
| 381 ] | |
| 382 expected = { | |
| 383 'root': { | |
| 384 'children': [], | |
| 385 'command': None, | |
| 386 'executable': None, | |
| 387 'files': [], | |
| 388 'initial_cwd': unicode(ROOT_DIR), | |
| 389 'pid': self._ROOT_PID, | |
| 390 }, | |
| 391 } | |
| 392 self.assertContext(lines, ROOT_DIR, expected, True) | |
| 393 | |
| 394 def test_futex_missing_in_action(self): | |
| 395 # That's how futex() calls roll. | |
| 396 lines = [ | |
| 397 (self._ROOT_PID, | |
| 398 'clone(child_stack=0x7fae9f4bed70, flags=CLONE_VM|CLONE_FS|' | |
| 399 'CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|' | |
| 400 'CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, ' | |
| 401 'parent_tidptr=0x7fae9f4bf9d0, tls=0x7fae9f4bf700, ' | |
| 402 'child_tidptr=0x7fae9f4bf9d0) = 3862'), | |
| 403 (self._ROOT_PID, | |
| 404 'futex(0x1407670, FUTEX_WAIT_PRIVATE, 2, {0, 0}) = -1 EAGAIN ' | |
| 405 '(Resource temporarily unavailable)'), | |
| 406 (self._ROOT_PID, 'futex(0x1407670, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 407 (self._ROOT_PID, 'close(9) = 0'), | |
| 408 (self._ROOT_PID, 'futex('), | |
| 409 ] | |
| 410 expected = { | |
| 411 'root': { | |
| 412 'children': [ | |
| 413 { | |
| 414 'children': [], | |
| 415 'command': None, | |
| 416 'executable': None, | |
| 417 'files': [], | |
| 418 'initial_cwd': unicode(ROOT_DIR), | |
| 419 'pid': 3862, | |
| 420 }, | |
| 421 ], | |
| 422 'command': None, | |
| 423 'executable': None, | |
| 424 'files': [], | |
| 425 'initial_cwd': unicode(ROOT_DIR), | |
| 426 'pid': self._ROOT_PID, | |
| 427 }, | |
| 428 } | |
| 429 self.assertContext(lines, ROOT_DIR, expected, True) | |
| 430 | |
| 431 def test_futex_missing_in_partial_action(self): | |
| 432 # That's how futex() calls roll even more. | |
| 433 lines = [ | |
| 434 (self._ROOT_PID, | |
| 435 'futex(0x7fff25718b14, FUTEX_CMP_REQUEUE_PRIVATE, 1, 2147483647, ' | |
| 436 '0x7fff25718ae8, 2) = 1'), | |
| 437 (self._ROOT_PID, 'futex(0x7fff25718ae8, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 438 (self._ROOT_PID, 'futex(0x697263c, FUTEX_WAIT_PRIVATE, 1, NULL) = 0'), | |
| 439 (self._ROOT_PID, 'futex(0x6972610, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 440 (self._ROOT_PID, 'futex(0x697263c, FUTEX_WAIT_PRIVATE, 3, NULL) = 0'), | |
| 441 (self._ROOT_PID, 'futex(0x6972610, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 442 (self._ROOT_PID, 'futex(0x697263c, FUTEX_WAIT_PRIVATE, 5, NULL) = 0'), | |
| 443 (self._ROOT_PID, 'futex(0x6972610, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 444 (self._ROOT_PID, 'futex(0x697263c, FUTEX_WAIT_PRIVATE, 7, NULL) = 0'), | |
| 445 (self._ROOT_PID, 'futex(0x6972610, FUTEX_WAKE_PRIVATE, 1) = 0'), | |
| 446 (self._ROOT_PID, 'futex(0x7f0c17780634, ' | |
| 447 'FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, ' | |
| 448 '{1351180745, 913067000}, ffffffff'), | |
| 449 ] | |
| 450 expected = { | |
| 451 'root': { | |
| 452 'children': [], | |
| 453 'command': None, | |
| 454 'executable': None, | |
| 455 'files': [], | |
| 456 'initial_cwd': unicode(ROOT_DIR), | |
| 457 'pid': self._ROOT_PID, | |
| 458 }, | |
| 459 } | |
| 460 self.assertContext(lines, ROOT_DIR, expected, True) | |
| 461 | |
| 462 def test_futex_missing_in_partial_action_with_no_process(self): | |
| 463 # That's how futex() calls roll even more (again). | |
| 464 lines = [ | |
| 465 (self._ROOT_PID, 'futex(0x7134840, FUTEX_WAIT_PRIVATE, 2, ' | |
| 466 'NULL <ptrace(SYSCALL):No such process>'), | |
| 467 ] | |
| 468 expected = { | |
| 469 'root': { | |
| 470 'children': [], | |
| 471 'command': None, | |
| 472 'executable': None, | |
| 473 'files': [], | |
| 474 'initial_cwd': unicode(ROOT_DIR), | |
| 475 'pid': self._ROOT_PID, | |
| 476 }, | |
| 477 } | |
| 478 self.assertContext(lines, ROOT_DIR, expected, True) | |
| 479 | |
| 480 def test_open(self): | |
| 481 lines = [ | |
| 482 (self._ROOT_PID, | |
| 483 'execve("../out/unittests", ' | |
| 484 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 485 (self._ROOT_PID, | |
| 486 'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'), | |
| 487 ] | |
| 488 files = [ | |
| 489 { | |
| 490 'mode': trace_inputs.Results.File.READ, | |
| 491 'path': u'/home/foo_bar_user/out/unittests', | |
| 492 'size': -1, | |
| 493 }, | |
| 494 { | |
| 495 'mode': trace_inputs.Results.File.WRITE, | |
| 496 'path': u'/home/foo_bar_user/src/out/unittests.log', | |
| 497 'size': -1, | |
| 498 }, | |
| 499 ] | |
| 500 self._test_lines(lines, u'/home/foo_bar_user/src', files) | |
| 501 | |
| 502 def test_open_resumed(self): | |
| 503 lines = [ | |
| 504 (self._ROOT_PID, | |
| 505 'execve("../out/unittests", ' | |
| 506 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 507 (self._ROOT_PID, | |
| 508 'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND ' | |
| 509 '<unfinished ...>'), | |
| 510 (self._ROOT_PID, '<... open resumed> ) = 3'), | |
| 511 ] | |
| 512 files = [ | |
| 513 { | |
| 514 'mode': trace_inputs.Results.File.READ, | |
| 515 'path': u'/home/foo_bar_user/out/unittests', | |
| 516 'size': -1, | |
| 517 }, | |
| 518 { | |
| 519 'mode': trace_inputs.Results.File.WRITE, | |
| 520 'path': u'/home/foo_bar_user/src/out/unittests.log', | |
| 521 'size': -1, | |
| 522 }, | |
| 523 ] | |
| 524 self._test_lines(lines, u'/home/foo_bar_user/src', files) | |
| 525 | |
| 526 def test_openat(self): | |
| 527 lines = [ | |
| 528 (self._ROOT_PID, | |
| 529 'execve("../out/unittests", ' | |
| 530 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 531 (self._ROOT_PID, | |
| 532 'openat(AT_FDCWD, "/home/foo_bar_user/file", O_RDONLY) = 0'), | |
| 533 ] | |
| 534 files = [ | |
| 535 { | |
| 536 'mode': trace_inputs.Results.File.READ, | |
| 537 'path': u'/home/foo_bar_user/file', | |
| 538 'size': -1, | |
| 539 }, | |
| 540 { | |
| 541 'mode': trace_inputs.Results.File.READ, | |
| 542 'path': u'/home/foo_bar_user/out/unittests', | |
| 543 'size': -1, | |
| 544 }, | |
| 545 ] | |
| 546 | |
| 547 self._test_lines(lines, u'/home/foo_bar_user/src', files) | |
| 548 | |
| 549 def test_openat_died(self): | |
| 550 lines = [ | |
| 551 # It's fine as long as there is nothing after. | |
| 552 ( self._ROOT_PID, | |
| 553 'openat(AT_FDCWD, "/tmp/random_dir/Plugins", ' | |
| 554 'O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC'), | |
| 555 ] | |
| 556 expected = { | |
| 557 'root': { | |
| 558 'children': [], | |
| 559 'command': None, | |
| 560 'executable': None, | |
| 561 'files': [], | |
| 562 'initial_cwd': unicode(ROOT_DIR), | |
| 563 'pid': self._ROOT_PID, | |
| 564 }, | |
| 565 } | |
| 566 self.assertContext(lines, ROOT_DIR, expected, True) | |
| 567 | |
| 568 def test_rmdir(self): | |
| 569 lines = [ | |
| 570 (self._ROOT_PID, 'rmdir("directory/to/delete") = 0'), | |
| 571 ] | |
| 572 self._test_lines(lines, u'/home/foo_bar_user/src', []) | |
| 573 | |
| 574 def test_setxattr(self): | |
| 575 lines = [ | |
| 576 (self._ROOT_PID, | |
| 577 'setxattr("file.exe", "attribute", "value", 0, 0) = 0'), | |
| 578 ] | |
| 579 self._test_lines(lines, u'/home/foo_bar_user/src', []) | |
| 580 | |
| 581 def test_sig_unexpected(self): | |
| 582 lines = [ | |
| 583 (self._ROOT_PID, 'exit_group(0) = ?'), | |
| 584 ] | |
| 585 self._test_lines(lines, u'/home/foo_bar_user/src', []) | |
| 586 | |
| 587 def test_stray(self): | |
| 588 lines = [ | |
| 589 (self._ROOT_PID, | |
| 590 'execve("../out/unittests", ' | |
| 591 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 592 (self._ROOT_PID, | |
| 593 ') = ? <unavailable>'), | |
| 594 ] | |
| 595 files = [ | |
| 596 { | |
| 597 'mode': trace_inputs.Results.File.READ, | |
| 598 'path': u'/home/foo_bar_user/out/unittests', | |
| 599 'size': -1, | |
| 600 }, | |
| 601 ] | |
| 602 self._test_lines(lines, u'/home/foo_bar_user/src', files) | |
| 603 | |
| 604 def test_truncate(self): | |
| 605 lines = [ | |
| 606 (self._ROOT_PID, | |
| 607 'execve("../out/unittests", ' | |
| 608 '["../out/unittests"...], [/* 44 vars */]) = 0'), | |
| 609 (self._ROOT_PID, | |
| 610 'truncate("file.exe", 0) = 0'), | |
| 611 ] | |
| 612 files = [ | |
| 613 { | |
| 614 'mode': trace_inputs.Results.File.READ, | |
| 615 'path': u'/home/foo_bar_user/out/unittests', | |
| 616 'size': -1, | |
| 617 }, | |
| 618 { | |
| 619 'mode': trace_inputs.Results.File.WRITE, | |
| 620 'path': u'/home/foo_bar_user/src/file.exe', | |
| 621 'size': -1, | |
| 622 }, | |
| 623 ] | |
| 624 self._test_lines(lines, u'/home/foo_bar_user/src', files) | |
| 625 | |
| 626 def test_vfork(self): | |
| 627 # vfork is the only function traced that doesn't take parameters. | |
| 628 lines = [ | |
| 629 (self._ROOT_PID, 'vfork() = %d' % self._CHILD_PID), | |
| 630 ] | |
| 631 expected = { | |
| 632 'root': { | |
| 633 'children': [ | |
| 634 { | |
| 635 'children': [], | |
| 636 'command': None, | |
| 637 'executable': None, | |
| 638 'files': [], | |
| 639 'initial_cwd': unicode(ROOT_DIR), | |
| 640 'pid': self._CHILD_PID, | |
| 641 } | |
| 642 ], | |
| 643 'command': None, | |
| 644 'executable': None, | |
| 645 'files': [], | |
| 646 'initial_cwd': unicode(ROOT_DIR), | |
| 647 'pid': self._ROOT_PID, | |
| 648 }, | |
| 649 } | |
| 650 self.assertContext(lines, ROOT_DIR, expected, False) | |
| 651 | |
| 652 | |
| 653 if __name__ == '__main__': | |
| 654 logging.basicConfig( | |
| 655 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | |
| 656 if '-v' in sys.argv: | |
| 657 unittest.TestCase.maxDiff = None | |
| 658 unittest.main() | |
| OLD | NEW |