-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_commands_ps.py
More file actions
528 lines (383 loc) · 15.4 KB
/
test_commands_ps.py
File metadata and controls
528 lines (383 loc) · 15.4 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# -*- coding: utf-8 -*-
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
import unittest
import gdb
import sys
import io
import re
import fnmatch
from crash.commands import CommandError, CommandLineError
from crash.commands.ps import PSCommand
import crash.types.task as tasks
def bad_command_line(fn, ignored=True):
"""Marks test to expect CommandLineError for unimplemented options"""
def test_decorator(fn):
def test_decorated(self, *args, **kwargs):
self.assertRaises(CommandLineError, fn, self, *args, **kwargs)
return test_decorated
test_decorator.__doc__ = fn.__doc__ + " (bad command line raises CommandLineError)"
return test_decorator
def unimplemented(fn, ignored=True):
"""Marks test to expect CommandError for unimplemented options"""
def test_decorator(fn):
def test_decorated(self, *args, **kwargs):
self.assertRaises(CommandError, fn, self, *args, **kwargs)
return test_decorated
test_decorator.__doc__ = fn.__doc__ + " (unimplemented command raises CommandError)"
return test_decorator
PF_KTHREAD = 0x200000
class TestCommandsPs(unittest.TestCase):
def setUp(self):
self.stdout = sys.stdout
self.redirected = io.StringIO()
sys.stdout = self.redirected
self.command = PSCommand()
self.do_output = False
def tearDown(self):
sys.stdout = self.stdout
if self.do_output:
print(self._output())
def _output(self):
return self.redirected.getvalue()
def output(self):
try:
return self.output_list
except AttributeError:
self.output_list = self._output().split("\n")
return self.output_list
def output_lines(self):
output = self.output()
return len(output) - 1
def get_wildcard_regex(self, wildcard):
return re.compile(fnmatch.translate(wildcard))
def check_line_count(self, count):
self.assertTrue(self.output_lines() == count)
def check_header(self, expected):
header = self.output()[0]
self.assertTrue(re.match(expected, header) is not None)
def check_task_header(self):
regex = "\s+PID\s+PPID\s+CPU\s+TASK\s+ST\s+%MEM\s+VSZ\s+RSS\s+COMM"
self.check_header(regex)
def check_kstack_header(self):
regex = "\s+PID\s+PPID\s+CPU\s+KSTACK\s+ST\s+%MEM\s+VSZ\s+RSS\s+COMM"
self.check_header(regex)
def check_threadnum_header(self):
regex = "\s+PID\s+PPID\s+CPU\s+THREAD#\s+ST\s+%MEM\s+VSZ\s+RSS\s+COMM"
self.check_header(regex)
def check_body(self, regex, start=1):
comp = re.compile(regex)
lines = 0
for line in self.output()[start:-1]:
self.assertTrue(comp.match(line) is not None)
lines += 1
self.assertTrue(lines > 0)
def check_threadnum_output(self):
regex = ">?\s+\d+\s+\d+\s+\d+\s+\d+\s+[A-Z]+\s+[\d\.]+\s+\d+\s+\d+\s+.*"
self.check_body(regex)
def check_normal_output(self):
regex = ">?\s+\d+\s+\d+\s+\d+\s+[\d+a-f]+\s+[A-Z]+\s+[\d\.]+\s+\d+\s+\d+\s+.*"
self.check_body(regex)
def check_last_run_output(self):
regex = "\[\d+\]\s+\[[A-Z][A-Z]\]\s+PID:\s+\d+\s+TASK:\s+[\da-f]+\s+CPU:\s+\d+\s+COMMAND: \".*\""
self.check_body(regex, 0)
def check_no_matches_output(self):
self.check_header('No matches for.*')
lines = self.output_lines()
self.assertTrue(lines == 1)
def is_kernel_thread(self, task):
return (int(task['flags']) & PF_KTHREAD)
def is_user_task(self, task):
return not self.is_kernel_thread(task)
def task_name(self, task_struct):
return task_struct['comm'].string()
def count_tasks(self, test=None, regex=None):
count = 0
for task in tasks.for_each_all_tasks():
if test is not None and not test(task):
continue
if regex is None or regex.match(self.task_name(task)):
count += 1
return count
def count_kernel_tasks(self, regex=None):
return self.count_tasks(self.is_kernel_thread, regex)
def count_user_tasks(self, regex=None):
return self.count_tasks(self.is_user_task, regex)
def count_thread_group_leaders(self, regex=None):
count = 0
for task in tasks.for_each_thread_group_leader():
if regex is None or regex.match(self.task_name(task)):
count += 1
return count
def test_ps_empty(self):
self.command.invoke_uncaught("")
self.assertTrue(self.output_lines() > 1)
def test_ps_wildcard(self):
self.command.invoke_uncaught("*worker*")
regex = self.get_wildcard_regex("*worker*")
self.check_line_count(self.count_tasks(regex=regex) + 1)
def test_ps_bad_wildcard(self):
"""Test `ps *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("*BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_k(self):
"""Test `ps -k' outputs all (and only) kernel threads"""
self.command.invoke_uncaught("-k")
lines = self.output_lines()
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_kernel_tasks() + 1)
def test_ps_k_wildcard(self):
"""Test `ps -k *wonder*' outputs only matching kernel threads"""
self.command.invoke_uncaught("-k *worker*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*worker*")
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_kernel_tasks(regex) + 1)
def test_ps_k_bad_wildcard(self):
"""Test `ps -k *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-k *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_u(self):
"""Test `ps -u' outputs all (and only) user tasks"""
self.command.invoke_uncaught("-u")
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_user_tasks() + 1)
def test_ps_u_wildcard(self):
"""Test `ps -u *wonder*' outputs only matching user tasks"""
self.command.invoke_uncaught("-u *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_user_tasks(regex) + 1)
def test_ps_u_bad_wildcard(self):
"""Test `ps -u *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-u *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_g(self):
"""Test `ps -G' outputs all (and only) thread group leaders"""
self.command.invoke_uncaught("-G")
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_thread_group_leaders() + 1)
def test_ps_g_wildcard(self):
"""Test `ps -G *nscd*' outputs only matching thread group leaders"""
self.command.invoke_uncaught("-G *nscd*")
regex = self.get_wildcard_regex("*nscd*")
self.check_task_header()
self.check_normal_output()
self.check_line_count(self.count_thread_group_leaders() + 1)
def test_ps_g_bad_wildcard(self):
"""Test `ps -G *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-G *BaDWiLdCaRd2019*")
self.check_no_matches_output()
@bad_command_line
def test_ps_uk(self):
"""Test `ps -u -k'"""
self.command.invoke_uncaught("-u -k")
@bad_command_line
def test_ps_uk_wildcard(self):
"""Test `ps -u -k *'"""
self.command.invoke_uncaught("-u -k *")
@bad_command_line
def test_ps_uG(self):
"""Test `ps -u -G'"""
self.command.invoke_uncaught("-u -k")
@bad_command_line
def test_ps_uG_wildcard(self):
"""Test `ps -u -G *'"""
self.command.invoke_uncaught("-u -k *")
@bad_command_line
def test_ps_kG(self):
"""Test `ps -k -G'"""
self.command.invoke_uncaught("-k -G")
@bad_command_line
def test_ps_kG_wildcard(self):
"""Test `ps -k -G *'"""
self.command.invoke_uncaught("-k -G *")
@bad_command_line
def test_ps_ukG(self):
"""Test `ps -u -k -G'"""
self.command.invoke_uncaught("-u -k -G")
@bad_command_line
def test_ps_ukG_wildcard(self):
"""Test `ps -u -k -G *'"""
self.command.invoke_uncaught("-u -k -G *")
def test_ps_s(self):
"""Test `ps -s'"""
self.command.invoke_uncaught("-s")
self.check_kstack_header()
self.check_normal_output()
self.check_line_count(self.count_tasks() + 1)
def test_ps_s_wildcard(self):
"""Test `ps -s *nscd*'"""
self.command.invoke_uncaught("-s *nscd*")
self.check_kstack_header()
self.check_normal_output()
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_tasks(regex=regex) + 1)
def test_ps_s_bad_wildcard(self):
"""Test `ps -s *BaDWiLdCaRd2019*'"""
self.command.invoke_uncaught("-s *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_n(self):
"""Test `ps -n'"""
self.command.invoke_uncaught("-n")
self.check_threadnum_header()
self.check_threadnum_output()
self.check_line_count(self.count_tasks() + 1)
def test_ps_n_wildcard(self):
"""Test `ps -n *nscd*'"""
self.command.invoke_uncaught("-n *nscd*")
self.check_threadnum_header()
self.check_threadnum_output()
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_tasks(regex=regex) + 1)
def test_ps_n_bad_wildcard(self):
"""Test `ps -n *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-n *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_nu(self):
"""Test `ps -n -u'"""
self.command.invoke_uncaught("-n -u")
self.check_threadnum_header()
self.check_threadnum_output()
self.check_line_count(self.count_user_tasks() + 1)
def test_ps_nu_wildcard(self):
"""Test `ps -n -u *nscd*'"""
self.command.invoke_uncaught("-n -u *nscd*")
self.check_threadnum_header()
self.check_threadnum_output()
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_user_tasks(regex) + 1)
def test_ps_nu_bad_wildcard(self):
"""Test `ps -n -u *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-n -u *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_nk(self):
"""Test `ps -n -k'"""
self.command.invoke_uncaught("-n -k")
self.check_threadnum_header()
self.check_threadnum_output()
self.check_line_count(self.count_kernel_tasks() + 1)
def test_ps_nk_wildcard(self):
"""Test `ps -n -k *worker*'"""
self.command.invoke_uncaught("-n -k *worker*")
self.check_threadnum_header()
self.check_threadnum_output()
regex = self.get_wildcard_regex("*worker*")
self.check_line_count(self.count_kernel_tasks(regex) + 1)
def test_ps_nk_bad_wildcard(self):
"""Test `ps -n -k *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-n -k *BaDWiLdCaRd2019*")
self.check_no_matches_output()
def test_ps_nG(self):
"""Test `ps -n -G'"""
self.command.invoke_uncaught("-n -G")
self.check_threadnum_header()
self.check_threadnum_output()
self.check_line_count(self.count_thread_group_leaders() + 1)
def test_ps_nG_wildcard(self):
"""Test `ps -n -G *nscd*'"""
self.command.invoke_uncaught("-n -G *nscd*")
self.check_threadnum_header()
self.check_threadnum_output()
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_thread_group_leaders(regex) + 1)
def test_ps_nG_bad_wildcard(self):
"""Test `ps -n -G *BaDWiLdCaRd2019*' returns no matches output"""
self.command.invoke_uncaught("-n -G *BaDWiLdCaRd2019*")
self.check_no_matches_output()
@unimplemented
def test_ps_t(self):
"""Test `ps -t'"""
self.command.invoke_uncaught("-t")
# Check format
self.check_line_count(self.count_tasks())
@unimplemented
def test_ps_t_wildcard(self):
"""Test `ps -t *nscd*'"""
self.command.invoke_uncaught("-t *nscd*")
# Check format
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_tasks(regex=regex))
def test_ps_l(self):
"""Test `ps -l'"""
self.command.invoke_uncaught("-l")
# No header to test
self.check_last_run_output()
self.check_line_count(self.count_tasks())
def test_ps_l_wildcard(self):
"""Test `ps -l *nscd*'"""
self.command.invoke_uncaught("-l *nscd*")
# No header to test
self.check_last_run_output()
regex = self.get_wildcard_regex("*nscd*")
self.check_line_count(self.count_tasks(regex=regex))
@unimplemented
def test_ps_p(self):
"""Test `ps -p'"""
self.command.invoke_uncaught("-p")
lines = self.output_lines()
self.assertTrue(lines > 1)
@unimplemented
def test_ps_p_wildcard(self):
"""Test `ps -p *nscd*'"""
self.command.invoke_uncaught("-p *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.assertTrue(lines > 1)
@unimplemented
def test_ps_c(self):
"""Test `ps -c'"""
self.command.invoke_uncaught("-c")
lines = self.output_lines()
self.assertTrue(lines > 1)
@unimplemented
def test_ps_c_wildcard(self):
"""Test `ps -c *nscd*'"""
self.command.invoke_uncaught("-c *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.assertTrue(lines > 1)
@unimplemented
def test_ps_a(self):
"""Test `ps -a'"""
self.command.invoke_uncaught("-a")
lines = self.output_lines()
self.assertTrue(lines > 1)
@unimplemented
def test_ps_a_wildcard(self):
"""Test `ps -a *nscd*'"""
self.command.invoke_uncaught("-a *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.assertTrue(lines > 1)
@unimplemented
def test_ps_g(self):
"""Test `ps -g'"""
self.command.invoke_uncaught("-g")
lines = self.output_lines()
self.assertTrue(lines > 1)
@unimplemented
def test_ps_g_wildcard(self):
"""Test `ps -g *nscd*'"""
self.command.invoke_uncaught("-g *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.assertTrue(lines > 1)
@unimplemented
def test_ps_r(self):
"""Test `ps -r'"""
self.command.invoke_uncaught("-r")
lines = self.output_lines()
self.assertTrue(lines > 1)
@unimplemented
def test_ps_r_wildcard(self):
"""Test `ps -r *nscd*'"""
self.command.invoke_uncaught("-r *nscd*")
lines = self.output_lines()
regex = self.get_wildcard_regex("*nscd*")
self.assertTrue(lines > 1)