| 1 |
# Copyright (C) 2026 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import io |
| 6 |
import jsonschema |
| 7 |
from packageurl import PackageURL |
| 8 |
import json |
| 9 |
from pathlib import Path |
| 10 |
import pytest |
| 11 |
|
| 12 |
from debsbom.graph.walker import GraphWalker, PackageRepr |
| 13 |
from debsbom.commands.tracepath import TracePathCmd, PathOutputFormat |
| 14 |
from debsbom.generate import SBOMType |
| 15 |
from debsbom.schema import tracepath as schema_tracepath |
| 16 |
|
| 17 |
from test_generation import sbom_generator |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.parametrize("sbom_type", list(SBOMType)) |
| 21 |
def test_tracepath_path(tmpdir, sbom_generator, sbom_type): |
| 22 |
match sbom_type: |
| 23 |
case SBOMType.SPDX: |
| 24 |
_spdx_tools = pytest.importorskip("spdx_tools") |
| 25 |
case SBOMType.CycloneDX: |
| 26 |
_cyclonedx = pytest.importorskip("cyclonedx") |
| 27 |
case _: |
| 28 |
assert False, "unreachable" |
| 29 |
|
| 30 |
dbom = sbom_generator("tests/root/dependency", sbom_types=[sbom_type]) |
| 31 |
outdir = Path(tmpdir) |
| 32 |
dbom.generate(str(outdir / "sbom"), validate=False) |
| 33 |
|
| 34 |
walker = GraphWalker.create(outdir / f"sbom.{sbom_type}.json") |
| 35 |
src = PackageURL.from_string("pkg:deb/debian/[email protected]%2Bdeb12u1?arch=amd64") |
| 36 |
path = walker.shortest(src) |
| 37 |
assert len(path) == 2 |
| 38 |
assert path[0].name == "libgcc-s1" |
| 39 |
assert path[1].name == "pytest-distro" |
| 40 |
|
| 41 |
paths = list(walker.all_shortest(src)) |
| 42 |
assert len(paths) == 1 |
| 43 |
assert len(paths[0]) == 2 |
| 44 |
|
| 45 |
paths = list(walker.all_simple(src)) |
| 46 |
assert len(paths) == 2 |
| 47 |
assert any(len(p) == 2 for p in paths) |
| 48 |
assert any(len(p) == 3 for p in paths) |
| 49 |
|
| 50 |
|
| 51 |
def test_tracepath_schema(): |
| 52 |
path = [ |
| 53 |
PackageRepr(name="test-src", ref="REF-test-src"), |
| 54 |
PackageRepr(name="test-middle", ref="REF-test-middle"), |
| 55 |
PackageRepr(name="test-root", ref="REF-root"), |
| 56 |
] |
| 57 |
|
| 58 |
raw = next(TracePathCmd.iter_component_path(path, PathOutputFormat.JSON)) |
| 59 |
data = json.loads(raw) |
| 60 |
assert len(data) == 3 |
| 61 |
jsonschema.validate(data, schema=schema_tracepath) |
| 62 |
|
| 63 |
|
| 64 |
def test_tracepath_dot(): |
| 65 |
paths = [ |
| 66 |
[ |
| 67 |
PackageRepr(name="pkg-a", ref="REF-a", maintainer="maint1"), |
| 68 |
PackageRepr(name="pkg-b", ref="REF-b", maintainer="maint2"), |
| 69 |
], |
| 70 |
[ |
| 71 |
PackageRepr(name="pkg-a", ref="REF-a", maintainer="maint1"), |
| 72 |
PackageRepr(name="pkg-c", ref="REF-c", maintainer="maint3"), |
| 73 |
], |
| 74 |
] |
| 75 |
|
| 76 |
buffer = io.StringIO() |
| 77 |
TracePathCmd.dump_as_dot_graph(paths, buffer) |
| 78 |
output = buffer.getvalue() |
| 79 |
|
| 80 |
# Check that output contains digraph declaration |
| 81 |
assert "digraph" in output |
| 82 |
assert "{" in output |
| 83 |
assert "}" in output |
| 84 |
|
| 85 |
# Check that nodes are present |
| 86 |
assert "pkg-a" in output |
| 87 |
assert "pkg-b" in output |
| 88 |
assert "pkg-c" in output |
| 89 |
|
| 90 |
# Check that edges are present (arrows) |
| 91 |
assert "->" in output |