forked from intel/cve-bin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
1008 lines (884 loc) · 35.7 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later
"""
CVE-bin-tool CLI tests
"""
import importlib
import logging
import os
import re
import shutil
import sys
import tempfile
from pathlib import Path
from test.utils import (
CURL_7_20_0_RPM,
CURL_7_20_0_URL,
DEB_FILE_PATH,
LONG_TESTS,
TempDirTest,
download_file,
)
from unittest.mock import patch
import pytest
from cve_bin_tool.cli import main
from cve_bin_tool.error_handler import ERROR_CODES, InsufficientArgs
from cve_bin_tool.extractor import Extractor
from cve_bin_tool.version_scanner import VersionScanner
class TestCLI(TempDirTest):
"""Tests the CVE Bin Tool CLI"""
TEST_PATH = Path(__file__).parent.resolve()
@pytest.mark.skipif(not LONG_TESTS(), reason="No file downloads in short tests")
def setup_method(self):
shutil.copyfile(DEB_FILE_PATH, Path(self.tempdir) / "test.deb")
download_file(CURL_7_20_0_URL, Path(self.tempdir) / CURL_7_20_0_RPM)
@pytest.mark.skipif(not LONG_TESTS(), reason="No file downloads in short tests")
def test_extract_curl_7_20_0(self):
"""Scanning curl-7.20.0"""
assert main(["cve-bin-tool", "-l", "debug", "-x", self.tempdir]) != 0
@pytest.mark.skipif(not LONG_TESTS(), reason="No file downloads in short tests")
def test_binary_curl_7_20_0(self):
"""Extracting from rpm and scanning curl-7.20.0"""
with Extractor() as ectx:
extracted_path = ectx.extract(str(Path(self.tempdir) / CURL_7_20_0_RPM))
assert (
main(
[
"cve-bin-tool",
"-l",
"debug",
str(Path(extracted_path) / "usr" / "bin" / "curl"),
]
)
!= 0
)
@pytest.mark.skipif(not LONG_TESTS(), reason="No file downloads in short tests")
def test_no_extraction(self):
"""Test scanner against curl-7.20.0 rpm with extraction turned off"""
assert main(["cve-bin-tool", str(Path(self.tempdir) / CURL_7_20_0_RPM)]) != 0
def test_extract_bad_zip_messages(self, caplog):
"""Test that bad zip files are logged as extraction failed, but
bad exe files produce no such message"""
bad_exe_file = str(Path(self.tempdir) / "empty-file.exe")
# creates an empty, invalid .exe test file
open(bad_exe_file, "w").close()
with caplog.at_level(logging.WARNING):
main(["cve-bin-tool", bad_exe_file])
assert "Failure extracting" not in caplog.text
bad_zip_file = str(Path(self.tempdir) / "empty-file.zip")
open(bad_zip_file, "w").close()
with caplog.at_level(logging.WARNING):
main(["cve-bin-tool", bad_zip_file])
assert "Failure extracting" in caplog.text
def test_extract_encrypted_zip_messages(self, caplog):
"""Test that encrypted zip file are logged as
extraction failure and the file is password protected"""
test_file = str(
Path(__file__).parent.resolve() / "assets" / "test-encrypted.zip"
)
with caplog.at_level(logging.ERROR):
main(["cve-bin-tool", str(test_file)])
assert "The file is password protected" in caplog.text
@pytest.mark.skipif(not LONG_TESTS(), reason="No file downloads in short tests")
def test_exclude(self, caplog):
"""Test that the exclude paths are not scanned"""
test_path = Path(__file__).parent.resolve()
exclude_path = str(test_path / "assets/")
checkers = list(VersionScanner().checkers.keys())
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", str(test_path), "-e", ",".join(exclude_path)])
self.check_exclude_log(caplog, exclude_path, checkers)
def test_usage(self):
"""Test that the usage returns 0"""
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool"])
assert e.value.args[0] == ERROR_CODES[InsufficientArgs]
def test_version(self):
"""Test that the version returns 0"""
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--version"])
assert e.value.args[0] == 0
def test_invalid_file_or_directory(self):
"""Test behaviour with an invalid file/directory"""
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "non-existant"])
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
def test_null_byte_in_filename(self):
"""Test behaviour with an invalid file/directory that contains a \0"""
# Put a null byte into the filename of a real file used in other tests
CSV_PATH = Path(__file__).parent.resolve() / "csv"
null_byte_file = str(CSV_PATH / "test_triage\0.csv")
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", null_byte_file])
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
null_byte_file = str(CSV_PATH / "test_triage.csv\0something")
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", null_byte_file])
assert e.value.args[0] == ERROR_CODES[FileNotFoundError]
def test_invalid_parameter(self):
"""Test that invalid parmeters exit with expected error code.
ArgParse calls sys.exit(2) for all errors"""
# no directory specified
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param"])
assert e.value.args[0] == 2
# bad parameter (but good directory)
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param", self.tempdir])
assert e.value.args[0] == 2
# worse parameter
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "--bad-param && cat hi", self.tempdir])
assert e.value.args[0] == 2
# bad parameter after directory
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", self.tempdir, "--bad-param;cat hi"])
assert e.value.args[0] == 2
@pytest.mark.skipif(not LONG_TESTS(), reason="Update flag tests are long tests")
def test_update_flags(self):
assert (
main(["cve-bin-tool", "-x", "-u", "never", "-n", "json", self.tempdir]) != 0
)
assert (
main(
["cve-bin-tool", "-x", "--update", "daily", "-n", "json", self.tempdir]
)
!= 0
)
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "-u", "whatever", "-n", "json", self.tempdir])
assert e.value.args[0] == 2
@staticmethod
def check_exclude_log(caplog, exclude_path, checkers):
# The final log has all the checkers detected
final_log = [
record for record in caplog.records if "NewFound CVEs" in record.message
]
assert len(final_log) == 0, "Checkers from excluded path scanned!!"
if final_log:
final_log = final_log[0].message
for checker in checkers:
assert checker in final_log, f"found a CVE {checker} in {exclude_path}"
caplog.clear()
@staticmethod
def check_checkers_log(caplog, skip_checkers, include_checkers):
# The final log has all the checkers detected
final_log = [
record for record in caplog.records if "Checkers:" in record.message
]
assert len(final_log) > 0, "Could not find checkers line in log"
final_log = final_log[0].message
for checker in skip_checkers:
assert checker not in final_log, f"found skipped checker {checker}"
for checker in include_checkers:
assert checker in final_log, f"could not find expected checker {checker}"
caplog.clear()
def test_skips(self, caplog):
"""Tests the skips option"""
test_path = str(Path(__file__).parent.resolve() / "csv")
skip_checkers = ["systemd", "xerces", "xml2", "kerberos"]
include_checkers = ["libexpat", "libgcrypt", "openssl", "sqlite"]
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", test_path, "-s", ",".join(skip_checkers)])
self.check_checkers_log(caplog, skip_checkers, include_checkers)
# swap skip_checkers and include_checkers
include_checkers, skip_checkers = skip_checkers, include_checkers
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", test_path, "-s", ",".join(skip_checkers)])
self.check_checkers_log(caplog, skip_checkers, include_checkers)
def test_runs(self, caplog):
test_path = str(Path(__file__).parent.resolve() / "csv")
runs = ["libexpat", "libgcrypt", "openssl", "sqlite"]
skip_checkers = ["systemd", "xerces", "xml2", "kerberos"]
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", test_path, "-r", ",".join(runs)])
self.check_checkers_log(caplog, skip_checkers, runs)
runs, skip_checkers = skip_checkers, runs
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", test_path, "-r", ",".join(runs)])
self.check_checkers_log(caplog, skip_checkers, runs)
import pytest
import logging
@pytest.mark.timeout(60) # Test fails if it runs longer than 60 seconds
@pytest.mark.skipif(not LONG_TESTS(), reason="Update flag tests are long tests")
def test_update(self, caplog):
test_path = str(Path(__file__).parent.resolve() / "csv")
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", "-u", "never", "-n", "json", test_path])
assert (
"cve_bin_tool",
logging.WARNING,
"Not verifying CVE DB cache",
) in caplog.record_tuples
caplog.clear()
with caplog.at_level(logging.DEBUG):
main(
["cve-bin-tool", "-l", "debug", "-u", "daily", "-n", "json", test_path]
)
assert (
"cve_bin_tool.CVEDB",
logging.INFO,
"Using cached CVE data (<24h old). Use -u now to update immediately.",
) in caplog.record_tuples or (
"cve_bin_tool.CVEDB",
logging.DEBUG,
"Updating CVE data. This will take a few minutes.",
) in caplog.record_tuples
caplog.clear()
with caplog.at_level(logging.DEBUG):
main(
["cve-bin-tool", "-l", "debug", "-u", "latest", "-n", "json", test_path]
)
assert (
"cve_bin_tool.CVEDB",
logging.DEBUG,
"Updating CVE data. This will take a few minutes.",
) in caplog.record_tuples
caplog.clear()
with caplog.at_level(logging.DEBUG):
main(
["cve-bin-tool", "-l", "debug", "-u", "latest", "-n", "json", test_path]
)
assert (
"cve_bin_tool.CVEDB",
logging.DEBUG,
"Updating CVE data. This will take a few minutes.",
) in caplog.record_tuples
caplog.clear()
def test_unknown_warning(self, caplog):
"""Test that an "UNKNOWN" file generates a log (only in debug mode)"""
# build the unknown test file in test/binaries
with tempfile.NamedTemporaryFile(
"w+b", suffix="png-unknown.out", delete=False
) as f:
signatures = [
b"\x7f\x45\x4c\x46\x02\x01\x01\x03\n",
b"Application uses deprecated png_write_init() and should be recompiled",
]
f.writelines(signatures)
filename = f.name
# Run against the "unknown" file
with caplog.at_level(logging.DEBUG):
main(["cve-bin-tool", filename, "-l", "debug"])
# clean up temporary file.
Path(filename).unlink()
warnings = [
record.message
for record in caplog.records
if record.levelname == "DEBUG" and record.module == "version_scanner"
]
assert len(warnings) > 0, "Unknown version warning didn't get generated"
assert f"png was detected with version UNKNOWN in file {filename}" in warnings
@patch("socket.socket")
def test_quiet_mode(self, mock_socket, capsys, caplog):
"""Test that a quiet mode isn't generating any output"""
for connection_error_scenario in [True, False]:
mock_socket.return_value.connect.side_effect = (
ConnectionError if connection_error_scenario else None
)
with tempfile.NamedTemporaryFile(
"w+b", suffix="strong-swan-4.6.3.out", delete=False
) as f:
signatures = [
b"\x7f\x45\x4c\x46\x02\x01\x01\x03\n",
b"strongSwan 4.6.3",
]
f.writelines(signatures)
filename = f.name
main(["cve-bin-tool", "-q", filename])
# clean up temporary file.
Path(filename).unlink()
# Make sure log is empty
assert not caplog.records
# Make sure nothing is getting printed on stdout or stderr
captured = capsys.readouterr()
assert not (captured.out or captured.err)
@pytest.mark.skip(reason="Temporarily disabled -- may need data changes")
@pytest.mark.parametrize(
"filename",
(
str(TEST_PATH / "config" / "cve_bin_tool_config.toml"),
str(TEST_PATH / "config" / "cve_bin_tool_config.yaml"),
),
)
def test_config_file(self, caplog, filename):
# scan with config file and overwrite output format
assert main(["cve-bin-tool", "-C", filename, "-l", "info"]) != 0
# assert only checkers for binutils and libcurl get to run
assert (
"cve_bin_tool.VersionScanner",
logging.INFO,
"Checkers: binutils, libcurl",
) in caplog.record_tuples
# assert only CVEs of libcurl get reflected. Because others are skipped
assert (
"cve_bin_tool",
logging.INFO,
"There are 1 products with known CVEs detected",
) in caplog.record_tuples
for record in caplog.record_tuples:
if record[1] < 20:
pytest.fail(
msg="cli option should override logging level specified in config file"
)
@staticmethod
def check_string_in_file(filename, string_to_find):
# Check if 'string_to_find' is in file
fh = open(filename)
file_contents = fh.readlines()
fh.close()
for line in file_contents:
if string_to_find in line:
return True
return False
import pytest
import requests
import logging
import time
from pathlib import Path
from cve_bin_tool.cli import main # Ensure this import matches your project structure
def is_nvd_reachable(retries=3, delay=5):
"""Check if the NVD API is reachable, with retries in case of errors."""
url = "https://services.nvd.nist.gov/rest/json/cves/1.0"
for attempt in range(retries):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
return True
except requests.exceptions.ChunkedEncodingError:
if attempt < retries - 1:
print(f"⚠️ NVD API response broken. Retrying in {delay} seconds...")
time.sleep(delay)
else:
print("🚨 NVD API is unreachable after multiple attempts.")
return False
except requests.RequestException:
return False
return False
def check_string_in_file(file_path, search_string):
"""Check if a specific string is present in a file."""
with open(file_path, "r") as f:
return search_string in f.read()
def test_severity(capsys, caplog, tmp_path):
if not is_nvd_reachable():
pytest.skip("Skipping test_severity because the NVD API is unreachable.")
tempdir = tmp_path / "test_dir"
tempdir.mkdir()
# Check command line parameters - wrong case
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "-S", "HIGH", str(tempdir)])
assert e.value.args[0] == 2
# Check command line parameters - wrong option
with pytest.raises(SystemExit) as e:
main(["cve-bin-tool", "-S", "ALL", str(tempdir)])
assert e.value.args[0] == 2
my_test_filename = "sevtest.csv"
my_test_filename_path = Path(my_test_filename)
# Remove the file if it already exists
if my_test_filename_path.exists():
my_test_filename_path.unlink()
# Run the scan and capture logs
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"-f",
"csv",
"-o",
my_test_filename,
"-S",
"high",
str(tempdir), # Removed `self.tempdir`, now using `tempdir`
]
)
# Verify that no CVEs with a severity of Medium are reported
assert not check_string_in_file(my_test_filename, "MEDIUM"), "❌ MEDIUM severity CVEs should not be present!"
# Verify that CVEs with a higher severity are reported
assert check_string_in_file(my_test_filename, "HIGH"), "❌ HIGH severity CVEs not found!"
caplog.clear()
# Clean up after test
if my_test_filename_path.exists():
my_test_filename_path.unlink()
def test_CVSS_score(self, capsys, caplog):
# scan with severity score to ensure only CVEs above score threshold are reported
my_test_filename = "sevtest.csv"
my_test_filename_pathlib = Path(my_test_filename)
# Check command line parameters. Less than 0 result in default behaviour.
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"-c",
"-1",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that some CVEs with a severity of Medium are reported
assert self.check_string_in_file(my_test_filename, "MEDIUM")
caplog.clear()
# Check command line parameters. >10 results in no CVEs being reported (Maximum CVSS score is 10)
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"-c",
"11",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that no CVEs are reported
with open(my_test_filename_pathlib) as fd:
assert not fd.read().split("\n")[1]
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that CVEs with a severity of Medium are reported
assert self.check_string_in_file(my_test_filename, "MEDIUM")
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
# Now check subset
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"-c",
"7",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that no CVEs with a severity of Medium are reported
assert not self.check_string_in_file(my_test_filename, "MEDIUM")
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
caplog.clear()
def test_basic_epss(self, caplog):
# test EPSS functionality
# updates EPSS in db, scans sbom with EPSS enabled and writes EPSS to csv
with caplog.at_level(logging.ERROR):
epss_filename = "epss_test.csv"
epss_filename_pathlib = Path(epss_filename)
if epss_filename_pathlib.exists():
epss_filename_pathlib.unlink()
SBOM_PATH = Path(__file__).parent.resolve() / "sbom"
# first let's check that sbom scan with epss enables and update of the epss source runs without error
with caplog.at_level(logging.ERROR):
main(
[
"cve-bin-tool",
"--metrics",
"-u",
"never",
"--disable-data-source",
"OSV,GAD,REDHAT,PURL2CPE",
"-n",
"json",
"--sbom",
"cyclonedx",
"--sbom-file",
str(SBOM_PATH / "cyclonedx_test.json"),
"-f",
"csv",
"-o",
epss_filename,
]
)
assert (
len(caplog.messages) == 0
), f"Error running basic epss with {';'.join(caplog.messages)}"
# as a second stept we check if there are EPSS values in the outputfile
content = epss_filename_pathlib.open(mode="r", newline="").read()
# filter out empty lines under windows
csv_rows = content.splitlines()
assert len(csv_rows) > 0
# row 0 is the header,
# vendor,product,version,cve_number,severity,score,source,cvss_version,cvss_vector,paths,
# remarks,comments,epss_probability,epss_percentile
row_zero = csv_rows[0].split(",")
# row 1 should contain some EPSS values
# gnu,glibc,2.11.1,NotFound,CVE-2009-5029,MEDIUM,6.8,NVD,2,AV:N/AC:M/Au:N/C:P/I:P/A:P,,
# NewFound,,0.00801,0.82134
row_one = csv_rows[1].split(",")
# epss_percentile is the last value
assert row_zero[-1] == "epss_percentile", (
"last header value in produced csv file must be " "'epss_percentile'"
)
assert len(row_one) == 14, "one csv row should have 14 values"
assert (
0.0 <= float(row_one[-1]) <= 1.0
), "last value in the row must be the epss percentile value, i.e., a floating point between 0.0 and 1.0"
# epss_probability second last value
assert (
row_zero[-2] == "epss_probability"
), "second last header value in produced csv file must be 'epss_probability'"
assert (
0.0 <= float(row_one[-2]) <= 1.0
), "last value in the row must be the epss probability value, i.e., a floating point between 0.0 and 1.0"
if epss_filename_pathlib.exists():
epss_filename_pathlib.unlink()
def test_EPSS_probability(self, capsys, caplog):
"""scan with EPSS probability to ensure only CVEs above score threshold are reported
Checks cannot placed on epss probability value as the value changes everyday
"""
my_test_filename = "epss_probability.csv"
my_test_filename_pathlib = Path(my_test_filename)
# Check command line parameters. Less than 0 result in default behaviour.
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"--epss-probability",
"-12",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that some CVEs with a severity of Medium are reported
# Checks cannot placed on epss probability value as the value changes everyday.
assert self.check_string_in_file(my_test_filename, "MEDIUM")
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"--epss-probability",
"110",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# FIXME: disabled due to test failures, needs better fix. issue #3674
# Verify that no CVEs are reported
# with open(my_test_filename_pathlib) as fd:
# assert not fd.read().split("\n")[1]
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
def test_EPSS_percentile(self, capsys, caplog):
"""scan with EPSS percentile to ensure only CVEs above score threshold are reported
Checks cannot placed on epss percentile value as the value changes everyday
"""
my_test_filename = "epss_percentile.csv"
my_test_filename_pathlib = Path(my_test_filename)
# Check command line parameters. Less than 0 result in default behaviour.
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"--epss-percentile",
"-1",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# Verify that some CVEs with a severity of Medium are reported
# Checks cannot placed on epss percentile value as the value changes everyday.
assert self.check_string_in_file(my_test_filename, "MEDIUM")
caplog.clear()
# Check command line parameters. >10 results in no CVEs being reported (Maximum EPSS percentile is 100)
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
with caplog.at_level(logging.DEBUG):
main(
[
"cve-bin-tool",
"-x",
"--epss-percentile",
"110",
"-f",
"csv",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
)
# FIXME: disabled due to test failures, needs better fix. issue #3674
# Verify that no CVEs are reported
# with open(my_test_filename_pathlib) as fd:
# assert not fd.read().split("\n")[1]
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
# @pytest.mark.skip(reason="Temporarily disabled -- may need data changes")
def test_SBOM(self, caplog):
# check sbom file option
SBOM_PATH = Path(__file__).parent.resolve() / "sbom"
with caplog.at_level(logging.INFO):
main(
[
"cve-bin-tool",
"--sbom",
"spdx",
"--sbom-file",
str(SBOM_PATH / "spdx_test.spdx"),
]
)
# find the "known CVEs detected" line from caplog
known_cves_message = None
# tuple is (tool_name, log_level, log_message) but we only care about the last
for _, _, log_message in caplog.record_tuples:
if re.search(r"with known CVEs detected", log_message):
known_cves_message = log_message
assert (
known_cves_message is not None
), "Expected 3 products with cves, none found"
# since sometimes this test breaks due to data changes, let's just say we want at least 2
# products with cves (though there should be 3 at time of writing)
m = re.match(
r"There are (?P<product_number>\d*) products with known CVEs detected",
known_cves_message,
)
assert (
int(m.group("product_number")) >= 2
), "Not enough products with cves found in output"
def test_sbom_detection(self, caplog):
SBOM_PATH = Path(__file__).parent.resolve() / "sbom"
with caplog.at_level(logging.INFO):
main(
[
"cve-bin-tool",
str(SBOM_PATH / "swid_test.xml"),
]
)
assert (
"cve_bin_tool",
logging.INFO,
"Using CVE Binary Tool SBOM Auto Detection",
) in caplog.record_tuples
@pytest.mark.skipif(not LONG_TESTS(), reason="Skipping long tests")
def test_console_output_depending_reportlab_existence(self, caplog):
import subprocess
from importlib.machinery import ModuleSpec
from importlib.util import find_spec, module_from_spec
if find_spec("reportlab"):
reportlab_was_installed = True
sys.modules.pop("reportlab")
subprocess.check_call(
[sys.executable, "-m", "pip", "uninstall", "-y", "reportlab"]
)
else:
reportlab_was_installed = False
my_test_filename_pathlib = Path(self.tempdir) / "reportlab_test_report.pdf"
my_test_filename = str(my_test_filename_pathlib)
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
pkg_to_spoof = "reportlab"
not_installed_msg = "PDF output not available."
execution = [
"cve-bin-tool",
"-f",
"pdf",
"-o",
my_test_filename,
str(Path(self.tempdir) / CURL_7_20_0_RPM),
]
with caplog.at_level(logging.INFO):
main(execution)
assert (
"cve_bin_tool",
logging.ERROR,
not_installed_msg,
) in caplog.record_tuples
caplog.clear()
if my_test_filename_pathlib.exists():
my_test_filename_pathlib.unlink()
if reportlab_was_installed:
subprocess.check_call([sys.executable, "-m", "pip", "install", "reportlab"])
else:
ms = ModuleSpec(pkg_to_spoof, "surelyanotexistentmodule")
m = module_from_spec(ms)
sys.modules[pkg_to_spoof] = m
with caplog.at_level(logging.INFO):
main(execution)
assert (
"cve_bin_tool",
logging.INFO,
not_installed_msg,
) not in caplog.record_tuples
@pytest.mark.skipif(
not importlib.util.find_spec("reportlab"),
reason="Reportlab needed for pdf test",
)
def test_0_cve_pdf_report(self, caplog):
"""Tests to make sure --report behaves as expected when 0 cves are found.
We expect a short pdf file saying 0 cves were found."""
with tempfile.TemporaryDirectory() as emptytemp:
# Set a filename for report in tempdir, make sure it doesn't exist.
report_0 = Path(self.tempdir) / "0_cve_report.pdf"
if report_0.exists():
report_0.unlink()
# Call cve-bin-tool to scan empty dir and product pdf report.
cbt_command = [
"cve-bin-tool",
"--offline",
"--format",
"pdf",
"-o",
str(report_0),
"--report",
str(emptytemp),
]
main(cbt_command)
# Make sure the report was created and has something in it.
# Testing what's in the report would increase test execution time
# so we're leaving that out for now
assert report_0.exists()
assert report_0.stat().st_size > 0
# get rid of generated file
report_0.unlink()
yamls = [
[
"cve-bin-tool",
"--generate-config",
"yaml",
"-n",
"api2",
"--format",
"csv",
"--severity",
"high",
"-i",
"test/test_json.py",
],
]
output_yamls = [
[
"nvd : api2",
"format : csv",
"severity : high",
"input_file : test/test_json.py",
"update : daily",
"log_level : info",
"nvd_api_key : ",
"offline : false",
"vex_file : ",
],
]
tomls = [
[
"cve-bin-tool",
"--generate-config",
"toml",
"--nvd",
"json",
"--sbom",
"swid",
"--log",
"warning",
"--offline",
],
]
output_tomls = [
[
'nvd = "json"',
'sbom = "swid"',
'log_level = "warning"',
"offline = true",
'input_file = ""',
'sbom_file = ""',
'runs = ""',
"extract = true",
"append = false",
'import = ""',
'vex_file = ""',
],
]
@pytest.mark.parametrize(
"args, expected_files, expected_contents",
[
(
yamls[0],
"config.yaml",
output_yamls[0],
),
(tomls[0], "config.toml", output_tomls[0]),
],
)
def test_config_generator(self, args, expected_files, expected_contents, caplog):
# When
with caplog.at_level(logging.INFO):
main(args)
# Then
assert os.path.exists(expected_files)
with open(expected_files) as f:
content = f.read()
for expected_content in expected_contents:
assert expected_content in content
# Cleanup
os.remove(expected_files)
def test_disabled_sources(self, caplog):
"""Attempts to disable various data sources and makes sure they appear
to be disabled correctly.
This only tests for disabled messages, it doesn't check on the update code
because we'd have to actually do updates then and they're slow.
"""
# attempt to call with all sources disabled
with caplog.at_level(logging.INFO):
main(
[
"cve-bin-tool",
"--update",
"never",
"--nvd-api-key",
"no",
"-n",
"json-mirror",
"--disable-data-source",