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