|
@@ -13,32 +13,40 @@
|
|
|
*/
|
|
|
package org.apache.hadoop.security.authentication.server;
|
|
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertNull;
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
+import static org.junit.Assert.fail;
|
|
|
+
|
|
|
+import java.util.Properties;
|
|
|
+import java.util.concurrent.Callable;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.hadoop.security.authentication.KerberosTestUtils;
|
|
|
import org.apache.hadoop.security.authentication.client.AuthenticationException;
|
|
|
import org.apache.hadoop.security.authentication.client.KerberosAuthenticator;
|
|
|
-import junit.framework.TestCase;
|
|
|
-import org.apache.commons.codec.binary.Base64;
|
|
|
import org.ietf.jgss.GSSContext;
|
|
|
import org.ietf.jgss.GSSManager;
|
|
|
import org.ietf.jgss.GSSName;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Ignore;
|
|
|
+import org.junit.Test;
|
|
|
import org.mockito.Mockito;
|
|
|
-import sun.security.jgss.GSSUtil;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.util.Properties;
|
|
|
-import java.util.concurrent.Callable;
|
|
|
+import sun.security.jgss.GSSUtil;
|
|
|
|
|
|
//Disabled because kerberos setup and valid keytabs are required.
|
|
|
@Ignore("requires kerberos setup")
|
|
|
-public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
+public class TestKerberosAuthenticationHandler {
|
|
|
|
|
|
private KerberosAuthenticationHandler handler;
|
|
|
|
|
|
- @Override
|
|
|
- protected void setUp() throws Exception {
|
|
|
- super.setUp();
|
|
|
+ @Before
|
|
|
+ public void setUp() throws Exception {
|
|
|
handler = new KerberosAuthenticationHandler();
|
|
|
Properties props = new Properties();
|
|
|
props.setProperty(KerberosAuthenticationHandler.PRINCIPAL, KerberosTestUtils.getServerPrincipal());
|
|
@@ -53,25 +61,27 @@ public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- protected void tearDown() throws Exception {
|
|
|
+ @After
|
|
|
+ public void tearDown() throws Exception {
|
|
|
if (handler != null) {
|
|
|
handler.destroy();
|
|
|
handler = null;
|
|
|
}
|
|
|
- super.tearDown();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testInit() throws Exception {
|
|
|
assertEquals(KerberosTestUtils.getServerPrincipal(), handler.getPrincipal());
|
|
|
assertEquals(KerberosTestUtils.getKeytabFile(), handler.getKeytab());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testType() throws Exception {
|
|
|
KerberosAuthenticationHandler handler = new KerberosAuthenticationHandler();
|
|
|
assertEquals(KerberosAuthenticationHandler.TYPE, handler.getType());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testRequestWithoutAuthorization() throws Exception {
|
|
|
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
|
|
|
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
|
|
@@ -80,7 +90,8 @@ public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
Mockito.verify(response).setHeader(KerberosAuthenticator.WWW_AUTHENTICATE, KerberosAuthenticator.NEGOTIATE);
|
|
|
Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testRequestWithInvalidAuthorization() throws Exception {
|
|
|
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
|
|
|
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
|
|
@@ -90,7 +101,8 @@ public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
Mockito.verify(response).setHeader(KerberosAuthenticator.WWW_AUTHENTICATE, KerberosAuthenticator.NEGOTIATE);
|
|
|
Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testRequestWithIncompleteAuthorization() throws Exception {
|
|
|
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
|
|
|
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
|
|
@@ -107,7 +119,7 @@ public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ @Test
|
|
|
public void testRequestWithAuthorization() throws Exception {
|
|
|
String token = KerberosTestUtils.doAsClient(new Callable<String>() {
|
|
|
@Override
|
|
@@ -157,7 +169,8 @@ public class TestKerberosAuthenticationHandler extends TestCase {
|
|
|
Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testRequestWithInvalidKerberosAuthorization() throws Exception {
|
|
|
|
|
|
String token = new Base64(0).encodeToString(new byte[]{0, 1, 2});
|