|
| 1 | +# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. |
| 3 | + |
| 4 | +"""Tests for types and utilities for Maven artifacts.""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from packageurl import PackageURL |
| 8 | + |
| 9 | +from macaron.artifact.maven import MavenArtifact, MavenArtifactType |
| 10 | +# , MavenSubjectPURLMatcher |
| 11 | +from macaron.slsa_analyzer.provenance.intoto import InTotoPayload, validate_intoto_payload |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("purl_str", "maven_artifact"), |
| 16 | + [ |
| 17 | + pytest.param( |
| 18 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=jar", |
| 19 | + MavenArtifact( |
| 20 | + group_id="com.fasterxml.jackson", |
| 21 | + artifact_id="jackson-annotations", |
| 22 | + version="2.9.9", |
| 23 | + artifact_type=MavenArtifactType.JAR, |
| 24 | + ), |
| 25 | + id="purl for jar artifact", |
| 26 | + ), |
| 27 | + pytest.param( |
| 28 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=javadoc", |
| 29 | + MavenArtifact( |
| 30 | + group_id="com.fasterxml.jackson", |
| 31 | + artifact_id="jackson-annotations", |
| 32 | + version="2.9.9", |
| 33 | + artifact_type=MavenArtifactType.JAVADOC, |
| 34 | + ), |
| 35 | + id="purl for javadoc artifact", |
| 36 | + ), |
| 37 | + pytest.param( |
| 38 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=sources", |
| 39 | + MavenArtifact( |
| 40 | + group_id="com.fasterxml.jackson", |
| 41 | + artifact_id="jackson-annotations", |
| 42 | + version="2.9.9", |
| 43 | + artifact_type=MavenArtifactType.JAVA_SOURCE, |
| 44 | + ), |
| 45 | + id="purl for java source artifact", |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=pom", |
| 49 | + MavenArtifact( |
| 50 | + group_id="com.fasterxml.jackson", |
| 51 | + artifact_id="jackson-annotations", |
| 52 | + version="2.9.9", |
| 53 | + artifact_type=MavenArtifactType.POM, |
| 54 | + ), |
| 55 | + id="purl for pom artifact", |
| 56 | + ), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_maven_artifact_from_purl(purl_str: str, maven_artifact: MavenArtifact) -> None: |
| 60 | + """Test creating a ``MavenArtifact`` object given a PackageURL.""" |
| 61 | + assert MavenArtifact.from_package_url(PackageURL.from_string(purl_str)) == maven_artifact |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + ("params", "maven_artifact"), |
| 66 | + [ |
| 67 | + pytest.param( |
| 68 | + { |
| 69 | + "artifact_name": "jackson-annotations-2.9.9.jar", |
| 70 | + "group_id": "com.fasterxml.jackson", |
| 71 | + "version": "2.9.9", |
| 72 | + }, |
| 73 | + MavenArtifact( |
| 74 | + group_id="com.fasterxml.jackson", |
| 75 | + artifact_id="jackson-annotations", |
| 76 | + version="2.9.9", |
| 77 | + artifact_type=MavenArtifactType.JAR, |
| 78 | + ), |
| 79 | + id="jar artifact", |
| 80 | + ), |
| 81 | + pytest.param( |
| 82 | + { |
| 83 | + "artifact_name": "jackson-annotations-2.9.9-javadoc.jar", |
| 84 | + "group_id": "com.fasterxml.jackson", |
| 85 | + "version": "2.9.9", |
| 86 | + }, |
| 87 | + MavenArtifact( |
| 88 | + group_id="com.fasterxml.jackson", |
| 89 | + artifact_id="jackson-annotations", |
| 90 | + version="2.9.9", |
| 91 | + artifact_type=MavenArtifactType.JAVADOC, |
| 92 | + ), |
| 93 | + id="javadoc artifact", |
| 94 | + ), |
| 95 | + pytest.param( |
| 96 | + { |
| 97 | + "artifact_name": "jackson-annotations-2.9.9-sources.jar", |
| 98 | + "group_id": "com.fasterxml.jackson", |
| 99 | + "version": "2.9.9", |
| 100 | + }, |
| 101 | + MavenArtifact( |
| 102 | + group_id="com.fasterxml.jackson", |
| 103 | + artifact_id="jackson-annotations", |
| 104 | + version="2.9.9", |
| 105 | + artifact_type=MavenArtifactType.JAVA_SOURCE, |
| 106 | + ), |
| 107 | + id="java-source artifact", |
| 108 | + ), |
| 109 | + pytest.param( |
| 110 | + { |
| 111 | + "artifact_name": "jackson-annotations-2.9.9.pom", |
| 112 | + "group_id": "com.fasterxml.jackson", |
| 113 | + "version": "2.9.9", |
| 114 | + }, |
| 115 | + MavenArtifact( |
| 116 | + group_id="com.fasterxml.jackson", |
| 117 | + artifact_id="jackson-annotations", |
| 118 | + version="2.9.9", |
| 119 | + artifact_type=MavenArtifactType.POM, |
| 120 | + ), |
| 121 | + id="pom artifact", |
| 122 | + ), |
| 123 | + ], |
| 124 | +) |
| 125 | +def test_maven_artifact_from_artifact_name(params: dict, maven_artifact: MavenArtifact) -> None: |
| 126 | + """Test creating a ``MavenArtifact`` object given an artifact name.""" |
| 127 | + assert MavenArtifact.from_artifact_name(**params) == maven_artifact |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + ("purl_str", "subject_index"), |
| 132 | + [ |
| 133 | + pytest.param( |
| 134 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=jar", |
| 135 | + 0, |
| 136 | + id="purl for jar artifact", |
| 137 | + ), |
| 138 | + pytest.param( |
| 139 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=javadoc", |
| 140 | + 1, |
| 141 | + id="purl for javadoc artifact", |
| 142 | + ), |
| 143 | + pytest.param( |
| 144 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=sources", |
| 145 | + 2, |
| 146 | + id="purl for java source artifact", |
| 147 | + ), |
| 148 | + pytest.param( |
| 149 | + "pkg:maven/com.fasterxml.jackson/[email protected]?type=pom", |
| 150 | + 3, |
| 151 | + id="purl for pom artifact", |
| 152 | + ), |
| 153 | + ], |
| 154 | +) |
| 155 | +def test_to_maven_artifact_subject( |
| 156 | + purl_str: str, |
| 157 | + subject_index: int, |
| 158 | +) -> None: |
| 159 | + """Test constructing a ``MavenArtifact`` object from a given artifact name.""" |
| 160 | + purl = PackageURL.from_string(purl_str) |
| 161 | + provenance_payload: InTotoPayload = validate_intoto_payload( |
| 162 | + { |
| 163 | + "_type": "https://in-toto.io/Statement/v0.1", |
| 164 | + "subject": [ |
| 165 | + { |
| 166 | + "name": "https://witness.dev/attestations/product/v0.1/file:target/jackson-annotations-2.9.9.jar", |
| 167 | + "digest": { |
| 168 | + "sha256": "6f97fe2094bd50435d6fbb7a2f6c2638fe44e6af17cfff98ce111d0abfffe17e", |
| 169 | + }, |
| 170 | + }, |
| 171 | + { |
| 172 | + "name": "https://witness.dev/attestations/product/v0.1/file:target/jackson-annotations-2.9.9-javadoc.jar", |
| 173 | + "digest": { |
| 174 | + "sha256": "6f97fe2094bd50435d6fbb7a2f6c2638fe44e6af17cfff98ce111d0abfffe17e", |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + "name": "https://witness.dev/attestations/product/v0.1/file:target/jackson-annotations-2.9.9-sources.jar", |
| 179 | + "digest": { |
| 180 | + "sha256": "6f97fe2094bd50435d6fbb7a2f6c2638fe44e6af17cfff98ce111d0abfffe17e", |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + "name": "https://witness.dev/attestations/product/v0.1/file:target/jackson-annotations-2.9.9.pom", |
| 185 | + "digest": { |
| 186 | + "sha256": "6f97fe2094bd50435d6fbb7a2f6c2638fe44e6af17cfff98ce111d0abfffe17e", |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + "name": "https://witness.dev/attestations/product/v0.1/file:target/foobar.txt", |
| 191 | + "digest": { |
| 192 | + "sha256": "6f97fe2094bd50435d6fbb7a2f6c2638fe44e6af17cfff98ce111d0abfffe17e", |
| 193 | + }, |
| 194 | + }, |
| 195 | + ], |
| 196 | + "predicateType": "https://witness.testifysec.com/attestation-collection/v0.1", |
| 197 | + } |
| 198 | + ) |
| 199 | + assert ( |
| 200 | + MavenSubjectPURLMatcher.get_subject_in_provenance_matching_purl( |
| 201 | + provenance_payload=provenance_payload, |
| 202 | + purl=purl, |
| 203 | + ) |
| 204 | + == provenance_payload.statement["subject"][subject_index] |
| 205 | + ) |
0 commit comments