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;
25 import static org.junit.Assert.assertNotNull;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
32 import java.io.ByteArrayOutputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.PrintStream;
37 import java.security.KeyStore;
38 import java.security.KeyStoreException;
39 import java.security.NoSuchAlgorithmException;
40 import java.security.cert.CertificateException;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.PropAccess;
44 import org.onap.aaf.cadi.config.Config;
45 import org.onap.aaf.cadi.config.SecurityInfo;
47 public class JU_SecurityInfo {
49 private static PropAccess access;
51 private static final String keyStoreFileName = "src/test/resources/keystore.p12";
52 private static final String keyStorePassword = "Password for the keystore";
53 private static final String keyPassword = "Password for the key";
55 private static final String trustStoreFileName = "src/test/resources/truststore.jks";
56 private static final String trustStorePasswd = "Password for the truststore";
59 public static void setupOnce() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException {
60 KeyStore keyStore = KeyStore.getInstance("PKCS12");
61 keyStore.load(null, null);
62 keyStore.store(new FileOutputStream(keyStoreFileName), keyStorePassword.toCharArray());
64 KeyStore trustStore = KeyStore.getInstance("JKS");
65 trustStore.load(null, null);
66 trustStore.store(new FileOutputStream(trustStoreFileName), trustStorePasswd.toCharArray());
70 public void setup() throws IOException {
71 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
73 access.setProperty(Config.CADI_KEYSTORE, keyStoreFileName);
74 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, access.encrypt(keyStorePassword));
75 access.setProperty(Config.CADI_KEY_PASSWORD, access.encrypt(keyPassword));
77 access.setProperty(Config.CADI_TRUSTSTORE, trustStoreFileName);
78 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, access.encrypt(trustStorePasswd));
82 public static void tearDownOnce() {
83 File keyStoreFile = new File(keyStoreFileName);
84 if (keyStoreFile.exists()) {
85 keyStoreFile.delete();
87 File trustStoreFile = new File(trustStoreFileName);
88 if (trustStoreFile.exists()) {
89 trustStoreFile.delete();
94 public void test() throws CadiException {
95 SecurityInfo si = new SecurityInfo(access);
97 assertNotNull(si.getSSLSocketFactory());
98 assertNotNull(si.getSSLContext());
99 assertNotNull(si.getKeyManagers());
101 access.setProperty(Config.CADI_TRUST_MASKS, "123.123.123.123");
102 si = new SecurityInfo(access);
105 @Test(expected = CadiException.class)
106 public void nullkeyStoreTest() throws CadiException {
107 access.setProperty(Config.CADI_KEYSTORE, "passwords.txt");
108 @SuppressWarnings("unused")
109 SecurityInfo si = new SecurityInfo(access);
112 @Test(expected = CadiException.class)
113 public void nullTrustStoreTest() throws CadiException {
114 access.setProperty(Config.CADI_TRUSTSTORE, "passwords.txt");
115 @SuppressWarnings("unused")
116 SecurityInfo si = new SecurityInfo(access);
120 @Test(expected = NumberFormatException.class)
121 public void badTrustMaskTest() throws CadiException {
122 access.setProperty(Config.CADI_TRUST_MASKS, "trustMask");
123 @SuppressWarnings("unused")
124 SecurityInfo si = new SecurityInfo(access);
128 public void coverageTest() throws CadiException {
129 PropAccess badAccess = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
130 @SuppressWarnings("unused")
131 SecurityInfo si = new SecurityInfo(badAccess);
132 badAccess.setProperty(Config.CADI_KEYSTORE, keyStoreFileName);
133 si = new SecurityInfo(badAccess);