2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.security;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
31 import java.io.FileInputStream;
33 import javax.net.ssl.SSLContext;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.openecomp.sparky.util.LogValidator;
40 import ch.qos.logback.classic.Level;
43 * The Class SecurityContextFactoryImplTest.
45 public class SecurityContextFactoryImplTest {
47 private LogValidator logValidator;
52 * @throws Exception the exception
55 public void init() throws Exception {
56 logValidator = new LogValidator();
57 logValidator.initializeLogger(Level.WARN);
61 * Basic construction test.
63 * @throws Exception the exception
66 public void basicConstructionTest() throws Exception {
68 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
70 assertEquals("TLS", sslContextFactory.getSslAlgorithm());
71 assertEquals("SunX509", sslContextFactory.getKeyManagerAlgortihm());
72 assertEquals("PKCS12", sslContextFactory.getKeyStoreType());
73 assertEquals(false, sslContextFactory.isServerCertificationChainValidationEnabled());
74 assertEquals(null, sslContextFactory.getClientCertFileInputStream());
78 * Validate secure context.
80 * @throws Exception the exception
83 public void validateSecureContext() throws Exception {
85 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
87 SSLContext sslContext = sslContextFactory.getSecureContext();
89 assertNotNull(sslContext);
93 * Validate secure context with server cert chain validation.
95 * @throws Exception the exception
98 public void validateSecureContext_withServerCertChainValidation() throws Exception {
100 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
101 sslContextFactory.setServerCertificationChainValidationEnabled(true);
102 sslContextFactory.setTrustStoreFileName("filename");
104 sslContextFactory.setClientCertFileName(null);
106 SSLContext sslContext = sslContextFactory.getSecureContext();
108 assertNotNull(sslContext);
112 * Validate accessors.
114 * @throws Exception the exception
117 public void validateAccessors() throws Exception {
119 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
121 FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
123 sslContextFactory.setSslAlgorithm("sslAlgorithm");
124 sslContextFactory.setKeyManagerAlgortihm("keyManagerAlgorithm");
125 sslContextFactory.setKeyStoreType("keyStoreType");
126 sslContextFactory.setClientCertFileInputStream(mockInputStream);
127 sslContextFactory.setServerCertificationChainValidationEnabled(true);
128 sslContextFactory.setTrustStoreFileName("truststoreFileName");
129 sslContextFactory.setClientCertPassword("password");
131 assertEquals("sslAlgorithm", sslContextFactory.getSslAlgorithm());
132 assertEquals("keyManagerAlgorithm", sslContextFactory.getKeyManagerAlgortihm());
133 assertEquals("keyStoreType", sslContextFactory.getKeyStoreType());
134 assertEquals(mockInputStream, sslContextFactory.getClientCertFileInputStream());
135 assertEquals(true, sslContextFactory.isServerCertificationChainValidationEnabled());
136 assertEquals("truststoreFileName", sslContextFactory.getTrustStoreFileName());
137 assertEquals("password", sslContextFactory.getClientCertPassword());