-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_extract_opossum_information_from_spdx.py
280 lines (246 loc) · 9.44 KB
/
test_extract_opossum_information_from_spdx.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
# SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
#
# SPDX-License-Identifier: Apache-2.0
from pathlib import Path
from typing import cast
from unittest import TestCase
from spdx_tools.spdx.model import Document
from spdx_tools.spdx.model.package import Package
from spdx_tools.spdx.parser.parse_anything import parse_file
from opossum_lib.opossum.opossum_file import (
ExternalAttributionSource,
OpossumInformation,
ResourceInFile,
)
from opossum_lib.spdx.constants import (
SPDX_FILE_IDENTIFIER,
SPDX_PACKAGE_IDENTIFIER,
SPDX_SNIPPET_IDENTIFIER,
)
from opossum_lib.spdx.convert_to_opossum import convert_tree_to_opossum
from opossum_lib.spdx.graph_generation import generate_graph_from_spdx
from opossum_lib.spdx.tree_generation import generate_tree_from_graph
from tests.test_spdx.helper_methods import (
_create_minimal_document,
_generate_document_with_from_root_node_unreachable_file,
)
def test_different_paths_graph() -> None:
"""Creating a tree from a directed graph with a cycle."""
expected_file_tree = {
"SPDX Lite Document": {
"DESCRIBES": {
"Example package A": {"CONTAINS": {"Example file": 1}},
"Example package B": {"CONTAINS": {"Example file": 1}},
}
}
}
document = _create_minimal_document()
opossum_information = _get_opossum_information_from_document(document)
file_tree = opossum_information.resources
assert file_tree == expected_file_tree
TestCase().assertCountEqual(
opossum_information.attribution_breakpoints,
[
"/SPDX Lite Document/DESCRIBES/",
"/SPDX Lite Document/DESCRIBES/Example package A/CONTAINS/",
"/SPDX Lite Document/DESCRIBES/Example package B/CONTAINS/",
],
)
assert opossum_information.resources_to_attributions == {
"/SPDX Lite Document": ["SPDXRef-DOCUMENT"],
"/SPDX Lite Document/DESCRIBES/Example package A": ["SPDXRef-Package-A"],
"/SPDX Lite Document/DESCRIBES/Example package A/CONTAINS/Example file": [
"SPDXRef-File"
],
"/SPDX Lite Document/DESCRIBES/Example package B": ["SPDXRef-Package-B"],
"/SPDX Lite Document/DESCRIBES/Example package B/CONTAINS/Example file": [
"SPDXRef-File"
],
}
TestCase().assertCountEqual(
opossum_information.external_attributions.keys(),
[
"SPDXRef-DOCUMENT",
"SPDXRef-Package-A",
"SPDXRef-File",
"SPDXRef-Package-B",
],
)
assert opossum_information.external_attribution_sources == {
SPDX_FILE_IDENTIFIER: ExternalAttributionSource(
name=SPDX_FILE_IDENTIFIER, priority=500
),
SPDX_PACKAGE_IDENTIFIER: ExternalAttributionSource(
name=SPDX_PACKAGE_IDENTIFIER, priority=500
),
SPDX_SNIPPET_IDENTIFIER: ExternalAttributionSource(
name=SPDX_SNIPPET_IDENTIFIER, priority=500
),
}
def test_unconnected_paths_graph() -> None:
"""Creating a tree from a directed graph with a cycle."""
expected_file_tree = {
"SPDX Lite Document": {
"DESCRIBES": {
"Example package A": {"CONTAINS": {"Example file": 1}},
"Example package B": {"CONTAINS": {"Example file": 1}},
}
},
"Package without connection to document": {},
}
document = _create_minimal_document()
document.packages += [
Package(
spdx_id="SPDXRef-Package-C",
name="Package without connection to document",
download_location="https://download.location.com",
)
]
opossum_information = _get_opossum_information_from_document(document)
file_tree = opossum_information.resources
assert file_tree == expected_file_tree
TestCase().assertCountEqual(
opossum_information.attribution_breakpoints,
[
"/SPDX Lite Document/DESCRIBES/",
"/SPDX Lite Document/DESCRIBES/Example package A/CONTAINS/",
"/SPDX Lite Document/DESCRIBES/Example package B/CONTAINS/",
],
)
assert opossum_information.resources_to_attributions == {
"/SPDX Lite Document": ["SPDXRef-DOCUMENT"],
"/SPDX Lite Document/DESCRIBES/Example package A": ["SPDXRef-Package-A"],
"/SPDX Lite Document/DESCRIBES/Example package A/CONTAINS/Example file": [
"SPDXRef-File"
],
"/SPDX Lite Document/DESCRIBES/Example package B": ["SPDXRef-Package-B"],
"/SPDX Lite Document/DESCRIBES/Example package B/CONTAINS/Example file": [
"SPDXRef-File"
],
"/Package without connection to document": ["SPDXRef-Package-C"],
}
TestCase().assertCountEqual(
opossum_information.external_attributions.keys(),
[
"SPDXRef-DOCUMENT",
"SPDXRef-Package-A",
"SPDXRef-File",
"SPDXRef-Package-B",
"SPDXRef-Package-C",
],
)
def get_value_at_file_tree_path(
file_tree: ResourceInFile, path_elements: list[str]
) -> ResourceInFile:
if len(path_elements) != 0:
assert isinstance(file_tree, dict)
file_tree = cast(dict[str, ResourceInFile], file_tree)
return get_value_at_file_tree_path(
file_tree[path_elements[0]], path_elements[1:]
)
return file_tree
def test_different_roots_graph() -> None:
"""Creating a tree from a connected graph where some edges are not reachable
from the SPDX Lite Document node. This means that the connected graph has multiple
sources and thus the result should be disconnected."""
expected_file_tree = {
"File-B": {"DESCRIBES": {"Package-B": {}}},
"Document": {
"DESCRIBES": {"Package-A": {"CONTAINS": {"File-A": 1}}, "Package-B": {}}
},
}
document = _generate_document_with_from_root_node_unreachable_file()
opossum_information = _get_opossum_information_from_document(document)
file_tree = opossum_information.resources
assert file_tree == expected_file_tree
TestCase().assertCountEqual(
opossum_information.attribution_breakpoints,
[
"/Document/DESCRIBES/",
"/Document/DESCRIBES/Package-A/CONTAINS/",
"/File-B/DESCRIBES/",
],
)
assert opossum_information.resources_to_attributions == {
"/File-B": ["SPDXRef-File-B"],
"/File-B/DESCRIBES/Package-B": ["SPDXRef-Package-B"],
"/Document": ["SPDXRef-DOCUMENT"],
"/Document/DESCRIBES/Package-A": ["SPDXRef-Package-A"],
"/Document/DESCRIBES/Package-A/CONTAINS/File-A": ["SPDXRef-File-A"],
"/Document/DESCRIBES/Package-B": ["SPDXRef-Package-B"],
}
TestCase().assertCountEqual(
opossum_information.external_attributions.keys(),
[
"SPDXRef-DOCUMENT",
"SPDXRef-Package-A",
"SPDXRef-File-A",
"SPDXRef-File-B",
"SPDXRef-Package-B",
],
)
def test_tree_generation_for_bigger_examples_json() -> None:
opossum_information = _get_opossum_information_from_file(
"SPDXJSONExample-v2.3.spdx.json"
)
file_tree = opossum_information.resources
expected_breakpoints = [
"/SPDX-Tools-v2.0/CONTAINS/glibc/CONTAINS/"
"lib-source/commons-lang3-3.1-sources.jar/GENERATED_FROM/",
"/SPDX-Tools-v2.0/CONTAINS/glibc/DYNAMIC_LINK/",
]
assert isinstance(file_tree, dict)
assert len(file_tree.keys()) == 3
for attribution_breakpoint in expected_breakpoints:
assert attribution_breakpoint in opossum_information.attribution_breakpoints
assert (
get_value_at_file_tree_path(
file_tree,
[
"SPDX-Tools-v2.0",
"COPY_OF",
"DocumentRef-spdx-tool-1.2:SPDXRef-ToolsElement",
],
)
== 1
)
assert (
get_value_at_file_tree_path(
file_tree, ["SPDX-Tools-v2.0", "CONTAINS", "glibc", "DYNAMIC_LINK", "Saxon"]
)
== {}
)
def test_tree_generation_for_bigger_examples_spdx() -> None:
opossum_information = _get_opossum_information_from_file("SPDX.spdx")
file_tree = opossum_information.resources
expected_breakpoints = [
"/SPDX Lite Document/DESCRIBES/Package A/CONTAINS/",
"/SPDX Lite Document/DESCRIBES/Package A/COPY_OF/Package C/CONTAINS/",
]
assert isinstance(file_tree, dict)
assert len(file_tree.keys()) == 2
for attribution_breakpoint in expected_breakpoints:
assert attribution_breakpoint in opossum_information.attribution_breakpoints
assert (
get_value_at_file_tree_path(
file_tree, ["SPDX Lite Document", "DESCRIBES", "Package B"]
)
== {}
)
assert (
get_value_at_file_tree_path(
file_tree,
["SPDX Lite Document", "DESCRIBES", "Package A", "CONTAINS", "File-C"],
)
== 1
)
def _get_opossum_information_from_file(file_name: str) -> OpossumInformation:
document = parse_file(
str(Path(__file__).resolve().parent.parent / "data" / file_name)
)
return _get_opossum_information_from_document(document)
def _get_opossum_information_from_document(document: Document) -> OpossumInformation:
graph = generate_graph_from_spdx(document)
tree = generate_tree_from_graph(graph)
opossum_information = convert_tree_to_opossum(tree)
return opossum_information.to_opossum_file_format().input_file