|
1
|
|
|
import os |
|
2
|
|
|
import glob |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
from org_fedora_oscap import content_handling as ch |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
TESTING_FILES_PATH = os.path.join( |
|
10
|
|
|
os.path.dirname(__file__), os.path.pardir, "testing_files") |
|
11
|
|
|
DS_FILEPATH = os.path.join( |
|
12
|
|
|
TESTING_FILES_PATH, "testing_ds.xml") |
|
13
|
|
|
|
|
14
|
|
|
DS_IDS = "scap_org.open-scap_datastream_tst" |
|
15
|
|
|
CHK_FIRST_ID = "scap_org.open-scap_cref_first-xccdf.xml" |
|
16
|
|
|
CHK_SECOND_ID = "scap_org.open-scap_cref_second-xccdf.xml" |
|
17
|
|
|
|
|
18
|
|
|
PROFILE1_ID = "xccdf_com.example_profile_my_profile" |
|
19
|
|
|
PROFILE2_ID = "xccdf_com.example_profile_my_profile2" |
|
20
|
|
|
PROFILE3_ID = "xccdf_com.example_profile_my_profile3" |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
@pytest.fixture() |
|
24
|
|
|
def ds_handler(): |
|
25
|
|
|
return ch.DataStreamHandler(DS_FILEPATH) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def test_init_invalid_file_path(): |
|
29
|
|
|
with pytest.raises(ch.DataStreamHandlingError) as excinfo: |
|
30
|
|
|
ch.DataStreamHandler("testing_ds.xmlll") |
|
31
|
|
|
assert "Invalid file path" in str(excinfo.value) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def test_init_not_scap_content(): |
|
35
|
|
|
with pytest.raises(ch.DataStreamHandlingError) as excinfo: |
|
36
|
|
|
ch.DataStreamHandler(os.path.join(TESTING_FILES_PATH, "testing_ks.cfg")) |
|
37
|
|
|
assert "not a valid SCAP content file" in str(excinfo.value) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def test_init_xccdf_content(): |
|
41
|
|
|
with pytest.raises(ch.DataStreamHandlingError) as excinfo: |
|
42
|
|
|
ch.DataStreamHandler(os.path.join(TESTING_FILES_PATH, "xccdf.xml")) |
|
43
|
|
|
assert "not a data stream collection" in str(excinfo.value) |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_get_data_streams(ds_handler): |
|
47
|
|
|
assert DS_IDS in ds_handler.get_data_streams() |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def test_get_data_streams_checklists(ds_handler): |
|
51
|
|
|
expected_ids = {DS_IDS: [CHK_FIRST_ID, CHK_SECOND_ID]} |
|
52
|
|
|
|
|
53
|
|
|
ds_ids = ds_handler.get_data_streams_checklists() |
|
54
|
|
|
assert expected_ids == ds_ids |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def test_get_checklists(ds_handler): |
|
58
|
|
|
expected_checklists = [CHK_FIRST_ID, CHK_SECOND_ID] |
|
59
|
|
|
|
|
60
|
|
|
chk_ids = ds_handler.get_checklists(DS_IDS) |
|
61
|
|
|
assert expected_checklists == chk_ids |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_get_checklists_invalid(ds_handler): |
|
65
|
|
|
with pytest.raises(ch.DataStreamHandlingError) as excinfo: |
|
66
|
|
|
ds_handler.get_checklists("invalid.id") |
|
67
|
|
|
assert "Invalid data stream id given" in str(excinfo.value) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def test_get_profiles(ds_handler): |
|
71
|
|
|
profile_ids = ds_handler.get_profiles(DS_IDS, CHK_FIRST_ID) |
|
72
|
|
|
|
|
73
|
|
|
# When Benchmark doesn't contain Rules selected by default |
|
74
|
|
|
# the default Profile should not be present |
|
75
|
|
|
assert 2 == len(profile_ids) |
|
76
|
|
|
assert PROFILE1_ID == profile_ids[0].id |
|
77
|
|
|
assert PROFILE2_ID == profile_ids[1].id |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
def test_get_profiles_with_default(ds_handler): |
|
81
|
|
|
profile_ids = ds_handler.get_profiles(DS_IDS, CHK_SECOND_ID) |
|
82
|
|
|
|
|
83
|
|
|
# When Benchmark contains Rules selected by default |
|
84
|
|
|
# the default Profile should be present |
|
85
|
|
|
assert 2 == len(profile_ids) |
|
86
|
|
|
assert "default" == profile_ids[0].id |
|
87
|
|
|
assert PROFILE3_ID == profile_ids[1].id |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_identify_files(): |
|
91
|
|
|
filenames = glob.glob(TESTING_FILES_PATH + "/*") |
|
92
|
|
|
identified = ch.identify_files(filenames) |
|
93
|
|
|
assert identified[DS_FILEPATH] == ch.CONTENT_TYPES["DATASTREAM"] |
|
94
|
|
|
assert identified[ |
|
95
|
|
|
os.path.join(TESTING_FILES_PATH, "scap-mycheck-oval.xml")] == ch.CONTENT_TYPES["OVAL"] |
|
96
|
|
|
assert identified[ |
|
97
|
|
|
os.path.join(TESTING_FILES_PATH, "tailoring.xml")] == ch.CONTENT_TYPES["TAILORING"] |
|
98
|
|
|
assert identified[ |
|
99
|
|
|
os.path.join(TESTING_FILES_PATH, "testing_xccdf.xml")] == ch.CONTENT_TYPES["XCCDF_CHECKLIST"] |
|
100
|
|
|
assert identified[ |
|
101
|
|
|
os.path.join(TESTING_FILES_PATH, "cpe-dict.xml")] == ch.CONTENT_TYPES["CPE_DICT"] |
|
102
|
|
|
|