| 1 |
# Copyright (C) 2025 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from debsbom.snapshot.client import ( |
| 8 |
BinaryPackage, |
| 9 |
NotFoundOnSnapshotError, |
| 10 |
Package, |
| 11 |
SnapshotDataLake, |
| 12 |
SnapshotDataLakeError, |
| 13 |
SnapshotRemoteFile, |
| 14 |
SourcePackage, |
| 15 |
) |
| 16 |
from debsbom.util.checksum import ChecksumAlgo |
| 17 |
|
| 18 |
|
| 19 |
def test_fromfileinfo_sanitization(): |
| 20 |
sdl = SnapshotDataLake() |
| 21 |
hash = "1f3a43c181b81e3578d609dc0931ff147623eb38" |
| 22 |
fileinfo = { |
| 23 |
"name": "pytest_8.4.2-1.dsc", |
| 24 |
"size": "2757", |
| 25 |
"archive_name": "debian", |
| 26 |
"path": "/pool/main/p/pytest", |
| 27 |
"first_seen": "2025-09-07T19:03:19", |
| 28 |
} |
| 29 |
|
| 30 |
rf = SnapshotRemoteFile.fromfileinfo(sdl, hash, fileinfo) |
| 31 |
assert rf.filename == "pytest_8.4.2-1.dsc" |
| 32 |
assert rf.checksums[ChecksumAlgo.SHA1SUM] == hash |
| 33 |
assert rf.size == 2757 |
| 34 |
assert rf.archive_name == "debian" |
| 35 |
assert rf.path == "/pool/main/p/pytest" |
| 36 |
assert rf.downloadurl == f"{sdl.url}/file/{hash}/pytest_8.4.2-1.dsc" |
| 37 |
|
| 38 |
# parent components that remain below the root are allowed |
| 39 |
fileinfo_with_parent = dict(fileinfo, path="/pool/main/../p/pytest") |
| 40 |
assert ( |
| 41 |
SnapshotRemoteFile.fromfileinfo(sdl, hash, fileinfo_with_parent).path |
| 42 |
== "/pool/main/../p/pytest" |
| 43 |
) |
| 44 |
|
| 45 |
# a non-hex/short hash is rejected |
| 46 |
with pytest.raises(ValueError): |
| 47 |
SnapshotRemoteFile.fromfileinfo(sdl, "nothex", fileinfo) |
| 48 |
|
| 49 |
# each malicious field must be rejected |
| 50 |
for field, value in [ |
| 51 |
("name", "../../../etc/foo"), |
| 52 |
("archive_name", "../debian"), |
| 53 |
("path", "pool/main/p/pytest"), # not absolute |
| 54 |
("path", "/pool/../../etc"), # traverses above root |
| 55 |
]: |
| 56 |
bad = dict(fileinfo) |
| 57 |
bad[field] = value |
| 58 |
with pytest.raises(ValueError): |
| 59 |
SnapshotRemoteFile.fromfileinfo(sdl, hash, bad) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.online |
| 63 |
def test_list_packages(sdl): |
| 64 |
pkgs = sdl.packages() |
| 65 |
assert any(filter(lambda p: p.name == "sed", pkgs)) |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.online |
| 69 |
def test_fileinfo(sdl): |
| 70 |
pkg_hash = "1f3a43c181b81e3578d609dc0931ff147623eb38" |
| 71 |
assert any(filter(lambda x: x.filename == "pytest_8.4.2-1.dsc", sdl.fileinfo(pkg_hash))) |
| 72 |
|
| 73 |
with pytest.raises(SnapshotDataLakeError): |
| 74 |
hash_invalid = "foobar" |
| 75 |
next(sdl.fileinfo(hash_invalid)) |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.online |
| 79 |
def test_binary_package(sdl): |
| 80 |
# no relation to src package |
| 81 |
pkg_hash = "c9e30e325d77dca96d85d094037ae2f7eac919ff" |
| 82 |
pkg_nosrc = BinaryPackage(sdl, "python3-pytest", "8.4.2-1", None, None) |
| 83 |
files = list(pkg_nosrc.files()) |
| 84 |
assert files[0].checksums.get(ChecksumAlgo.SHA1SUM) == pkg_hash |
| 85 |
|
| 86 |
# with relation to src package |
| 87 |
pkg = BinaryPackage(sdl, pkg_nosrc.binname, pkg_nosrc.binversion, "pytest", "8.4.2-1") |
| 88 |
files = list(pkg.files()) |
| 89 |
assert files[0].checksums.get(ChecksumAlgo.SHA1SUM) == pkg_hash |
| 90 |
|
| 91 |
pkg = BinaryPackage(sdl, "sed", "4.9-2", "sed", "4.9-2") |
| 92 |
assert all(map(lambda f: f.architecture == "riscv64", pkg.files(arch="riscv64"))) |
| 93 |
|
| 94 |
with pytest.raises(NotFoundOnSnapshotError): |
| 95 |
next(BinaryPackage(sdl, "python3", "8.4.2-1", None, None).files()) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.online |
| 99 |
def test_source_package(sdl): |
| 100 |
pkg = SourcePackage(sdl, "pytest", "8.4.2-1") |
| 101 |
dsc_hash = "1f3a43c181b81e3578d609dc0931ff147623eb38" |
| 102 |
src_files = list(pkg.srcfiles()) |
| 103 |
assert any(filter(lambda s: s.checksums.get(ChecksumAlgo.SHA1SUM) == dsc_hash, src_files)) |
| 104 |
|
| 105 |
binpkgs = list(pkg.binpackages()) |
| 106 |
assert any(filter(lambda b: b.binname == "python3-pytest", binpkgs)) |
| 107 |
|
| 108 |
with pytest.raises(NotFoundOnSnapshotError): |
| 109 |
# the name corresponds to a binary package |
| 110 |
next(SourcePackage(sdl, "python3-pytest", "8.4.2-1").srcfiles()) |
| 111 |
|
| 112 |
with pytest.raises(SnapshotDataLakeError): |
| 113 |
next(SourcePackage(sdl, "python3-pytest", "8.4.2-1").binpackages()) |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.online |
| 117 |
def test_generic_package(sdl): |
| 118 |
""" |
| 119 |
list all versions of a source package |
| 120 |
""" |
| 121 |
pkg = Package(sdl, "pytest") |
| 122 |
assert any(filter(lambda p: p.version == "8.4.2-1", pkg.versions())) |
| 123 |
|
| 124 |
with pytest.raises(NotFoundOnSnapshotError): |
| 125 |
next(Package(sdl, "python3-pytest").versions()) |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.online |
| 129 |
def test_srcpkg_limit_archive(sdl): |
| 130 |
""" |
| 131 |
list all files related to a source package, but limit to a specific archive |
| 132 |
""" |
| 133 |
pkg = SourcePackage(sdl, "sratom", "0.6.14-1") |
| 134 |
files = list(pkg.srcfiles(archive="debian")) |
| 135 |
# debian.tar.xz, .dsc, orig.tar.xz, .asc |
| 136 |
assert len(files) == 4 |