-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_ssp.py
232 lines (194 loc) · 7.56 KB
/
test_ssp.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
import json
import tempfile
from io import StringIO
import pytest
from ssp import AllComponentsFilter
from ssp import ComponentFilter
from ssp import control_id
from ssp import CSVControlStatementReader
from ssp import PatternBuilder
from ssp import PSVControlStatementReader
class TestControlID(object):
@pytest.mark.parametrize(
"control,expected",
(
("", ""),
("1.2.3", "1.2.3"),
("AC-3", "AC-3"),
("AC-3(a)", "AC-3(a)"),
("1.2.3 foo", "1.2.3"),
("AC-3 foo", "AC-3"),
("[AC-3]", "AC-3"),
("[AC-3 foo", "AC-3"),
),
)
def test_control_id(self, control, expected):
assert control_id(control) == expected
class TestAllComponentFilter(object):
test_filter = AllComponentsFilter()
def test_all(self):
assert self.test_filter.filter(set(("A",))) == set(("A",))
assert self.test_filter.filter(set(("A", "B", "C"))) == set(("A", "B", "C"))
assert self.test_filter.filter(set()) == set()
class TestPatternBuilder(object):
def test_empty(self):
components = {"components": {}}
patterns = PatternBuilder(components, "S-Component").patterns()
assert patterns == []
def test_component(self):
components = {"components": {"Component A": {}}}
patterns = PatternBuilder(components, "S-Component").patterns()
assert patterns == [
{"label": "S-Component", "pattern": "Component A", "id": "Component A"}
]
def test_component_with_aka(self):
components = {"components": {"Component A": {"aka": ["Comp-A", "Comp A"]}}}
patterns = PatternBuilder(components, "S-Component").patterns()
assert len(patterns) == 3
assert {
"label": "S-Component",
"pattern": "Component A",
"id": "Component A",
} in patterns
assert {
"label": "S-Component",
"pattern": "Comp-A",
"id": "Component A",
} in patterns
assert {
"label": "S-Component",
"pattern": "Comp A",
"id": "Component A",
} in patterns
class TestComponentFilter(object):
@staticmethod
def filter_from_objects(objects):
string = json.dumps(objects)
stream = StringIO(string)
return ComponentFilter(stream)
def test_empty(self):
filter = self.filter_from_objects({})
assert filter.filter(set(("A", "B", "C"))) == set(("A", "B", "C"))
def test_not_components(self):
filter = self.filter_from_objects(dict(not_components=["A"]))
assert filter.filter(set(("A", "B"))) == set(("B"))
def test_known_components(self):
filter = self.filter_from_objects(dict(components=dict(A={})))
assert filter.filter(set(("A", "B"))) == set(("A", "B"))
assert filter.filter(set(("a", "B"))) == set(("A", "B"))
def test_aka_component(self):
filter = self.filter_from_objects(
dict(components=dict(A=dict(aka=["component A"])))
)
test_data = (
(set(("A", "B")), set(("A", "B"))),
(set(("a", "B")), set(("A", "B"))),
(set(("component A", "B")), set(("A", "B"))),
(set(("Component A", "B")), set(("A", "B"))),
(set(("Component C", "B")), set(("B", "Component C"))),
)
for args, expected in test_data:
assert filter.filter(args) == expected
def test_multiple_aka_components(self):
components = {
"A": {"aka": ["Microsoft A"]},
"B": {"aka": ["Cisco B", "Cisco B System"]},
}
filter = self.filter_from_objects(dict(components=components))
test_data = (
(set(("A", "B")), set(("A", "B"))),
(set(("a", "B")), set(("A", "B"))),
(set(("Microsoft A", "Cisco B")), set(("A", "B"))),
(set(("microsoft a", "B")), set(("A", "B"))),
(set(("microsoft a", "cisco b", "Jira")), set(("A", "B", "Jira"))),
)
for args, expected in test_data:
assert filter.filter(args) == expected
def test_soup_to_nuts(self):
components = {
"A": {"aka": ["Microsoft A"]},
"B": {"aka": ["Cisco B", "Cisco B System"]},
}
not_components = ["Chicken"]
filter = self.filter_from_objects(
dict(components=components, not_components=not_components)
)
test_data = (
(set(("A", "B")), set(("A", "B"))),
(set(("a", "B")), set(("A", "B"))),
(set(("Microsoft A", "Cisco B")), set(("A", "B"))),
(set(("microsoft a", "B")), set(("A", "B"))),
(set(("microsoft a", "cisco b", "Jira")), set(("A", "B", "Jira"))),
(set(("A", "B", "Chicken")), set(("A", "B"))),
(set(("A", "B", "Component C", "chicken")), set(("A", "B", "Component C"))),
)
for args, expected in test_data:
assert filter.filter(args) == expected
class TestCSVControlStatementReader(object):
def test_reader(self):
text = """\
AC-1, Statement for AC-1
IA-4, Statement for IA-4
"""
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", delete=False
) as fp:
fp.write(text)
fp.close()
reader = CSVControlStatementReader(fp.name, "utf-8")
statements = reader.read()
assert len(statements) == 2
assert statements[0].control == "AC-1"
assert statements[0].text == "Statement for AC-1"
assert statements[1].control == "IA-4"
assert statements[1].text == "Statement for IA-4"
class TestPSVControlStatementReader(object):
def test_reader(self):
text = """
AC-1 | Statement for AC-1
IA-4 | Statement for IA-4
RA-3 extra text | Statement for RA-3
[RA-4] | Statement for RA-4
"""
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", delete=False
) as fp:
fp.write(text)
fp.close()
reader = PSVControlStatementReader(fp.name, "utf-8")
statements = reader.read()
assert len(statements) == 4
assert statements[0].control == "AC-1"
assert statements[0].text == "Statement for AC-1"
assert statements[1].control == "IA-4"
assert statements[1].text == "Statement for IA-4"
assert statements[2].control == "RA-3"
assert statements[2].text == "Statement for RA-3"
assert statements[3].control == "RA-4"
assert statements[3].text == "Statement for RA-4"
class TestPSV2ControlStatementReader(object):
def test_reader(self):
text = """
Access Control | AC-1 | Statement for AC-1
Identity |IA-4 | Statement for IA-4
Risk | RA-3 extra text | Statement for RA-3
Risk | [RA-4] | Statement for RA-4
"""
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", delete=False
) as fp:
fp.write(text)
fp.close()
reader = PSVControlStatementReader(
fp.name, "utf-8", control_id_col=1, statement_col=2
)
statements = reader.read()
assert len(statements) == 4
assert statements[0].control == "AC-1"
assert statements[0].text == "Statement for AC-1"
assert statements[1].control == "IA-4"
assert statements[1].text == "Statement for IA-4"
assert statements[2].control == "RA-3"
assert statements[2].text == "Statement for RA-3"
assert statements[3].control == "RA-4"
assert statements[3].text == "Statement for RA-4"