Passed
Pull Request — master (#100)
by Jan
05:43
created

tests.unit_tests.test_full_text_parser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
A test_parsing_of_text() 0 46 1
A test_replace_sub_tag() 0 11 1
A get_report() 0 3 2
1
import pytest
2
from lxml import etree
3
4
from openscap_report.scap_results_parser import SCAPResultsParser
5
from openscap_report.scap_results_parser.parsers import FullTextParser
6
7
from ..constants import (PATH_TO_ARF,
8
                         PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
9
10
REF_VALUES = {
11
    "id-1234": "text1",
12
    "1234": 'echo "nwm"'
13
}
14
FULL_TEXT_PARSER = FullTextParser(REF_VALUES)
15
16
17
@pytest.mark.unit_test
18
@pytest.mark.parametrize("tag, return_data", [
19
    (etree.Element("sub", idref="id-1234"), "text1"),
20
    (etree.Element("sub", idref="1234"), 'echo "nwm"'),
21
    (
22
        etree.Element("sub", idref="hello-ID"),
23
        "!!! Error !!! - Sub tag reference does not exist: hello-ID"
24
    ),
25
])
26
def test_replace_sub_tag(tag, return_data):
27
    assert FULL_TEXT_PARSER.replace_sub_tag(tag) == return_data
28
29
30
def get_report(src):
31
    with open(src, "r") as report_file:
0 ignored issues
show
introduced by
Using open without explicitly specifying an encoding
Loading history...
32
        return SCAPResultsParser(report_file.read().encode()).parse_report()
33
34
35
BASIC_REPORT = get_report(PATH_TO_ARF)
36
REPORT_REPRODUCING_DANGLING_REFERENCE_TO = get_report(PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
37
38
39
@pytest.mark.integration_test
40
@pytest.mark.parametrize("report, rule_id, expected_data", [
41
    (
42
        REPORT_REPRODUCING_DANGLING_REFERENCE_TO,
43
        "xccdf_org.ssgproject.content_rule_grub2_l1tf_argument",
44
        (
45
            "L1 Terminal Fault (L1TF) is a hardware vulnerability which allows unprivileged\n"
46
            "speculative access to data which is available in the Level 1 Data Cache when\n"
47
            "the page table entry isn't present.\n\nSelect the appropriate mitigation by"
48
            " adding the argument\n"
49
            "<code>l1tf=!!! Error !!! - Sub tag reference does not exist: dangling reference to !"
50
            "</code> to the default\nGRUB 2 command line for the Linux operating system.\n"
51
            "Configure the default Grub2 kernel command line to contain "
52
            "l1tf=xccdf_value(var_l1tf_options) as follows:\n"
53
            "<pre># grub2-editenv - set &quot;$(grub2-editenv - list | grep kernelopts) "
54
            "l1tf=xccdf_value(var_l1tf_options)&quot;</pre>\n\nSince Linux Kernel 4.19 "
55
            "you can check the L1TF vulnerability state with the\n"
56
            "following command:\n<code>cat /sys/devices/system/cpu/vulnerabilities/l1tf</code>"
57
        )
58
    ),
59
    (
60
        BASIC_REPORT,
61
        "xccdf_org.ssgproject.content_rule_postfix_client_configure_mail_alias",
62
        (
63
            "Make sure that mails delivered to root user are forwarded to a monitored\n"
64
            "email address. Make sure that the address\[email protected] is"
65
            " a valid email address\nreachable from the system in question. Use the following"
66
            " command to\nconfigure the alias:\n<pre>$ sudo echo &quot;root: "
67
            "[email protected]&quot; &gt;&gt; /etc/aliases\n$ sudo newaliases</pre>"
68
        )
69
    ),
70
    (
71
        REPORT_REPRODUCING_DANGLING_REFERENCE_TO,
72
        "xccdf_org.ssgproject.content_rule_postfix_client_configure_mail_alias",
73
        (
74
            "Make sure that mails delivered to root user are forwarded to a monitored\n"
75
            "email address. Make sure that the address\[email protected] is"
76
            " a valid email address\nreachable from the system in question. Use the following"
77
            " command to\nconfigure the alias:\n<pre>$ sudo echo &quot;root: "
78
            "[email protected]&quot; &gt;&gt; /etc/aliases\n$ sudo newaliases</pre>"
79
        )
80
    ),
81
])
82
def test_parsing_of_text(report, rule_id, expected_data):
83
    print(repr(report.rules[rule_id].description))
84
    assert report.rules[rule_id].description == expected_data
85