| 1 |
# Copyright (C) 2025 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
from datetime import datetime |
| 6 |
from email.utils import parsedate_to_datetime |
| 7 |
import io |
| 8 |
from pathlib import Path |
| 9 |
import tarfile |
| 10 |
from debian import deb822 |
| 11 |
import pytest |
| 12 |
import requests |
| 13 |
import zstandard |
| 14 |
import lz4.frame |
| 15 |
from debsbom.download import PackageDownloader |
| 16 |
from debsbom.repack import SourceArchiveMerger |
| 17 |
import debsbom.dpkg.package as dpkg |
| 18 |
from debsbom.repack.merger import CorruptedFileError, DscFileNotFoundError |
| 19 |
import debsbom.snapshot.client as sdlclient |
| 20 |
from debsbom.util import Compression |
| 21 |
from debsbom.util.checksum import ChecksumAlgo |
| 22 |
|
| 23 |
|
| 24 |
def test_compressor_from_tool(): |
| 25 |
assert Compression.from_tool(None) == Compression.NONE |
| 26 |
for c in Compression.formats(): |
| 27 |
assert Compression.from_tool(c.tool) == c |
| 28 |
with pytest.raises(RuntimeError): |
| 29 |
Compression.from_tool("false") |
| 30 |
|
| 31 |
|
| 32 |
def test_compressor_from_ext(): |
| 33 |
assert Compression.from_ext("") == Compression.NONE |
| 34 |
assert Compression.from_ext(None) == Compression.NONE |
| 35 |
for c in Compression.formats(): |
| 36 |
assert Compression.from_ext(c.fileext) == c |
| 37 |
with pytest.raises(ValueError): |
| 38 |
Compression.from_ext("foobar") |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture(scope="session") |
| 42 |
def dldir(tmp_path_factory): |
| 43 |
return tmp_path_factory.mktemp("downloads") |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture(scope="session") |
| 47 |
def some_packages(dldir, http_session): |
| 48 |
sdl = sdlclient.SnapshotDataLake(session=http_session) |
| 49 |
dl = PackageDownloader(dldir, http_session) |
| 50 |
|
| 51 |
packages = [ |
| 52 |
# .orig.tar and .debian.tar |
| 53 |
dpkg.SourcePackage( |
| 54 |
"libnet-smtp-ssl-perl", |
| 55 |
"1.04-2", |
| 56 |
checksums={ |
| 57 |
ChecksumAlgo.SHA256SUM: "b5e63090e1608c37ead4432206028fda37046128bfcaf3eb7ba58251875295a1" |
| 58 |
}, |
| 59 |
), |
| 60 |
# .orig.tar and .debian.tar with epoch |
| 61 |
dpkg.SourcePackage("libcap2", "1:2.75-10"), |
| 62 |
# debian dir in sources |
| 63 |
dpkg.SourcePackage("dgit", "13.13"), |
| 64 |
# debian dir via compressed .diff |
| 65 |
dpkg.SourcePackage("libdata-validate-domain-perl", "0.10-1.1"), |
| 66 |
] |
| 67 |
for p in packages: |
| 68 |
dl.register(list(sdlclient.SourcePackage(sdl, p.name, str(p.version)).srcfiles()), p) |
| 69 |
list(dl.download()) |
| 70 |
return packages |
| 71 |
|
| 72 |
|
| 73 |
EXPECTED_CHANGELOG_TIMESTAMPS = { |
| 74 |
("libnet-smtp-ssl-perl", "1.04-2"): "Fri, 01 Jul 2022 00:09:44 +0100", |
| 75 |
("libcap2", "1:2.75-10"): "Sat, 26 Jul 2025 20:46:06 +0200", |
| 76 |
("dgit", "13.13"): "Sun, 24 Aug 2025 11:43:28 +0100", |
| 77 |
("libdata-validate-domain-perl", "0.10-1.1"): "Sun, 27 Dec 2020 17:26:02 +0100", |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize("compress", [None, "bzip2", "gzip", "xz", "zstd", "lz4"]) |
| 82 |
@pytest.mark.parametrize("mtime", [None, "Wed, 01 Oct 2025 12:34:56 +0100"]) |
| 83 |
@pytest.mark.online |
| 84 |
def test_merger(tmpdir, some_packages, dldir, compress, mtime): |
| 85 |
outdir = Path(tmpdir / "merged") |
| 86 |
sam = SourceArchiveMerger(dldir / "sources", outdir, compress=Compression.from_tool(compress)) |
| 87 |
|
| 88 |
for p in some_packages: |
| 89 |
if mtime: |
| 90 |
expected_timestamp_str = mtime |
| 91 |
else: |
| 92 |
expected_timestamp_str = EXPECTED_CHANGELOG_TIMESTAMPS[(p.name, p.version)] |
| 93 |
dt_object = parsedate_to_datetime(expected_timestamp_str) |
| 94 |
expected_timestamp = int(dt_object.timestamp()) |
| 95 |
result = sam.merge( |
| 96 |
p, |
| 97 |
apply_patches=True, |
| 98 |
mtime=dt_object if mtime else None, |
| 99 |
) |
| 100 |
assert p.name in result.name |
| 101 |
|
| 102 |
extract_path = Path(tmpdir) / f"extracted_{p.name}" |
| 103 |
extract_path.mkdir(exist_ok=True, parents=False) |
| 104 |
|
| 105 |
tar_open_args = {"name": result} |
| 106 |
if compress == "zstd" or compress == "lz4": |
| 107 |
with open(result, mode="rb") as compressed_file: |
| 108 |
if compress == "zstd": |
| 109 |
dctx = zstandard.ZstdDecompressor() |
| 110 |
decompressed_data_buffer = io.BytesIO() |
| 111 |
dctx.copy_stream(compressed_file, decompressed_data_buffer) |
| 112 |
else: |
| 113 |
decompressed_data_buffer = io.BytesIO( |
| 114 |
lz4.frame.decompress(compressed_file.read()) |
| 115 |
) |
| 116 |
decompressed_data_buffer.seek(0) |
| 117 |
tar_open_args = {"fileobj": decompressed_data_buffer} |
| 118 |
|
| 119 |
with tarfile.open(**tar_open_args, mode="r") as tar: |
| 120 |
tar.extraction_filter = getattr(tarfile, "data_filter", (lambda member, path: member)) |
| 121 |
tar.extractall(path=extract_path) |
| 122 |
|
| 123 |
found = False |
| 124 |
for item_path in extract_path.rglob("*"): |
| 125 |
found = True |
| 126 |
mtime_unix = item_path.stat().st_mtime |
| 127 |
assert mtime_unix == expected_timestamp, ( |
| 128 |
f"Expected mtime {expected_timestamp_str} (Unix: {expected_timestamp}) " |
| 129 |
f"but got {datetime.fromtimestamp(mtime_unix)} for file {item_path}" |
| 130 |
) |
| 131 |
assert found, "No files found in the extracted archive to check timestamps" |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.online |
| 135 |
def test_merger_bad_checksum(tmpdir, some_packages, dldir): |
| 136 |
outdir = Path(tmpdir / "merged") |
| 137 |
sam = SourceArchiveMerger(dldir / "sources", outdir) |
| 138 |
print(dldir / "sources") |
| 139 |
|
| 140 |
# Test 1: tamper dsc file |
| 141 |
pkg = some_packages[0] |
| 142 |
dsc_file = sam.locate_artifact(pkg, sam.dldir) |
| 143 |
# tamper dsc file by appending data |
| 144 |
with open(dsc_file, "a") as f: |
| 145 |
# note: this only tampers the checksum but not the signature |
| 146 |
# as we append data outside of the signed block. However, debsbom |
| 147 |
# anyways only relies on checksums, not signatures |
| 148 |
f.write("\n") |
| 149 |
|
| 150 |
# we don't get a corruption error as the dsc file is looked up by checksum |
| 151 |
# as there might be multiple .dsc on the snapshot mirror with the same name |
| 152 |
# and we only return the dsc that matches the checksum (if we have a checksum) |
| 153 |
with pytest.raises(DscFileNotFoundError): |
| 154 |
sam.merge(pkg) |
| 155 |
|
| 156 |
# Test 2: tamper binary artifacts |
| 157 |
pkg = some_packages[1] |
| 158 |
dsc_file = sam.locate_artifact(pkg, sam.dldir) |
| 159 |
with open(dsc_file, "r") as f: |
| 160 |
d = deb822.Dsc(f) |
| 161 |
filename = d.get("Checksums-Sha1")[0]["name"] |
| 162 |
suffix = Path(filename).suffix |
| 163 |
# replace with tampered version (correctly compressed tar, but empty) |
| 164 |
with tarfile.open(dsc_file.parent / filename, f"w:{suffix[1:]}") as tar: |
| 165 |
pass |
| 166 |
|
| 167 |
# as we tamper a binary, the checksum in the dsc file does not match the |
| 168 |
# one of the binary and we get the corrupted file error |
| 169 |
with pytest.raises(CorruptedFileError): |
| 170 |
sam.merge(pkg) |