1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
21 ******************************************************************************/
22 package org.onap.aaf.auth.cm.ca;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.CALLS_REAL_METHODS;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import java.math.BigInteger;
31 import java.security.InvalidKeyException;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.NoSuchProviderException;
34 import java.security.Principal;
35 import java.security.PublicKey;
36 import java.security.SignatureException;
37 import java.security.cert.CertificateEncodingException;
38 import java.security.cert.CertificateException;
39 import java.security.cert.CertificateExpiredException;
40 import java.security.cert.CertificateNotYetValidException;
41 import java.security.cert.X509Certificate;
42 import java.util.ArrayList;
43 import java.util.Date;
46 import javax.security.auth.x500.X500Principal;
47 import javax.servlet.http.HttpServletRequest;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.runners.MockitoJUnitRunner;
55 import org.onap.aaf.auth.cm.cert.CSRMeta;
56 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
57 import org.onap.aaf.cadi.configure.CertException;
58 import org.onap.aaf.misc.env.Trans;
60 //TODO: Gabe [JUnit] Import does not exist
61 @RunWith(MockitoJUnitRunner.class)
62 public class JU_AppCA {
65 private static CachedCertDAO certDAO;
68 private static HttpServletRequest req;
71 private static CSRMeta csrMeta;
75 static X509andChain cert1;
76 static byte [] name = {1,23,4,54,6,56};
78 private static LocalCA localCA;
81 public static void setUp() throws CertificateException, CertException, IOException {
82 String str = "core java api";
83 byte[] b = str.getBytes();
84 Principal prc = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US");
85 req = mock(HttpServletRequest.class);
86 localCA = mock(LocalCA.class);
87 X509Certificate cert = new X509Certificate() {
90 public boolean hasUnsupportedCriticalExtension() {
95 public Set<String> getNonCriticalExtensionOIDs() {
101 public byte[] getExtensionValue(String oid) {
107 public Set<String> getCriticalExtensionOIDs() {
113 public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException,
114 InvalidKeyException, NoSuchProviderException, SignatureException {
120 public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
121 NoSuchProviderException, SignatureException {
127 public String toString() {
133 public PublicKey getPublicKey() {
139 public byte[] getEncoded() throws CertificateEncodingException {
145 public int getVersion() {
151 public byte[] getTBSCertificate() throws CertificateEncodingException {
157 public boolean[] getSubjectUniqueID() {
163 public Principal getSubjectDN() {
169 public byte[] getSignature() {
175 public byte[] getSigAlgParams() {
181 public String getSigAlgOID() {
187 public String getSigAlgName() {
193 public BigInteger getSerialNumber() {
199 public Date getNotBefore() {
205 public Date getNotAfter() {
211 public boolean[] getKeyUsage() {
217 public boolean[] getIssuerUniqueID() {
223 public Principal getIssuerDN() {
229 public int getBasicConstraints() {
235 public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
241 public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
245 X509andChain xac = new X509andChain(cert, new ArrayList<>());
246 when(localCA.sign(Mockito.any(Trans.class), Mockito.any(CSRMeta.class))).thenReturn(xac);
247 certDAO = mock(CachedCertDAO.class, CALLS_REAL_METHODS);
251 public void identity_True() throws CertificateException, IOException, CertException {
252 assertNotNull(localCA.sign(trans, csrMeta));
257 public void identityNull() throws CertificateException {
259 assertNotNull(localCA.sign(null, csrMeta));
260 } catch (IOException e) {
263 } catch (CertException e) {
270 public void identityBothNull() throws CertificateException {
272 assertNotNull(localCA.sign(null, null));
273 } catch (IOException e) {
276 } catch (CertException e) {