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.http.test;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.net.HttpURLConnection;
28 import java.security.PrivateKey;
29 import java.security.cert.CertificateEncodingException;
30 import java.security.cert.X509Certificate;
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.X509KeyManager;
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.*;
37 import static org.hamcrest.CoreMatchers.*;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.PropAccess;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.config.SecurityInfoC;
45 import org.onap.aaf.cadi.http.HX509SS;
46 import org.onap.aaf.misc.env.APIException;
48 public class JU_HX509SS {
50 @Mock X509Certificate x509Mock;
51 @Mock X509KeyManager keyManagerMock;
52 @Mock PrivateKey privateKeyMock;
53 @Mock SecurityInfoC<HttpURLConnection> siMock;
54 @Mock HttpURLConnection hucMock;
55 @Mock HttpsURLConnection hucsMock;
57 private final static String alias = "Some alias";
58 private final static String algName = "Some algName";
59 private final static byte[] publicKeyBytes = "a public key".getBytes();
61 private PropAccess access;
62 private SecurityInfoC<HttpURLConnection> si;
65 public void setup() throws IOException, CadiException, CertificateEncodingException {
66 MockitoAnnotations.initMocks(this);
68 when(x509Mock.getSigAlgName()).thenReturn(algName);
69 when(x509Mock.getEncoded()).thenReturn(publicKeyBytes);
71 when(keyManagerMock.getCertificateChain(alias)).thenReturn(new X509Certificate[] {x509Mock});
72 when(keyManagerMock.getPrivateKey(alias)).thenReturn(privateKeyMock);
74 when(siMock.getKeyManagers()).thenReturn(new X509KeyManager[] {keyManagerMock});
76 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
77 access.setProperty(Config.CADI_ALIAS, alias);
78 si = SecurityInfoC.instance(access, HttpURLConnection.class);
82 public void test() throws APIException, CadiException {
83 HX509SS x509 = new HX509SS(alias, siMock);
84 assertThat(x509.getID(), is(alias));
85 assertThat(x509.setLastResponse(0), is(0));
86 assertThat(x509.setLastResponse(1), is(0));
87 assertThat(x509.setLastResponse(2), is(0));
90 x509.setSecurity(hucMock);
91 x509.setSecurity(hucsMock);
94 // TODO: Test the setSecurity method - Ian
96 // public void test2() throws APIException, CadiException {
97 // HX509SS x509 = new HX509SS(si, false);
98 // x509.setSecurity(hucMock);
99 // x509.setSecurity(hucsMock);
102 @Test(expected = APIException.class)
103 public void throws1Test() throws APIException, CadiException {
104 @SuppressWarnings("unused")
105 HX509SS x509 = new HX509SS(siMock);
108 @Test(expected = APIException.class)
109 public void throws2Test() throws APIException, CadiException {
110 @SuppressWarnings("unused")
111 HX509SS x509 = new HX509SS(si, false);
114 @Test(expected = APIException.class)
115 public void throws3Test() throws APIException, CadiException {
116 when(keyManagerMock.getCertificateChain(alias)).thenReturn(new X509Certificate[0]);
117 @SuppressWarnings("unused")
118 HX509SS x509 = new HX509SS(alias, siMock);