[DMAAP-KAFKA] Release image 1.1.0
[dmaap/kafka11aaf.git] / src / test / java / org / onap / dmaap / kafkaAuthorize / PlainSaslServer1Test.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  
20  *******************************************************************************/
21 package org.onap.dmaap.kafkaAuthorize;
22
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;
27
28 import java.util.Map;
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;
46
47 @RunWith(PowerMockRunner.class)
48 @PowerMockIgnore({"javax.security.auth.*", "jdk.internal.reflect.*", "javax.crypto.*"})
49 @PrepareForTest({ AuthorizationProviderFactory.class })
50 public class PlainSaslServer1Test {
51
52         PlainSaslServer1 sslServer = new PlainSaslServer1();
53
54         @Mock
55         AuthorizationProviderFactory factory;
56         @Mock
57         AuthorizationProvider provider;
58         @Mock
59         CallbackHandler callbackHandler;
60         @Mock
61         static Map<String, String> props;
62
63         @Before
64         public void setUp() {
65                 MockitoAnnotations.initMocks(this);
66                 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
67                 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
68                 PowerMockito.when(factory.getProvider()).thenReturn(provider);
69         }
70
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()));
75
76         }
77
78         @Test
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()));
83         }
84
85         @Test
86         public void testAuthenticationEmptyUser() throws Exception {
87                 String response = "authorizationID\u0000\u0000password";
88                 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
89                 try {
90                         sslServer.evaluateResponse(response.getBytes());
91                 }
92                 catch (SaslAuthenticationException e) {
93                         assertNotNull(e);
94                 }
95         }
96
97         @Test
98         public void testAuthenticationEmptyPassword() throws Exception {
99                 String response = "authorizationID\u0000username\u0000";
100                 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
101                 try {
102                         sslServer.evaluateResponse(response.getBytes());
103                 }
104                 catch (SaslAuthenticationException e) {
105                         assertNotNull(e);
106                 }
107         }
108         
109         @Test
110         public void testGetAuthorizationIdWithException() {
111                 try {
112                 sslServer.getAuthorizationID();
113                 }
114                 catch (IllegalStateException ise) {
115                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
116                 }
117         }
118
119         @Test
120         public void testGetNegotiatedPropertyWithException() {
121                 try {
122                 sslServer.getNegotiatedProperty("test");
123                 }
124                 catch (IllegalStateException ise) {
125                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
126                 }
127         }
128         
129         @Test
130         public void testIsComplete() {
131                 try {
132                 sslServer.getNegotiatedProperty("test");
133                 }
134                 catch (IllegalStateException ise) {
135                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
136                 }
137                 assert(true);
138         }       
139
140         @Test
141         public void testUnwrap() {
142                 try {
143                 sslServer.unwrap(new byte[1], 0, 0);
144                 }
145                 catch (IllegalStateException ise) {
146                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
147                 } catch (SaslAuthenticationException e) {
148                         e.printStackTrace();
149                 }
150                 assert(true);
151         }       
152         
153         @Test
154         public void testWrap() {
155                 try {
156                 sslServer.wrap(new byte[1], 0, 0);
157                 }
158                 catch (IllegalStateException ise) {
159                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
160                 } catch (SaslAuthenticationException e) {
161                         e.printStackTrace();
162                 }
163                 assert(true);
164         }
165
166         @Test
167         public void testGetMech() {
168                 assertEquals("PLAIN", sslServer.getMechanismName());
169         }
170
171         @Test
172         public void testIsCompleteBool() {
173                 assertFalse(sslServer.isComplete());
174         }
175
176         @Test
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));
183         }
184 }