|
| 1 | +"""Test that framework extraction handles multiple archive formats. |
| 2 | +
|
| 3 | +The bug: _download_and_extract_to_temp() in framework_esp32.py hardcoded |
| 4 | +`tarfile.open(archive_path, "r:xz")` which fails when the framework or |
| 5 | +skeleton library archives are in .tar.gz or .zip format. |
| 6 | +
|
| 7 | +CI error: "tarfile.ReadError: not an lzma file" |
| 8 | +""" |
| 9 | + |
| 10 | +import io |
| 11 | +import os |
| 12 | +import tarfile |
| 13 | +import zipfile |
| 14 | +from pathlib import Path |
| 15 | +from unittest.mock import MagicMock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from fbuild.packages.cache import Cache |
| 20 | +from fbuild.packages.framework_esp32 import FrameworkESP32 |
| 21 | + |
| 22 | + |
| 23 | +def _create_tar_xz_archive(archive_path: Path, content_dir_name: str = "framework") -> None: |
| 24 | + """Create a valid .tar.xz archive with a single top-level directory.""" |
| 25 | + with tarfile.open(archive_path, "w:xz") as tar: |
| 26 | + # Create a directory entry |
| 27 | + dirinfo = tarfile.TarInfo(name=content_dir_name) |
| 28 | + dirinfo.type = tarfile.DIRTYPE |
| 29 | + dirinfo.mode = 0o755 |
| 30 | + tar.addfile(dirinfo) |
| 31 | + |
| 32 | + # Create a file with random data (incompressible) to exceed 1024-byte validation |
| 33 | + fileinfo = tarfile.TarInfo(name=f"{content_dir_name}/package.json") |
| 34 | + data = os.urandom(4096) |
| 35 | + fileinfo.size = len(data) |
| 36 | + tar.addfile(fileinfo, io.BytesIO(data)) |
| 37 | + |
| 38 | + |
| 39 | +def _create_tar_gz_archive(archive_path: Path, content_dir_name: str = "framework") -> None: |
| 40 | + """Create a valid .tar.gz archive with a single top-level directory.""" |
| 41 | + with tarfile.open(archive_path, "w:gz") as tar: |
| 42 | + dirinfo = tarfile.TarInfo(name=content_dir_name) |
| 43 | + dirinfo.type = tarfile.DIRTYPE |
| 44 | + dirinfo.mode = 0o755 |
| 45 | + tar.addfile(dirinfo) |
| 46 | + |
| 47 | + fileinfo = tarfile.TarInfo(name=f"{content_dir_name}/package.json") |
| 48 | + data = os.urandom(4096) |
| 49 | + fileinfo.size = len(data) |
| 50 | + tar.addfile(fileinfo, io.BytesIO(data)) |
| 51 | + |
| 52 | + |
| 53 | +def _create_zip_archive(archive_path: Path, content_dir_name: str = "framework") -> None: |
| 54 | + """Create a valid .zip archive with a single top-level directory.""" |
| 55 | + with zipfile.ZipFile(archive_path, "w") as zf: |
| 56 | + zf.writestr(f"{content_dir_name}/package.json", os.urandom(4096).hex()) |
| 57 | + |
| 58 | + |
| 59 | +class TestArchiveMagicByteValidation: |
| 60 | + """Test magic byte validation for different archive formats.""" |
| 61 | + |
| 62 | + def test_xz_magic_bytes_valid(self, tmp_path: Path) -> None: |
| 63 | + """Valid .tar.xz file passes magic byte check.""" |
| 64 | + archive_path = tmp_path / "framework.tar.xz" |
| 65 | + _create_tar_xz_archive(archive_path) |
| 66 | + |
| 67 | + with open(archive_path, "rb") as f: |
| 68 | + magic = f.read(6) |
| 69 | + assert magic == b"\xfd7zXZ\x00" |
| 70 | + |
| 71 | + def test_gz_magic_bytes_valid(self, tmp_path: Path) -> None: |
| 72 | + """Valid .tar.gz file has correct gzip magic bytes.""" |
| 73 | + archive_path = tmp_path / "framework.tar.gz" |
| 74 | + _create_tar_gz_archive(archive_path) |
| 75 | + |
| 76 | + with open(archive_path, "rb") as f: |
| 77 | + magic = f.read(2) |
| 78 | + assert magic == b"\x1f\x8b" |
| 79 | + |
| 80 | + def test_zip_magic_bytes_valid(self, tmp_path: Path) -> None: |
| 81 | + """Valid .zip file has correct PK magic bytes.""" |
| 82 | + archive_path = tmp_path / "framework.zip" |
| 83 | + _create_zip_archive(archive_path) |
| 84 | + |
| 85 | + with open(archive_path, "rb") as f: |
| 86 | + magic = f.read(4) |
| 87 | + assert magic == b"PK\x03\x04" |
| 88 | + |
| 89 | + |
| 90 | +class TestFrameworkExtractionFormats: |
| 91 | + """Test that _download_and_extract_to_temp handles all supported formats.""" |
| 92 | + |
| 93 | + def _run_extraction(self, tmp_path: Path, archive_filename: str, create_fn) -> Path: |
| 94 | + """Helper: create archive, mock download, run extraction, return extracted dir. |
| 95 | +
|
| 96 | + The _download_framework_components_parallel method uses cache_dir = install_dir.parent, |
| 97 | + so we place the install_dir inside a parent directory and pre-place the archive there. |
| 98 | + """ |
| 99 | + parent_dir = tmp_path / "parent" |
| 100 | + parent_dir.mkdir() |
| 101 | + install_dir = parent_dir / "install" |
| 102 | + install_dir.mkdir() |
| 103 | + |
| 104 | + # Place the archive in parent_dir (where cache_dir points) |
| 105 | + archive_path = parent_dir / archive_filename |
| 106 | + create_fn(archive_path) |
| 107 | + |
| 108 | + # Build a minimal FrameworkESP32 with a dummy URL ending in the archive filename |
| 109 | + mock_cache = MagicMock(spec=Cache) |
| 110 | + mock_cache.platforms_dir = tmp_path / "platforms" |
| 111 | + mock_cache.platforms_dir.mkdir() |
| 112 | + mock_cache.get_platform_path.return_value = install_dir |
| 113 | + |
| 114 | + framework = FrameworkESP32( |
| 115 | + cache=mock_cache, |
| 116 | + framework_url=f"https://example.com/releases/download/1.0.0/{archive_filename}", |
| 117 | + libs_url="", |
| 118 | + show_progress=False, |
| 119 | + ) |
| 120 | + |
| 121 | + # Directly call the extraction logic by invoking the parallel download method |
| 122 | + # The archive already exists in cache_dir (install_dir.parent), so download is skipped |
| 123 | + framework._download_framework_components_parallel(install_dir) |
| 124 | + |
| 125 | + return install_dir |
| 126 | + |
| 127 | + def test_tar_xz_extraction(self, tmp_path: Path) -> None: |
| 128 | + """Framework in .tar.xz format extracts correctly.""" |
| 129 | + install_dir = self._run_extraction(tmp_path, "esp32-core-1.0.0.tar.xz", _create_tar_xz_archive) |
| 130 | + assert (install_dir / "package.json").exists() |
| 131 | + |
| 132 | + def test_tar_gz_extraction(self, tmp_path: Path) -> None: |
| 133 | + """Framework in .tar.gz format extracts correctly.""" |
| 134 | + install_dir = self._run_extraction(tmp_path, "esp32-core-1.0.0.tar.gz", _create_tar_gz_archive) |
| 135 | + assert (install_dir / "package.json").exists() |
| 136 | + |
| 137 | + def test_zip_extraction(self, tmp_path: Path) -> None: |
| 138 | + """Framework in .zip format extracts correctly.""" |
| 139 | + install_dir = self._run_extraction(tmp_path, "esp32-core-1.0.0.zip", _create_zip_archive) |
| 140 | + assert (install_dir / "package.json").exists() |
| 141 | + |
| 142 | + def test_tar_gz_not_treated_as_xz(self, tmp_path: Path) -> None: |
| 143 | + """A .tar.gz file must not fail with 'not an lzma file' error.""" |
| 144 | + # This is the exact bug that was happening: .tar.gz or .zip was opened with "r:xz" |
| 145 | + cache_dir = tmp_path / "cache" |
| 146 | + cache_dir.mkdir() |
| 147 | + |
| 148 | + archive_path = cache_dir / "framework.tar.gz" |
| 149 | + _create_tar_gz_archive(archive_path) |
| 150 | + |
| 151 | + temp_dir = cache_dir / "_temp_extract_test" |
| 152 | + temp_dir.mkdir() |
| 153 | + |
| 154 | + # Opening a .tar.gz with "r:*" (auto-detect) should work |
| 155 | + with tarfile.open(archive_path, "r:*") as tar: |
| 156 | + tar.extractall(temp_dir) |
| 157 | + |
| 158 | + assert len(list(temp_dir.iterdir())) > 0 |
| 159 | + |
| 160 | + # Opening with "r:xz" should fail - this is what the old code did |
| 161 | + with pytest.raises(Exception): |
| 162 | + with tarfile.open(archive_path, "r:xz") as tar: |
| 163 | + tar.extractall(temp_dir) |
0 commit comments