|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import re |
|
5
|
|
|
import ssg.build_cpe |
|
6
|
|
|
import ssg.xml |
|
7
|
|
|
from ssg.yaml import open_raw |
|
8
|
|
|
|
|
9
|
|
|
ET = ssg.xml.ElementTree |
|
10
|
|
|
DATADIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def test_extract_element(): |
|
14
|
|
|
obj = """<?xml version="1.0"?> |
|
15
|
|
|
<variables> |
|
16
|
|
|
<var> |
|
17
|
|
|
<subelement> |
|
18
|
|
|
<random id="test">This</random> |
|
19
|
|
|
</subelement> |
|
20
|
|
|
</var> |
|
21
|
|
|
<var> |
|
22
|
|
|
<subelement> |
|
23
|
|
|
<random random="not-me">That</random> |
|
24
|
|
|
</subelement> |
|
25
|
|
|
</var> |
|
26
|
|
|
</variables> |
|
27
|
|
|
""" |
|
28
|
|
|
tree = ET.fromstring(obj) |
|
29
|
|
|
|
|
30
|
|
|
assert ssg.build_cpe.extract_subelement(tree, 'id') == 'test' |
|
31
|
|
|
assert ssg.build_cpe.extract_subelement(tree, 'random') == 'not-me' |
|
32
|
|
|
assert ssg.build_cpe.extract_subelement(tree, 'missing') is None |
|
33
|
|
|
assert ssg.build_cpe.extract_subelement(tree, 'subelement') is None |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def test_extract_env_obj(): |
|
37
|
|
|
local_var_text = """ |
|
38
|
|
|
<var> |
|
39
|
|
|
<subelement> |
|
40
|
|
|
<random object_ref="magical">elements</random> |
|
41
|
|
|
</subelement> |
|
42
|
|
|
</var> |
|
43
|
|
|
""" |
|
44
|
|
|
local_var = ET.fromstring(local_var_text) |
|
45
|
|
|
|
|
46
|
|
|
local_var_missing_text = """ |
|
47
|
|
|
<var> |
|
48
|
|
|
<subelement> |
|
49
|
|
|
<random object_ref="nothing">here</random> |
|
50
|
|
|
</subelement> |
|
51
|
|
|
</var> |
|
52
|
|
|
""" |
|
53
|
|
|
local_var_missing = ET.fromstring(local_var_missing_text) |
|
54
|
|
|
|
|
55
|
|
|
objects_text = """ |
|
56
|
|
|
<objects> |
|
57
|
|
|
<object id="something">something</object> |
|
58
|
|
|
<object id="magical">magical</object> |
|
59
|
|
|
<object id="here">here</object> |
|
60
|
|
|
</objects> |
|
61
|
|
|
""" |
|
62
|
|
|
objects = ET.fromstring(objects_text) |
|
63
|
|
|
|
|
64
|
|
|
present = ssg.build_cpe.extract_env_obj(objects, local_var) |
|
65
|
|
|
assert present is not None |
|
66
|
|
|
assert present.text == 'magical' |
|
67
|
|
|
|
|
68
|
|
|
missing = ssg.build_cpe.extract_env_obj(objects, local_var_missing) |
|
69
|
|
|
assert missing is None |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_extract_referred_nodes(): |
|
73
|
|
|
tree_with_refs_text = """ |
|
74
|
|
|
<references> |
|
75
|
|
|
<reference object_ref="something_borrowed" /> |
|
76
|
|
|
<reference object_ref="something_missing" /> |
|
77
|
|
|
</references> |
|
78
|
|
|
""" |
|
79
|
|
|
tree_with_refs = ET.fromstring(tree_with_refs_text) |
|
80
|
|
|
|
|
81
|
|
|
tree_with_ids_text = """ |
|
82
|
|
|
<objects> |
|
83
|
|
|
<object id="something_old">Brno</object> |
|
84
|
|
|
<object id="something_new">Boston</object> |
|
85
|
|
|
<object id="something_borrowed">Source Code</object> |
|
86
|
|
|
<object id="something_blue">Fedora</object> |
|
87
|
|
|
</objects> |
|
88
|
|
|
""" |
|
89
|
|
|
tree_with_ids = ET.fromstring(tree_with_ids_text) |
|
90
|
|
|
|
|
91
|
|
|
results = ssg.build_cpe.extract_referred_nodes(tree_with_refs, tree_with_ids, 'object_ref') |
|
92
|
|
|
|
|
93
|
|
|
assert len(results) == 1 |
|
94
|
|
|
assert results[0].text == 'Source Code' |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
############################################# |
|
98
|
|
|
# Unit tests for ProductCPEs.get_cpe() method |
|
99
|
|
|
############################################# |
|
100
|
|
|
# |
|
101
|
|
|
# Note that there are 2 types of CPE definitions that differ by the source they |
|
102
|
|
|
# come from: |
|
103
|
|
|
# * Product CPEs, loaded from product YAML |
|
104
|
|
|
# * Content CPEs, loaded from directory specified by the `cpes_root` key in |
|
105
|
|
|
# product YML, usually from the `/applicability` directory |
|
106
|
|
|
# |
|
107
|
|
|
# This test case test that both types are used by the ProductCPEs class and |
|
108
|
|
|
# that both CPE types are handled equally. |
|
109
|
|
|
def test_product_cpes(): |
|
110
|
|
|
|
|
111
|
|
|
# CPEs are loaded from `DATADIR/product.yml` but also from |
|
112
|
|
|
# `DATADIR/applicability` because `DATADIR/product.yml` references the |
|
113
|
|
|
# `DATADIR/applicability` directory in the `cpes_root` key |
|
114
|
|
|
product_yaml_path = os.path.join(DATADIR, "product.yml") |
|
115
|
|
|
product_yaml = open_raw(product_yaml_path) |
|
116
|
|
|
product_yaml["product_dir"] = os.path.dirname(product_yaml_path) |
|
117
|
|
|
product_cpes = ssg.build_cpe.ProductCPEs(product_yaml) |
|
118
|
|
|
|
|
119
|
|
|
# get a product CPE by name and verify it's loaded |
|
120
|
|
|
# this CPE is defined in `DATADIR/product.yml` |
|
121
|
|
|
rhel7_cpe = product_cpes.get_cpe("rhel7") |
|
122
|
|
|
assert(rhel7_cpe.name == "cpe:/o:redhat:enterprise_linux:7") |
|
123
|
|
|
assert(rhel7_cpe.title == "Red Hat Enterprise Linux 7") |
|
124
|
|
|
assert(rhel7_cpe.check_id == "installed_OS_is_rhel7") |
|
125
|
|
|
assert(rhel7_cpe.bash_conditional == "") |
|
126
|
|
|
assert(rhel7_cpe.ansible_conditional == "") |
|
127
|
|
|
|
|
128
|
|
|
# get CPE by ID and verify it's loaded, the get_cpe method should return |
|
129
|
|
|
# the same object as when CPE name was used above |
|
130
|
|
|
rhel7_cpe_2 = product_cpes.get_cpe("cpe:/o:redhat:enterprise_linux:7") |
|
131
|
|
|
assert(rhel7_cpe_2.name == rhel7_cpe.name) |
|
132
|
|
|
assert(rhel7_cpe_2.title == rhel7_cpe_2.title) |
|
133
|
|
|
assert(rhel7_cpe_2.check_id == rhel7_cpe.check_id) |
|
134
|
|
|
assert(rhel7_cpe_2.bash_conditional == rhel7_cpe.bash_conditional) |
|
135
|
|
|
assert(rhel7_cpe_2.ansible_conditional == rhel7_cpe.ansible_conditional) |
|
136
|
|
|
|
|
137
|
|
|
# get a content CPE by name and verify it's loaded |
|
138
|
|
|
# this CPE is defined in `DATADIR/applicability/virtualization.yml` |
|
139
|
|
|
cpe1 = product_cpes.get_cpe("machine") |
|
140
|
|
|
assert(cpe1.name == "cpe:/a:machine") |
|
141
|
|
|
assert(cpe1.title == "Bare-metal or Virtual Machine") |
|
142
|
|
|
assert(cpe1.check_id == "installed_env_is_a_machine") |
|
143
|
|
|
assert(cpe1.ansible_conditional == "ansible_virtualization_type not in [\"docker\", \"lxc\", \"openvz\", \"podman\", \"container\"]") |
|
144
|
|
|
assert(cpe1.bash_conditional == "[ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]") |
|
145
|
|
|
|
|
146
|
|
|
# get CPE by ID and verify it's loaded, the get_cpe method should return |
|
147
|
|
|
# the same object as when CPE name was used above |
|
148
|
|
|
cpe2 = product_cpes.get_cpe("cpe:/a:machine") |
|
149
|
|
|
assert(cpe2.name == cpe1.name) |
|
150
|
|
|
assert(cpe2.title == cpe1.title) |
|
151
|
|
|
assert(cpe2.check_id == cpe1.check_id) |
|
152
|
|
|
assert(cpe2.ansible_conditional == cpe1.ansible_conditional) |
|
153
|
|
|
assert(cpe2.bash_conditional == cpe1.bash_conditional) |
|
154
|
|
|
|