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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
29 import javax.security.auth.callback.CallbackHandler;
30 import javax.security.sasl.Sasl;
31 import javax.security.sasl.SaslException;
32 import org.apache.kafka.common.errors.SaslAuthenticationException;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
40 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
41 import org.onap.dmaap.kafkaAuthorize.PlainSaslServer1.PlainSaslServerFactory1;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 @RunWith(PowerMockRunner.class)
48 @PowerMockIgnore({"javax.security.auth.*", "jdk.internal.reflect.*", "javax.crypto.*"})
49 @PrepareForTest({ AuthorizationProviderFactory.class })
50 public class PlainSaslServer1Test {
52 PlainSaslServer1 sslServer = new PlainSaslServer1();
55 AuthorizationProviderFactory factory;
57 AuthorizationProvider provider;
59 CallbackHandler callbackHandler;
61 static Map<String, String> props;
65 MockitoAnnotations.initMocks(this);
66 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
67 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
68 PowerMockito.when(factory.getProvider()).thenReturn(provider);
71 public void testAuthentication() throws Exception {
72 String response = "authorizationID\u0000username\u0000password";
73 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
74 assertNotNull(sslServer.evaluateResponse(response.getBytes()));
79 public void testAuthenticationEmptyAuth() throws Exception {
80 String response = "\u0000username\u0000password";
81 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
82 assertNotNull(sslServer.evaluateResponse(response.getBytes()));
86 public void testAuthenticationEmptyUser() throws Exception {
87 String response = "authorizationID\u0000\u0000password";
88 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
90 sslServer.evaluateResponse(response.getBytes());
92 catch (SaslAuthenticationException e) {
98 public void testAuthenticationEmptyPassword() throws Exception {
99 String response = "authorizationID\u0000username\u0000";
100 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
102 sslServer.evaluateResponse(response.getBytes());
104 catch (SaslAuthenticationException e) {
110 public void testGetAuthorizationIdWithException() {
112 sslServer.getAuthorizationID();
114 catch (IllegalStateException ise) {
115 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
120 public void testGetNegotiatedPropertyWithException() {
122 sslServer.getNegotiatedProperty("test");
124 catch (IllegalStateException ise) {
125 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
130 public void testIsComplete() {
132 sslServer.getNegotiatedProperty("test");
134 catch (IllegalStateException ise) {
135 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
141 public void testUnwrap() {
143 sslServer.unwrap(new byte[1], 0, 0);
145 catch (IllegalStateException ise) {
146 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
147 } catch (SaslAuthenticationException e) {
154 public void testWrap() {
156 sslServer.wrap(new byte[1], 0, 0);
158 catch (IllegalStateException ise) {
159 assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
160 } catch (SaslAuthenticationException e) {
167 public void testGetMech() {
168 assertEquals("PLAIN", sslServer.getMechanismName());
172 public void testIsCompleteBool() {
173 assertFalse(sslServer.isComplete());
177 public void testPlainSaslServer1() throws SaslException {
178 PlainSaslServerFactory1 plainSaslServerFactory1 = new PlainSaslServerFactory1();
179 PlainSaslServer1 saslServer1 = (PlainSaslServer1) plainSaslServerFactory1.createSaslServer(PlainSaslServer1.PLAIN_MECHANISM, "https", "mySaslServer", props, callbackHandler);
180 assertNotNull(saslServer1);
181 Mockito.when(props.get(Sasl.POLICY_NOPLAINTEXT)).thenReturn("javax.security.sasl.policy.noplaintext");
182 assertEquals(new String[]{"PLAIN"}, plainSaslServerFactory1.getMechanismNames(props));