1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import unittest
import sys
FILE_NAME = os.path.abspath(__file__)
ROOT_DIR = os.path.dirname(FILE_NAME)
import trace_inputs
def join_norm(*args):
"""Joins and normalizes path in a single step."""
return unicode(os.path.normpath(os.path.join(*args)))
class TraceInputs(unittest.TestCase):
def test_process_quoted_arguments(self):
test_cases = (
('"foo"', ['foo']),
('"foo", "bar"', ['foo', 'bar']),
('"foo"..., "bar"', ['foo', 'bar']),
('"foo", "bar"...', ['foo', 'bar']),
(
'"/browser_tests", "--type=use,comma"',
['/browser_tests', '--type=use,comma']
),
(
'"/browser_tests", "--ignored=\\" --type=renderer \\""',
['/browser_tests', '--ignored=" --type=renderer "']
),
)
for actual, expected in test_cases:
self.assertEquals(expected, trace_inputs.process_quoted_arguments(actual))
def test_variable_abs(self):
value = trace_inputs.Results.File(None, '/foo/bar', False)
actual = value.replace_variables({'$FOO': '/foo'})
self.assertEquals('$FOO/bar', actual.path)
self.assertEquals('$FOO/bar', actual.full_path)
self.assertEquals(True, actual.tainted)
def test_variable_rel(self):
value = trace_inputs.Results.File('/usr', 'foo/bar', False)
actual = value.replace_variables({'$FOO': 'foo'})
self.assertEquals('$FOO/bar', actual.path)
self.assertEquals(os.path.join('/usr', '$FOO/bar'), actual.full_path)
self.assertEquals(True, actual.tainted)
def test_native_case_windows(self):
if sys.platform != 'win32':
return
windows_path = os.environ['SystemRoot']
self.assertEquals(
trace_inputs.get_native_path_case(windows_path.lower()),
trace_inputs.get_native_path_case(windows_path.upper()))
if sys.platform != 'win32':
class StraceInputs(unittest.TestCase):
# Represents the root process pid (an arbitrary number).
_ROOT_PID = 27
_CHILD_PID = 14
_GRAND_CHILD_PID = 70
@staticmethod
def _load_context(lines, initial_cwd):
context = trace_inputs.Strace.Context(lambda _: False, initial_cwd)
for line in lines:
context.on_line(*line)
return context.to_results().flatten()
def _test_lines(self, lines, initial_cwd, files, command=None):
filepath = join_norm(initial_cwd, '../out/unittests')
command = command or ['../out/unittests']
expected = {
'root': {
'children': [],
'command': command,
'executable': filepath,
'files': files,
'initial_cwd': initial_cwd,
'pid': self._ROOT_PID,
}
}
if not files:
expected['root']['command'] = None
expected['root']['executable'] = None
self.assertEquals(expected, self._load_context(lines, initial_cwd))
def test_execve(self):
lines = [
(self._ROOT_PID,
'execve("/home/foo_bar_user/out/unittests", '
'["/home/foo_bar_user/out/unittests", '
'"--gtest_filter=AtExitTest.Basic"], [/* 44 vars */]) = 0'),
(self._ROOT_PID,
'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'),
]
files = [
{
'path': u'/home/foo_bar_user/out/unittests',
'size': -1,
},
{
'path': u'/home/foo_bar_user/src/out/unittests.log',
'size': -1,
},
]
command = [
'/home/foo_bar_user/out/unittests', '--gtest_filter=AtExitTest.Basic',
]
self._test_lines(lines, '/home/foo_bar_user/src', files, command)
def test_empty(self):
try:
self._load_context([], None)
self.fail()
except trace_inputs.TracingFailure, e:
expected = (
'Found internal inconsitency in process lifetime detection',
None,
None,
None,
[])
self.assertEquals(expected, e.args)
def test_close(self):
lines = [
(self._ROOT_PID, 'close(7) = 0'),
]
self._test_lines(lines, '/home/foo_bar_user/src', [])
def test_clone(self):
# Grand-child with relative directory.
lines = [
(self._ROOT_PID,
'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID'
'|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' %
self._CHILD_PID),
(self._CHILD_PID,
'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID'
'|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' %
self._GRAND_CHILD_PID),
(self._GRAND_CHILD_PID,
'open("%s", O_RDONLY) = 76' % os.path.basename(FILE_NAME)),
]
size = os.stat(FILE_NAME).st_size
expected = {
'root': {
'children': [
{
'children': [
{
'children': [],
'command': None,
'executable': None,
'files': [
{
'path': unicode(FILE_NAME),
'size': size,
},
],
'initial_cwd': ROOT_DIR,
'pid': self._GRAND_CHILD_PID,
},
],
'command': None,
'executable': None,
'files': [],
'initial_cwd': ROOT_DIR,
'pid': self._CHILD_PID,
},
],
'command': None,
'executable': None,
'files': [],
'initial_cwd': ROOT_DIR,
'pid': self._ROOT_PID,
},
}
self.assertEquals(expected, self._load_context(lines, ROOT_DIR))
def test_clone_chdir(self):
# Grand-child with relative directory.
lines = [
(self._ROOT_PID,
'execve("../out/unittests", '
'["../out/unittests"...], [/* 44 vars */]) = 0'),
(self._ROOT_PID,
'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID'
'|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' %
self._CHILD_PID),
(self._CHILD_PID,
'chdir("/home_foo_bar_user/path1") = 0'),
(self._CHILD_PID,
'clone(child_stack=0, flags=CLONE_CHILD_CLEARTID'
'|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f5350f829d0) = %d' %
self._GRAND_CHILD_PID),
(self._GRAND_CHILD_PID,
'execve("../out/unittests", '
'["../out/unittests"...], [/* 44 vars */]) = 0'),
(self._ROOT_PID, 'chdir("/home_foo_bar_user/path2") = 0'),
(self._GRAND_CHILD_PID,
'open("random.txt", O_RDONLY) = 76'),
]
expected = {
'root': {
'children': [
{
'children': [
{
'children': [],
'command': ['../out/unittests'],
'executable': '/home_foo_bar_user/out/unittests',
'files': [
{
'path': u'/home_foo_bar_user/out/unittests',
'size': -1,
},
{
'path': u'/home_foo_bar_user/path1/random.txt',
'size': -1,
},
],
'initial_cwd': u'/home_foo_bar_user/path1',
'pid': self._GRAND_CHILD_PID,
},
],
# clone does not carry over the command and executable so it is
# clear if an execve() call was done or not.
'command': None,
'executable': None,
# This is important, since no execve call was done, it didn't
# touch the executable file.
'files': [],
'initial_cwd': unicode(ROOT_DIR),
'pid': self._CHILD_PID,
},
],
'command': ['../out/unittests'],
'executable': join_norm(ROOT_DIR, '../out/unittests'),
'files': [
{
'path': join_norm(ROOT_DIR, '../out/unittests'),
'size': -1,
},
],
'initial_cwd': unicode(ROOT_DIR),
'pid': self._ROOT_PID,
},
}
self.assertEquals(expected, self._load_context(lines, ROOT_DIR))
def test_open(self):
lines = [
(self._ROOT_PID,
'execve("../out/unittests", '
'["../out/unittests"...], [/* 44 vars */]) = 0'),
(self._ROOT_PID,
'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 8'),
]
files = [
{
'path': u'/home/foo_bar_user/out/unittests',
'size': -1,
},
{
'path': u'/home/foo_bar_user/src/out/unittests.log',
'size': -1,
},
]
self._test_lines(lines, '/home/foo_bar_user/src', files)
def test_open_resumed(self):
lines = [
(self._ROOT_PID,
'execve("../out/unittests", '
'["../out/unittests"...], [/* 44 vars */]) = 0'),
(self._ROOT_PID,
'open("out/unittests.log", O_WRONLY|O_CREAT|O_APPEND '
'<unfinished ...>'),
(self._ROOT_PID, '<... open resumed> ) = 3'),
]
files = [
{
'path': u'/home/foo_bar_user/out/unittests',
'size': -1,
},
{
'path': u'/home/foo_bar_user/src/out/unittests.log',
'size': -1,
},
]
self._test_lines(lines, '/home/foo_bar_user/src', files)
def test_sig_unexpected(self):
lines = [
(self._ROOT_PID, 'exit_group(0) = ?'),
]
self._test_lines(lines, '/home/foo_bar_user/src', [])
def test_stray(self):
lines = [
(self._ROOT_PID,
'execve("../out/unittests", '
'["../out/unittests"...], [/* 44 vars */]) = 0'),
(self._ROOT_PID,
') = ? <unavailable>'),
]
files = [
{
'path': u'/home/foo_bar_user/out/unittests',
'size': -1,
},
]
self._test_lines(lines, '/home/foo_bar_user/src', files)
if __name__ == '__main__':
VERBOSE = '-v' in sys.argv
logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR)
unittest.main()
|