4f26527c88507a1dff2f079920409971debd6eaf
[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.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import javax.security.sasl.SaslException;
27
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;
41
42 @RunWith(PowerMockRunner.class)
43 @PowerMockIgnore("javax.security.auth.*")
44 @PrepareForTest({ AuthorizationProviderFactory.class })
45 public class PlainSaslServer1Test {
46
47         PlainSaslServer1 sslServer = new PlainSaslServer1();
48         @Mock
49         JaasContext jaasContext;
50         @Mock
51         AuthorizationProviderFactory factory;
52         @Mock
53         AuthorizationProvider provider;
54
55         @Before
56         public void setUp() throws Exception {
57
58                 MockitoAnnotations.initMocks(this);
59                 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
60                 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
61                 PowerMockito.when(factory.getProvider()).thenReturn(provider);
62         }
63
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()));
68
69         }
70
71         @Test
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());
76                 assert(true);
77         }
78
79         @Test
80         public void testAuthenticationEmptyUser() throws Exception {
81                 String response = "authorizationID\u0000\u0000password";
82                 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
83                 
84                 try {
85                         sslServer.evaluateResponse(response.getBytes());
86                 }
87                 catch (SaslAuthenticationException e) {
88                         assertNotNull(e);
89                 }
90         }
91         @Test
92         public void testAuthenticationEmptyPassword() throws Exception {
93                 String response = "authorizationID\u0000username\u0000";
94                 PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
95                 try {
96                         sslServer.evaluateResponse(response.getBytes());
97                 }
98                 catch (SaslAuthenticationException e) {
99                         assertNotNull(e);
100                 }
101         }
102         
103         @Test
104         public void testGetAuthorizationIdWithException() {
105                 
106                 try {
107                 sslServer.getAuthorizationID();
108                 }
109                 catch (IllegalStateException ise) {
110                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
111                 }
112         }
113
114         @Test
115         public void testGetNegotiatedPropertyWithException() {
116                 
117                 try {
118                 sslServer.getNegotiatedProperty("test");
119                 }
120                 catch (IllegalStateException ise) {
121                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
122                 }
123         }
124         
125         @Test
126         public void testIsComplete() {
127                 
128                 try {
129                 sslServer.getNegotiatedProperty("test");
130                 }
131                 catch (IllegalStateException ise) {
132                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
133                 }
134                 assert(true);
135         }       
136
137         
138         @Test
139         public void testUnwrap() {
140                 try {
141                 sslServer.unwrap(new byte[1], 0, 0);
142                 }
143                 catch (IllegalStateException ise) {
144                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
145                 } catch (SaslAuthenticationException e) {
146                         e.printStackTrace();
147                 }
148                 assert(true);
149         }       
150         
151         @Test
152         public void testWrap() {
153                 try {
154                 sslServer.wrap(new byte[1], 0, 0);
155                 }
156                 catch (IllegalStateException ise) {
157                         assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
158                 } catch (SaslAuthenticationException e) {
159                         e.printStackTrace();
160                 }
161                 assert(true);
162         }       
163 }