@@ -9,6 +9,7 @@ import hashlib
from datetime import datetime, timezone
from oeqa.selftest.case import OESelftestTestCase
from oeqa.utils.commands import bitbake, get_bb_var, get_bb_vars
+import oe.purl
import oe.spdx30
@@ -543,3 +544,124 @@ class SPDX30Check(SPDX3CheckBase, OESelftestTestCase):
# Document should be fully linked
self.check_objset_missing_ids(objset)
+
+ def test_crate_package_identity(self):
+ """Test that crate downloads carry name, version and a cargo PURL."""
+ objset = self.check_recipe_spdx(
+ "python3-bcrypt",
+ "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json",
+ )
+
+ prefix = "https://static.crates.io/crates/"
+ crates = 0
+ for pkg in objset.foreach_type(oe.spdx30.software_Package):
+ location = pkg.software_downloadLocation or ""
+ if not location.startswith(prefix):
+ continue
+ name, version, _ = location[len(prefix):].split("/")
+ self.assertEqual(pkg.name, name)
+ self.assertEqual(pkg.software_packageVersion, version)
+ self.assertEqual(
+ pkg.software_packageUrl,
+ f"pkg:cargo/{oe.purl.quote(name)}@{oe.purl.quote(version)}",
+ )
+ crates += 1
+
+ self.assertGreater(crates, 0, "no crate downloads found")
+
+ def test_crate_dependency_scope(self):
+ """Test that crate downloads get a lifecycle scope for the target.
+
+ Every crate download is either the target of one scoped dependsOn
+ relationship or annotated as not built. python3-bcrypt fetches crates
+ for other platforms (WebAssembly, UEFI), which are never built for a
+ Linux target. blowfish is the cipher of bcrypt, and syn only runs at
+ build time in proc-macros, so these names do not change on upgrades.
+ """
+ objset = self.check_recipe_spdx(
+ "python3-bcrypt",
+ "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json",
+ )
+
+ crates = {
+ pkg._id: pkg.name
+ for pkg in objset.foreach_type(oe.spdx30.software_Package)
+ if (pkg.software_packageUrl or "").startswith("pkg:cargo/")
+ and (pkg.software_downloadLocation or "").startswith("https://static.crates.io/")
+ }
+
+ scope_of = {}
+ for rel in objset.foreach_type(oe.spdx30.LifecycleScopedRelationship):
+ if rel.relationshipType != oe.spdx30.RelationshipType.dependsOn:
+ continue
+ for to in rel.to:
+ to_id = to if isinstance(to, str) else to._id
+ if to_id in crates:
+ self.assertNotIn(crates[to_id], scope_of)
+ scope_of[crates[to_id]] = rel.scope
+
+ not_built = set()
+ for annotation in objset.foreach_type(oe.spdx30.Annotation):
+ subject = annotation.subject
+ subject_id = subject if isinstance(subject, str) else subject._id
+ if subject_id in crates and "not built" in annotation.statement:
+ not_built.add(crates[subject_id])
+
+ self.assertEqual(set(scope_of) | not_built, set(crates.values()))
+ self.assertFalse(set(scope_of) & not_built)
+ self.assertTrue(not_built, "no crate for another platform found")
+ self.assertEqual(scope_of["syn"], oe.spdx30.LifecycleScopeType.build)
+ self.assertEqual(scope_of["blowfish"], oe.spdx30.LifecycleScopeType.runtime)
+
+ def test_crate_purls_in_output_package(self):
+ """Test that the output package inherits the PURLs of its crates.
+
+ The package gets the PURLs of the crates with the runtime scope in
+ the build SPDX, and no other crate PURLs. syn only runs at build
+ time in proc-macros, so it is not in the binary.
+ """
+ objset = self.check_recipe_spdx(
+ "python3-bcrypt",
+ "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/packages/package-python3-bcrypt.spdx.json",
+ )
+
+ package = next(
+ pkg
+ for pkg in objset.foreach_type(oe.spdx30.software_Package)
+ if pkg.name == "python3-bcrypt"
+ )
+ purls = {
+ e.identifier
+ for e in package.externalIdentifier
+ if e.externalIdentifierType == oe.spdx30.ExternalIdentifierType.packageUrl
+ }
+ crates = {p[len("pkg:cargo/"):].split("@")[0] for p in purls if p.startswith("pkg:cargo/")}
+
+ # The package's own PURL stays first
+ own_purls = get_bb_var("SPDX_PACKAGE_URLS", "python3-bcrypt").split()
+ self.assertEqual(package.software_packageUrl, own_purls[0])
+ self.assertTrue(set(own_purls) <= purls)
+ self.assertIn("blowfish", crates)
+ self.assertNotIn("syn", crates)
+
+ spdx_vars = get_bb_vars(["DEPLOY_DIR_SPDX", "SSTATE_PKGARCH"], "python3-bcrypt")
+ build_objset = self.check_spdx_file(
+ "{DEPLOY_DIR_SPDX}/{SSTATE_PKGARCH}/builds/build-python3-bcrypt.spdx.json".format(**spdx_vars)
+ )
+ downloads = {
+ pkg._id: pkg.software_packageUrl
+ for pkg in build_objset.foreach_type(oe.spdx30.software_Package)
+ if (pkg.software_packageUrl or "").startswith("pkg:cargo/")
+ }
+ runtime = set()
+ for rel in build_objset.foreach_type(oe.spdx30.LifecycleScopedRelationship):
+ if rel.scope != oe.spdx30.LifecycleScopeType.runtime:
+ continue
+ for to in rel.to:
+ to_id = to if isinstance(to, str) else to._id
+ if to_id in downloads:
+ runtime.add(downloads[to_id])
+
+ self.assertTrue(runtime, "no crate with the runtime scope found")
+ cargo_purls = {p for p in purls if p.startswith("pkg:cargo/")}
+ self.assertEqual(cargo_purls - set(own_purls), runtime)