Skip to content

Commit 003df2a

Browse files
fix(test_scanner.py): Add test for unopenable file condition (#4834)
* Fixes #762
1 parent 8691433 commit 003df2a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/test_scanner.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,10 @@ def test_does_not_scan_symlinks(self):
358358
non_existant_link.unlink()
359359

360360
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+
"""
362365
self.scanner.logger.setLevel(logging.DEBUG)
363366
with pytest.raises(StopIteration):
364367
next(
@@ -368,6 +371,31 @@ def test_cannot_open_file(self, caplog):
368371
)
369372
assert str.find("Invalid file", caplog.text)
370373

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+
371399
def test_clean_file_path(self):
372400
filepath = "/tmp/cve-bin-tool/dhtei34fd/file_name.extracted/usr/bin/vulnerable_file" # nosec
373401
# temp path is hardcoded for testing, not for usage

0 commit comments

Comments
 (0)