authorization check for more Kafka operations
[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.security.JaasContext;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
35 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41 @RunWith(PowerMockRunner.class)
42 @PowerMockIgnore("javax.security.auth.*")
43 @PrepareForTest({ AuthorizationProviderFactory.class })
44 public class PlainSaslServer1Test {
45
46         PlainSaslServer1 sslServer = new PlainSaslServer1();
47         @Mock
48         JaasContext jaasContext;
49         @Mock
50         AuthorizationProviderFactory factory;
51         @Mock
52         AuthorizationProvider provider;
53
54         @Before
55         public void setUp() throws Exception {
56
57                 MockitoAnnotations.initMocks(this);
58                 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
59                 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
60                 PowerMockito.when(factory.getProvider()).thenReturn(provider);
61         }
62
63         @Test
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 (SaslException e) {
88                         assertTrue(e.getMessage().equalsIgnoreCase("Authentication failed: username not specified"));
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 (SaslException e) {
99                         assertTrue(e.getMessage().equalsIgnoreCase("Invalid SASL/PLAIN response: expected 3 tokens, got 2"));
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 (SaslException 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 (SaslException e) {
159                         e.printStackTrace();
160                 }
161                 assert(true);
162         }       
163 }