Skip to content

Commit 027654b

Browse files
author
Shrishti Banshiar
committed
.pre-commit-config.yaml
1 parent 19ba192 commit 027654b

File tree

1 file changed

+98
-35
lines changed

1 file changed

+98
-35
lines changed

test/test_cli.py

+98-35
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def test_runs(self, caplog):
236236
main(["cve-bin-tool", test_path, "-r", ",".join(runs)])
237237
self.check_checkers_log(caplog, skip_checkers, runs)
238238

239+
import pytest
240+
import logging
241+
242+
@pytest.mark.timeout(60) # Test fails if it runs longer than 60 seconds
239243
@pytest.mark.skipif(not LONG_TESTS(), reason="Update flag tests are long tests")
240244
def test_update(self, caplog):
241245
test_path = str(Path(__file__).parent.resolve() / "csv")
@@ -275,6 +279,17 @@ def test_update(self, caplog):
275279
) in caplog.record_tuples
276280
caplog.clear()
277281

282+
with caplog.at_level(logging.DEBUG):
283+
main(
284+
["cve-bin-tool", "-l", "debug", "-u", "latest", "-n", "json", test_path]
285+
)
286+
assert (
287+
"cve_bin_tool.CVEDB",
288+
logging.DEBUG,
289+
"Updating CVE data. This will take a few minutes.",
290+
) in caplog.record_tuples
291+
caplog.clear()
292+
278293
def test_unknown_warning(self, caplog):
279294
"""Test that an "UNKNOWN" file generates a log (only in debug mode)"""
280295

@@ -376,45 +391,93 @@ def check_string_in_file(filename, string_to_find):
376391
if string_to_find in line:
377392
return True
378393
return False
394+
395+
import pytest
396+
import requests
397+
import logging
398+
import time
399+
from pathlib import Path
400+
from cve_bin_tool.cli import main # Ensure this import matches your project structure
401+
402+
403+
def is_nvd_reachable(retries=3, delay=5):
404+
"""Check if the NVD API is reachable, with retries in case of errors."""
405+
url = "https://services.nvd.nist.gov/rest/json/cves/1.0"
406+
for attempt in range(retries):
407+
try:
408+
response = requests.get(url, timeout=10)
409+
response.raise_for_status()
410+
return True
411+
except requests.exceptions.ChunkedEncodingError:
412+
if attempt < retries - 1:
413+
print(f"⚠️ NVD API response broken. Retrying in {delay} seconds...")
414+
time.sleep(delay)
415+
else:
416+
print("🚨 NVD API is unreachable after multiple attempts.")
417+
return False
418+
except requests.RequestException:
419+
return False
420+
return False
421+
422+
423+
def check_string_in_file(file_path, search_string):
424+
"""Check if a specific string is present in a file."""
425+
with open(file_path, "r") as f:
426+
return search_string in f.read()
427+
428+
429+
def test_severity(capsys, caplog, tmp_path):
430+
if not is_nvd_reachable():
431+
pytest.skip("Skipping test_severity because the NVD API is unreachable.")
432+
433+
tempdir = tmp_path / "test_dir"
434+
tempdir.mkdir()
435+
436+
# Check command line parameters - wrong case
437+
with pytest.raises(SystemExit) as e:
438+
main(["cve-bin-tool", "-S", "HIGH", str(tempdir)])
439+
assert e.value.args[0] == 2
440+
441+
# Check command line parameters - wrong option
442+
with pytest.raises(SystemExit) as e:
443+
main(["cve-bin-tool", "-S", "ALL", str(tempdir)])
444+
assert e.value.args[0] == 2
445+
446+
my_test_filename = "sevtest.csv"
447+
my_test_filename_path = Path(my_test_filename)
448+
449+
# Remove the file if it already exists
450+
if my_test_filename_path.exists():
451+
my_test_filename_path.unlink()
452+
453+
# Run the scan and capture logs
454+
with caplog.at_level(logging.DEBUG):
455+
main(
456+
[
457+
"cve-bin-tool",
458+
"-x",
459+
"-f",
460+
"csv",
461+
"-o",
462+
my_test_filename,
463+
"-S",
464+
"high",
465+
str(tempdir), # Removed `self.tempdir`, now using `tempdir`
466+
]
467+
)
379468

380-
def test_severity(self, capsys, caplog):
381-
# scan with severity setting to ensure only CVEs above severity threshold are reported
469+
# Verify that no CVEs with a severity of Medium are reported
470+
assert not check_string_in_file(my_test_filename, "MEDIUM"), "❌ MEDIUM severity CVEs should not be present!"
382471

383-
# Check command line parameters - wrong case
384-
with pytest.raises(SystemExit) as e:
385-
main(["cve-bin-tool", "-S", "HIGH", self.tempdir])
386-
assert e.value.args[0] == 2
387-
# Check command line parameters - wrong option
388-
with pytest.raises(SystemExit) as e:
389-
main(["cve-bin-tool", "-S", "ALL", self.tempdir])
390-
assert e.value.args[0] == 2
472+
# Verify that CVEs with a higher severity are reported
473+
assert check_string_in_file(my_test_filename, "HIGH"), "❌ HIGH severity CVEs not found!"
391474

392-
my_test_filename = "sevtest.csv"
393-
my_test_filename_pathlib = Path(my_test_filename)
475+
caplog.clear()
476+
477+
# Clean up after test
478+
if my_test_filename_path.exists():
479+
my_test_filename_path.unlink()
394480

395-
if my_test_filename_pathlib.exists():
396-
my_test_filename_pathlib.unlink()
397-
with caplog.at_level(logging.DEBUG):
398-
main(
399-
[
400-
"cve-bin-tool",
401-
"-x",
402-
"-f",
403-
"csv",
404-
"-o",
405-
my_test_filename,
406-
"-S",
407-
"high",
408-
str(Path(self.tempdir) / CURL_7_20_0_RPM),
409-
]
410-
)
411-
# Verify that no CVEs with a severity of Medium are reported
412-
assert not self.check_string_in_file(my_test_filename, "MEDIUM")
413-
# Verify that CVEs with a higher severity are reported
414-
assert self.check_string_in_file(my_test_filename, "HIGH")
415-
caplog.clear()
416-
if my_test_filename_pathlib.exists():
417-
my_test_filename_pathlib.unlink()
418481

419482
def test_CVSS_score(self, capsys, caplog):
420483
# scan with severity score to ensure only CVEs above score threshold are reported

0 commit comments

Comments
 (0)