2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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====================================================
22 package org.onap.aaf.cadi.config.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
30 import java.io.ByteArrayOutputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.security.KeyStore;
36 import java.security.KeyStoreException;
37 import java.security.NoSuchAlgorithmException;
38 import java.security.cert.CertificateException;
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.cadi.config.SecurityInfo;
45 public class JU_SecurityInfo {
47 private static PropAccess access;
49 private static final String keyStoreFileName = "src/test/resources/keystore.p12";
50 private static final String keyStorePassword = "Password for the keystore";
51 private static final String keyPassword = "Password for the key";
53 private static final String trustStoreFileName = "src/test/resources/truststore.jks";
54 private static final String trustStorePasswd = "Password for the truststore";
57 public static void setupOnce() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException {
58 KeyStore keyStore = KeyStore.getInstance("PKCS12");
59 keyStore.load(null, null);
60 keyStore.store(new FileOutputStream(keyStoreFileName), keyStorePassword.toCharArray());
62 KeyStore trustStore = KeyStore.getInstance("JKS");
63 trustStore.load(null, null);
64 trustStore.store(new FileOutputStream(trustStoreFileName), trustStorePasswd.toCharArray());
68 public void setup() throws IOException {
69 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
71 access.setProperty(Config.CADI_KEYSTORE, keyStoreFileName);
72 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, access.encrypt(keyStorePassword));
73 access.setProperty(Config.CADI_KEY_PASSWORD, access.encrypt(keyPassword));
75 access.setProperty(Config.CADI_TRUSTSTORE, trustStoreFileName);
76 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, access.encrypt(trustStorePasswd));
80 public static void tearDownOnce() {
81 File keyStoreFile = new File(keyStoreFileName);
82 if (keyStoreFile.exists()) {
83 keyStoreFile.delete();
85 File trustStoreFile = new File(trustStoreFileName);
86 if (trustStoreFile.exists()) {
87 trustStoreFile.delete();
92 public void test() throws CadiException {
93 SecurityInfo si = new SecurityInfo(access);
95 assertNotNull(si.getSSLSocketFactory());
96 assertNotNull(si.getSSLContext());
97 assertNotNull(si.getKeyManagers());
100 @Test(expected = CadiException.class)
101 public void nullkeyStoreTest() throws CadiException {
102 access.setProperty(Config.CADI_KEYSTORE, "passwords.txt");
103 @SuppressWarnings("unused")
104 SecurityInfo si = new SecurityInfo(access);
107 @Test(expected = CadiException.class)
108 public void nullTrustStoreTest() throws CadiException {
109 access.setProperty(Config.CADI_TRUSTSTORE, "passwords.txt");
110 @SuppressWarnings("unused")
111 SecurityInfo si = new SecurityInfo(access);
115 public void coverageTest() throws CadiException {
116 PropAccess badAccess = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
117 @SuppressWarnings("unused")
118 SecurityInfo si = new SecurityInfo(badAccess);
119 badAccess.setProperty(Config.CADI_KEYSTORE, keyStoreFileName);
120 si = new SecurityInfo(badAccess);