Skip to content

Commit

Permalink
test: added 0-cve display test (intel#3982)
Browse files Browse the repository at this point in the history
Co-authored-by: Joydeep Tripathy <113792434+crazytrain328@users.noreply.github.com>
Co-authored-by: Terri Oda <terri.oda@intel.com>
  • Loading branch information
3 people committed Apr 4, 2024
1 parent bd631ab commit 09c5af4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
39 changes: 20 additions & 19 deletions cve_bin_tool/output_engine/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,26 @@ def output_html(

cve_by_metrics: defaultdict[Remarks, list[dict[str, str]]] = defaultdict(list)
for product_info, cve_data in all_cve_data.items():
for cve in cve_data["cves"]:
probability = "-"
percentile = "-"

for metric, field in cve.metric.items():
if metric == "EPSS":
probability = round(field[0] * 100, 4)
percentile = field[1]

cve_by_metrics[cve.remarks].append(
{
"cve_number": cve.cve_number,
"cvss_version": str(cve.cvss_version),
"cvss_score": str(cve.score),
"epss_probability": str(probability),
"epss_percentile": str(percentile),
"severity": cve.severity,
}
)
if cve_data["cves"]:
for cve in cve_data["cves"]:
probability = "-"
percentile = "-"

for metric, field in cve.metric.items():
if metric == "EPSS":
probability = round(field[0] * 100, 4)
percentile = field[1]

cve_by_metrics[cve.remarks].append(
{
"cve_number": cve.cve_number,
"cvss_version": str(cve.cvss_version),
"cvss_score": str(cve.score),
"epss_probability": str(probability),
"epss_percentile": str(percentile),
"severity": cve.severity,
}
)

cve_metric_html_rows = []
for remarks in sorted(cve_by_metrics):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<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 \>
</div>
<!-- If CVE Number UNKNOWN don't render -->
{% if cve_data["cves"][0][1] != "UNKNOWN" %}
{% if cve_data["cves"] and cve_data["cves"][0][1] != "UNKNOWN" %}
<div class="col-12 mt-2">
<table class="table table-bordered text-center">
<tr class="table-secondary">
Expand Down
17 changes: 12 additions & 5 deletions test/pages/html_report.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright (C) 2021 Intel Corporation
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import logging
from os import unlink
import os

# from os import unlink
from pathlib import Path
from tempfile import NamedTemporaryFile

Expand All @@ -14,7 +16,6 @@


class HTMLReport:

def __init__(self, page: Page, all_cve_data: dict[ProductInfo, CVEData]):
self.html_output = NamedTemporaryFile(
"w+", delete=False, suffix=".html", encoding="utf-8"
Expand Down Expand Up @@ -81,8 +82,14 @@ def load(self) -> None:
self.page.goto(f"file://{self.html_output.name}")

def cleanup(self) -> None:
self.html_output.close()
unlink(self.html_output.name)
"""Cleanup method for HTMLReport."""
# Close the HTML output file if it's open
if self.html_output:
self.html_output.close()

# Remove the temporary HTML file if it exists
if os.path.exists(self.html_output.name):
os.unlink(self.html_output.name)

def search_product(self, product: str) -> None:
self.product_search_field.fill(product)
Expand Down
52 changes: 49 additions & 3 deletions test/test_html.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2021 Intel Corporation
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import re
Expand All @@ -12,7 +12,6 @@


class TestOutputHTML:

MOCK_OUTPUT = {
ProductInfo("vendor0", "product0", "1.0"): CVEData(
cves=[
Expand Down Expand Up @@ -111,11 +110,15 @@ class TestOutputHTML:

@pytest.fixture(autouse=True)
def setup_method(self, page: Page) -> None:
"""Setup method for HTML Testing."""
self.page = page
self.html_report_page = HTMLReport(page, self.MOCK_OUTPUT)
self.html_report_page.load()

def teardown_method(self) -> None:
self.html_report_page.cleanup()
"""Teardown method for HTML Testing."""
if hasattr(self, "html_report_page") and self.html_report_page is not None:
self.html_report_page.cleanup()

def check_products_visible_hidden(
self, visible_row: Locator, *hidden_rows: Locator
Expand Down Expand Up @@ -296,3 +299,46 @@ def test_cve_remarks_table(self) -> None:
"NOT AFFECTED",
]
)

# Test for empty cve_data["cves"] list
def test_empty_cve_list(self) -> None:
"""Test that the HTML report renders correctly with an empty cve_data["cves"] list."""
empty_output = {
ProductInfo("vendor0", "product0", "1.0"): CVEData(
cves=[],
paths={""},
)
}
if hasattr(self, "html_report_page") and self.html_report_page is not None:
self.html_report_page.cleanup() # Clean up the previous page
self.html_report_page = HTMLReport(self.page, empty_output)
self.html_report_page.load()
product_rows = self.html_report_page.product_rows
expect(product_rows).to_have_count(0)

# Test for cve_data["cves"] list with an element containing "UNKNOWN" CVE number
def test_unknown_cve_number(self) -> None:
"""Test that the HTML report renders correctly with a cve_data["cves"] list containing an 'UNKNOWN' CVE number."""
unknown_cve_output = {
ProductInfo("vendor0", "product0", "1.0"): CVEData(
cves=[
CVE(
"UNKNOWN",
"MEDIUM",
score=4.2,
cvss_version=2,
cvss_vector="C:H",
remarks=Remarks.NewFound,
comments="showup",
)
],
paths={""},
)
}
self.html_report_page.cleanup() # Clean up the previous page
self.html_report_page = HTMLReport(
self.html_report_page.page, unknown_cve_output
)
self.html_report_page.load()
product_rows = self.html_report_page.product_rows
expect(product_rows).to_have_count(1)

0 comments on commit 09c5af4

Please sign in to comment.