@@ -358,7 +358,10 @@ def test_does_not_scan_symlinks(self):
358
358
non_existant_link .unlink ()
359
359
360
360
def test_cannot_open_file (self , caplog ):
361
- """Test behaviour when file cannot be opened"""
361
+ """
362
+ Test behavior when the file does not exist.
363
+ This covers the case where scan_file immediately detects that the file is missing.
364
+ """
362
365
self .scanner .logger .setLevel (logging .DEBUG )
363
366
with pytest .raises (StopIteration ):
364
367
next (
@@ -368,6 +371,31 @@ def test_cannot_open_file(self, caplog):
368
371
)
369
372
assert str .find ("Invalid file" , caplog .text )
370
373
374
+ def test_unopenable_file (self ):
375
+ """
376
+ Test behavior when the file exists but cannot be opened.
377
+ Simulates a permission error by patching open to raise an OSError.
378
+ This is significant because it tests the error handling beyond just checking for file existence.
379
+ """
380
+ # Create a temporary file that exists
381
+ tmp = tempfile .NamedTemporaryFile (
382
+ "w+b" ,
383
+ suffix = "-test-unopenable.out" ,
384
+ dir = self .mapping_test_dir ,
385
+ delete = False ,
386
+ )
387
+ tmp_filename = tmp .name
388
+ tmp .close ()
389
+ try :
390
+ # Patch open to force OSError signaling that the file is unopenable
391
+ with unittest .mock .patch (
392
+ "builtins.open" , side_effect = OSError ("Permission denied" )
393
+ ):
394
+ with pytest .raises (StopIteration ):
395
+ next (self .scanner .scan_file (tmp_filename ))
396
+ finally :
397
+ os .remove (tmp_filename )
398
+
371
399
def test_clean_file_path (self ):
372
400
filepath = "/tmp/cve-bin-tool/dhtei34fd/file_name.extracted/usr/bin/vulnerable_file" # nosec
373
401
# temp path is hardcoded for testing, not for usage
0 commit comments