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