|
1
|
|
|
import os |
|
2
|
|
|
import time |
|
3
|
|
|
import pathlib |
|
4
|
|
|
import hashlib |
|
5
|
|
|
|
|
6
|
|
|
import pytest |
|
7
|
|
|
|
|
8
|
|
|
import org_fedora_oscap.content_discovery as tested_module |
|
9
|
|
|
from org_fedora_oscap import content_handling |
|
10
|
|
|
from org_fedora_oscap import utils |
|
11
|
|
|
|
|
12
|
|
|
import test_data_fetch |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@pytest.fixture |
|
16
|
|
|
def labelled_files(): |
|
17
|
|
|
return { |
|
18
|
|
|
"dir/datastream": "D", |
|
19
|
|
|
"dir/datastream2": "D", |
|
20
|
|
|
"dir/dir/datastream3": "D", |
|
21
|
|
|
"dir/dir/datastream3": "D", |
|
22
|
|
|
"dir/XCCDF": "X", |
|
23
|
|
|
"XCCDF2": "X", |
|
24
|
|
|
"cpe": "C", |
|
25
|
|
|
"t1": "T", |
|
26
|
|
|
"dir3/t2": "T", |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def test_reduce(labelled_files): |
|
31
|
|
|
analyzer = tested_module.ContentAnalyzer() |
|
32
|
|
|
|
|
33
|
|
|
d_count = 0 |
|
34
|
|
|
x_count = 0 |
|
35
|
|
|
for l in labelled_files.values(): |
|
36
|
|
|
if l == "D": |
|
37
|
|
|
d_count += 1 |
|
38
|
|
|
elif l == "X": |
|
39
|
|
|
x_count += 1 |
|
40
|
|
|
|
|
41
|
|
|
reduced = analyzer.reduce_files(labelled_files, "dir/datastream", ["D"]) |
|
42
|
|
|
assert len(reduced) == len(labelled_files) - d_count + 1 |
|
43
|
|
|
assert "dir/datastream" in reduced |
|
44
|
|
|
|
|
45
|
|
|
reduced = analyzer.reduce_files(labelled_files, "dir/datastream", ["D", "X"]) |
|
46
|
|
|
assert len(reduced) == len(labelled_files) - d_count - x_count + 1 |
|
47
|
|
|
assert "dir/datastream" in reduced |
|
48
|
|
|
|
|
49
|
|
|
reduced = analyzer.reduce_files(labelled_files, "dir/XCCDF", ["D", "X"]) |
|
50
|
|
|
assert len(reduced) == len(labelled_files) - d_count - x_count + 1 |
|
51
|
|
|
assert "dir/XCCDF" in reduced |
|
52
|
|
|
|
|
53
|
|
|
with pytest.raises(content_handling.ContentHandlingError, match="dir/datastream4"): |
|
54
|
|
|
analyzer.reduce_files(labelled_files, "dir/datastream4", ["D"]) |
|
55
|
|
|
|
|
56
|
|
|
reduced = analyzer.reduce_files(labelled_files, "cpe", ["C"]) |
|
57
|
|
|
assert reduced == labelled_files |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_path_presence_detection(): |
|
61
|
|
|
list_of_paths = ["file1", os.path.abspath("file2"), os.path.abspath("dir///file3")] |
|
62
|
|
|
|
|
63
|
|
|
list_of_paths_in_list = [ |
|
64
|
|
|
"file1", os.path.abspath("file1"), "./file1", |
|
65
|
|
|
"file2", "dir/..//file2", |
|
66
|
|
|
"dir/../dir/file3", "dir/file3", |
|
67
|
|
|
] |
|
68
|
|
|
list_of_paths_not_in_list = [ |
|
69
|
|
|
"../file1", "file3" |
|
70
|
|
|
] |
|
71
|
|
|
|
|
72
|
|
|
for path in list_of_paths_in_list: |
|
73
|
|
|
assert tested_module.path_is_present_among_paths(path, list_of_paths) |
|
74
|
|
|
|
|
75
|
|
|
for path in list_of_paths_not_in_list: |
|
76
|
|
|
assert not tested_module.path_is_present_among_paths(path, list_of_paths) |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
class SlowBringer(tested_module.ContentBringer): |
|
80
|
|
|
def fetch_operation(self, out_file): |
|
81
|
|
|
time.sleep(1) |
|
82
|
|
|
super().fetch_operation(out_file) |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def if_problem_raise_exception(exc): |
|
86
|
|
|
raise(exc) |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
def test_bringer_blocks_double_download_and_finishes_the_first(tmp_path): |
|
90
|
|
|
source_path = pathlib.Path(__file__).absolute() |
|
91
|
|
|
source_fingerprint = utils.get_file_fingerprint(str(source_path), hashlib.sha512()) |
|
92
|
|
|
|
|
93
|
|
|
dest_path = tmp_path / "dest" |
|
94
|
|
|
uri = f"file://{source_path}" |
|
95
|
|
|
|
|
96
|
|
|
bringer = SlowBringer(if_problem_raise_exception) |
|
97
|
|
|
thread_name = bringer.fetch_content(uri) |
|
98
|
|
|
second_thread_name = bringer.fetch_content(uri) |
|
99
|
|
|
assert second_thread_name is None |
|
100
|
|
|
|
|
101
|
|
|
bringer.finish_content_fetch(thread_name, source_fingerprint) |
|
102
|
|
|
|