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
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 *******************************************************************************/
21 package org.onap.dmaap.kafkaAuthorize;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import javax.security.sasl.SaslException;
28 import org.apache.kafka.common.errors.SaslAuthenticationException;
29 import org.apache.kafka.common.security.JaasContext;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
36 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 @RunWith(PowerMockRunner.class)
43 @PowerMockIgnore({"javax.security.auth.*", "jdk.internal.reflect.*", "javax.crypto.*"})
44 @PrepareForTest({ AuthorizationProviderFactory.class })
45 public class PlainSaslServer1Test {
47 PlainSaslServer1 sslServer = new PlainSaslServer1();
49 JaasContext jaasContext;
51 AuthorizationProviderFactory factory;
53 AuthorizationProvider provider;
56 public void setUp() throws Exception {
58 MockitoAnnotations.initMocks(this);
59 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
60 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
61 PowerMockito.when(factory.getProvider()).thenReturn(provider);
64 public void testAuthentication() throws Exception {
65 String response = "authorizationID\u0000username\u0000password";
66 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
67 assertNotNull(sslServer.evaluateResponse(response.getBytes()));
72 public void testAuthenticationEmptyAuth() throws Exception {
73 String response = "\u0000username\u0000password";
74 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
75 sslServer.evaluateResponse(response.getBytes());
80 public void testAuthenticationEmptyUser() throws Exception {
81 String response = "authorizationID\u0000\u0000password";
82 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
85 sslServer.evaluateResponse(response.getBytes());
87 catch (SaslAuthenticationException e) {
92 public void testAuthenticationEmptyPassword() throws Exception {
93 String response = "authorizationID\u0000username\u0000";
94 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
96 sslServer.evaluateResponse(response.getBytes());
98 catch (SaslAuthenticationException e) {
104 public void testGetAuthorizationIdWithException() {
107 sslServer.getAuthorizationID();
109 catch (IllegalStateException ise) {
110 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
115 public void testGetNegotiatedPropertyWithException() {
118 sslServer.getNegotiatedProperty("test");
120 catch (IllegalStateException ise) {
121 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
126 public void testIsComplete() {
129 sslServer.getNegotiatedProperty("test");
131 catch (IllegalStateException ise) {
132 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
139 public void testUnwrap() {
141 sslServer.unwrap(new byte[1], 0, 0);
143 catch (IllegalStateException ise) {
144 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
145 } catch (SaslAuthenticationException e) {
152 public void testWrap() {
154 sslServer.wrap(new byte[1], 0, 0);
156 catch (IllegalStateException ise) {
157 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
158 } catch (SaslAuthenticationException e) {