@@ -300,3 +300,138 @@ def test_config_file(self, caplog, filename):
300300 pytest .fail (
301301 msg = "cli option should override logging level specified in config file"
302302 )
303+
304+ @staticmethod
305+ def check_string_in_file (filename , string_to_find ):
306+ # Check if 'string_to_find' is in file
307+ fh = open (filename )
308+ file_contents = fh .readlines ()
309+ fh .close ()
310+ for line in file_contents :
311+ if string_to_find in line :
312+ return True
313+ return False
314+
315+ def test_severity (self , capsys , caplog ):
316+ # scan with severity setting to ensure only CVEs above severity threshold are reported
317+
318+ # Check command line parameters - wrong case
319+ with pytest .raises (SystemExit ) as e :
320+ main (["cve-bin-tool" , "-S" , "HIGH" , self .tempdir ])
321+ assert e .value .args [0 ] == - 2
322+ # Check command line parameters - wrong option
323+ with pytest .raises (SystemExit ) as e :
324+ main (["cve-bin-tool" , "-S" , "ALL" , self .tempdir ])
325+ assert e .value .args [0 ] == - 2
326+
327+ my_test_filename = "sevtest.csv"
328+
329+ if os .path .exists (my_test_filename ):
330+ os .remove (my_test_filename )
331+ with caplog .at_level (logging .DEBUG ):
332+ main (
333+ [
334+ "cve-bin-tool" ,
335+ "-x" ,
336+ "-f" ,
337+ "csv" ,
338+ "-o" ,
339+ my_test_filename ,
340+ "-S" ,
341+ "high" ,
342+ os .path .join (self .tempdir , CURL_7_20_0_RPM ),
343+ ]
344+ )
345+ # Verify that no CVEs with a severity of Medium are reported
346+ assert not self .check_string_in_file (my_test_filename , "MEDIUM" )
347+ # Verify that CVEs with a higher severity are reported
348+ assert self .check_string_in_file (my_test_filename , "HIGH" )
349+ caplog .clear ()
350+ if os .path .exists (my_test_filename ):
351+ os .remove (my_test_filename )
352+
353+ def test_CVSS_score (self , capsys , caplog ):
354+ # scan with severity score to ensure only CVEs above score threshold are reported
355+
356+ my_test_filename = "sevtest.csv"
357+
358+ # Check command line parameters. Less than 0 result in default behaviour.
359+ if os .path .exists (my_test_filename ):
360+ os .remove (my_test_filename )
361+ with caplog .at_level (logging .DEBUG ):
362+ main (
363+ [
364+ "cve-bin-tool" ,
365+ "-x" ,
366+ "-c" ,
367+ "-1" ,
368+ "-f" ,
369+ "csv" ,
370+ "-o" ,
371+ my_test_filename ,
372+ os .path .join (self .tempdir , CURL_7_20_0_RPM ),
373+ ]
374+ )
375+ # Verify that some CVEs with a severity of Medium are reported
376+ assert self .check_string_in_file (my_test_filename , "MEDIUM" )
377+ caplog .clear ()
378+
379+ # Check command line parameters. >10 results in no CVEs being reported (Maximum CVSS score is 10)
380+ if os .path .exists (my_test_filename ):
381+ os .remove (my_test_filename )
382+ with caplog .at_level (logging .DEBUG ):
383+ main (
384+ [
385+ "cve-bin-tool" ,
386+ "-x" ,
387+ "-c" ,
388+ "11" ,
389+ "-f" ,
390+ "csv" ,
391+ "-o" ,
392+ my_test_filename ,
393+ os .path .join (self .tempdir , CURL_7_20_0_RPM ),
394+ ]
395+ )
396+ # Verify that no CVEs are reported (no file is created)
397+ assert not os .path .exists (my_test_filename )
398+ caplog .clear ()
399+
400+ with caplog .at_level (logging .DEBUG ):
401+ main (
402+ [
403+ "cve-bin-tool" ,
404+ "-x" ,
405+ "-f" ,
406+ "csv" ,
407+ "-o" ,
408+ my_test_filename ,
409+ os .path .join (self .tempdir , CURL_7_20_0_RPM ),
410+ ]
411+ )
412+ # Verify that CVEs with a severity of Medium are reported
413+ assert self .check_string_in_file (my_test_filename , "MEDIUM" )
414+ caplog .clear ()
415+ if os .path .exists (my_test_filename ):
416+ os .remove (my_test_filename )
417+
418+ # Now check subset
419+ with caplog .at_level (logging .DEBUG ):
420+ main (
421+ [
422+ "cve-bin-tool" ,
423+ "-x" ,
424+ "-c" ,
425+ "7" ,
426+ "-f" ,
427+ "csv" ,
428+ "-o" ,
429+ my_test_filename ,
430+ os .path .join (self .tempdir , CURL_7_20_0_RPM ),
431+ ]
432+ )
433+ # Verify that no CVEs with a severity of Medium are reported
434+ assert not self .check_string_in_file (my_test_filename , "MEDIUM" )
435+ if os .path .exists (my_test_filename ):
436+ os .remove (my_test_filename )
437+ caplog .clear ()
0 commit comments