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 static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
26 import ch.qos.logback.access.spi.IAccessEvent;
28 import java.security.cert.X509Certificate;
30 import javax.security.auth.x500.X500Principal;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.springframework.mock.web.MockHttpServletRequest;
41 @RunWith(MockitoJUnitRunner.class)
42 public class CNNameTest {
47 IAccessEvent accessEvent;
51 private CNName cnName;
55 when(cnName.isStarted()).thenReturn(true);
59 public void basicAuthTest() {
61 MockHttpServletRequest https = new MockHttpServletRequest();
62 https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
63 https.setAttribute("javax.servlet.request.cipher_suite", "");
64 https.setAttribute("javax.servlet.request.X509Certificate", null);
66 when(accessEvent.getRequest()).thenReturn(https);
67 assertEquals("user", cnName.convert(accessEvent));
72 public void incorrectHeaderBasicAuthTest() {
74 MockHttpServletRequest https = new MockHttpServletRequest();
76 https.addHeader("Authorization", "dXNlcjpwYXNzd29yZA==");
77 https.setAttribute("javax.servlet.request.cipher_suite", "");
78 https.setAttribute("javax.servlet.request.X509Certificate", null);
80 when(accessEvent.getRequest()).thenReturn(https);
81 assertEquals("-", cnName.convert(accessEvent));
86 public void noCipherSuiteTest() {
88 MockHttpServletRequest https = new MockHttpServletRequest();
90 https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
91 https.setAttribute("javax.servlet.request.cipher_suite", null);
92 https.setAttribute("javax.servlet.request.X509Certificate", null);
94 when(accessEvent.getRequest()).thenReturn(https);
95 assertEquals("-", cnName.convert(accessEvent));
100 public void certificateTest() {
101 String testSubject = "CN=TestName, OU=TestOU, O=TestOrg, C=Country";
102 X509Certificate[] certChain = {cert};
103 MockHttpServletRequest https = new MockHttpServletRequest();
105 https.setAttribute("javax.servlet.request.cipher_suite", "");
106 https.setAttribute("javax.servlet.request.X509Certificate", certChain);
108 when(accessEvent.getRequest()).thenReturn(https);
109 when(cert.getSubjectX500Principal()).thenReturn(new X500Principal(testSubject));
111 assertEquals(testSubject, cnName.convert(accessEvent));