diff options
3 files changed, 367 insertions, 17 deletions
diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java index afa0e2d..59131de 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java @@ -24,6 +24,8 @@ final class ManifestConsts { static final String SOURCE_TAG_SECTION = "source"; static final String ALGORITHM = "algorithm"; static final String HASH = "hash"; + static final String SIGNATURE = "signature"; + static final String CERTIFICATE = "certificate"; static final String NON_MANO_ARTIFACT_SETS_TAG_SECTION = "non_mano_artifact_sets"; static final String PRODUCT_NAME = "pnfd_name"; static final String PROVIDER_ID = "pnfd_provider"; diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java index 9cbef8a..b25fc40 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java @@ -54,6 +54,10 @@ public class SourcesParser { handleAlgorithmLine(errors, source, lineNumber, manifestLine); } else if (!isSpecialTagReached && manifestLine.startsWith(HASH)) { handleHashLine(errors, source, lineNumber, manifestLine); + } else if (!isSpecialTagReached && manifestLine.startsWith(SIGNATURE)) { + handleSignatureLine(errors, source, lineNumber, manifestLine); + } else if (!isSpecialTagReached && manifestLine.startsWith(CERTIFICATE)) { + handleCertificateLine(errors, source, lineNumber, manifestLine); } } @@ -62,8 +66,8 @@ public class SourcesParser { private boolean isContainSpecialTag(String line, ManifestLine manifestLine) { return manifestLine.startsWith(METADATA_SECTION_TAG_SECTION) - || manifestLine.startsWith(NON_MANO_ARTIFACT_SETS_TAG_SECTION) - || line.contains(CMS); + || manifestLine.startsWith(NON_MANO_ARTIFACT_SETS_TAG_SECTION) + || line.contains(CMS); } private Source handleSourceLine(List<Source> sources, List<CSARArchive.CSARError> errors, int lineNumber, ManifestLine manifestLine) { @@ -80,14 +84,30 @@ public class SourcesParser { private void handleAlgorithmLine(List<CSARArchive.CSARError> errors, Source source, int lineNumber, ManifestLine manifestLine) { String algorithm = parseSourceSectionLine(manifestLine, lineNumber, errors); - if (source != null) + if (source != null) { source.setAlgorithm(algorithm); + } } private void handleHashLine(List<CSARArchive.CSARError> errors, Source source, int lineNumber, ManifestLine manifestLine) { String hash = parseSourceSectionLine(manifestLine, lineNumber, errors); - if (source != null) + if (source != null) { source.setHash(hash); + } + } + + private void handleSignatureLine(List<CSARArchive.CSARError> errors, Source source, int lineNumber, ManifestLine manifestLine) { + String signature = parseSourceSectionLine(manifestLine, lineNumber, errors); + if (source != null) { + source.setSignature(signature); + } + } + + private void handleCertificateLine(List<CSARArchive.CSARError> errors, Source source, int lineNumber, ManifestLine manifestLine) { + String certificate = parseSourceSectionLine(manifestLine, lineNumber, errors); + if (source != null) { + source.setCertificate(certificate); + } } private String parseSourceSectionLine(ManifestLine line, int lineNumber, List<CSARArchive.CSARError> errors) { @@ -110,12 +130,19 @@ public class SourcesParser { private final String value; private String algorithm; private String hash; + private String signature; + private String certificate; - public Source(String value, String algorithm, String hash) { - + public Source(String value, String algorithm, String hash, String signature, String certificate) { this.value = value; this.algorithm = algorithm; this.hash = hash; + this.signature = signature; + this.certificate = certificate; + } + + public Source(String source, String algorithm, String hash) { + this(source, algorithm, hash, "", ""); } public Source(String source) { @@ -142,34 +169,52 @@ public class SourcesParser { this.hash = hash; } + public String getSignature() { + return signature; + } + + public void setSignature(String signature) { + this.signature = signature; + } + + public String getCertificate() { + return certificate; + } + + public void setCertificate(String certificate) { + this.certificate = certificate; + } + @Override public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { return false; } - - Source source1 = (Source) o; - return Objects.equals(value, source1.value) && - Objects.equals(algorithm, source1.algorithm) && - Objects.equals(hash, source1.hash); + Source source = (Source) o; + return Objects.equals(value, source.value) && + Objects.equals(algorithm, source.algorithm) && + Objects.equals(hash, source.hash) && + Objects.equals(signature, source.signature) && + Objects.equals(certificate, source.certificate); } @Override public int hashCode() { - return Objects.hash(value, algorithm, hash); + return Objects.hash(value, algorithm, hash, signature, certificate); } @Override public String toString() { return "Source{" + - "value='" + value + '\'' + - ", algorithm='" + algorithm + '\'' + - ", hash='" + hash + '\'' + - '}'; + "value='" + value + '\'' + + ", algorithm='" + algorithm + '\'' + + ", hash='" + hash + '\'' + + ", signature='" + signature + '\'' + + ", certificate='" + certificate + '\'' + + '}'; } } } diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/parser/SourcesParserTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/parser/SourcesParserTest.java new file mode 100644 index 0000000..6611484 --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/parser/SourcesParserTest.java @@ -0,0 +1,303 @@ +/* + * Copyright 2020 Nokia + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.onap.cvc.csar.parser; + +import org.junit.Test; +import org.onap.cvc.csar.PnfCSARError; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SourcesParserTest { + + final private String TEST_FILE_NAME = "test_file.mf"; + + @Test + public void shouldReturnEmptyListOfSourcesFromEmptyListOfLines() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final List<String> manifestLines = List.of(); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + assertThat(parsingResult.getRight()).isEmpty(); + assertThat(parsingResult.getLeft()).isEmpty(); + } + + @Test + public void shouldCreateListOfSourcesFromListOfLinesContainingTwoSources() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: pnf_main_descriptor.mf", + "Source: Definitions/pnf_main_descriptor.yaml" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source("pnf_main_descriptor.mf"), + new SourcesParser.Source("Definitions/pnf_main_descriptor.yaml") + ); + } + + @Test + public void shouldCreateListOfSourcesFromListOfLinesContainingTwoSourcesWithHashesAndAlgorithms() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: Definitions/pnf_main_descriptor.yaml", + "Algorithm: SHA-256", + "Hash: 8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "Definitions/pnf_main_descriptor.yaml", + "SHA-256", + "8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703"), + new SourcesParser.Source( + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "SHA-256", + "9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0") + ); + } + + @Test + public void shouldCreateListOfSourcesFromListOfLinesContainingTwoSourcesWithHashesAndAlgorithmsAndTwoSourcesWithOnlyValue() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: Definitions/pnf_main_descriptor.yaml", + "Algorithm: SHA-256", + "Hash: 8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Source: pnf_main_descriptor.mf", + "Source: TOSCA-Metadata/TOSCA.meta" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "Definitions/pnf_main_descriptor.yaml", + "SHA-256", + "8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703"), + new SourcesParser.Source( + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "SHA-256", + "9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0"), + new SourcesParser.Source( + "pnf_main_descriptor.mf"), + new SourcesParser.Source( + "TOSCA-Metadata/TOSCA.meta") + ); + } + + @Test + public void shouldCreateListOfSourcesFromListOfLinesContainingTwoSourcesWithHashesAlgorithmsSignatureAndCertificate() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: Definitions/pnf_main_descriptor.yaml", + "Algorithm: SHA-256", + "Hash: 8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Signature: Definitions/pnf_main_descriptor.sig.cms", + "Certificate: Definitions/pnf_main_descriptor.cert", + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Signature: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Certificate: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "Definitions/pnf_main_descriptor.yaml", + "SHA-256", + "8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Definitions/pnf_main_descriptor.sig.cms", + "Definitions/pnf_main_descriptor.cert"), + new SourcesParser.Source( + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "SHA-256", + "9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert") + ); + } + + + @Test + public void shouldCreateListOfSourcesFromListOfLinesContainingSourcesWithVariousDataProvided() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: pnf_main_descriptor.mf", + "Source: Definitions/pnf_main_descriptor.yaml", + "Algorithm: SHA-256", + "Hash: 8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Signature: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Certificate: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "pnf_main_descriptor.mf"), + new SourcesParser.Source( + "Definitions/pnf_main_descriptor.yaml", + "SHA-256", + "8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703"), + new SourcesParser.Source( + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "SHA-256", + "9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert") + ); + } + + @Test + public void shouldStopParsingWhenLineWithSpecialTagIsReached() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: pnf_main_descriptor.mf", + "Source: Definitions/pnf_main_descriptor.yaml", + "Algorithm: SHA-256", + "Hash: 8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703", + "Metadata: test special metadata", + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Signature: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Certificate: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).isEmpty(); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "pnf_main_descriptor.mf"), + new SourcesParser.Source( + "Definitions/pnf_main_descriptor.yaml", + "SHA-256", + "8a041578eefd22c10418600e4c3cb8c5d1ff5703ae2785ed53540263f4030703") + ); + } + + @Test + public void shouldCreateListContainingErrorWhenListOfSourcesContainIncorrectLine() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source pnf_main_descriptor.mf" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).usingFieldByFieldElementComparator().containsExactly( + new PnfCSARError.PnfCSARErrorWarning("Source pnf_main_descriptor.mf", "test_file.mf", 0) + ); + var sources = parsingResult.getLeft(); + assertThat(sources).isEmpty(); + } + + + @Test + public void shouldCreateListContainingOneSourceAndOneErrorWhenListOfSourcesContainOneCorrectLineAndOneIncorrectLine() { + // given + final var parser = new SourcesParser(TEST_FILE_NAME); + final var manifestLines = List.of( + "Source: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "Algorithm: SHA-256", + "Hash: 9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Signature: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Certificate: Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert", + "Source pnf_main_descriptor.mf" + ); + + // when + final var parsingResult = parser.parse(manifestLines); + + // then + var errors = parsingResult.getRight(); + assertThat(errors).usingFieldByFieldElementComparator().containsExactly( + new PnfCSARError.PnfCSARErrorWarning("Source pnf_main_descriptor.mf", "test_file.mf", 5) + ); + var sources = parsingResult.getLeft(); + assertThat(sources).containsExactly( + new SourcesParser.Source( + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", + "SHA-256", + "9c82363306531b5f087f11058a7b18021e4597a75ca7c5a72d0893893646bcb0", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.sig.cms", + "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.cert") + ); + } +} |