2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017 Amdocs
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 package org.onap.aai.sparky.util;
\r
25 import java.io.ByteArrayInputStream;
\r
26 import java.io.File;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStream;
\r
29 import java.lang.reflect.InvocationTargetException;
\r
30 import java.lang.reflect.Method;
\r
31 import java.net.UnknownHostException;
\r
32 import java.security.KeyManagementException;
\r
33 import java.security.KeyStoreException;
\r
34 import java.security.NoSuchAlgorithmException;
\r
35 import java.security.cert.CertificateException;
\r
36 import java.security.cert.CertificateFactory;
\r
37 import java.security.cert.X509Certificate;
\r
39 import javax.net.ssl.SSLSocket;
\r
40 import javax.net.ssl.SSLSocketFactory;
\r
42 import org.junit.Assert;
\r
43 import org.junit.Before;
\r
44 import org.junit.Rule;
\r
45 import org.junit.Test;
\r
46 import org.junit.rules.TemporaryFolder;
\r
47 import org.mockito.Matchers;
\r
48 import org.mockito.Mockito;
\r
50 public class KeystoreBuilderTest {
\r
53 public TemporaryFolder folder = new TemporaryFolder();
\r
55 KeystoreBuilder ksb;
\r
56 org.onap.aai.sparky.util.test.KeystoreBuilder ksb1;
\r
59 public void setUp() throws IOException, NoSuchAlgorithmException {
\r
60 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
\r
62 folder.newFile("file1.xml");
\r
63 folder.newFile("file2.xml");
\r
65 String endPointList = "https://localhost:9517;https://localhost:8443";
\r
66 ksb = new KeystoreBuilder(endPointList);
\r
67 ksb1 = new org.onap.aai.sparky.util.test.KeystoreBuilder(endPointList);
\r
70 @Test(expected = IOException.class)
\r
71 public void testUpdateKeyStore() throws KeyManagementException, KeyStoreException,
\r
72 CertificateException, IOException, NoSuchAlgorithmException {
\r
73 ksb.updateKeystore(folder.getRoot().getAbsolutePath(), "password-1");
\r
74 ksb1.updateKeystore(folder.getRoot().getAbsolutePath(), "password-1");
\r
77 @Test(expected = InvocationTargetException.class)
\r
78 public void testCertificateChainMethods()
\r
79 throws NoSuchMethodException, SecurityException, IllegalAccessException,
\r
80 IllegalArgumentException, InvocationTargetException, UnknownHostException, IOException {
\r
81 SSLSocketFactory factory = Mockito.mock(SSLSocketFactory.class);
\r
82 SSLSocket socket = Mockito.mock(SSLSocket.class);
\r
83 Mockito.when(factory.createSocket("localhost", 9517)).thenReturn(socket);
\r
84 Method method = KeystoreBuilder.class.getDeclaredMethod("getCertificateChainForRemoteEndpoint",
\r
85 String.class, int.class);
\r
86 method.setAccessible(true);
\r
87 X509Certificate[] certChain = (X509Certificate[]) method.invoke(ksb, "localhost", 9517);
\r
88 Assert.assertNotNull(certChain);
\r
91 @Test(expected = InvocationTargetException.class)
\r
92 public void testCertificateChainMethods1()
\r
93 throws NoSuchMethodException, SecurityException, IllegalAccessException,
\r
94 IllegalArgumentException, InvocationTargetException, UnknownHostException, IOException {
\r
95 SSLSocketFactory factory = Mockito.mock(SSLSocketFactory.class);
\r
96 SSLSocket socket = Mockito.mock(SSLSocket.class);
\r
97 Mockito.when(factory.createSocket("localhost", 9517)).thenReturn(socket);
\r
98 Method method = org.onap.aai.sparky.util.test.KeystoreBuilder.class
\r
99 .getDeclaredMethod("getCertificateChainForRemoteEndpoint", String.class, int.class);
\r
100 method.setAccessible(true);
\r
101 X509Certificate[] certChain = (X509Certificate[]) method.invoke(ksb1, "localhost", 9517);
\r
102 Assert.assertNotNull(certChain);
\r