Merge "Improve coverage of cadi-client"
[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 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27 import org.junit.*;
28 import org.mockito.*;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
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;
39
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;
44
45 public class JU_SecurityInfo {
46         
47         private static PropAccess access;
48         
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";
52                 
53         private static final String trustStoreFileName = "src/test/resources/truststore.jks";
54         private static final String trustStorePasswd = "Password for the truststore";
55         
56         @BeforeClass
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());
61
62                 KeyStore trustStore = KeyStore.getInstance("JKS");
63                 trustStore.load(null, null);
64                 trustStore.store(new FileOutputStream(trustStoreFileName), trustStorePasswd.toCharArray());
65         }
66         
67         @Before
68         public void setup() throws IOException {
69                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
70
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));
74                 
75                 access.setProperty(Config.CADI_TRUSTSTORE, trustStoreFileName);
76                 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, access.encrypt(trustStorePasswd));
77         }
78
79         @AfterClass
80         public static void tearDownOnce() {
81                 File keyStoreFile = new File(keyStoreFileName);
82                 if (keyStoreFile.exists()) {
83                         keyStoreFile.delete();
84                 }
85                 File trustStoreFile = new File(trustStoreFileName);
86                 if (trustStoreFile.exists()) {
87                         trustStoreFile.delete();
88                 }
89         }
90
91         @Test
92         public void test() throws CadiException {
93                 SecurityInfo si = new SecurityInfo(access);
94
95                 assertNotNull(si.getSSLSocketFactory());
96                 assertNotNull(si.getSSLContext());
97                 assertNotNull(si.getKeyManagers());
98         }
99
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);
105         }
106
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);
112         }
113
114         @Test
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);
121         }
122
123 }