48c81c50879c5565987033f4797ea206f78e5893
[aaf/authz.git] / auth / auth-certman / src / test / java / org / onap / aaf / auth / cm / ca / JU_AppCA.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
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
23 package org.onap.aaf.auth.cm.ca;
24
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.Mockito.CALLS_REAL_METHODS;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.IOException;
31 import java.math.BigInteger;
32 import java.security.InvalidKeyException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.NoSuchProviderException;
35 import java.security.Principal;
36 import java.security.PublicKey;
37 import java.security.SignatureException;
38 import java.security.cert.CertificateEncodingException;
39 import java.security.cert.CertificateException;
40 import java.security.cert.CertificateExpiredException;
41 import java.security.cert.CertificateNotYetValidException;
42 import java.security.cert.X509Certificate;
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.Set;
46
47 import javax.security.auth.x500.X500Principal;
48 import javax.servlet.http.HttpServletRequest;
49
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.runners.MockitoJUnitRunner;
56 import org.onap.aaf.auth.cm.cert.CSRMeta;
57 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
58 import org.onap.aaf.cadi.configure.CertException;
59 import org.onap.aaf.misc.env.Trans;
60
61 //TODO: Gabe [JUnit] Import does not exist
62 @RunWith(MockitoJUnitRunner.class)
63 public class JU_AppCA {
64     
65     @Mock
66     private static CachedCertDAO certDAO;
67     
68     @Mock
69     private static HttpServletRequest req;
70     
71     @Mock
72     private static CSRMeta csrMeta;
73     
74     static Trans trans;
75     
76     static X509andChain cert1;
77     static byte [] name = {1,23,4,54,6,56};
78     
79     private static LocalCA localCA;
80     
81     @BeforeClass
82     public static void setUp() throws CertificateException, CertException, IOException {
83         String str = "core java api";
84         byte[] b = str.getBytes();
85         Principal prc = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US");
86         req = mock(HttpServletRequest.class);
87         localCA = mock(LocalCA.class);
88         X509Certificate cert = new X509Certificate() {
89             
90             @Override
91             public boolean hasUnsupportedCriticalExtension() {
92                 return false;
93             }
94             
95             @Override
96             public Set<String> getNonCriticalExtensionOIDs() {
97                  
98                 return null;
99             }
100             
101             @Override
102             public byte[] getExtensionValue(String oid) {
103                  
104                 return null;
105             }
106             
107             @Override
108             public Set<String> getCriticalExtensionOIDs() {
109                  
110                 return null;
111             }
112             
113             @Override
114             public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException,
115                     InvalidKeyException, NoSuchProviderException, SignatureException {
116                  
117                 
118             }
119             
120             @Override
121             public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
122                     NoSuchProviderException, SignatureException {
123                  
124                 
125             }
126             
127             @Override
128             public String toString() {
129                  
130                 return null;
131             }
132             
133             @Override
134             public PublicKey getPublicKey() {
135                  
136                 return null;
137             }
138             
139             @Override
140             public byte[] getEncoded() throws CertificateEncodingException {
141                  
142                 return null;
143             }
144             
145             @Override
146             public int getVersion() {
147                  
148                 return 0;
149             }
150             
151             @Override
152             public byte[] getTBSCertificate() throws CertificateEncodingException {
153                  
154                 return null;
155             }
156             
157             @Override
158             public boolean[] getSubjectUniqueID() {
159                  
160                 return null;
161             }
162             
163             @Override
164             public Principal getSubjectDN() {
165                  
166                 return null;
167             }
168             
169             @Override
170             public byte[] getSignature() {
171                  
172                 return null;
173             }
174             
175             @Override
176             public byte[] getSigAlgParams() {
177                  
178                 return null;
179             }
180             
181             @Override
182             public String getSigAlgOID() {
183                  
184                 return null;
185             }
186             
187             @Override
188             public String getSigAlgName() {
189                  
190                 return null;
191             }
192             
193             @Override
194             public BigInteger getSerialNumber() {
195                  
196                 return null;
197             }
198             
199             @Override
200             public Date getNotBefore() {
201                  
202                 return null;
203             }
204             
205             @Override
206             public Date getNotAfter() {
207                  
208                 return null;
209             }
210             
211             @Override
212             public boolean[] getKeyUsage() {
213                  
214                 return null;
215             }
216             
217             @Override
218             public boolean[] getIssuerUniqueID() {
219                  
220                 return null;
221             }
222             
223             @Override
224             public Principal getIssuerDN() {
225                  
226                 return null;
227             }
228             
229             @Override
230             public int getBasicConstraints() {
231                  
232                 return 0;
233             }
234             
235             @Override
236             public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
237                  
238                 
239             }
240             
241             @Override
242             public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
243                 
244             }
245         };
246         X509andChain xac = new X509andChain(cert, new ArrayList<>());
247         when(localCA.sign(Mockito.any(Trans.class), Mockito.any(CSRMeta.class))).thenReturn(xac);
248         certDAO = mock(CachedCertDAO.class, CALLS_REAL_METHODS);
249     }
250     
251     @Test
252     public void identity_True() throws CertificateException, IOException, CertException {
253         assertNotNull(localCA.sign(trans, csrMeta));
254     }
255     
256     
257     @Test
258     public void identityNull() throws CertificateException {
259         try {
260             assertNotNull(localCA.sign(null, csrMeta));
261         } catch (IOException e) {
262         
263             e.printStackTrace();
264         } catch (CertException e) {
265             
266             e.printStackTrace();
267         }
268     }
269     
270     @Test
271     public void identityBothNull() throws CertificateException {
272         try {
273             assertNotNull(localCA.sign(null, null));
274         } catch (IOException e) {
275         
276             e.printStackTrace();
277         } catch (CertException e) {
278             
279             e.printStackTrace();
280         }
281     }
282
283 }