-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add explicit img disk image support #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16a4f22
Add explicit img disk image support
popey c7e4db7
Address img fallback review findings
popey 521927b
Reject compressed img inputs safely
popey 35ab51e
Preserve raw format detection precedence
popey 8494e41
Pass image format to qemu-nbd
minnielove2026 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import importlib.util | ||
| import logging | ||
| from pathlib import Path | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
| from unittest.mock import Mock | ||
|
|
||
|
|
||
| MODULE_PATH = Path(__file__).resolve().parents[1] / "sbom-vm.py" | ||
| SPEC = importlib.util.spec_from_file_location("sbom_vm", MODULE_PATH) | ||
| assert SPEC is not None and SPEC.loader is not None | ||
| sbom_vm = importlib.util.module_from_spec(SPEC) | ||
| SPEC.loader.exec_module(sbom_vm) | ||
|
|
||
|
|
||
| class ImageFormatTest(unittest.TestCase): | ||
| def setUp(self): | ||
| setattr(sbom_vm, "logger", logging.getLogger("sbom-vm-test")) | ||
|
|
||
| def test_qemu_detected_format_takes_precedence_over_img_suffix(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.img" | ||
| image_path.write_bytes(b"qcow2 image content") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock( | ||
| return_value=subprocess.CompletedProcess( | ||
| args=["qemu-img"], | ||
| returncode=0, | ||
| stdout="image: firmware.img\nfile format: qcow2\n", | ||
| ) | ||
| ) | ||
|
popey marked this conversation as resolved.
|
||
|
|
||
| self.assertEqual(mounter._detect_image_format(), "qcow2") | ||
|
|
||
| def test_img_falls_back_to_raw_when_qemu_cannot_identify_it(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.img" | ||
| image_path.write_bytes(b"raw image content") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock( | ||
| side_effect=subprocess.CalledProcessError( | ||
| returncode=1, | ||
| cmd=["qemu-img", "info", str(image_path)], | ||
| ) | ||
| ) | ||
|
|
||
| self.assertEqual(mounter._detect_image_format(), "raw") | ||
|
|
||
| def test_img_falls_back_to_raw_when_qemu_is_unavailable(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.IMG" | ||
| image_path.write_bytes(b"raw image content") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock(side_effect=FileNotFoundError("qemu-img")) | ||
|
|
||
| self.assertEqual(mounter._detect_image_format(), "raw") | ||
|
|
||
| def test_img_with_gzip_magic_is_rejected(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.img" | ||
| image_path.write_bytes(b"\x1f\x8bcompressed image") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock() | ||
|
|
||
| with self.assertRaisesRegex( | ||
| RuntimeError, | ||
| r"Compressed \.img files are not supported", | ||
| ): | ||
| mounter._detect_image_format() | ||
|
|
||
| mounter._run_command.assert_not_called() | ||
|
|
||
| def test_gzipped_raw_image_keeps_existing_gzip_handling(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.raw" | ||
| image_path.write_bytes(b"\x1f\x8bcompressed image") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock( | ||
| side_effect=subprocess.CalledProcessError( | ||
| returncode=1, | ||
| cmd=["qemu-img", "info", str(image_path)], | ||
| ) | ||
| ) | ||
|
|
||
| self.assertEqual(mounter._detect_image_format(), "gzip") | ||
|
|
||
| def test_raw_content_detection_precedes_gzip_fallback(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| image_path = Path(temp_dir) / "firmware.raw" | ||
| image_path.write_bytes(b"\x1f\x8braw image content") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._run_command = Mock( | ||
| return_value=subprocess.CompletedProcess( | ||
| args=["qemu-img"], | ||
| returncode=0, | ||
| stdout="image: firmware.raw\nfile format: raw\n", | ||
| ) | ||
| ) | ||
|
|
||
| self.assertEqual(mounter._detect_image_format(), "raw") | ||
|
|
||
| def test_connect_passes_prepared_format_and_read_only_to_qemu_nbd(self): | ||
| image_path = Path("firmware.img") | ||
| mounter = sbom_vm.ImageMounter(str(image_path)) | ||
| mounter._prepare_image = Mock(return_value=image_path) | ||
| mounter._find_free_nbd_device = Mock(return_value="/dev/nbd0") | ||
| mounter._run_command = Mock() | ||
| mounter.prepared_image_format = "raw" | ||
|
|
||
| mounter.connect_image() | ||
|
|
||
| self.assertEqual( | ||
| mounter._run_command.call_args_list[0].args[0], | ||
| [ | ||
| "qemu-nbd", | ||
| "--read-only", | ||
| "--format=raw", | ||
| "--connect", "/dev/nbd0", | ||
| "firmware.img", | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.