2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-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=========================================================
21 package org.onap.aai.logging;
23 import ch.qos.logback.access.spi.IAccessEvent;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Spy;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.springframework.mock.web.MockHttpServletRequest;
33 import javax.security.auth.x500.X500Principal;
34 import java.security.cert.X509Certificate;
36 import static org.junit.Assert.assertEquals;
37 import static org.mockito.Mockito.when;
39 @RunWith(MockitoJUnitRunner.class)
40 public class CNNameTest {
45 IAccessEvent accessEvent;
49 private CNName cnName;
53 when(cnName.isStarted()).thenReturn(true);
56 public void basicAuthTest(){
58 MockHttpServletRequest https = new MockHttpServletRequest();
59 https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
60 https.setAttribute("javax.servlet.request.cipher_suite", "");
61 https.setAttribute("javax.servlet.request.X509Certificate", null);
63 when(accessEvent.getRequest()).thenReturn(https);
64 assertEquals("user", cnName.convert(accessEvent));
68 public void incorrectHeaderBasicAuthTest(){
70 MockHttpServletRequest https = new MockHttpServletRequest();
72 https.addHeader("Authorization", "dXNlcjpwYXNzd29yZA==");
73 https.setAttribute("javax.servlet.request.cipher_suite", "");
74 https.setAttribute("javax.servlet.request.X509Certificate", null);
76 when(accessEvent.getRequest()).thenReturn(https);
77 assertEquals("-", cnName.convert(accessEvent));
81 public void noCipherSuiteTest(){
83 MockHttpServletRequest https = new MockHttpServletRequest();
85 https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
86 https.setAttribute("javax.servlet.request.cipher_suite", null);
87 https.setAttribute("javax.servlet.request.X509Certificate", null);
89 when(accessEvent.getRequest()).thenReturn(https);
90 assertEquals("-", cnName.convert(accessEvent));
94 public void certificateTest(){
95 String testSubject = "CN=TestName, OU=TestOU, O=TestOrg, C=Country";
96 X509Certificate[] certChain = { cert };
97 MockHttpServletRequest https = new MockHttpServletRequest();
99 https.setAttribute("javax.servlet.request.cipher_suite", "");
100 https.setAttribute("javax.servlet.request.X509Certificate", certChain );
102 when(accessEvent.getRequest()).thenReturn(https);
103 when(cert.getSubjectX500Principal()).thenReturn(new X500Principal(testSubject) );
105 assertEquals(testSubject, cnName.convert(accessEvent));