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.After;
25 import org.junit.Before;
26 import org.junit.Test;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
34 import static junit.framework.TestCase.assertEquals;
35 import static junit.framework.TestCase.assertTrue;
37 public class SecurityManagerTest {
39 private SecurityManager securityManager;
42 public void setUp() throws IOException {
43 certDir = new File("/tmp/cert");
48 securityManager = new SecurityManager(certDir.getPath());
52 public void tearDown() throws IOException {
53 if(certDir.exists()) {
54 FileUtils.deleteDirectory(certDir);
56 securityManager.cleanTrustedCertificates();
60 public void testGetCertificates() throws IOException, SecurityManagerException {
61 File origFile = new File("src/test/resources/cert/root-certificate.pem");
62 File newFile = new File("/tmp/cert/root-certificate.pem");
63 newFile.createNewFile();
64 FileUtils.copyFile(origFile, newFile);
65 assertEquals(1, securityManager.getTrustedCertificates().size());
67 assertEquals(0, securityManager.getTrustedCertificates().size());
71 public void testGetCertificatesNoDirectory() throws IOException, SecurityManagerException {
73 assertEquals(0, securityManager.getTrustedCertificates().size());
76 @Test(expected = SecurityManagerException.class)
77 public void testGetCertificatesException() throws IOException, SecurityManagerException {
78 File newFile = new File("/tmp/cert/root-certificate.pem");
79 newFile.createNewFile();
80 assertEquals(1, securityManager.getTrustedCertificates().size());
82 assertEquals(0, securityManager.getTrustedCertificates().size());
86 public void testGetCertificatesUpdated() throws IOException, SecurityManagerException {
87 File origFile = new File("src/test/resources/cert/root-certificate.pem");
88 File newFile = new File("/tmp/cert/root-certificate.pem");
89 newFile.createNewFile();
90 FileUtils.copyFile(origFile, newFile);
91 assertTrue(securityManager.getTrustedCertificates().size() == 1);
92 File otherOrigFile = new File("src/test/resources/cert/package-certificate.pem");
93 File otherNewFile = new File("/tmp/cert/package-certificate.pem");
94 newFile.createNewFile();
95 FileUtils.copyFile(otherOrigFile, otherNewFile);
96 assertEquals(2, securityManager.getTrustedCertificates().size());
97 otherNewFile.delete();
98 assertEquals(1, securityManager.getTrustedCertificates().size());
100 assertEquals(0, securityManager.getTrustedCertificates().size());
104 public void verifySignedDataTestCertIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
105 File origFile = new File("src/test/resources/cert/root.cert");
106 File newFile = new File("/tmp/cert/root.cert");
107 newFile.createNewFile();
108 FileUtils.copyFile(origFile, newFile);
109 byte[] signature = Files.readAllBytes(Paths.get(getClass().getResource("/cert/2-file-signed-package/dummyPnfv4.cms").toURI()));
110 byte[] archive = Files.readAllBytes(Paths.get(getClass().getResource("/cert/2-file-signed-package/dummyPnfv4.csar").toURI()));
111 assertTrue(securityManager.verifySignedData(signature, null, archive));
114 @Test(expected = SecurityManagerException.class)
115 public void verifySignedDataTestCertNotIncludedIntoSignatureButExpected() throws IOException, URISyntaxException, SecurityManagerException {
116 File origFile = new File("src/test/resources/cert/root.cert");
117 File newFile = new File("/tmp/cert/root.cert");
118 newFile.createNewFile();
119 FileUtils.copyFile(origFile, newFile);
120 byte[] signature = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.cms").toURI()));
121 byte[] archive = Files.readAllBytes(Paths.get(getClass().getResource("/cert/2-file-signed-package/dummyPnfv4.csar").toURI()));
122 securityManager.verifySignedData(signature, null, archive);
126 public void verifySignedDataTestCertNotIncludedIntoSignature() throws IOException, URISyntaxException, SecurityManagerException {
127 File origFile = new File("src/test/resources/cert/root.cert");
128 File newFile = new File("/tmp/cert/root.cert");
129 newFile.createNewFile();
130 FileUtils.copyFile(origFile, newFile);
131 byte[] signature = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.cms").toURI()));
132 byte[] archive = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.csar").toURI()));
133 byte[] cert = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.cert").toURI()));
134 assertTrue(securityManager.verifySignedData(signature, cert, archive));
137 @Test(expected = SecurityManagerException.class)
138 public void verifySignedDataTestWrongCertificate() throws IOException, URISyntaxException, SecurityManagerException {
139 File origFile = new File("src/test/resources/cert/root-certificate.pem");
140 File newFile = new File("/tmp/cert/root-certificate.cert");
141 newFile.createNewFile();
142 FileUtils.copyFile(origFile, newFile);
143 byte[] signature = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.cms").toURI()));
144 byte[] archive = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.csar").toURI()));
145 byte[] cert = Files.readAllBytes(Paths.get(getClass().getResource("/cert/3-file-signed-package/dummyPnfv4.cert").toURI()));
146 securityManager.verifySignedData(signature, cert, archive);
149 @Test(expected = SecurityManagerException.class)
150 public void verifySignedDataTestChangedArchive() throws IOException, URISyntaxException, SecurityManagerException {
151 File origFile = new File("src/test/resources/cert/root.cert");
152 File newFile = new File("/tmp/cert/root.cert");
153 newFile.createNewFile();
154 FileUtils.copyFile(origFile, newFile);
155 byte[] signature = Files.readAllBytes(Paths.get(getClass().getResource("/cert/tampered-signed-package/dummyPnfv4.cms").toURI()));
156 byte[] archive = Files.readAllBytes(Paths.get(getClass().getResource("/cert/tampered-signed-package/dummyPnfv4.csar").toURI()));
157 securityManager.verifySignedData(signature, null, archive);