-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_gdt.py
762 lines (588 loc) · 28.4 KB
/
test_gdt.py
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import gdt
import mock
import os
import pytest
import socket
JSON_DATA = {"gdb_path": "/gdb",
"project_root_path": "/project",
"symbol_root_path" : "/symbol",
"excluded_dir_names": [".vscode", ".git"],
"target_ip": gdt.DEFAULT_IP,
"target_debug_port": gdt.DEFAULT_DEBUG_PORT,
"target_user": gdt.DEFAULT_USER,
"target_password": gdt.DEFAULT_PASSWORD,
"target_prompt": gdt.DEFAULT_PROMPT}
PROGRAM_NAME = 'test_program'
PROGRAM_BASENAME = PROGRAM_NAME + ".full"
PROGRAM_PATH = os.path.join(JSON_DATA['project_root_path'], PROGRAM_BASENAME)
PROGRAM_MOCK = mock.MagicMock()
PROGRAM_MOCK.name = PROGRAM_PATH
CONFIG_MOCK = mock.MagicMock()
CONFIG_MOCK.name = '/config.json'
@pytest.fixture
def os_mocks(mocker):
return mocker.patch.multiple('os.path', isdir=mock.MagicMock(return_value=True),
isfile=mock.MagicMock(return_value=True),
abspath=mock.MagicMock(side_effect=lambda path: path))
@pytest.fixture
def json_mocks(mocker):
return mocker.patch.multiple('json', load=mock.MagicMock(return_value=JSON_DATA),
dump=mock.MagicMock())
@pytest.fixture
def mock_open(mocker):
return mocker.patch('__builtin__.open', mocker.mock_open(mock=mocker.MagicMock(return_value='arg', read_data='test_file_data')))
class MockArgs:
config = CONFIG_MOCK
root = "/root"
symbols = "/symbols"
program = PROGRAM_MOCK
core = mock.MagicMock()
report = False
report_out = gdt.DEFAULT_CORE_REPORT_FILE
command_file = '/command_file'
input = mock.MagicMock()
other_target = False
breakpoints = ""
reload = None
class MockReportArgs:
report = False
report_out = ""
def __init__(self, report, report_out):
self.report = report
self.report_out = report_out
class TestUtilities(object):
@pytest.mark.parametrize('test_input, expected', [
('192.168.33.42', True),
('192.168.3z.42', False),
('192.266.33.266', False),
('192', False),
('test', False)
])
def test_validate_ipv4_address(self, test_input, expected):
assert (gdt.validate_ipv4_address(test_input) is not None) == expected
@pytest.mark.parametrize('test_input, expected', [
('0', True),
('65535', True),
('-1', False),
('65536', False)
])
def test_validate_port(self, test_input, expected):
assert (gdt.validate_port(test_input) is not None) == expected
@pytest.mark.parametrize('test_input, expected', [
(os.path.dirname(__file__), True),
('bobo', False)
])
def test_validate_dir(self, test_input, expected):
assert (gdt.validate_dir(test_input) is not None) == expected
@pytest.mark.parametrize('test_input, expected', [
('test.h', True),
('test.cc', True),
('test.cpp', True),
('test.c', True),
('test.cc', True),
('test.hpp', True),
('test.py', False),
('test.txt', False),
('test', False)
])
def test_is_cpp_file(self, test_input, expected):
assert gdt.is_cpp_file(test_input) == expected
@pytest.mark.parametrize('test_input, expected', [
('test.so', True),
('test.so.42', True),
('test.42.so', True),
('test.py', False),
('test.txt', False),
('test.a', False),
('test', False)
])
def test_is_shared_library(self, test_input, expected):
assert gdt.is_shared_library(test_input) == expected
def test_extract_filename(self):
assert gdt.extract_filename(__file__) == os.path.basename(__file__)[:-3]
@pytest.mark.parametrize('test_input, expected', [
('', ''),
('TestStr', os.path.join(os.getcwd(), 'TestStr')),
('/TestStr', os.path.join(os.getcwd(), '/TestStr')),
(r'\\TestStr', os.path.join(os.getcwd(), r'\\\\TestStr')),
(r'\\TestStr', os.path.join(os.getcwd(), r'\\\\TestStr')),
('Project/Test', os.path.join(os.getcwd(), 'Project', "Test")),
(r'Project\Test', os.path.join(os.getcwd(), r'Project\\Test')),
(r'Project\\Test', os.path.join(os.getcwd(), r'Project\\\\Test')),
('/Project/Test', os.path.join(os.getcwd(), '/Project', 'Test')),
(r'\Project\Test', os.path.join(os.getcwd(), r'\\Project\\Test')),
(r'\\Project\\Test', os.path.join(os.getcwd(), r'\\\\Project\\\\Test')),
])
def test_get_str_repr(self, test_input, expected):
assert gdt.get_str_repr(test_input) == expected
def test_verify_required_files_exist_when_missing_config_dir(self):
old_dir = gdt.GDT_CONFIG_DIR
gdt.GDT_CONFIG_DIR = 'bobo'
with pytest.raises(gdt.RequiredFileMissing):
gdt.verify_required_files_exist()
gdt.GDT_CONFIG_DIR = old_dir
def test_verify_required_files_exist_when_missing_commands_file(self, tmpdir):
gdt.GDT_CONFIG_DIR = tmpdir.strpath
gdt.CORE_COMMANDS_FILE = tmpdir.join("corecommands").strpath
gdt.DEFAULT_GDBINIT_FILE = tmpdir.join("gdbinit").strpath
tmpdir.join("gdbinit").write("")
with pytest.raises(gdt.RequiredFileMissing):
gdt.verify_required_files_exist()
def test_verify_required_files_exist(self, tmpdir):
gdt.GDT_CONFIG_DIR = tmpdir.strpath
gdt.CORE_COMMANDS_FILE = tmpdir.join("corecommands").strpath
gdt.DEFAULT_GDBINIT_FILE = tmpdir.join("gdbinit").strpath
tmpdir.join("corecommands").write("")
tmpdir.join("gdbinit").write("")
try:
gdt.verify_required_files_exist()
except Exception as err:
pytest.fail("Unexpected error: " + err.message)
def test_target_class(self):
target = gdt.Target('192.168.33.42', 'user', 'password', '4242')
assert target.full_address() == '192.168.33.42:4242'
def test_gdbcommand_class(self):
gdb_command = gdt.GDBCommand('prefix', 'value')
assert str(gdb_command) == 'prefix value'
class TestConfigGenerator(object):
def test_config_generator(self, mocker, mock_open):
mock_dump = mocker.patch('json.dump')
mocker.patch('gdt.ConfigFileOption', autospec=True)
generator = gdt.ConfigFileGenerator()
assert not generator.run_gdb
mock_open.assert_called_once_with(gdt.GDT_CONFIG_FILE, 'w')
mock_dump.assert_called_once()
class TestConfigFileOption(object):
@pytest.fixture
def config_file_option(self):
return gdt.ConfigFileOption(key="key", ask_user=False)
@pytest.mark.parametrize('default_value, value, validate_func, expected', [
("default_value", "", lambda value: None, 'default_value'),
("default_value", "value", lambda value: 'value', 'value')
])
def test_init_value(self, mocker, config_file_option, default_value, value, validate_func, expected):
mock_input = mocker.patch('__builtin__.raw_input', return_value=value)
config_file_option.ask_user = True
config_file_option.default_value = default_value
config_file_option.validate_func = validate_func
config_file_option.init_value()
mock_input.assert_called_once()
assert config_file_option.value == expected
def test_init_value_no_user_input(self, mocker, config_file_option):
mock_input = mocker.patch('__builtin__.raw_input', return_value='bad_value')
config_file_option.ask_user = False
config_file_option.init_value()
mock_input.assert_not_called()
assert config_file_option.value is None
@pytest.mark.parametrize('default_value, initial_desc, expected', [
("value", "key", "key [Default: \"value\"]: "),
("", "key", "key: ")
])
def test_init_desc(self, config_file_option, default_value, initial_desc, expected):
config_file_option.default_value = default_value
config_file_option.desc = initial_desc
config_file_option.init_desc()
assert config_file_option.desc == expected
def test_get_valid_value(self, config_file_option, mocker):
value = 'valid_value'
mock_input = mocker.patch('__builtin__.raw_input', return_value=value)
config_file_option.validate_func = mocker.MagicMock(side_effect=[None, value])
assert config_file_option.value is None
config_file_option.get_valid_value()
assert config_file_option.value == value
mock_input.assert_called_once()
config_file_option.validate_func.assert_has_calls(calls=[mocker.call(None), mocker.call(value)])
class TestTelnetConnection(object):
response = 'telnet response'
@pytest.fixture
def session(self, mocker):
session_mock = mocker.MagicMock()
session_mock.read_until = mocker.MagicMock(return_value=self.response)
return session_mock
@pytest.fixture
def telnet(self, session, mocker):
connect_mock = mocker.patch('gdt.TelnetConnection.connect')
target = gdt.Target(gdt.DEFAULT_IP, 'user', 'passwd', gdt.DEFAULT_DEBUG_PORT)
port = gdt.DEFAULT_PROMPT
connection = gdt.TelnetConnection(target, port)
assert connection.prompt == gdt.DEFAULT_PROMPT
assert connection.session is None
assert target.ip == connection.target.ip
assert target.user == connection.target.user
assert target.password == connection.target.password
assert target.port == connection.target.port
connect_mock.assert_called_once()
connection.session = session
return connection
def test_read_response(self, telnet, session):
assert telnet.read_response(gdt.DEFAULT_PROMPT) == self.response
session.read_until.assert_called_once()
def test_send_command(self, telnet, session):
assert telnet.send_command('ls') == self.response
session.write.assert_called_once_with('ls\n')
session.read_until.assert_called_once()
def test_get_pid_of(self, mocker, telnet, session):
pid = '4242'
session.read_until = mocker.MagicMock(return_value=pid + ' test_program')
telnet.session = session
assert telnet.get_pid_of('test_program') == pid
session.write.assert_called_once_with(telnet.PID_CMD + 'test_program\n')
session.read_until.assert_called_once()
def test_change_prompt(self, telnet, session):
new_prompt = "$ "
assert telnet.prompt != new_prompt
telnet.change_prompt(new_prompt)
assert telnet.prompt == new_prompt
session.write.assert_called_once_with('PS1="' + new_prompt + '"\n')
assert session.read_until.call_count == 2
def test_connect(self, telnet, session, mocker):
mocker.stopall()
mocker.patch('telnetlib.Telnet', return_value=session)
session.read_until = mocker.MagicMock(return_value=telnet.prompt)
try:
telnet.connect()
session.read_until.assert_has_calls(
calls=[mocker.call('login: ', telnet.TIMEOUT),
mocker.call('Password:', telnet.TIMEOUT),
mocker.call(telnet.prompt, telnet.TIMEOUT)])
session.write.assert_has_calls(
calls=[mocker.call(telnet.target.user + '\n'),
mocker.call(telnet.target.password + '\n')])
except Exception as err:
pytest.fail("Unexpected error: " + err.message)
def test_connect_server_failed(self, telnet, mocker):
mocker.stopall()
mocker.patch('telnetlib.Telnet', side_effect=socket.error)
with pytest.raises(gdt.TelnetError):
telnet.connect()
def test_connect_bad_credentials(self, telnet, session, mocker):
mocker.stopall()
mocker.patch('telnetlib.Telnet', return_value=session)
session.read_until = mocker.MagicMock(return_value='login: ')
with pytest.raises(gdt.TelnetError):
telnet.connect()
def test_close_with_active_session(self, telnet):
telnet.session.reset_mock()
assert telnet.session is not None
telnet.close()
telnet.session.close.assert_called_once()
def test_close_with_no_session(self, telnet, session):
telnet.session.reset_mock()
telnet.session = None
telnet.close()
session.close.assert_not_called()
class TestBaseCommand(object):
def test_constructor(self, mock_open, os_mocks, json_mocks):
args = MockArgs()
cmd = gdt.BaseCommand(args)
assert cmd.run_gdb
assert cmd.json_data == JSON_DATA
assert cmd.config_file == args.config.name
assert cmd.gdb_path == os.path.abspath(JSON_DATA['gdb_path'])
assert cmd.excluded_dir_names == JSON_DATA["excluded_dir_names"]
return cmd
class TestGeneratedCommand(object):
@pytest.fixture
def cmd(self, mocker, mock_open, os_mocks, json_mocks):
check_dir_mock = mocker.patch('gdt.GeneratedCommand.check_dir_exists')
args = MockArgs()
cmd = gdt.GeneratedCommand(args)
assert cmd.run_gdb
assert cmd.json_data == JSON_DATA
assert cmd.config_file == args.config.name
assert cmd.gdb_path == os.path.abspath(JSON_DATA['gdb_path'])
assert cmd.excluded_dir_names == JSON_DATA["excluded_dir_names"]
assert 'program' in cmd.opts and cmd.opts['program'].prefix == 'file'
assert cmd.symbol_root_path == args.symbols
assert cmd.project_path == args.root
assert cmd.command_file == gdt.DEFAULT_COMMANDS_FILE
assert cmd.program_name == PROGRAM_NAME
check_dir_mock.assert_has_calls(
calls=[mocker.call(cmd.project_path, 'project'),
mocker.call(cmd.symbol_root_path, 'symbol')])
return cmd
def test_check_dir_success(self, cmd, mocker):
mocker.stopall()
validate_dir_mock = mocker.patch('gdt.validate_dir', return_value=True)
try:
cmd.check_dir_exists(cmd.project_path, 'test_dir')
validate_dir_mock.assert_called_once_with(cmd.project_path)
except Exception as err:
pytest.fail("Unexpected error: " + err.message)
def test_check_dir_fail(self, cmd, mocker):
mocker.stopall()
validate_dir_mock = mocker.patch('gdt.validate_dir', return_value=False)
with pytest.raises(IOError):
cmd.check_dir_exists(cmd.project_path, 'test_dir')
validate_dir_mock.assert_called_once_with(cmd.project_path)
def test_add_option(self, cmd):
assert 'key' not in cmd.opts
cmd.add_option("key", "option")
assert 'key' in cmd.opts
assert cmd.opts['key'] == 'option'
def test_init_search_paths_with_same_dirs(self, cmd, mocker):
search_paths_mock = mocker.patch('gdt.GeneratedCommand.generate_search_paths', return_value=(['/solib1', '/solib2'], ['/src1', '/src2']))
generate_path_mock = mocker.patch('gdt.GeneratedCommand.generate_search_path', return_value=[])
cmd.symbol_root_path = "/root"
cmd.project_path = "/root"
assert cmd.symbol_root_path == cmd.project_path
assert 'solib_path' not in cmd.opts
assert 'source_path' not in cmd.opts
cmd.init_search_paths()
assert 'solib_path' in cmd.opts
assert 'source_path' in cmd.opts
assert cmd.opts['solib_path'].prefix == 'set solib-search-path' and cmd.opts['solib_path'].value == '/solib1;/solib2'
assert cmd.opts['source_path'].prefix == 'dir' and cmd.opts['source_path'].value == '/src1;/src2'
generate_path_mock.assert_not_called()
search_paths_mock.assert_called_once()
@pytest.mark.skip(reason="This test needs to be updated because of refactoring generate_search_path")
def test_init_search_paths_with_different_dirs(self, cmd, mocker):
search_paths_mock = mocker.patch('gdt.GeneratedCommand.generate_search_paths', return_value=())
generate_path_mock = mocker.patch('gdt.GeneratedCommand.generate_search_path', return_value=['/src1', '/src2'])
assert cmd.symbol_root_path != cmd.project_path
assert 'solib_path' not in cmd.opts
assert 'source_path' not in cmd.opts
cmd.init_search_paths()
assert 'solib_path' in cmd.opts
assert 'source_path' in cmd.opts
assert cmd.opts['solib_path'].prefix == 'set solib-search-path' and cmd.opts['solib_path'].value == '/solib1;/solib2'
assert cmd.opts['source_path'].prefix == 'dir' and cmd.opts['source_path'].value == '/src1;/src2'
generate_path_mock.assert_called()
search_paths_mock.assert_not_called()
def test_generate_command_file(self, cmd, mocker, mock_open):
cmd.generate_command_file()
mock_open.assert_has_calls(calls=[
mocker.call(cmd.command_file, 'w'),
mocker.call(gdt.GDBINIT_FILE, 'r')
], any_order=True)
mock_open().write.assert_called()
def test_generate_solib_search_path(self, cmd, tmpdir, mocker):
mocker.stopall()
solib_dir = tmpdir.mkdir('solib_a')
solib_dir2 = tmpdir.mkdir('solib_z')
static_dir = tmpdir.mkdir('static')
src_dir = tmpdir.mkdir('src')
excluded_dir = tmpdir.mkdir('.git')
nested_solib_dir = solib_dir.mkdir('more_solib')
nested_solib_dir2 = src_dir.mkdir('libs')
solib_dir.join('shared_lib.so').write('')
solib_dir2.join('shared_lib.so.42').write('')
static_dir.join('static_lib.a').write('')
src_dir.join('main.cpp').write('')
excluded_dir.join('shared_lib_2.so').write('')
nested_solib_dir.join('test_lib.so').write('')
nested_solib_dir2.join('test.so.77').write('')
expected = [nested_solib_dir2.strpath, solib_dir2.strpath, nested_solib_dir.strpath, solib_dir.strpath]
cmd.symbol_root_path = tmpdir.strpath
actual = cmd.generate_search_path(cmd.symbol_root_path, cmd.update_solib_list)
assert expected == actual
def test_generate_solib_search_path_empty(self, cmd, tmpdir):
cmd.symbol_root_path = tmpdir.strpath
assert [] == cmd.generate_search_path(cmd.symbol_root_path, cmd.update_solib_list)
def test_generate_source_search_path(self, cmd, tmpdir, mocker):
mocker.stopall()
solib_dir = tmpdir.mkdir('solib')
static_dir = tmpdir.mkdir('static')
src_dir = tmpdir.mkdir('src')
excluded_dir = tmpdir.mkdir('.git')
source_dir2 = tmpdir.mkdir(PROGRAM_NAME)
nested_src_dir = src_dir.mkdir('nested')
nested_src_dir_2 = solib_dir.mkdir('src')
solib_dir.join('shared_lib.so').write('')
static_dir.join('static_lib.a').write('')
src_dir.join('main.cpp').write('')
source_dir2.join('utility.h').write('')
excluded_dir.join('excluded.cpp').write('')
nested_src_dir.join('other.c').write('')
nested_src_dir_2.join('other.cpp').write('')
expected = [source_dir2.strpath, nested_src_dir_2.strpath, src_dir.strpath, nested_src_dir.strpath]
cmd.program_name = PROGRAM_NAME
cmd.project_path = tmpdir.strpath
actual = cmd.generate_search_path(cmd.project_path, cmd.update_source_list)
assert expected == actual
def test_generate_source_search_path_empty(self, cmd, tmpdir):
cmd.symbol_root_path = tmpdir.strpath
assert [] == cmd.generate_search_path(cmd.project_path, cmd.update_source_list)
class TestCoreCommand(object):
@pytest.fixture
def core_cmd(self, mocker, mock_open, os_mocks, json_mocks):
search_path_mock = mocker.patch('gdt.GeneratedCommand.init_search_paths')
init_mock = mocker.patch('gdt.CoreCommand.init')
args = MockArgs()
cmd = gdt.CoreCommand(args)
assert cmd.run_gdb
assert cmd.json_data == JSON_DATA
assert cmd.config_file == args.config.name
assert cmd.gdb_path == os.path.abspath(JSON_DATA['gdb_path'])
assert cmd.excluded_dir_names == JSON_DATA["excluded_dir_names"]
assert 'core' in cmd.opts and cmd.opts['core'].prefix == 'core-file'
assert cmd.report_file == args.report_out
assert cmd.program_name == PROGRAM_NAME
search_path_mock.assert_called_once()
init_mock.assert_called_once()
return cmd
def test_init_with_no_report(self, mocker, core_cmd):
mocker.stopall()
gen_mock = mocker.patch('gdt.GeneratedCommand.generate_command_file')
gen_report_mock = mocker.patch('gdt.CoreCommand.generate_report_file')
core_cmd.init(MockReportArgs(False, ""))
gen_mock.assert_called_once()
gen_report_mock.assert_not_called()
def test_init_with_report(self, mocker, core_cmd):
mocker.stopall()
gen_mock = mocker.patch('gdt.GeneratedCommand.generate_command_file')
gen_report_mock = mocker.patch('gdt.CoreCommand.generate_report_file')
core_cmd.init(MockReportArgs(True, ""))
gen_mock.assert_called_once()
gen_report_mock.assert_called_once()
@pytest.mark.parametrize('generate_report_file', [False, True])
def test_validate_args_success(self, core_cmd, generate_report_file):
try:
core_cmd.validate_args(MockReportArgs(generate_report_file, gdt.DEFAULT_CORE_REPORT_FILE))
except Exception as err:
pytest.fail("Unexpected error: " + err.message)
def test_validate_args_fail(self, core_cmd):
with pytest.raises(gdt.InvalidArgs):
output_file = '/bobo'
assert output_file != gdt.DEFAULT_CORE_REPORT_FILE
core_cmd.validate_args(MockReportArgs(False, output_file))
def test_generate_report_file(self, core_cmd, mocker, mock_open):
core_cmd.generate_report_file()
mock_open.assert_has_calls(
[mocker.call(core_cmd.command_file, 'r+'),
mocker.call(gdt.CORE_COMMANDS_FILE, 'r')],
any_order=True)
mock_open().write.assert_has_calls(
[mocker.call('set logging overwrite on\n'),
mocker.call('set logging file ' + core_cmd.report_file + '\n'),
mocker.call('set logging on\n'),
mocker.call('set logging redirect on\n')])
class TestCmdFileCommand(object):
def test_constructor(self, mock_open, os_mocks, json_mocks, mocker):
update_mock = mocker.patch('gdt.CmdFileCommand.update_commands_file')
command_file = '/home/command_file'
args = MockArgs()
args.input.name = command_file
args.reload = True
cmd = gdt.CmdFileCommand(args)
assert cmd.run_gdb
assert cmd.json_data == JSON_DATA
assert cmd.gdb_path == os.path.abspath(JSON_DATA['gdb_path'])
assert cmd.command_file == command_file
update_mock.assert_called_once()
class TestRemoteCommand(object):
PID = '425679'
@pytest.fixture
def telnet(self, mocker):
telnet = mocker.patch('gdt.TelnetConnection', spec=gdt.TelnetConnection)
telnet().get_pid_of.return_value = self.PID
return telnet
@pytest.fixture
def remote_cmd(self, mocker, mock_open, telnet, os_mocks, json_mocks):
init_mock = mocker.patch('gdt.RemoteCommand.init')
args = MockArgs()
cmd = gdt.RemoteCommand(args)
assert cmd.run_gdb
assert cmd.json_data == JSON_DATA
assert cmd.gdb_path == JSON_DATA['gdb_path']
assert cmd.command_file == gdt.DEFAULT_COMMANDS_FILE
assert cmd.is_qnx_target != args.other_target
assert not cmd.is_unit_test
assert cmd.target.user == JSON_DATA['target_user']
assert cmd.target.password == JSON_DATA['target_password']
assert cmd.target.ip == JSON_DATA['target_ip']
assert cmd.target.port == JSON_DATA['target_debug_port']
assert cmd.source_separator == ';'
assert cmd.program_name == PROGRAM_NAME
init_mock.assert_called_once()
return cmd
def test_init_with_no_unittest(self, remote_cmd, mocker):
mocker.stopall()
search_mock = mocker.patch('gdt.GeneratedCommand.init_search_paths')
gen_mock = mocker.patch('gdt.GeneratedCommand.generate_command_file')
target_mock = mocker.patch('gdt.RemoteCommand.init_target')
pid_mock = mocker.patch('gdt.RemoteCommand.init_pid')
upload_mock = mocker.patch('gdt.RemoteCommand.init_unit_test')
breakpoint_mock = mocker.patch('gdt.RemoteCommand.init_breakpoints')
args = mock.sentinel
args.breakpoints = 'breakpoints'
remote_cmd.init(args)
search_mock.assert_called_once()
target_mock.assert_called_once()
pid_mock.assert_called_once()
upload_mock.assert_not_called()
breakpoint_mock.assert_called_once_with(args.breakpoints)
gen_mock.assert_called_once()
def test_init_with_unittest(self, remote_cmd, mocker):
mocker.stopall()
search_mock = mocker.patch('gdt.GeneratedCommand.init_search_paths')
gen_mock = mocker.patch('gdt.GeneratedCommand.generate_command_file')
target_mock = mocker.patch('gdt.RemoteCommand.init_target')
pid_mock = mocker.patch('gdt.RemoteCommand.init_pid')
upload_mock = mocker.patch('gdt.RemoteCommand.init_unit_test')
breakpoint_mock = mocker.patch('gdt.RemoteCommand.init_breakpoints')
args = mock.sentinel
args.breakpoints = 'breakpoints'
remote_cmd.is_unit_test = True
remote_cmd.init(args)
search_mock.assert_called_once()
target_mock.assert_called_once()
pid_mock.assert_not_called()
upload_mock.assert_called_once()
breakpoint_mock.assert_called_once_with(args.breakpoints)
gen_mock.assert_called_once()
def test_init_unit_test(self, remote_cmd, telnet, mocker):
assert 'upload' not in remote_cmd.opts
assert 'gtest_args' not in remote_cmd.opts
remote_cmd.init_unit_test()
expected_value = PROGRAM_PATH + " " + os.path.join(gdt.UNITTEST_OUTPUT_DIR, PROGRAM_BASENAME)
assert 'upload' in remote_cmd.opts
assert 'upload' == remote_cmd.opts['upload'].prefix
assert expected_value == remote_cmd.opts['upload'].value
assert 'gtest_args' in remote_cmd.opts
assert 'set args' == remote_cmd.opts['gtest_args'].prefix
assert '--gtest_color=yes --gtest_log_to_console' == remote_cmd.opts['gtest_args'].value
telnet().send_command.assert_has_calls(
[mocker.call('rm -rf ' + gdt.UNITTEST_OUTPUT_DIR),
mocker.call('mkdir -p ' + gdt.UNITTEST_OUTPUT_DIR)]
)
def test_init_breakpoints_with_file(self, remote_cmd, mocker):
assert 'breakpoint' not in remote_cmd.opts
args = mocker.MagicMock()
args.name = '/breakpoint_file'
remote_cmd.init_breakpoints(args)
assert 'breakpoint' in remote_cmd.opts
assert remote_cmd.opts['breakpoint'].prefix == 'source'
assert remote_cmd.opts['breakpoint'].value == '/breakpoint_file'
def test_init_breakpoints_without_file(self, remote_cmd):
assert 'breakpoint' not in remote_cmd.opts
remote_cmd.init_breakpoints(None)
assert 'breakpoint' not in remote_cmd.opts
def test_init_pid_with_running_process(self, remote_cmd):
assert 'pid' not in remote_cmd.opts
remote_cmd.init_pid()
remote_cmd.telnet.get_pid_of.assert_called_once_with(remote_cmd.program_name)
assert 'pid' in remote_cmd.opts
assert remote_cmd.opts['pid'].prefix == 'attach'
assert remote_cmd.opts['pid'].value == self.PID
def test_init_pid_with_unknown_process(self, remote_cmd, telnet):
telnet().get_pid_of.return_value = None
remote_cmd.program_name = 'unknown_program'
assert 'pid' not in remote_cmd.opts
remote_cmd.init_pid()
remote_cmd.telnet.get_pid_of.assert_called_once_with(remote_cmd.program_name)
assert 'pid' not in remote_cmd.opts
@pytest.mark.parametrize('is_qnx_target, prefix', [
(False, 'target extended-remote'),
(True, 'target qnx')
])
def test_init_target(self, remote_cmd, is_qnx_target, prefix):
target = gdt.Target(gdt.DEFAULT_IP, gdt.DEFAULT_USER, gdt.DEFAULT_PASSWORD, gdt.DEFAULT_DEBUG_PORT)
remote_cmd.program_name = PROGRAM_NAME
remote_cmd.is_qnx_target = is_qnx_target
remote_cmd.target = gdt.Target(gdt.DEFAULT_IP, gdt.DEFAULT_USER, gdt.DEFAULT_PASSWORD, gdt.DEFAULT_DEBUG_PORT)
remote_cmd.opts.clear()
remote_cmd.init_target()
assert 'target' in remote_cmd.opts
assert remote_cmd.opts['target'].prefix == prefix
assert remote_cmd.opts['target'].value == target.full_address()