2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.security;
23 import org.apache.commons.io.FileUtils;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.Assertions;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
35 import static junit.framework.TestCase.assertEquals;
36 import static junit.framework.TestCase.assertTrue;
38 public class SecurityManagerTest {
40 private String cerDirPath = "/tmp/cert/";
41 private SecurityManager securityManager;
43 private File PrepareCertFiles(String origFilePath, String newFilePath) throws IOException, URISyntaxException {
44 File origFile = new File(getClass().getResource(origFilePath).toURI());
45 File newFile = new File(newFilePath);
46 newFile.createNewFile();
47 FileUtils.copyFile(origFile, newFile);
51 private byte[] readAllBytes(String path) throws URISyntaxException, IOException {
52 return Files.readAllBytes(Paths.get(getClass().getResource(path).toURI()));
56 public void setUp() throws IOException {
57 certDir = new File(cerDirPath);
58 if (certDir.exists()) {
62 securityManager = new SecurityManager(certDir.getPath());
66 public void tearDown() throws IOException {
67 if (certDir.exists()) {
68 FileUtils.deleteDirectory(certDir);
70 securityManager.cleanTrustedCertificates();
74 public void testGetCertificates() throws IOException, SecurityManagerException, URISyntaxException {
75 File newFile = PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "/root-certificate.pem");
76 assertEquals(1, securityManager.getTrustedCertificates().size());
78 assertEquals(0, securityManager.getTrustedCertificates().size());
82 public void testGetCertificatesNoDirectory() throws IOException, SecurityManagerException {
84 assertEquals(0, securityManager.getTrustedCertificates().size());
88 public void testGetCertificatesException() throws IOException, SecurityManagerException {
89 Assertions.assertThrows(SecurityManagerException.class, () -> {
90 File newFile = new File(cerDirPath + "root-certificate.pem");
91 newFile.createNewFile();
92 assertEquals(1, securityManager.getTrustedCertificates().size());
94 assertEquals(0, securityManager.getTrustedCertificates().size());
99 public void testGetCertificatesUpdated() throws IOException, SecurityManagerException, URISyntaxException {
100 File newFile = PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "root-certificate.pem");
101 assertTrue(securityManager.getTrustedCertificates().size() == 1);
102 File otherNewFile = PrepareCertFiles("/cert/package-certificate.pem", cerDirPath + "package-certificate.pem");
103 assertEquals(2, securityManager.getTrustedCertificates().size());
104 otherNewFile.delete();
105 assertEquals(1, securityManager.getTrustedCertificates().size());
107 assertEquals(0, securityManager.getTrustedCertificates().size());
111 public void verifySignedDataTestCertIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
112 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
113 byte[] signature = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.cms");
114 byte[] archive = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.csar");
115 assertTrue(securityManager.verifySignedData(signature, null, archive));
119 public void verifySignedDataTestCertNotIncludedIntoSignatureButExpected() throws IOException, URISyntaxException, SecurityManagerException {
120 Assertions.assertThrows(SecurityManagerException.class, () -> {
121 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
122 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
123 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
124 securityManager.verifySignedData(signature, null, archive);
130 public void verifySignedDataTestCertNotIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
131 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
132 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
133 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
134 byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
135 assertTrue(securityManager.verifySignedData(signature, cert, archive));
139 public void verifySignedDataTestCertIntermediateNotIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
140 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
141 PrepareCertFiles("/cert/signing-ca2.crt", cerDirPath + "signing-ca2.crt");
142 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
143 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
144 byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4-no-intermediate.cert");
145 assertTrue(securityManager.verifySignedData(signature, cert, archive));
149 public void verifySignedDataTestCertWrongIntermediate() throws IOException, URISyntaxException, SecurityManagerException {
150 Assertions.assertThrows(SecurityManagerException.class, () -> {
151 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
152 PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
153 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
154 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
155 byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4-no-intermediate.cert");
156 securityManager.verifySignedData(signature, cert, archive);
162 public void verifySignedDataTestCertIncludedIntoSignatureWithWrongIntermediateInDirectory() throws IOException, URISyntaxException, SecurityManagerException {
163 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
164 PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
165 byte[] signature = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.cms");
166 byte[] archive = readAllBytes("/cert/2-file-signed-package/dummyPnfv4.csar");
167 assertTrue(securityManager.verifySignedData(signature, null, archive));
171 public void verifySignedDataTestCertWrongIntermediateInDirectory() throws IOException, URISyntaxException, SecurityManagerException {
172 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
173 PrepareCertFiles("/cert/signing-ca1.crt", cerDirPath + "signing-ca1.crt");
174 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
175 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
176 byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
177 assertTrue(securityManager.verifySignedData(signature, cert, archive));
181 public void verifySignedDataTestWrongCertificate() throws IOException, URISyntaxException, SecurityManagerException {
182 Assertions.assertThrows(SecurityManagerException.class, () -> {
183 PrepareCertFiles("/cert/root-certificate.pem", cerDirPath + "root-certificate.cert");
184 byte[] signature = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cms");
185 byte[] archive = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.csar");
186 byte[] cert = readAllBytes("/cert/3-file-signed-package/dummyPnfv4.cert");
187 securityManager.verifySignedData(signature, cert, archive);
193 public void verifySignedDataTestChangedArchive() throws IOException, URISyntaxException, SecurityManagerException {
194 Assertions.assertThrows(SecurityManagerException.class, () -> {
195 PrepareCertFiles("/cert/root.cert", cerDirPath + "root.cert");
196 byte[] signature = readAllBytes("/cert/tampered-signed-package/dummyPnfv4.cms");
197 byte[] archive = readAllBytes("/cert/tampered-signed-package/dummyPnfv4.csar");
198 securityManager.verifySignedData(signature, null, archive);