-
Notifications
You must be signed in to change notification settings - Fork 244
/
main_test.py
118 lines (101 loc) · 4.57 KB
/
main_test.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
from unittest import mock
from .base_test_case import BaseTestCase
from pyt.__main__ import discover_files, main
class MainTest(BaseTestCase):
@mock.patch('pyt.__main__.discover_files')
@mock.patch('pyt.__main__.parse_args')
@mock.patch('pyt.__main__.find_vulnerabilities')
@mock.patch('pyt.formatters.text')
def test_text_output(self, mock_text, mock_find_vulnerabilities, mock_parse_args, mock_discover_files):
mock_find_vulnerabilities.return_value = 'stuff'
example_file = 'examples/vulnerable_code/inter_command_injection.py'
output_file = 'mocked_outfile'
import pyt.formatters.text
mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
project_root=None,
baseline=None,
formatter=pyt.formatters.text,
output_file=output_file,
only_unsanitised=False,
)
with self.assertRaises(SystemExit):
main(['parse_args is mocked'])
assert mock_text.report.call_count == 1
mock_text.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file,
True,
)
@mock.patch('pyt.__main__.discover_files')
@mock.patch('pyt.__main__.parse_args')
@mock.patch('pyt.__main__.find_vulnerabilities')
@mock.patch('pyt.formatters.text')
def test_no_vulns_found(self, mock_text, mock_find_vulnerabilities, mock_parse_args, mock_discover_files):
mock_find_vulnerabilities.return_value = []
example_file = 'examples/vulnerable_code/inter_command_injection.py'
output_file = 'mocked_outfile'
import pyt.formatters.text
mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
project_root=None,
baseline=None,
formatter=pyt.formatters.text,
output_file=output_file,
only_unsanitised=True,
)
main(['parse_args is mocked']) # No SystemExit
assert mock_text.report.call_count == 1
mock_text.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file,
False,
)
@mock.patch('pyt.__main__.discover_files')
@mock.patch('pyt.__main__.parse_args')
@mock.patch('pyt.__main__.find_vulnerabilities')
@mock.patch('pyt.formatters.json')
def test_json_output(self, mock_json, mock_find_vulnerabilities, mock_parse_args, mock_discover_files):
mock_find_vulnerabilities.return_value = 'stuff'
example_file = 'examples/vulnerable_code/inter_command_injection.py'
output_file = 'mocked_outfile'
import pyt.formatters.json
mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
project_root=None,
baseline=None,
formatter=pyt.formatters.json,
output_file=output_file,
only_unsanitised=False,
)
with self.assertRaises(SystemExit):
main(['parse_args is mocked'])
assert mock_json.report.call_count == 1
mock_json.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file,
True,
)
class DiscoverFilesTest(BaseTestCase):
def test_targets_with_no_excluded(self):
targets = ["examples/vulnerable_code/inter_command_injection.py"]
excluded_files = ""
included_files = discover_files(targets, excluded_files)
expected = ["examples/vulnerable_code/inter_command_injection.py"]
self.assertListEqual(included_files, expected)
def test_targets_with_exluded(self):
targets = ["examples/vulnerable_code/inter_command_injection.py"]
excluded_files = "examples/vulnerable_code/inter_command_injection.py"
included_files = discover_files(targets, excluded_files)
expected = []
self.assertListEqual(included_files, expected)
def test_targets_with_recursive(self):
targets = ["examples/vulnerable_code/"]
excluded_files = ""
included_files = discover_files(targets, excluded_files, True)
self.assertEqual(len(included_files), 34)
def test_targets_with_recursive_and_excluded(self):
targets = ["examples/vulnerable_code/"]
excluded_files = "inter_command_injection.py"
included_files = discover_files(targets, excluded_files, True)
self.assertEqual(len(included_files), 33)