Skip to content

Commit 09c5af4

Browse files
joydeep049terriko
andauthored
test: added 0-cve display test (#3982)
Co-authored-by: Joydeep Tripathy <[email protected]> Co-authored-by: Terri Oda <[email protected]>
1 parent bd631ab commit 09c5af4

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

cve_bin_tool/output_engine/html.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,25 +216,26 @@ def output_html(
216216

217217
cve_by_metrics: defaultdict[Remarks, list[dict[str, str]]] = defaultdict(list)
218218
for product_info, cve_data in all_cve_data.items():
219-
for cve in cve_data["cves"]:
220-
probability = "-"
221-
percentile = "-"
222-
223-
for metric, field in cve.metric.items():
224-
if metric == "EPSS":
225-
probability = round(field[0] * 100, 4)
226-
percentile = field[1]
227-
228-
cve_by_metrics[cve.remarks].append(
229-
{
230-
"cve_number": cve.cve_number,
231-
"cvss_version": str(cve.cvss_version),
232-
"cvss_score": str(cve.score),
233-
"epss_probability": str(probability),
234-
"epss_percentile": str(percentile),
235-
"severity": cve.severity,
236-
}
237-
)
219+
if cve_data["cves"]:
220+
for cve in cve_data["cves"]:
221+
probability = "-"
222+
percentile = "-"
223+
224+
for metric, field in cve.metric.items():
225+
if metric == "EPSS":
226+
probability = round(field[0] * 100, 4)
227+
percentile = field[1]
228+
229+
cve_by_metrics[cve.remarks].append(
230+
{
231+
"cve_number": cve.cve_number,
232+
"cvss_version": str(cve.cvss_version),
233+
"cvss_score": str(cve.score),
234+
"epss_probability": str(probability),
235+
"epss_percentile": str(percentile),
236+
"severity": cve.severity,
237+
}
238+
)
238239

239240
cve_metric_html_rows = []
240241
for remarks in sorted(cve_by_metrics):

cve_bin_tool/output_engine/print_mode/templates/content.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<span class="h5 float-end mr-5">CVE Count: {{ 0 if (not cve_data["cves"] or cve_data["cves"][0][1] == "UNKNOWN") else cve_data['cves'] | length }}</span><hr \>
2525
</div>
2626
<!-- If CVE Number UNKNOWN don't render -->
27-
{% if cve_data["cves"][0][1] != "UNKNOWN" %}
27+
{% if cve_data["cves"] and cve_data["cves"][0][1] != "UNKNOWN" %}
2828
<div class="col-12 mt-2">
2929
<table class="table table-bordered text-center">
3030
<tr class="table-secondary">

test/pages/html_report.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Copyright (C) 2021 Intel Corporation
1+
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
import logging
5-
from os import unlink
5+
import os
6+
7+
# from os import unlink
68
from pathlib import Path
79
from tempfile import NamedTemporaryFile
810

@@ -14,7 +16,6 @@
1416

1517

1618
class HTMLReport:
17-
1819
def __init__(self, page: Page, all_cve_data: dict[ProductInfo, CVEData]):
1920
self.html_output = NamedTemporaryFile(
2021
"w+", delete=False, suffix=".html", encoding="utf-8"
@@ -81,8 +82,14 @@ def load(self) -> None:
8182
self.page.goto(f"file://{self.html_output.name}")
8283

8384
def cleanup(self) -> None:
84-
self.html_output.close()
85-
unlink(self.html_output.name)
85+
"""Cleanup method for HTMLReport."""
86+
# Close the HTML output file if it's open
87+
if self.html_output:
88+
self.html_output.close()
89+
90+
# Remove the temporary HTML file if it exists
91+
if os.path.exists(self.html_output.name):
92+
os.unlink(self.html_output.name)
8693

8794
def search_product(self, product: str) -> None:
8895
self.product_search_field.fill(product)

test/test_html.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2021 Intel Corporation
1+
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
import re
@@ -12,7 +12,6 @@
1212

1313

1414
class TestOutputHTML:
15-
1615
MOCK_OUTPUT = {
1716
ProductInfo("vendor0", "product0", "1.0"): CVEData(
1817
cves=[
@@ -111,11 +110,15 @@ class TestOutputHTML:
111110

112111
@pytest.fixture(autouse=True)
113112
def setup_method(self, page: Page) -> None:
113+
"""Setup method for HTML Testing."""
114+
self.page = page
114115
self.html_report_page = HTMLReport(page, self.MOCK_OUTPUT)
115116
self.html_report_page.load()
116117

117118
def teardown_method(self) -> None:
118-
self.html_report_page.cleanup()
119+
"""Teardown method for HTML Testing."""
120+
if hasattr(self, "html_report_page") and self.html_report_page is not None:
121+
self.html_report_page.cleanup()
119122

120123
def check_products_visible_hidden(
121124
self, visible_row: Locator, *hidden_rows: Locator
@@ -296,3 +299,46 @@ def test_cve_remarks_table(self) -> None:
296299
"NOT AFFECTED",
297300
]
298301
)
302+
303+
# Test for empty cve_data["cves"] list
304+
def test_empty_cve_list(self) -> None:
305+
"""Test that the HTML report renders correctly with an empty cve_data["cves"] list."""
306+
empty_output = {
307+
ProductInfo("vendor0", "product0", "1.0"): CVEData(
308+
cves=[],
309+
paths={""},
310+
)
311+
}
312+
if hasattr(self, "html_report_page") and self.html_report_page is not None:
313+
self.html_report_page.cleanup() # Clean up the previous page
314+
self.html_report_page = HTMLReport(self.page, empty_output)
315+
self.html_report_page.load()
316+
product_rows = self.html_report_page.product_rows
317+
expect(product_rows).to_have_count(0)
318+
319+
# Test for cve_data["cves"] list with an element containing "UNKNOWN" CVE number
320+
def test_unknown_cve_number(self) -> None:
321+
"""Test that the HTML report renders correctly with a cve_data["cves"] list containing an 'UNKNOWN' CVE number."""
322+
unknown_cve_output = {
323+
ProductInfo("vendor0", "product0", "1.0"): CVEData(
324+
cves=[
325+
CVE(
326+
"UNKNOWN",
327+
"MEDIUM",
328+
score=4.2,
329+
cvss_version=2,
330+
cvss_vector="C:H",
331+
remarks=Remarks.NewFound,
332+
comments="showup",
333+
)
334+
],
335+
paths={""},
336+
)
337+
}
338+
self.html_report_page.cleanup() # Clean up the previous page
339+
self.html_report_page = HTMLReport(
340+
self.html_report_page.page, unknown_cve_output
341+
)
342+
self.html_report_page.load()
343+
product_rows = self.html_report_page.product_rows
344+
expect(product_rows).to_have_count(1)

0 commit comments

Comments
 (0)