1
|
|
|
import argparse |
2
|
|
|
import subprocess |
3
|
|
|
import tempfile |
4
|
|
|
from io import BytesIO |
5
|
|
|
from pathlib import Path |
6
|
|
|
from unittest import mock |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
|
10
|
|
|
from oscap_report.cli import CommandLineAPI |
11
|
|
|
from oscap_report.scap_results_parser.scap_results_parser import \ |
12
|
|
|
SCAPResultsParser |
13
|
|
|
|
14
|
|
|
from .constants import (PATH_TO_ARF, PATH_TO_ARF_WITH_ONLY_PASSING_RULE, |
15
|
|
|
PATH_TO_EMPTY_FILE) |
16
|
|
|
|
17
|
|
|
PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html" |
18
|
|
|
OSCAP_REPORT_COMMAND = "oscap-report" |
19
|
|
|
CAT_ARF_FILE = ["cat", str(PATH_TO_ARF)] |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def get_fake_args(): |
23
|
|
|
# pylint: disable=R1732 |
24
|
|
|
input_file = open(PATH_TO_ARF, "r", encoding="utf-8") |
25
|
|
|
output_file = open(PATH_TO_RESULT_FILE, "wb") |
26
|
|
|
return argparse.Namespace( |
27
|
|
|
FILE=input_file, output=output_file, |
28
|
|
|
log_file=None, log_level="WARNING", format="HTML", |
29
|
|
|
debug=[""], |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
@mock.patch('argparse.ArgumentParser.parse_args', |
34
|
|
|
return_value=get_fake_args()) |
35
|
|
|
def test_load_file(mock_args): # pylint: disable=W0613 |
36
|
|
|
api = CommandLineAPI() |
37
|
|
|
xml_report = api.load_file() |
38
|
|
|
parser = SCAPResultsParser(xml_report) |
39
|
|
|
assert parser.validate(parser.arf_schemas_path) |
40
|
|
|
api.close_files() |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
44
|
|
|
@mock.patch('argparse.ArgumentParser.parse_args', |
45
|
|
|
return_value=get_fake_args()) |
46
|
|
|
def test_store_file(mock_args): # pylint: disable=W0613 |
47
|
|
|
api = CommandLineAPI() |
48
|
|
|
data = BytesIO(b'<html><h1>TEST DATA</h1></html>') |
49
|
|
|
api.store_file(data) |
50
|
|
|
api.close_files() |
51
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
52
|
|
|
assert result_file.read() == data.getvalue().decode("utf-8") |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@mock.patch('argparse.ArgumentParser.parse_args', |
56
|
|
|
return_value=get_fake_args()) |
57
|
|
|
def test_generate_report(mock_args): # pylint: disable=W0613 |
58
|
|
|
data = None |
59
|
|
|
api = CommandLineAPI() |
60
|
|
|
with open(PATH_TO_ARF, "r", encoding="utf-8") as arf_report: |
61
|
|
|
parser = SCAPResultsParser(arf_report.read().encode()) |
62
|
|
|
data = api.generate_report(parser) |
63
|
|
|
assert data.read().decode("utf-8").startswith("<!DOCTYPE html>") |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
67
|
|
|
def test_command_with_input_from_stdin_and_output_to_stdout(): |
68
|
|
|
command_stdout = None |
69
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
70
|
|
|
command_stdout = subprocess.check_output(OSCAP_REPORT_COMMAND, stdin=cat_command.stdout) |
71
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
75
|
|
|
def test_command_with_input_from_file_and_output_to_stdout(): |
76
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, str(PATH_TO_ARF)]) |
77
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
81
|
|
|
def test_command_with_input_from_stdin_and_output_to_file(): |
82
|
|
|
command_stdout = None |
83
|
|
|
arguments = ["-o", str(PATH_TO_RESULT_FILE)] |
84
|
|
|
with subprocess.Popen(CAT_ARF_FILE, stdout=subprocess.PIPE) as cat_command: |
85
|
|
|
command_stdout = subprocess.check_output( |
86
|
|
|
[OSCAP_REPORT_COMMAND, *arguments], stdin=cat_command.stdout) |
87
|
|
|
assert not command_stdout.decode("utf-8") |
88
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
89
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
93
|
|
|
def test_command_with_input_from_file_and_output_to_file(): |
94
|
|
|
arguments = [str(PATH_TO_ARF), "-o", str(PATH_TO_RESULT_FILE)] |
95
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, *arguments]) |
96
|
|
|
assert not command_stdout.decode("utf-8") |
97
|
|
|
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file: |
98
|
|
|
assert result_file.read().startswith("<!DOCTYPE html>") |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
102
|
|
|
def test_logging_to_file(): |
103
|
|
|
log_file_path = Path(tempfile.gettempdir()) / "oscap-report-tests_log-file.log" |
104
|
|
|
command = [ |
105
|
|
|
OSCAP_REPORT_COMMAND, |
106
|
|
|
str(PATH_TO_ARF), |
107
|
|
|
"--log-level", "DEBUG", |
108
|
|
|
"--log-file", str(log_file_path) |
109
|
|
|
] |
110
|
|
|
command_stdout = subprocess.check_output(command) |
111
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
112
|
|
|
with open(log_file_path, "r", encoding="utf-8") as result_file: |
113
|
|
|
assert result_file.read().startswith("DEBUG:") |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
def test_command_with_empty_input_file(): |
117
|
|
|
arguments = [str(PATH_TO_EMPTY_FILE)] |
118
|
|
|
with subprocess.Popen([OSCAP_REPORT_COMMAND, *arguments], stderr=subprocess.PIPE) as command: |
119
|
|
|
command.wait() |
120
|
|
|
assert command.returncode == 1 |
121
|
|
|
std_err = command.stderr.read().decode("utf-8") |
122
|
|
|
assert std_err.startswith("CRITICAL:") |
123
|
|
|
assert "empty" in std_err |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
def test_command_with_empty_stdin(): |
127
|
|
|
cat_command_args = ["cat", str(PATH_TO_EMPTY_FILE)] |
128
|
|
|
with subprocess.Popen(cat_command_args, stdout=subprocess.PIPE) as cat_command: |
129
|
|
|
with subprocess.Popen( |
130
|
|
|
OSCAP_REPORT_COMMAND, |
131
|
|
|
stdin=cat_command.stdout, |
132
|
|
|
stderr=subprocess.PIPE) as command: |
133
|
|
|
command.wait() |
134
|
|
|
assert command.returncode == 1 |
135
|
|
|
std_err = command.stderr.read().decode("utf-8") |
136
|
|
|
assert std_err.startswith("CRITICAL:") |
137
|
|
|
assert "empty" in std_err |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
@pytest.mark.parametrize("arf_file_path", [ |
141
|
|
|
PATH_TO_ARF, |
142
|
|
|
PATH_TO_ARF_WITH_ONLY_PASSING_RULE, |
143
|
|
|
]) |
144
|
|
|
@pytest.mark.usefixtures("remove_generated_file") |
145
|
|
|
def test_report_generation(arf_file_path): |
146
|
|
|
command_stdout = subprocess.check_output([OSCAP_REPORT_COMMAND, str(arf_file_path)]) |
147
|
|
|
assert command_stdout.decode("utf-8").startswith("<!DOCTYPE html>") |
148
|
|
|
|