OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import cStringIO | 6 import cStringIO |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import unittest | 9 import unittest |
10 import sys | 10 import sys |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 " 'variables': {\n" | 87 " 'variables': {\n" |
88 " },\n" | 88 " },\n" |
89 " }, {\n" | 89 " }, {\n" |
90 " 'variables': {\n" | 90 " 'variables': {\n" |
91 " },\n" | 91 " },\n" |
92 " }],\n" | 92 " }],\n" |
93 " ],\n" | 93 " ],\n" |
94 "}\n") | 94 "}\n") |
95 self._test(value, expected) | 95 self._test(value, expected) |
96 | 96 |
| 97 def test_process_quoted_arguments(self): |
| 98 test_cases = ( |
| 99 ('"foo"', ['foo']), |
| 100 ('"foo", "bar"', ['foo', 'bar']), |
| 101 ('"foo"..., "bar"', ['foo', 'bar']), |
| 102 ('"foo", "bar"...', ['foo', 'bar']), |
| 103 ) |
| 104 for actual, expected in test_cases: |
| 105 self.assertEquals(expected, trace_inputs.process_quoted_arguments(actual)) |
| 106 |
97 | 107 |
98 def join_norm(*args): | 108 def join_norm(*args): |
99 """Joins and normalizes path in a single step.""" | 109 """Joins and normalizes path in a single step.""" |
100 return unicode(os.path.normpath(os.path.join(*args))) | 110 return unicode(os.path.normpath(os.path.join(*args))) |
101 | 111 |
102 | 112 |
103 if trace_inputs.get_flavor() == 'linux': | 113 if trace_inputs.get_flavor() == 'linux': |
104 class StraceInputs(unittest.TestCase): | 114 class StraceInputs(unittest.TestCase): |
105 # Represents the root process pid (an arbitrary number). | 115 # Represents the root process pid (an arbitrary number). |
106 _ROOT_PID = 27 | 116 _ROOT_PID = 27 |
107 _CHILD_PID = 14 | 117 _CHILD_PID = 14 |
108 _GRAND_CHILD_PID = 70 | 118 _GRAND_CHILD_PID = 70 |
109 | 119 |
110 @staticmethod | 120 @staticmethod |
111 def _load_context(lines, initial_cwd): | 121 def _load_context(lines, initial_cwd): |
112 context = trace_inputs.Strace.Context(lambda _: False, initial_cwd) | 122 context = trace_inputs.Strace.Context(lambda _: False, initial_cwd) |
113 for line in lines: | 123 for line in lines: |
114 context.on_line(*line) | 124 context.on_line(*line) |
115 return context.to_results().flatten() | 125 return context.to_results().flatten() |
116 | 126 |
117 def _test_lines(self, lines, initial_cwd, files, command=None): | 127 def _test_lines(self, lines, initial_cwd, files, command=None): |
| 128 filepath = join_norm(initial_cwd, '../out/unittests') |
118 command = command or ['../out/unittests'] | 129 command = command or ['../out/unittests'] |
119 expected = { | 130 expected = { |
120 'root': { | 131 'root': { |
121 'children': [], | 132 'children': [], |
122 'command': None, | 133 'command': command, |
123 'executable': None, | 134 'executable': filepath, |
124 'files': files, | 135 'files': files, |
125 'initial_cwd': initial_cwd, | 136 'initial_cwd': initial_cwd, |
126 'pid': self._ROOT_PID, | 137 'pid': self._ROOT_PID, |
127 } | 138 } |
128 } | 139 } |
129 if not files: | 140 if not files: |
130 expected['root']['command'] = None | 141 expected['root']['command'] = None |
131 expected['root']['executable'] = None | 142 expected['root']['executable'] = None |
132 self.assertEquals(expected, self._load_context(lines, initial_cwd)) | 143 self.assertEquals(expected, self._load_context(lines, initial_cwd)) |
133 | 144 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 (self._ROOT_PID, | 238 (self._ROOT_PID, |
228 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | 239 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' |
229 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | 240 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % |
230 self._CHILD_PID), | 241 self._CHILD_PID), |
231 (self._CHILD_PID, | 242 (self._CHILD_PID, |
232 'chdir("/home_foo_bar_user/path1") = 0'), | 243 'chdir("/home_foo_bar_user/path1") = 0'), |
233 (self._CHILD_PID, | 244 (self._CHILD_PID, |
234 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | 245 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' |
235 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % | 246 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' % |
236 self._GRAND_CHILD_PID), | 247 self._GRAND_CHILD_PID), |
| 248 (self._GRAND_CHILD_PID, |
| 249 'execve("../out/unittests", ' |
| 250 '["../out/unittests"...], [/* 44 vars */]) = 0'), |
237 (self._ROOT_PID, 'chdir("/home_foo_bar_user/path2") = 0'), | 251 (self._ROOT_PID, 'chdir("/home_foo_bar_user/path2") = 0'), |
238 (self._GRAND_CHILD_PID, | 252 (self._GRAND_CHILD_PID, |
239 'open("random.txt", O_RDONLY) = 76'), | 253 'open("random.txt", O_RDONLY) = 76'), |
240 ] | 254 ] |
241 expected = { | 255 expected = { |
242 'root': { | 256 'root': { |
243 'children': [ | 257 'children': [ |
244 { | 258 { |
245 'children': [ | 259 'children': [ |
246 { | 260 { |
247 'children': [], | 261 'children': [], |
248 'command': None, | 262 'command': ['../out/unittests'], |
249 'executable': None, | 263 'executable': '/home_foo_bar_user/out/unittests', |
250 'files': [ | 264 'files': [ |
251 { | 265 { |
| 266 'path': u'/home_foo_bar_user/out/unittests', |
| 267 'size': -1, |
| 268 }, |
| 269 { |
252 'path': u'/home_foo_bar_user/path1/random.txt', | 270 'path': u'/home_foo_bar_user/path1/random.txt', |
253 'size': -1, | 271 'size': -1, |
254 }, | 272 }, |
255 ], | 273 ], |
256 'initial_cwd': '/home_foo_bar_user/path1', | 274 'initial_cwd': u'/home_foo_bar_user/path1', |
257 'pid': self._GRAND_CHILD_PID, | 275 'pid': self._GRAND_CHILD_PID, |
258 }, | 276 }, |
259 ], | 277 ], |
| 278 # clone does not carry over the command and executable so it is |
| 279 # clear if an execve() call was done or not. |
260 'command': None, | 280 'command': None, |
261 'executable': None, | 281 'executable': None, |
262 # This is important, since no execve call was done, it didn't | 282 # This is important, since no execve call was done, it didn't |
263 # touch the executable file. | 283 # touch the executable file. |
264 'files': [], | 284 'files': [], |
265 'initial_cwd': ROOT_DIR, | 285 'initial_cwd': unicode(ROOT_DIR), |
266 'pid': self._CHILD_PID, | 286 'pid': self._CHILD_PID, |
267 }, | 287 }, |
268 ], | 288 ], |
269 'command': None, | 289 'command': ['../out/unittests'], |
270 'executable': None, | 290 'executable': join_norm(ROOT_DIR, '../out/unittests'), |
271 'files': [ | 291 'files': [ |
272 { | 292 { |
273 'path': join_norm(ROOT_DIR, '../out/unittests'), | 293 'path': join_norm(ROOT_DIR, '../out/unittests'), |
274 'size': -1, | 294 'size': -1, |
275 }, | 295 }, |
276 ], | 296 ], |
277 'initial_cwd': ROOT_DIR, | 297 'initial_cwd': unicode(ROOT_DIR), |
278 'pid': self._ROOT_PID, | 298 'pid': self._ROOT_PID, |
279 }, | 299 }, |
280 } | 300 } |
281 self.assertEquals(expected, self._load_context(lines, ROOT_DIR)) | 301 self.assertEquals(expected, self._load_context(lines, ROOT_DIR)) |
282 | 302 |
283 def test_open(self): | 303 def test_open(self): |
284 lines = [ | 304 lines = [ |
285 (self._ROOT_PID, | 305 (self._ROOT_PID, |
286 'execve("../out/unittests", ' | 306 'execve("../out/unittests", ' |
287 '["../out/unittests"...], [/* 44 vars */]) = 0'), | 307 '["../out/unittests"...], [/* 44 vars */]) = 0'), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 'size': -1, | 362 'size': -1, |
343 }, | 363 }, |
344 ] | 364 ] |
345 self._test_lines(lines, '/home/foo_bar_user/src', files) | 365 self._test_lines(lines, '/home/foo_bar_user/src', files) |
346 | 366 |
347 | 367 |
348 if __name__ == '__main__': | 368 if __name__ == '__main__': |
349 VERBOSE = '-v' in sys.argv | 369 VERBOSE = '-v' in sys.argv |
350 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | 370 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
351 unittest.main() | 371 unittest.main() |
OLD | NEW |