| 1 |
# Copyright (C) 2025 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import io |
| 6 |
from pathlib import Path |
| 7 |
from debian.deb822 import PkgRelation |
| 8 |
from debian.debian_support import Version |
| 9 |
from packageurl import PackageURL |
| 10 |
import pytest |
| 11 |
|
| 12 |
from debsbom.dpkg import package as dpkg_package |
| 13 |
from debsbom.dpkg.package import ( |
| 14 |
ChecksumAlgo, |
| 15 |
Dependency, |
| 16 |
BinaryPackage, |
| 17 |
SourcePackage, |
| 18 |
Package, |
| 19 |
DpkgStatus, |
| 20 |
filter_binaries, |
| 21 |
) |
| 22 |
from debsbom.resolver.resolver import PackageResolver |
| 23 |
from debsbom.sbom import Reference, SBOMType |
| 24 |
|
| 25 |
|
| 26 |
def test_parse_dependency(): |
| 27 |
dependency_line = ( |
| 28 |
"binutils-common (= 2.40-2), libbinutils (= 2.40-2), binutils-x86-64-linux-gnu (= 2.40-2)" |
| 29 |
) |
| 30 |
deps = Dependency.parse_depends_line(dependency_line) |
| 31 |
assert deps == [ |
| 32 |
Dependency(name="binutils-common", version=("=", Version("2.40-2"))), |
| 33 |
Dependency(name="libbinutils", version=("=", Version("2.40-2"))), |
| 34 |
Dependency(name="binutils-x86-64-linux-gnu", version=("=", Version("2.40-2"))), |
| 35 |
] |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.parametrize("mode", ["file", "stream"]) |
| 39 |
def test_parse_minimal_status_file(mode): |
| 40 |
status_file = Path("tests/data/dpkg-status-minimal") |
| 41 |
if mode == "file": |
| 42 |
packages = list( |
| 43 |
BinaryPackage.inject_src_packages(BinaryPackage.parse_status_file(status_file)) |
| 44 |
) |
| 45 |
elif mode == "stream": |
| 46 |
with open(status_file, "r") as stream: |
| 47 |
packages = list(BinaryPackage.parse_pkglist_stream(stream)) |
| 48 |
|
| 49 |
bpkg = [p for p in packages if isinstance(p, BinaryPackage)][0] |
| 50 |
|
| 51 |
assert bpkg.name == "binutils" |
| 52 |
assert bpkg.section == "devel" |
| 53 |
assert bpkg.essential is False |
| 54 |
assert bpkg.maintainer == "Matthias Klose <[email protected]>" |
| 55 |
assert bpkg.source == Dependency(bpkg.name, None, ("=", bpkg.version), arch="source") |
| 56 |
assert bpkg.version == "2.40-2" |
| 57 |
assert bpkg.pre_depends == [ |
| 58 |
Dependency("init-system-helpers", None, (">=", "1.54~")), |
| 59 |
] |
| 60 |
assert bpkg.depends == [ |
| 61 |
Dependency("binutils-common", None, ("=", bpkg.version)), |
| 62 |
Dependency("libbinutils", None, ("=", bpkg.version)), |
| 63 |
Dependency("binutils-x86-64-linux-gnu", None, ("=", bpkg.version)), |
| 64 |
] |
| 65 |
assert ( |
| 66 |
bpkg.description == "GNU assembler, linker and binary utilities\n" |
| 67 |
"The programs in this package are used to assemble, link and manipulate binary and object files. " |
| 68 |
"They may be used in conjunction with a compiler and various libraries to build programs." |
| 69 |
) |
| 70 |
assert bpkg.homepage == "https://www.gnu.org/software/binutils/" |
| 71 |
assert bpkg.status == DpkgStatus.INSTALLED |
| 72 |
|
| 73 |
coreutils = [p for p in packages if isinstance(p, BinaryPackage)][1] |
| 74 |
assert coreutils.name == "coreutils" |
| 75 |
assert coreutils.essential is True |
| 76 |
|
| 77 |
spkg = packages[1] |
| 78 |
assert spkg.name == "binutils" |
| 79 |
assert spkg.version == bpkg.version |
| 80 |
assert spkg.maintainer == bpkg.maintainer |
| 81 |
|
| 82 |
|
| 83 |
def test_parse_status_file_as_utf8(c_ctype_locale, monkeypatch): |
| 84 |
monkeypatch.setattr(dpkg_package, "HAS_PYTHON_APT", False) |
| 85 |
|
| 86 |
packages = list(BinaryPackage.parse_status_file(Path("tests/data/utf8-status"))) |
| 87 |
package = next(package for package in packages if isinstance(package, BinaryPackage)) |
| 88 |
|
| 89 |
assert package.maintainer == "Şule Çiğdem Işıl ÖĞÜTÇÜ <[email protected]>" |
| 90 |
|
| 91 |
|
| 92 |
def test_parse_source_status_file(): |
| 93 |
packages = list( |
| 94 |
BinaryPackage.inject_src_packages( |
| 95 |
BinaryPackage.parse_status_file(Path("tests/data/dpkg-status-source")) |
| 96 |
) |
| 97 |
) |
| 98 |
bpkg = [p for p in packages if isinstance(p, BinaryPackage)][0] |
| 99 |
|
| 100 |
assert bpkg.name == "apt-utils" |
| 101 |
assert bpkg.section == "admin" |
| 102 |
assert bpkg.maintainer == "APT Development Team <[email protected]>" |
| 103 |
assert bpkg.source == Dependency("apt", None, ("=", bpkg.version), arch="source") |
| 104 |
assert bpkg.version == "2.6.1" |
| 105 |
assert bpkg.depends == [ |
| 106 |
Dependency("apt", None, ("=", bpkg.version)), |
| 107 |
Dependency("libapt-pkg6.0", None, (">=", bpkg.version)), |
| 108 |
Dependency("libc6", None, (">=", Version("2.34"))), |
| 109 |
Dependency("libdb5.3", None, None), |
| 110 |
Dependency("libgcc-s1", None, (">=", Version("3.0"))), |
| 111 |
Dependency("libstdc++6", None, (">=", Version("11"))), |
| 112 |
] |
| 113 |
assert ( |
| 114 |
bpkg.description == "package management related utility programs\n" |
| 115 |
"This package contains some less used commandline utilities related " |
| 116 |
"to package management with APT.\n\n" |
| 117 |
" * apt-extracttemplates is used by debconf to prompt for configuration\n" |
| 118 |
" questions before installation.\n" |
| 119 |
" * apt-ftparchive is used to create Packages and other index files\n" |
| 120 |
" needed to publish an archive of Debian packages\n" |
| 121 |
" * apt-sortpkgs is a Packages/Sources file normalizer." |
| 122 |
) |
| 123 |
assert bpkg.homepage is None |
| 124 |
|
| 125 |
spkg = [p for p in packages if isinstance(p, SourcePackage)][0] |
| 126 |
assert spkg.name == "apt" |
| 127 |
assert spkg.version == bpkg.version |
| 128 |
assert spkg.maintainer == bpkg.maintainer |
| 129 |
|
| 130 |
# check source package with version != binary version |
| 131 |
spkg = [p for p in packages if isinstance(p, SourcePackage)][1] |
| 132 |
assert spkg.name == "guestfs-tools" |
| 133 |
assert spkg.version == "1.52.3-1" |
| 134 |
|
| 135 |
|
| 136 |
def test_package_merge(): |
| 137 |
pkg_foo = BinaryPackage( |
| 138 |
name="foo", |
| 139 |
version="1.0-r1", |
| 140 |
depends=[Dependency("bar")], |
| 141 |
built_using=[Dependency("bar-src")], |
| 142 |
checksums={ChecksumAlgo.MD5SUM: "50a2fabfdd276f573ff97ace8b11c5f4"}, |
| 143 |
) |
| 144 |
pkg_foo_apt = BinaryPackage( |
| 145 |
name="foo", |
| 146 |
version="1.0-r1", |
| 147 |
depends=[Dependency("top")], |
| 148 |
built_using=[Dependency("bar-src"), Dependency("foo-src")], |
| 149 |
checksums={ChecksumAlgo.SHA1SUM: "34973274ccef6ab4dfaaf86599792fa9c3fe4689"}, |
| 150 |
description="description of foo", |
| 151 |
homepage="http://example.com/", |
| 152 |
manually_installed=True, |
| 153 |
) |
| 154 |
pkg_foo.merge_with(pkg_foo_apt) |
| 155 |
assert "bar" in [d.name for d in pkg_foo.depends] |
| 156 |
assert "top" in [d.name for d in pkg_foo.depends] |
| 157 |
assert "bar-src" in [d.name for d in pkg_foo.built_using] |
| 158 |
assert "foo-src" in [d.name for d in pkg_foo.built_using] |
| 159 |
assert ChecksumAlgo.MD5SUM in pkg_foo.checksums.keys() |
| 160 |
assert ChecksumAlgo.SHA1SUM in pkg_foo.checksums.keys() |
| 161 |
assert pkg_foo.description.startswith("desc") |
| 162 |
assert pkg_foo.manually_installed |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.parametrize( |
| 166 |
"data", |
| 167 |
[ |
| 168 |
["binutils-arm-none-eabi 2.40-2+18+b1 amd64", "binutils-bpf 2.40-2+1 amd64"], |
| 169 |
[ |
| 170 |
"pkg:deb/debian/[email protected]+18+b1?arch=amd64", |
| 171 |
"pkg:deb/debian/[email protected]+1?arch=amd64", |
| 172 |
], |
| 173 |
[ |
| 174 |
"binutils-arm-none-eabi|2.40-2+18+b1|binutils-arm-none-eabi:amd64|2.40-2+18+b1", |
| 175 |
"binutils-bpf|2.40-2+1|binutils-bpf:amd64|2.40-2+1", |
| 176 |
], |
| 177 |
], |
| 178 |
) |
| 179 |
def test_parse_pkgs_stream(data): |
| 180 |
stream = io.BytesIO("\n".join(data).encode()) |
| 181 |
pkgs_it = filter_binaries(Package.parse_pkglist_stream(stream)) |
| 182 |
|
| 183 |
pkg: BinaryPackage = next(pkgs_it) |
| 184 |
assert pkg.name == "binutils-arm-none-eabi" |
| 185 |
assert pkg.version.debian_revision == "2+18+b1" |
| 186 |
assert pkg.architecture == "amd64" |
| 187 |
|
| 188 |
pkg: BinaryPackage = next(pkgs_it) |
| 189 |
assert pkg.name == "binutils-bpf" |
| 190 |
assert pkg.version.upstream_version == "2.40" |
| 191 |
assert pkg.architecture == "amd64" |
| 192 |
|
| 193 |
|
| 194 |
def test_unique_depends(): |
| 195 |
deps = [ |
| 196 |
Dependency("libc6", None, (">>", Version("2.41")), arch="amd64"), |
| 197 |
Dependency("libc6", None, ("<<", Version("2.42")), arch="amd64"), |
| 198 |
Dependency("foo", None, ("", Version("2.41"))), |
| 199 |
] |
| 200 |
pkg = BinaryPackage("foo", "1.0", architecture="amd64", depends=deps) |
| 201 |
assert len(pkg.depends) == 3 |
| 202 |
assert len(pkg.unique_depends) == 2 |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.parametrize("sbom_type", [SBOMType.SPDX, SBOMType.CycloneDX]) |
| 206 |
def test_cross_arch_lookup(sbom_type): |
| 207 |
bar_dep = Dependency("bar", None, ("=", Version("1.0"))) |
| 208 |
foo = BinaryPackage("foo", "1.0", architecture="all", depends=[bar_dep]) |
| 209 |
bar = BinaryPackage("bar", "1.0", architecture="riscv64") |
| 210 |
|
| 211 |
refs = dict(map(lambda p: (Reference.make_from_pkg(p).as_str(sbom_type), p), [foo, bar])) |
| 212 |
ref_bar = Reference.lookup(foo, bar_dep, sbom_type, refs, native_arch="riscv64") |
| 213 |
assert "riscv64" in ref_bar |
| 214 |
ref_bar = Reference.lookup(foo, bar_dep, sbom_type, refs, native_arch="amd64") |
| 215 |
assert ref_bar is None |
| 216 |
|
| 217 |
|
| 218 |
def test_package_str_repr(): |
| 219 |
spkg = SourcePackage("foo", "1.0") |
| 220 |
assert str(spkg) == "[email protected]" |
| 221 |
|
| 222 |
bpkg = BinaryPackage("bar", "2.0") |
| 223 |
assert str(bpkg) == "[email protected]" |
| 224 |
|
| 225 |
|
| 226 |
def test_package_resolver_purl(): |
| 227 |
deb_purls_valid = [ |
| 228 |
"pkg:deb/debian/[email protected]?arch=source", |
| 229 |
"pkg:deb/debian/[email protected]?arch=binary", |
| 230 |
"pkg:deb/ubuntu/[email protected]?arch=source", |
| 231 |
] |
| 232 |
deb_purls_invalid = [ |
| 233 |
"pkg:deb/debian/[email protected]", |
| 234 |
"pkg:generic/[email protected]", |
| 235 |
] |
| 236 |
assert all([PackageResolver.is_debian_purl(PackageURL.from_string(p)) for p in deb_purls_valid]) |
| 237 |
assert not any( |
| 238 |
[PackageResolver.is_debian_purl(PackageURL.from_string(p)) for p in deb_purls_invalid] |
| 239 |
) |