Skip to content

Commit ad48173

Browse files
authored
Merge pull request #262 from ruromero/spdx-2.2
feat: support spdx 2.2
2 parents 2b4d76e + fa52b65 commit ad48173

File tree

5 files changed

+9715
-7
lines changed

5 files changed

+9715
-7
lines changed

src/main/java/com/redhat/exhort/integration/backend/sbom/spdx/SpdxProcessingException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public SpdxProcessingException(String msg) {
2424
super(msg);
2525
}
2626

27+
public SpdxProcessingException(Throwable e) {
28+
super(e);
29+
}
30+
2731
public SpdxProcessingException(String msg, Throwable e) {
2832
super(msg, e);
2933
}

src/main/java/com/redhat/exhort/integration/backend/sbom/spdx/SpdxWrapper.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.spdx.jacksonstore.MultiFormatStore;
2828
import org.spdx.library.InvalidSPDXAnalysisException;
2929
import org.spdx.library.SpdxConstants;
30-
import org.spdx.library.Version;
3130
import org.spdx.library.model.ExternalRef;
3231
import org.spdx.library.model.SpdxDocument;
3332
import org.spdx.library.model.SpdxPackage;
@@ -37,7 +36,6 @@
3736

3837
public class SpdxWrapper {
3938

40-
private static final String SUPPORTED_VERSION = Version.TWO_POINT_THREE_VERSION;
4139
private static final String PURL_REFERENCE = "http://spdx.org/rdf/references/purl";
4240

4341
private MultiFormatStore inputStore;
@@ -48,12 +46,17 @@ public class SpdxWrapper {
4846
public SpdxWrapper(MultiFormatStore inputStore, InputStream input)
4947
throws InvalidSPDXAnalysisException, IOException {
5048
this.inputStore = inputStore;
51-
this.inputStore.deSerialize(input, false);
52-
this.uri = inputStore.getDocumentUris().get(0);
53-
this.doc = new SpdxDocument(inputStore, uri, null, false);
54-
var verify = doc.verify(SUPPORTED_VERSION);
49+
try {
50+
this.inputStore.deSerialize(input, false);
51+
this.uri = inputStore.getDocumentUris().get(0);
52+
this.doc = new SpdxDocument(inputStore, uri, null, false);
53+
} catch (InvalidSPDXAnalysisException e) {
54+
throw new SpdxProcessingException(e);
55+
}
56+
var version = doc.getSpecVersion();
57+
var verify = doc.verify(version);
5558
if (!verify.isEmpty()) {
56-
throw new SpdxValidationException(SUPPORTED_VERSION, verify);
59+
throw new SpdxValidationException(version, verify);
5760
}
5861
this.packages = parsePackages();
5962
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.redhat.exhort.integration.backend.sbom;
20+
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.io.IOException;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
import org.spdx.jacksonstore.MultiFormatStore;
30+
import org.spdx.jacksonstore.MultiFormatStore.Format;
31+
import org.spdx.library.InvalidSPDXAnalysisException;
32+
import org.spdx.library.Version;
33+
import org.spdx.storage.simple.InMemSpdxStore;
34+
35+
import com.redhat.exhort.integration.backend.sbom.spdx.SpdxProcessingException;
36+
import com.redhat.exhort.integration.backend.sbom.spdx.SpdxWrapper;
37+
38+
public class SpdxWrapperTest {
39+
40+
private static final MultiFormatStore inputStore =
41+
new MultiFormatStore(new InMemSpdxStore(), Format.JSON_PRETTY);
42+
43+
@ParameterizedTest
44+
@ValueSource(strings = {Version.TWO_POINT_THREE_VERSION, Version.TWO_POINT_TWO_VERSION})
45+
void testVersions(String version) throws InvalidSPDXAnalysisException, IOException {
46+
var wrapper =
47+
new SpdxWrapper(
48+
inputStore,
49+
this.getClass()
50+
.getClassLoader()
51+
.getResourceAsStream("spdx/versions/" + version + ".json"));
52+
assertNotNull(wrapper);
53+
assertNotNull(wrapper.getPackages());
54+
}
55+
56+
@Test
57+
void testInvalidDocument() {
58+
assertThrows(
59+
SpdxProcessingException.class,
60+
() ->
61+
new SpdxWrapper(
62+
inputStore,
63+
this.getClass().getClassLoader().getResourceAsStream("cyclonedx/empty-sbom.json")));
64+
}
65+
}

0 commit comments

Comments
 (0)