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 @staticmethod | 115 @staticmethod |
106 def _load_context(lines, initial_cwd): | 116 def _load_context(lines, initial_cwd): |
107 context = trace_inputs.Strace.Context(lambda _: False, initial_cwd) | 117 context = trace_inputs.Strace.Context(lambda _: False, initial_cwd) |
108 for line in lines: | 118 for line in lines: |
109 context.on_line(*line) | 119 context.on_line(*line) |
110 return context.to_results().flatten() | 120 return context.to_results().flatten() |
111 | 121 |
112 def _test_lines(self, lines, initial_cwd, files, command=None): | 122 def _test_lines(self, lines, initial_cwd, files, command=None): |
| 123 filepath = join_norm(initial_cwd, '../out/unittests') |
113 command = command or ['../out/unittests'] | 124 command = command or ['../out/unittests'] |
114 expected = { | 125 expected = { |
115 'root': { | 126 'root': { |
116 'children': [], | 127 'children': [], |
117 'command': None, | 128 'command': command, |
118 'executable': None, | 129 'executable': filepath, |
119 'files': files, | 130 'files': files, |
120 'initial_cwd': initial_cwd, | 131 'initial_cwd': initial_cwd, |
121 'pid': 27, | 132 'pid': 27, |
122 } | 133 } |
123 } | 134 } |
124 if not files: | 135 if not files: |
125 expected['root']['command'] = None | 136 expected['root']['command'] = None |
126 expected['root']['executable'] = None | 137 expected['root']['executable'] = None |
127 self.assertEquals(expected, self._load_context(lines, initial_cwd)) | 138 self.assertEquals(expected, self._load_context(lines, initial_cwd)) |
128 | 139 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 def test_clone_chdir(self): | 220 def test_clone_chdir(self): |
210 # Grand-child with relative directory. | 221 # Grand-child with relative directory. |
211 lines = [ | 222 lines = [ |
212 (27, 'execve("../out/unittests", ' | 223 (27, 'execve("../out/unittests", ' |
213 '["../out/unittests"...], [/* 44 vars */]) = 0'), | 224 '["../out/unittests"...], [/* 44 vars */]) = 0'), |
214 (27, 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | 225 (27, 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' |
215 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = 14'), | 226 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = 14'), |
216 (14, 'chdir("/home_foo_bar_user/path1") = 0'), | 227 (14, 'chdir("/home_foo_bar_user/path1") = 0'), |
217 (14, 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' | 228 (14, 'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID' |
218 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = 70'), | 229 '|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = 70'), |
| 230 (70, 'execve("../out/unittests", ' |
| 231 '["../out/unittests"...], [/* 44 vars */]) = 0'), |
219 (27, 'chdir("/home_foo_bar_user/path2") = 0'), | 232 (27, 'chdir("/home_foo_bar_user/path2") = 0'), |
220 (70, 'open("random.txt", O_RDONLY) = 76'), | 233 (70, 'open("random.txt", O_RDONLY) = 76'), |
221 ] | 234 ] |
222 expected = { | 235 expected = { |
223 'root': { | 236 'root': { |
224 'children': [ | 237 'children': [ |
225 { | 238 { |
226 'children': [ | 239 'children': [ |
227 { | 240 { |
228 'children': [], | 241 'children': [], |
229 'command': None, | 242 'command': ['../out/unittests'], |
230 'executable': None, | 243 'executable': '/home_foo_bar_user/out/unittests', |
231 'files': [ | 244 'files': [ |
232 { | 245 { |
| 246 'path': u'/home_foo_bar_user/out/unittests', |
| 247 'size': -1, |
| 248 }, |
| 249 { |
233 'path': u'/home_foo_bar_user/path1/random.txt', | 250 'path': u'/home_foo_bar_user/path1/random.txt', |
234 'size': -1, | 251 'size': -1, |
235 }, | 252 }, |
236 ], | 253 ], |
237 'initial_cwd': '/home_foo_bar_user/path1', | 254 'initial_cwd': u'/home_foo_bar_user/path1', |
238 'pid': 70, | 255 'pid': 70, |
239 }, | 256 }, |
240 ], | 257 ], |
| 258 # clone does not carry over the command and executable so it is |
| 259 # clear if an execve() call was done or not. |
241 'command': None, | 260 'command': None, |
242 'executable': None, | 261 'executable': None, |
243 # This is important, since no execve call was done, it didn't | 262 # This is important, since no execve call was done, it didn't |
244 # touch the executable file. | 263 # touch the executable file. |
245 'files': [], | 264 'files': [], |
246 'initial_cwd': ROOT_DIR, | 265 'initial_cwd': unicode(ROOT_DIR), |
247 'pid': 14, | 266 'pid': 14, |
248 }, | 267 }, |
249 ], | 268 ], |
250 'command': None, | 269 'command': ['../out/unittests'], |
251 'executable': None, | 270 'executable': join_norm(ROOT_DIR, '../out/unittests'), |
252 'files': [ | 271 'files': [ |
253 { | 272 { |
254 'path': join_norm(ROOT_DIR, '../out/unittests'), | 273 'path': join_norm(ROOT_DIR, '../out/unittests'), |
255 'size': -1, | 274 'size': -1, |
256 }, | 275 }, |
257 ], | 276 ], |
258 'initial_cwd': ROOT_DIR, | 277 'initial_cwd': unicode(ROOT_DIR), |
259 'pid': 27, | 278 'pid': 27, |
260 }, | 279 }, |
261 } | 280 } |
262 self.assertEquals(expected, self._load_context(lines, ROOT_DIR)) | 281 self.assertEquals(expected, self._load_context(lines, ROOT_DIR)) |
263 | 282 |
264 def test_open(self): | 283 def test_open(self): |
265 lines = [ | 284 lines = [ |
266 (27, 'execve("../out/unittests", ' | 285 (27, 'execve("../out/unittests", ' |
267 '["../out/unittests"...], [/* 44 vars */]) = 0'), | 286 '["../out/unittests"...], [/* 44 vars */]) = 0'), |
268 (27, 'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'), | 287 (27, 'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'), |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 'size': -1, | 336 'size': -1, |
318 }, | 337 }, |
319 ] | 338 ] |
320 self._test_lines(lines, '/home/foo_bar_user/src', files) | 339 self._test_lines(lines, '/home/foo_bar_user/src', files) |
321 | 340 |
322 | 341 |
323 if __name__ == '__main__': | 342 if __name__ == '__main__': |
324 VERBOSE = '-v' in sys.argv | 343 VERBOSE = '-v' in sys.argv |
325 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | 344 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
326 unittest.main() | 345 unittest.main() |
OLD | NEW |