|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | + |
| 42 | +package org.graalvm.buildtools.maven |
| 43 | + |
| 44 | +import com.fasterxml.jackson.databind.node.ObjectNode |
| 45 | +import org.graalvm.buildtools.maven.sbom.SBOMGenerator |
| 46 | +import org.graalvm.buildtools.utils.NativeImageUtils |
| 47 | +import spock.lang.Requires |
| 48 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 49 | + |
| 50 | +class SBOMFunctionalTest extends AbstractGraalVMMavenFunctionalTest { |
| 51 | + private static boolean EE() { |
| 52 | + NativeCompileNoForkMojo.isOracleGraalVM(null) |
| 53 | + } |
| 54 | + |
| 55 | + private static boolean CE() { |
| 56 | + !EE() |
| 57 | + } |
| 58 | + |
| 59 | + private static boolean jdkVersionSupportsAugmentedSBOM() { |
| 60 | + NativeImageUtils.getMajorJDKVersion(NativeCompileNoForkMojo.getVersionInformation(null)) >= SBOMGenerator.requiredNativeImageVersion |
| 61 | + } |
| 62 | + |
| 63 | + private static boolean unsupportedJDKVersion() { |
| 64 | + !jdkVersionSupportsAugmentedSBOM() |
| 65 | + } |
| 66 | + |
| 67 | + private static boolean supportedAugmentedSBOMVersion() { |
| 68 | + EE() && jdkVersionSupportsAugmentedSBOM() |
| 69 | + } |
| 70 | + |
| 71 | + @Requires({ supportedAugmentedSBOMVersion() }) |
| 72 | + def "sbom is created when buildArg '--enable-sbom=export,embed' is used"() { |
| 73 | + withSample 'java-application' |
| 74 | + |
| 75 | + when: |
| 76 | + /* The 'native-sbom' profile sets the '--enable-sbom' argument. */ |
| 77 | + mvn '-Pnative-sbom', '-DquickBuild', '-DskipTests', 'package', 'exec:exec@native' |
| 78 | + |
| 79 | + def sbom = file("target/example-app.sbom.json") |
| 80 | + |
| 81 | + then: |
| 82 | + buildSucceeded |
| 83 | + outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\) and exported as JSON \\(see build artifacts\\)\\." |
| 84 | + outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)" |
| 85 | + validateSbom sbom |
| 86 | + !file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists() |
| 87 | + outputContains "Hello, native!" |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * If user sets {@link NativeCompileNoForkMojo#AUGMENTED_SBOM_PARAM_NAME} to true then an SBOM should be generated |
| 92 | + * with default SBOM arguments even if user did not explicitly specify '--enable-sbom' as a buildArg. |
| 93 | + */ |
| 94 | + @Requires({ supportedAugmentedSBOMVersion() }) |
| 95 | + def "sbom is created when only the augmented sbom parameter is used (but not the '--enable-sbom' buildArg)"() { |
| 96 | + withSample 'java-application' |
| 97 | +
|
| 98 | + when: |
| 99 | + mvn '-Pnative-augmentedSBOM-only', '-DquickBuild', '-DskipTests', 'package', 'exec:exec@native' |
| 100 | +
|
| 101 | + def sbom = file("target/example-app.sbom.json") |
| 102 | +
|
| 103 | + then: |
| 104 | + buildSucceeded |
| 105 | + outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\)." |
| 106 | + outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)" |
| 107 | + validateSbom sbom |
| 108 | + !file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists() |
| 109 | + outputContains "Hello, native!" |
| 110 | + } |
| 111 | +
|
| 112 | + @Requires({ CE() }) |
| 113 | + def "error is thrown when augmented sbom parameter is used with CE"() { |
| 114 | + withSample 'java-application' |
| 115 | +
|
| 116 | + when: |
| 117 | + mvn '-Pnative-augmentedSBOM-only', '-DquickBuild', '-DskipTests', 'package' |
| 118 | +
|
| 119 | + then: |
| 120 | + buildFailed |
| 121 | + } |
| 122 | +
|
| 123 | + @Requires({ EE() && unsupportedJDKVersion() }) |
| 124 | + def "error is thrown when augmented sbom parameter is used with EE but not with an unsupported JDK version"() { |
| 125 | + withSample 'java-application' |
| 126 | +
|
| 127 | + when: |
| 128 | + mvn '-Pnative-augmentedSBOM-only', '-DquickBuild', '-DskipTests', 'package' |
| 129 | +
|
| 130 | + then: |
| 131 | + buildFailed |
| 132 | + } |
| 133 | +
|
| 134 | + /** |
| 135 | + * Validates the SBOM produced from 'java-application'. |
| 136 | + * @param sbom path to the SBOM. |
| 137 | + * @return true if validation succeeded. |
| 138 | + */ |
| 139 | + private static boolean validateSbom(File sbom) { |
| 140 | + try { |
| 141 | + if (!sbom.exists()) { |
| 142 | + println "SBOM not found: ${sbom}" |
| 143 | + return false |
| 144 | + } |
| 145 | +
|
| 146 | + def mapper = new ObjectMapper() |
| 147 | + def rootNode = mapper.readTree(sbom) |
| 148 | +
|
| 149 | + // Check root fields |
| 150 | + assert rootNode.has('bomFormat') |
| 151 | + assert rootNode.get('bomFormat').asText() == 'CycloneDX' |
| 152 | + assert rootNode.has('specVersion') |
| 153 | + assert rootNode.has('serialNumber') |
| 154 | + assert rootNode.has('version') |
| 155 | + assert rootNode.has('metadata') |
| 156 | + assert rootNode.has('components') |
| 157 | + assert rootNode.has('dependencies') |
| 158 | +
|
| 159 | + // Check metadata/component |
| 160 | + def metadataComponent = rootNode.path('metadata').path('component') |
| 161 | + assert metadataComponent.has('group') |
| 162 | + assert metadataComponent.get('group').asText() == 'org.graalvm.buildtools.examples' |
| 163 | + assert metadataComponent.has('name') |
| 164 | + assert metadataComponent.get('name').asText() == 'maven' |
| 165 | +
|
| 166 | + // Check that components and dependencies are non-empty |
| 167 | + assert !rootNode.get('components').isEmpty() |
| 168 | + assert !rootNode.get('dependencies').isEmpty() |
| 169 | +
|
| 170 | + // Check that the main component has no dependencies |
| 171 | + def mainComponentId = metadataComponent.get('bom-ref').asText() |
| 172 | + def mainComponentDependency = rootNode.get('dependencies').find { it.get('ref').asText() == mainComponentId } as ObjectNode |
| 173 | + assert mainComponentDependency.get('dependsOn').isEmpty() |
| 174 | +
|
| 175 | + // Check that the main component is not found in "components" |
| 176 | + assert !rootNode.get('components').any { it.get('bom-ref').asText() == mainComponentId } |
| 177 | +
|
| 178 | + return true |
| 179 | + } catch (AssertionError | Exception e) { |
| 180 | + println "SBOM validation failed: ${e.message}" |
| 181 | + return false |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments