Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_SecurityInfo.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.config.test;
23
24
25 import static org.junit.Assert.assertNotNull;
26
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.File;
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;
41
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;
46
47 public class JU_SecurityInfo {
48
49     private static PropAccess access;
50
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";
54
55     private static final String trustStoreFileName = "src/test/resources/truststore.jks";
56     private static final String trustStorePasswd = "Password for the truststore";
57
58     @BeforeClass
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());
63
64         KeyStore trustStore = KeyStore.getInstance("JKS");
65         trustStore.load(null, null);
66         trustStore.store(new FileOutputStream(trustStoreFileName), trustStorePasswd.toCharArray());
67     }
68
69     @Before
70     public void setup() throws IOException {
71         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
72
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));
76
77         access.setProperty(Config.CADI_TRUSTSTORE, trustStoreFileName);
78         access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, access.encrypt(trustStorePasswd));
79     }
80
81     @AfterClass
82     public static void tearDownOnce() {
83         File keyStoreFile = new File(keyStoreFileName);
84         if (keyStoreFile.exists()) {
85             keyStoreFile.delete();
86         }
87         File trustStoreFile = new File(trustStoreFileName);
88         if (trustStoreFile.exists()) {
89             trustStoreFile.delete();
90         }
91     }
92
93     @Test
94     public void test() throws CadiException {
95         SecurityInfo si = new SecurityInfo(access);
96
97         assertNotNull(si.getSSLSocketFactory());
98         assertNotNull(si.getSSLContext());
99         assertNotNull(si.getKeyManagers());
100
101         access.setProperty(Config.CADI_TRUST_MASKS, "123.123.123.123");
102         si = new SecurityInfo(access);
103     }
104
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);
110     }
111
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);
117     }
118
119
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);
125     }
126
127     @Test
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);
134     }
135
136 }