Upgrading tests.
This commit is contained in:
@@ -20,9 +20,9 @@ import javax.security.auth.callback.Callback;
|
||||
import javax.security.auth.callback.CallbackHandler;
|
||||
import javax.security.auth.callback.UnsupportedCallbackException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CallbackHandlerChainTest extends TestCase {
|
||||
public class CallbackHandlerChainTest {
|
||||
|
||||
private CallbackHandler supported = new CallbackHandler() {
|
||||
public void handle(Callback[] callbacks) {
|
||||
@@ -38,28 +38,21 @@ public class CallbackHandlerChainTest extends TestCase {
|
||||
private Callback callback = new Callback() {
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupported() throws Exception {
|
||||
CallbackHandlerChain chain = new CallbackHandlerChain(new CallbackHandler[]{supported});
|
||||
chain.handle(new Callback[]{callback});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnsupportedSupported() throws Exception {
|
||||
CallbackHandlerChain chain = new CallbackHandlerChain(new CallbackHandler[]{unsupported, supported});
|
||||
chain.handle(new Callback[]{callback});
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedCallbackException.class)
|
||||
public void testUnsupported() throws Exception {
|
||||
CallbackHandlerChain chain = new CallbackHandlerChain(new CallbackHandler[]{unsupported});
|
||||
try {
|
||||
chain.handle(new Callback[]{callback});
|
||||
fail("Expected UnsupportedCallbackException");
|
||||
}
|
||||
catch (UnsupportedCallbackException ex) {
|
||||
// expected behavior
|
||||
}
|
||||
chain.handle(new Callback[]{callback});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,11 +35,16 @@ import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SaajWss4jMessageInterceptorSignTest extends Wss4jMessageInterceptorSignTestCase {
|
||||
|
||||
private static final String PAYLOAD =
|
||||
"<tru:StockSymbol xmlns:tru=\"http://fabrikam123.com/payloads\">QQQ</tru:StockSymbol>";
|
||||
|
||||
@Test
|
||||
public void testSignAndValidate() throws Exception {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
interceptor.setSecurementActions("Signature");
|
||||
@@ -54,7 +59,7 @@ public class SaajWss4jMessageInterceptorSignTest extends Wss4jMessageInterceptor
|
||||
interceptor.secureMessage(message, messageContext);
|
||||
|
||||
SOAPHeader header = ((SaajSoapMessage) message).getSaajMessage().getSOAPHeader();
|
||||
Iterator iterator = header.getChildElements(new QName(
|
||||
Iterator<?> iterator = header.getChildElements(new QName(
|
||||
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"));
|
||||
assertTrue("No security header", iterator.hasNext());
|
||||
SOAPHeaderElement securityHeader = (SOAPHeaderElement) iterator.next();
|
||||
|
||||
@@ -22,9 +22,15 @@ import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.WsSecuritySecurementException;
|
||||
import org.springframework.ws.soap.security.WsSecurityValidationException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public abstract class Wss4jInterceptorTestCase extends Wss4jTestCase {
|
||||
|
||||
public void testhandleRequest() throws Exception {
|
||||
@Test
|
||||
public void testHandleRequest() throws Exception {
|
||||
SoapMessage request = loadSoap11Message("empty-soap.xml");
|
||||
final Object requestMessage = getMessage(request);
|
||||
SoapMessage validatedRequest = loadSoap11Message("empty-soap.xml");
|
||||
@@ -48,7 +54,8 @@ public abstract class Wss4jInterceptorTestCase extends Wss4jTestCase {
|
||||
assertEquals("Invalid request", validatedRequestMessage, getMessage((SoapMessage) context.getRequest()));
|
||||
}
|
||||
|
||||
public void testhandleResponse() throws Exception {
|
||||
@Test
|
||||
public void testHandleResponse() throws Exception {
|
||||
SoapMessage securedResponse = loadSoap11Message("empty-soap.xml");
|
||||
final Object securedResponseMessage = getMessage(securedResponse);
|
||||
|
||||
|
||||
@@ -18,15 +18,16 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ws.security.components.crypto.Crypto;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.KeyStoreCallbackHandler;
|
||||
import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
|
||||
|
||||
import org.apache.ws.security.components.crypto.Crypto;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorEncryptionTestCase extends Wss4jTestCase {
|
||||
|
||||
protected Wss4jSecurityInterceptor interceptor;
|
||||
@@ -61,6 +62,7 @@ public abstract class Wss4jMessageInterceptorEncryptionTestCase extends Wss4jTes
|
||||
interceptor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptRequest() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("encrypted-soap.xml");
|
||||
MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
|
||||
@@ -72,6 +74,7 @@ public abstract class Wss4jMessageInterceptorEncryptionTestCase extends Wss4jTes
|
||||
getDocument(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptResponse() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("empty-soap.xml");
|
||||
MessageContext messageContext = getSoap11MessageContext(message);
|
||||
|
||||
@@ -28,6 +28,11 @@ import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.WsSecurityValidationException;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCase {
|
||||
|
||||
private Wss4jSecurityInterceptor interceptor;
|
||||
@@ -46,6 +51,7 @@ public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCas
|
||||
interceptor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenPlainText() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("usernameTokenPlainTextWithHeaders-soap.xml");
|
||||
MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
|
||||
@@ -53,8 +59,8 @@ public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCas
|
||||
Object result = getMessage(message);
|
||||
assertNotNull("No result returned", result);
|
||||
|
||||
for (Iterator i = message.getEnvelope().getHeader().examineAllHeaderElements(); i.hasNext();) {
|
||||
SoapHeaderElement element = (SoapHeaderElement) i.next();
|
||||
for (Iterator<SoapHeaderElement> i = message.getEnvelope().getHeader().examineAllHeaderElements(); i.hasNext();) {
|
||||
SoapHeaderElement element = i.next();
|
||||
QName name = element.getName();
|
||||
if (name.getNamespaceURI()
|
||||
.equals("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")) {
|
||||
@@ -70,6 +76,7 @@ public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCas
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptySecurityHeader() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("emptySecurityHeader-soap.xml");
|
||||
MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
|
||||
@@ -82,6 +89,7 @@ public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCas
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreserveCustomHeaders() throws Exception {
|
||||
interceptor.setSecurementActions("UsernameToken");
|
||||
interceptor.setSecurementUsername("Bert");
|
||||
|
||||
@@ -18,15 +18,17 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ws.security.components.crypto.Crypto;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorSignTestCase extends Wss4jTestCase {
|
||||
|
||||
protected Wss4jSecurityInterceptor interceptor;
|
||||
@@ -47,14 +49,15 @@ public abstract class Wss4jMessageInterceptorSignTestCase extends Wss4jTestCase
|
||||
cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.file", "private.jks");
|
||||
cryptoFactoryBean.setConfiguration(cryptoFactoryBeanConfig);
|
||||
cryptoFactoryBean.afterPropertiesSet();
|
||||
interceptor.setValidationSignatureCrypto((Crypto) cryptoFactoryBean
|
||||
interceptor.setValidationSignatureCrypto(cryptoFactoryBean
|
||||
.getObject());
|
||||
interceptor.setSecurementSignatureCrypto((Crypto) cryptoFactoryBean
|
||||
interceptor.setSecurementSignatureCrypto(cryptoFactoryBean
|
||||
.getObject());
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCertificate() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("signed-soap.xml");
|
||||
|
||||
@@ -66,6 +69,7 @@ public abstract class Wss4jMessageInterceptorSignTestCase extends Wss4jTestCase
|
||||
getDocument(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCertificateWithSignatureConfirmation() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("signed-soap.xml");
|
||||
MessageContext messageContext = getSoap11MessageContext(message);
|
||||
@@ -79,6 +83,7 @@ public abstract class Wss4jMessageInterceptorSignTestCase extends Wss4jTestCase
|
||||
"/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse11:SignatureConfirmation", document);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignResponse() throws Exception {
|
||||
interceptor.setSecurementActions("Signature");
|
||||
interceptor.setEnableSignatureConfirmation(false);
|
||||
@@ -98,6 +103,7 @@ public abstract class Wss4jMessageInterceptorSignTestCase extends Wss4jTestCase
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignResponseWithSignatureUser() throws Exception {
|
||||
interceptor.setSecurementActions("Signature");
|
||||
interceptor.setEnableSignatureConfirmation(false);
|
||||
|
||||
@@ -18,13 +18,17 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTestCase {
|
||||
|
||||
@@ -49,6 +53,7 @@ public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTes
|
||||
interceptor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreserveSoapActionOnValidation() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("usernameTokenPlainText-soap.xml");
|
||||
message.setSoapAction(SOAP_ACTION);
|
||||
@@ -59,6 +64,7 @@ public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTes
|
||||
assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreserveSoap12ActionOnValidation() throws Exception {
|
||||
SoapMessage message = loadSoap12Message("usernameTokenPlainText-soap12.xml");
|
||||
message.setSoapAction(SOAP_ACTION);
|
||||
@@ -70,6 +76,7 @@ public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTes
|
||||
assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreserveSoapActionOnSecurement() throws Exception {
|
||||
SoapMessage message = loadSoap11Message("empty-soap.xml");
|
||||
message.setSoapAction(SOAP_ACTION);
|
||||
@@ -83,6 +90,7 @@ public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTes
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreserveSoap12ActionOnSecurement() throws Exception {
|
||||
SoapMessage message = loadSoap12Message("empty-soap12.xml");
|
||||
message.setSoapAction(SOAP_ACTION);
|
||||
|
||||
@@ -18,9 +18,6 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.AuthenticationManager;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
@@ -35,27 +32,33 @@ import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.SpringDigestPasswordValidationCallbackHandler;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.SpringPlainTextPasswordValidationCallbackHandler;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCase extends Wss4jTestCase {
|
||||
|
||||
private Properties users = new Properties();
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private AuthenticationManager mock;
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@Override
|
||||
protected void onSetup() throws Exception {
|
||||
control = MockControl.createControl(AuthenticationManager.class);
|
||||
mock = (AuthenticationManager) control.getMock();
|
||||
authenticationManager = createMock(AuthenticationManager.class);
|
||||
users.setProperty("Bert", "Ernie,ROLE_TEST");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
control.verify();
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
verify(authenticationManager);
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenPlainText() throws Exception {
|
||||
EndpointInterceptor interceptor = prepareInterceptor("UsernameToken", true, false);
|
||||
SoapMessage message = loadSoap11Message("usernameTokenPlainText-soap.xml");
|
||||
@@ -69,6 +72,7 @@ public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCa
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenDigest() throws Exception {
|
||||
EndpointInterceptor interceptor = prepareInterceptor("UsernameToken", true, true);
|
||||
SoapMessage message = loadSoap11Message("usernameTokenDigest-soap.xml");
|
||||
@@ -114,15 +118,14 @@ public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCa
|
||||
SpringPlainTextPasswordValidationCallbackHandler callbackHandler =
|
||||
new SpringPlainTextPasswordValidationCallbackHandler();
|
||||
Authentication authResult = new TestingAuthenticationToken("Bert", "Ernie", new GrantedAuthority[0]);
|
||||
control.expectAndReturn(mock.authenticate(new UsernamePasswordAuthenticationToken("Bert", "Ernie")),
|
||||
authResult);
|
||||
callbackHandler.setAuthenticationManager(mock);
|
||||
expect(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("Bert", "Ernie"))).andReturn(authResult);
|
||||
callbackHandler.setAuthenticationManager(authenticationManager);
|
||||
callbackHandler.afterPropertiesSet();
|
||||
interceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
|
||||
interceptor.setValidationCallbackHandler(callbackHandler);
|
||||
interceptor.afterPropertiesSet();
|
||||
}
|
||||
control.replay();
|
||||
replay(authenticationManager);
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,15 +19,19 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.WsSecurityValidationException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorTimestampTestCase extends Wss4jTestCase {
|
||||
|
||||
@Test
|
||||
public void testAddTimestamp() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
|
||||
interceptor.setSecurementActions("Timestamp");
|
||||
@@ -40,6 +44,7 @@ public abstract class Wss4jMessageInterceptorTimestampTestCase extends Wss4jTest
|
||||
"/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsu:Timestamp", document);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateTimestamp() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
|
||||
interceptor.setValidationActions("Timestamp");
|
||||
@@ -52,26 +57,23 @@ public abstract class Wss4jMessageInterceptorTimestampTestCase extends Wss4jTest
|
||||
getDocument(message));
|
||||
}
|
||||
|
||||
@Test(expected = WsSecurityValidationException.class)
|
||||
public void testValidateTimestampWithExpiredTtl() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
|
||||
interceptor.setValidationActions("Timestamp");
|
||||
interceptor.afterPropertiesSet();
|
||||
SoapMessage message = loadSoap11Message("expiredTimestamp-soap.xml");
|
||||
MessageContext context = new DefaultMessageContext(message, getSoap11MessageFactory());
|
||||
try {
|
||||
interceptor.validateMessage(message, context);
|
||||
fail();
|
||||
}
|
||||
catch (WsSecurityValidationException e) {
|
||||
// expected
|
||||
}
|
||||
interceptor.validateMessage(message, context);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSecureTimestampWithCustomTtl() throws Exception {
|
||||
int ttlInSeconds = 1;
|
||||
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
|
||||
interceptor.setSecurementActions("Timestamp");
|
||||
interceptor.setTimestampStrict(true);
|
||||
int ttlInSeconds = 1;
|
||||
interceptor.setSecurementTimeToLive(ttlInSeconds);
|
||||
interceptor.afterPropertiesSet();
|
||||
SoapMessage message = loadSoap11Message("empty-soap.xml");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,13 +16,15 @@
|
||||
|
||||
package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorUsernameTokenSignatureTestCase extends Wss4jTestCase {
|
||||
|
||||
@Test
|
||||
public void testAddUsernameTokenSignature() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
|
||||
interceptor.setSecurementActions("UsernameTokenSignature");
|
||||
|
||||
@@ -18,14 +18,17 @@ package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler;
|
||||
|
||||
import org.apache.ws.security.WSConstants;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4jTestCase {
|
||||
|
||||
private Properties users = new Properties();
|
||||
@@ -35,6 +38,7 @@ public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4j
|
||||
users.setProperty("Bert", "Ernie");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenPlainText() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = prepareInterceptor("UsernameToken", true, false);
|
||||
SoapMessage message = loadSoap11Message("usernameTokenPlainText-soap.xml");
|
||||
@@ -43,6 +47,7 @@ public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4j
|
||||
assertValidateUsernameToken(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenDigest() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = prepareInterceptor("UsernameToken", true, true);
|
||||
SoapMessage message = loadSoap11Message("usernameTokenDigest-soap.xml");
|
||||
@@ -51,6 +56,7 @@ public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4j
|
||||
assertValidateUsernameToken(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateUsernameTokenWithQualifiedType() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = prepareInterceptor("UsernameToken", true, false);
|
||||
SoapMessage message = loadSoap11Message("usernameTokenPlainTextQualifiedType-soap.xml");
|
||||
@@ -59,6 +65,7 @@ public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4j
|
||||
assertValidateUsernameToken(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddUsernameTokenPlainText() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = prepareInterceptor("UsernameToken", false, false);
|
||||
interceptor.setSecurementUsername("Bert");
|
||||
@@ -71,6 +78,7 @@ public abstract class Wss4jMessageInterceptorUsernameTokenTestCase extends Wss4j
|
||||
assertAddUsernameTokenPlainText(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddUsernameTokenDigest() throws Exception {
|
||||
Wss4jSecurityInterceptor interceptor = prepareInterceptor("UsernameToken", false, true);
|
||||
interceptor.setSecurementUsername("Bert");
|
||||
|
||||
@@ -16,15 +16,16 @@
|
||||
|
||||
package org.springframework.ws.soap.security.wss4j;
|
||||
|
||||
import org.apache.ws.security.components.crypto.Crypto;
|
||||
import org.apache.ws.security.components.crypto.Merlin;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
|
||||
|
||||
import org.apache.ws.security.components.crypto.Crypto;
|
||||
import org.apache.ws.security.components.crypto.Merlin;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public abstract class Wss4jMessageInterceptorX509TestCase extends Wss4jTestCase {
|
||||
|
||||
protected Wss4jSecurityInterceptor interceptor;
|
||||
@@ -49,6 +50,7 @@ public abstract class Wss4jMessageInterceptorX509TestCase extends Wss4jTestCase
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCertificate() throws Exception {
|
||||
|
||||
interceptor.setSecurementPassword("123456");
|
||||
@@ -64,7 +66,7 @@ public abstract class Wss4jMessageInterceptorX509TestCase extends Wss4jTestCase
|
||||
assertXpathExists("Absent BinarySecurityToken element",
|
||||
"/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse:BinarySecurityToken", document);
|
||||
|
||||
// lets verfiy the signature that we've just generated
|
||||
// lets verify the signature that we've just generated
|
||||
interceptor.validateMessage(message, messageContext);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,15 +43,19 @@ import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.springframework.xml.xpath.Jaxp13XPathTemplate;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.axiom.soap.SOAP12Constants;
|
||||
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public abstract class Wss4jTestCase extends TestCase {
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public abstract class Wss4jTestCase {
|
||||
|
||||
protected MessageFactory saajSoap11MessageFactory;
|
||||
|
||||
protected MessageFactory saajSoap12MessageFactory;
|
||||
|
||||
protected final boolean axiomTest = this.getClass().getSimpleName().startsWith("Axiom");
|
||||
@@ -60,10 +64,10 @@ public abstract class Wss4jTestCase extends TestCase {
|
||||
|
||||
protected Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();
|
||||
|
||||
@Override
|
||||
protected final void setUp() throws Exception {
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
if (!axiomTest && !saajTest) {
|
||||
throw new IllegalArgumentException("test class name must statrt with either Axiom or Saaj");
|
||||
throw new IllegalArgumentException("test class name must start with either Axiom or Saaj");
|
||||
}
|
||||
saajSoap11MessageFactory = MessageFactory.newInstance();
|
||||
saajSoap12MessageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
|
||||
@@ -87,7 +91,7 @@ public abstract class Wss4jTestCase extends TestCase {
|
||||
String xpathExpression,
|
||||
Document document) {
|
||||
String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new DOMSource(document));
|
||||
assertEquals(message, expectedValue, actualValue);
|
||||
Assert.assertEquals(message, expectedValue, actualValue);
|
||||
}
|
||||
|
||||
protected void assertXpathEvaluatesTo(String message,
|
||||
@@ -95,22 +99,22 @@ public abstract class Wss4jTestCase extends TestCase {
|
||||
String xpathExpression,
|
||||
String document) {
|
||||
String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new StringSource(document));
|
||||
assertEquals(message, expectedValue, actualValue);
|
||||
Assert.assertEquals(message, expectedValue, actualValue);
|
||||
}
|
||||
|
||||
protected void assertXpathExists(String message, String xpathExpression, Document document) {
|
||||
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
|
||||
assertNotNull(message, node);
|
||||
Assert.assertNotNull(message, node);
|
||||
}
|
||||
|
||||
protected void assertXpathNotExists(String message, String xpathExpression, Document document) {
|
||||
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
|
||||
assertNull(message, node);
|
||||
Assert.assertNull(message, node);
|
||||
}
|
||||
|
||||
protected void assertXpathNotExists(String message, String xpathExpression, String document) {
|
||||
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new StringSource(document));
|
||||
assertNull(message, node);
|
||||
Assert.assertNull(message, node);
|
||||
}
|
||||
|
||||
protected SaajSoapMessage loadSaaj11Message(String fileName) throws Exception {
|
||||
@@ -160,6 +164,7 @@ public abstract class Wss4jTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("Since15")
|
||||
protected AxiomSoapMessage loadAxiom12Message(String fileName) throws Exception {
|
||||
Resource resource = new ClassPathResource(fileName, getClass());
|
||||
InputStream is = resource.getInputStream();
|
||||
|
||||
@@ -18,20 +18,22 @@ package org.springframework.ws.soap.security.wss4j.callback;
|
||||
|
||||
import java.security.KeyStore;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.ws.security.WSPasswordCallback;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.ws.soap.security.support.KeyStoreFactoryBean;
|
||||
|
||||
public class KeyStoreCallbackHandlerTest extends TestCase {
|
||||
import org.apache.ws.security.WSPasswordCallback;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class KeyStoreCallbackHandlerTest {
|
||||
|
||||
private KeyStoreCallbackHandler callbackHandler;
|
||||
|
||||
private WSPasswordCallback callback;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
callbackHandler = new KeyStoreCallbackHandler();
|
||||
callback = new WSPasswordCallback("secretkey", WSPasswordCallback.KEY_NAME);
|
||||
|
||||
@@ -45,9 +47,10 @@ public class KeyStoreCallbackHandlerTest extends TestCase {
|
||||
callbackHandler.setSymmetricKeyPassword("123456");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleKeyName() throws Exception {
|
||||
callbackHandler.handleInternal(callback);
|
||||
assertNotNull("symmetric key must not be null", callback.getKey());
|
||||
Assert.assertNotNull("symmetric key must not be null", callback.getKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.ws.soap.security.wss4j.callback;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.ws.security.WSUsernameTokenPrincipal;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
@@ -29,50 +25,54 @@ import org.springframework.security.userdetails.User;
|
||||
import org.springframework.security.userdetails.UserDetails;
|
||||
import org.springframework.security.userdetails.UserDetailsService;
|
||||
|
||||
import org.apache.ws.security.WSUsernameTokenPrincipal;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
/** @author tareq */
|
||||
public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase {
|
||||
public class SpringDigestPasswordValidationCallbackHandlerTest {
|
||||
|
||||
private SpringDigestPasswordValidationCallbackHandler callbackHandler;
|
||||
|
||||
private GrantedAuthorityImpl grantedAuthority;
|
||||
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private WSUsernameTokenPrincipal principal;
|
||||
|
||||
private UsernameTokenPrincipalCallback callback;
|
||||
|
||||
private UserDetails user;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
callbackHandler = new SpringDigestPasswordValidationCallbackHandler();
|
||||
|
||||
grantedAuthority = new GrantedAuthorityImpl("ROLE_1");
|
||||
user = new User("Ernie", "Bert", true, true, true, true, new GrantedAuthority[]{grantedAuthority});
|
||||
|
||||
control = MockControl.createControl(UserDetailsService.class);
|
||||
userDetailsService = (UserDetailsService) control.getMock();
|
||||
userDetailsService.loadUserByUsername("Ernie");
|
||||
control.setDefaultReturnValue(user);
|
||||
control.replay();
|
||||
WSUsernameTokenPrincipal principal = new WSUsernameTokenPrincipal("Ernie", true);
|
||||
callback = new UsernameTokenPrincipalCallback(principal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleUsernameTokenPrincipal() throws Exception {
|
||||
UserDetailsService userDetailsService = createMock(UserDetailsService.class);
|
||||
callbackHandler.setUserDetailsService(userDetailsService);
|
||||
|
||||
principal = new WSUsernameTokenPrincipal("Ernie", true);
|
||||
callback = new UsernameTokenPrincipalCallback(principal);
|
||||
expect(userDetailsService.loadUserByUsername("Ernie")).andReturn(user).anyTimes();
|
||||
|
||||
}
|
||||
replay(userDetailsService);
|
||||
|
||||
public void testHandleUsernameTokenPrincipal() throws Exception {
|
||||
callbackHandler.handleUsernameTokenPrincipal(callback);
|
||||
SecurityContext context = SecurityContextHolder.getContext();
|
||||
assertNotNull("SecurityContext must not be null", context);
|
||||
Assert.assertNotNull("SecurityContext must not be null", context);
|
||||
Authentication authentication = context.getAuthentication();
|
||||
assertNotNull("Authentication must not be null", authentication);
|
||||
Assert.assertNotNull("Authentication must not be null", authentication);
|
||||
GrantedAuthority[] authorities = authentication.getAuthorities();
|
||||
assertTrue("GrantedAuthority[] must not be null or empty", (authorities != null && authorities.length > 0));
|
||||
assertEquals("Unexpected authority", grantedAuthority, authorities[0]);
|
||||
Assert.assertTrue("GrantedAuthority[] must not be null or empty",
|
||||
(authorities != null && authorities.length > 0));
|
||||
Assert.assertEquals("Unexpected authority", grantedAuthority, authorities[0]);
|
||||
|
||||
verify(userDetailsService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,21 +18,24 @@ package org.springframework.ws.soap.security.wss4j.support;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.ws.security.components.crypto.Merlin;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class CryptoFactoryBeanTest extends TestCase {
|
||||
import org.apache.ws.security.components.crypto.Merlin;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CryptoFactoryBeanTest {
|
||||
|
||||
private CryptoFactoryBean factoryBean;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
factoryBean = new CryptoFactoryBean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetConfiguration() throws Exception {
|
||||
Properties configuration = new Properties();
|
||||
configuration.setProperty("org.apache.ws.security.crypto.provider",
|
||||
@@ -46,10 +49,11 @@ public class CryptoFactoryBeanTest extends TestCase {
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
Object result = factoryBean.getObject();
|
||||
assertNotNull("No result", result);
|
||||
assertTrue("Not a Merlin instance", result instanceof Merlin);
|
||||
Assert.assertNotNull("No result", result);
|
||||
Assert.assertTrue("Not a Merlin instance", result instanceof Merlin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProperties() throws Exception {
|
||||
factoryBean.setKeyStoreType("jceks");
|
||||
factoryBean.setKeyStorePassword("123456");
|
||||
@@ -57,7 +61,7 @@ public class CryptoFactoryBeanTest extends TestCase {
|
||||
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
|
||||
factoryBean.afterPropertiesSet();
|
||||
Object result = factoryBean.getObject();
|
||||
assertNotNull("No result", result);
|
||||
assertTrue("Not a Merlin instance", result instanceof Merlin);
|
||||
Assert.assertNotNull("No result", result);
|
||||
Assert.assertTrue("Not a Merlin instance", result instanceof Merlin);
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,6 @@ package org.springframework.ws.soap.security.xwss;
|
||||
import javax.xml.soap.MessageFactory;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
@@ -28,16 +26,21 @@ import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
|
||||
import org.springframework.ws.soap.security.WsSecurityValidationException;
|
||||
|
||||
public class XwsSecurityInterceptorTest extends TestCase {
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XwsSecurityInterceptorTest {
|
||||
|
||||
private MessageFactory messageFactory;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
messageFactory = MessageFactory.newInstance();
|
||||
}
|
||||
|
||||
public void testhandleServerRequest() throws Exception {
|
||||
@Test
|
||||
public void testHandleServerRequest() throws Exception {
|
||||
final SOAPMessage request = messageFactory.createMessage();
|
||||
final SOAPMessage validatedRequest = messageFactory.createMessage();
|
||||
XwsSecurityInterceptor interceptor = new XwsSecurityInterceptor() {
|
||||
@@ -45,14 +48,14 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
@Override
|
||||
protected void secureMessage(SoapMessage soapMessage, MessageContext messageContext)
|
||||
throws XwsSecuritySecurementException {
|
||||
fail("secure not expected");
|
||||
Assert.fail("secure not expected");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validateMessage(SoapMessage message, MessageContext messageContext)
|
||||
throws WsSecurityValidationException {
|
||||
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
|
||||
assertEquals("Invalid message", request, saajSoapMessage.getSaajMessage());
|
||||
Assert.assertEquals("Invalid message", request, saajSoapMessage.getSaajMessage());
|
||||
saajSoapMessage.setSaajMessage(validatedRequest);
|
||||
}
|
||||
|
||||
@@ -60,10 +63,12 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
MessageContext context =
|
||||
new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
|
||||
interceptor.handleRequest(context, null);
|
||||
assertEquals("Invalid request", validatedRequest, ((SaajSoapMessage) context.getRequest()).getSaajMessage());
|
||||
Assert.assertEquals("Invalid request", validatedRequest,
|
||||
((SaajSoapMessage) context.getRequest()).getSaajMessage());
|
||||
}
|
||||
|
||||
public void testhandleServerResponse() throws Exception {
|
||||
@Test
|
||||
public void testHandleServerResponse() throws Exception {
|
||||
final SOAPMessage securedResponse = messageFactory.createMessage();
|
||||
XwsSecurityInterceptor interceptor = new XwsSecurityInterceptor() {
|
||||
|
||||
@@ -77,7 +82,7 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
@Override
|
||||
protected void validateMessage(SoapMessage soapMessage, MessageContext messageContext)
|
||||
throws WsSecurityValidationException {
|
||||
fail("validate not expected");
|
||||
Assert.fail("validate not expected");
|
||||
}
|
||||
|
||||
};
|
||||
@@ -86,9 +91,11 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
|
||||
context.getResponse();
|
||||
interceptor.handleResponse(context, null);
|
||||
assertEquals("Invalid response", securedResponse, ((SaajSoapMessage) context.getResponse()).getSaajMessage());
|
||||
Assert.assertEquals("Invalid response", securedResponse,
|
||||
((SaajSoapMessage) context.getResponse()).getSaajMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testhandleClientRequest() throws Exception {
|
||||
final SOAPMessage request = messageFactory.createMessage();
|
||||
final SOAPMessage securedRequest = messageFactory.createMessage();
|
||||
@@ -98,31 +105,33 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
protected void secureMessage(SoapMessage soapMessage, MessageContext messageContext)
|
||||
throws XwsSecuritySecurementException {
|
||||
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) soapMessage;
|
||||
assertEquals("Invalid message", request, saajSoapMessage.getSaajMessage());
|
||||
Assert.assertEquals("Invalid message", request, saajSoapMessage.getSaajMessage());
|
||||
saajSoapMessage.setSaajMessage(securedRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validateMessage(SoapMessage message, MessageContext messageContext)
|
||||
throws WsSecurityValidationException {
|
||||
fail("validate not expected");
|
||||
Assert.fail("validate not expected");
|
||||
}
|
||||
|
||||
};
|
||||
MessageContext context =
|
||||
new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
|
||||
interceptor.handleRequest(context);
|
||||
assertEquals("Invalid request", securedRequest, ((SaajSoapMessage) context.getRequest()).getSaajMessage());
|
||||
Assert.assertEquals("Invalid request", securedRequest,
|
||||
((SaajSoapMessage) context.getRequest()).getSaajMessage());
|
||||
}
|
||||
|
||||
public void testhandleClientResponse() throws Exception {
|
||||
@Test
|
||||
public void testHandleClientResponse() throws Exception {
|
||||
final SOAPMessage validatedResponse = messageFactory.createMessage();
|
||||
XwsSecurityInterceptor interceptor = new XwsSecurityInterceptor() {
|
||||
|
||||
@Override
|
||||
protected void secureMessage(SoapMessage message, MessageContext messageContext)
|
||||
throws XwsSecuritySecurementException {
|
||||
fail("secure not expected");
|
||||
Assert.fail("secure not expected");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,7 +147,8 @@ public class XwsSecurityInterceptorTest extends TestCase {
|
||||
new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
|
||||
context.getResponse();
|
||||
interceptor.handleResponse(context);
|
||||
assertEquals("Invalid response", validatedResponse, ((SaajSoapMessage) context.getResponse()).getSaajMessage());
|
||||
Assert.assertEquals("Invalid response", validatedResponse,
|
||||
((SaajSoapMessage) context.getResponse()).getSaajMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,23 +17,26 @@
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DefaultTimestampValidatorTest extends TestCase {
|
||||
public class DefaultTimestampValidatorTest {
|
||||
|
||||
private DefaultTimestampValidator validator;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
validator = new DefaultTimestampValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate() throws Exception {
|
||||
TimestampValidationCallback.Request request = new TimestampValidationCallback.UTCTimestampRequest(
|
||||
"2006-09-25T20:42:50Z", "2107-09-25T20:42:50Z", 100, Long.MAX_VALUE);
|
||||
validator.validate(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateNoExpired() throws Exception {
|
||||
TimestampValidationCallback.Request request =
|
||||
new TimestampValidationCallback.UTCTimestampRequest("2006-09-25T20:42:50Z", null, 100, Long.MAX_VALUE);
|
||||
|
||||
@@ -16,17 +16,19 @@
|
||||
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class KeyStoreCallbackHandlerTest extends TestCase {
|
||||
public class KeyStoreCallbackHandlerTest {
|
||||
|
||||
private KeyStoreCallbackHandler handler;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
handler = new KeyStoreCallbackHandler();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadDefaultTrustStore() throws Exception {
|
||||
System.setProperty("javax.net.ssl.trustStore",
|
||||
"/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/");
|
||||
|
||||
@@ -16,50 +16,56 @@
|
||||
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class SimplePasswordValidationCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimplePasswordValidationCallbackHandlerTest {
|
||||
|
||||
private SimplePasswordValidationCallbackHandler handler;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
handler = new SimplePasswordValidationCallbackHandler();
|
||||
Properties users = new Properties();
|
||||
users.setProperty("Bert", "Ernie");
|
||||
handler.setUsers(users);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlainTextPasswordValid() throws Exception {
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Ernie");
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
handler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlainTextPasswordInvalid() throws Exception {
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Big bird");
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
handler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlainTextPasswordNoSuchUser() throws Exception {
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
new PasswordValidationCallback.PlainTextPasswordRequest("Big bird", "Bert");
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
handler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigestPasswordValid() throws Exception {
|
||||
String username = "Bert";
|
||||
String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
|
||||
@@ -70,10 +76,11 @@ public class SimplePasswordValidationCallbackHandlerTest extends TestCase {
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
handler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Authenticated", authenticated);
|
||||
Assert.assertTrue("Authenticated", authenticated);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigestPasswordInvalid() throws Exception {
|
||||
String username = "Bert";
|
||||
String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
|
||||
@@ -84,7 +91,7 @@ public class SimplePasswordValidationCallbackHandlerTest extends TestCase {
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
handler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -18,28 +18,32 @@ package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordCallback;
|
||||
import com.sun.xml.wss.impl.callback.UsernameCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleUsernamePasswordCallbackHandlerTest extends TestCase {
|
||||
public class SimpleUsernamePasswordCallbackHandlerTest {
|
||||
|
||||
private SimpleUsernamePasswordCallbackHandler handler;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
handler = new SimpleUsernamePasswordCallbackHandler();
|
||||
handler.setUsername("Bert");
|
||||
handler.setPassword("Ernie");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsernameCallback() throws Exception {
|
||||
UsernameCallback usernameCallback = new UsernameCallback();
|
||||
handler.handleInternal(usernameCallback);
|
||||
assertEquals("Invalid username", "Bert", usernameCallback.getUsername());
|
||||
Assert.assertEquals("Invalid username", "Bert", usernameCallback.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordCallback() throws Exception {
|
||||
PasswordCallback passwordCallback = new PasswordCallback();
|
||||
handler.handleInternal(passwordCallback);
|
||||
assertEquals("Invalid username", "Ernie", passwordCallback.getPassword());
|
||||
Assert.assertEquals("Invalid username", "Ernie", passwordCallback.getPassword());
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,6 @@ import java.io.InputStream;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.security.AuthenticationManager;
|
||||
import org.springframework.security.BadCredentialsException;
|
||||
@@ -33,24 +29,29 @@ import org.springframework.security.providers.TestingAuthenticationToken;
|
||||
import org.springframework.security.providers.x509.X509AuthenticationToken;
|
||||
import org.springframework.ws.soap.security.callback.CleanupCallback;
|
||||
|
||||
public class SpringCertificateValidationCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class SpringCertificateValidationCallbackHandlerTest {
|
||||
|
||||
private SpringCertificateValidationCallbackHandler callbackHandler;
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private AuthenticationManager mock;
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
private X509Certificate certificate;
|
||||
|
||||
private CertificateValidationCallback callback;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
callbackHandler = new SpringCertificateValidationCallbackHandler();
|
||||
control = MockControl.createControl(AuthenticationManager.class);
|
||||
mock = (AuthenticationManager) control.getMock();
|
||||
callbackHandler.setAuthenticationManager(mock);
|
||||
authenticationManager = createMock(AuthenticationManager.class);
|
||||
callbackHandler.setAuthenticationManager(authenticationManager);
|
||||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
InputStream is = null;
|
||||
try {
|
||||
@@ -66,35 +67,42 @@ public class SpringCertificateValidationCallbackHandlerTest extends TestCase {
|
||||
callback = new CertificateValidationCallback(certificate);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCertificateValid() throws Exception {
|
||||
mock.authenticate(new X509AuthenticationToken(certificate));
|
||||
control.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
control.setReturnValue(new TestingAuthenticationToken(certificate, null, new GrantedAuthority[0]));
|
||||
control.replay();
|
||||
expect(authenticationManager.authenticate(isA(X509AuthenticationToken.class)))
|
||||
.andReturn(new TestingAuthenticationToken(certificate, null, new GrantedAuthority[0]));
|
||||
|
||||
replay(authenticationManager);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(authenticationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCertificateInvalid() throws Exception {
|
||||
mock.authenticate(new X509AuthenticationToken(certificate));
|
||||
control.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
control.setThrowable(new BadCredentialsException(""));
|
||||
control.replay();
|
||||
expect(authenticationManager.authenticate(isA(X509AuthenticationToken.class)))
|
||||
.andThrow(new BadCredentialsException(""));
|
||||
|
||||
replay(authenticationManager);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(authenticationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCleanUp() throws Exception {
|
||||
TestingAuthenticationToken authentication =
|
||||
new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[0]);
|
||||
@@ -102,7 +110,7 @@ public class SpringCertificateValidationCallbackHandlerTest extends TestCase {
|
||||
|
||||
CleanupCallback cleanupCallback = new CleanupCallback();
|
||||
callbackHandler.handleInternal(cleanupCallback);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,12 +16,8 @@
|
||||
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.DisabledException;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.security.providers.TestingAuthenticationToken;
|
||||
import org.springframework.security.userdetails.User;
|
||||
@@ -29,13 +25,19 @@ import org.springframework.security.userdetails.UserDetailsService;
|
||||
import org.springframework.security.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.ws.soap.security.callback.CleanupCallback;
|
||||
|
||||
public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class SpringDigestPasswordValidationCallbackHandlerTest {
|
||||
|
||||
private SpringDigestPasswordValidationCallbackHandler callbackHandler;
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private UserDetailsService mock;
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
private String username;
|
||||
|
||||
@@ -43,12 +45,11 @@ public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase
|
||||
|
||||
private PasswordValidationCallback callback;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
callbackHandler = new SpringDigestPasswordValidationCallbackHandler();
|
||||
control = MockControl.createControl(UserDetailsService.class);
|
||||
mock = (UserDetailsService) control.getMock();
|
||||
callbackHandler.setUserDetailsService(mock);
|
||||
userDetailsService = createMock(UserDetailsService.class);
|
||||
callbackHandler.setUserDetailsService(userDetailsService);
|
||||
username = "Bert";
|
||||
password = "Ernie";
|
||||
String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
|
||||
@@ -59,55 +60,72 @@ public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase
|
||||
callback = new PasswordValidationCallback(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserDigestUserNotFound() throws Exception {
|
||||
control.expectAndThrow(mock.loadUserByUsername(username), new UsernameNotFoundException(username));
|
||||
control.replay();
|
||||
expect(userDetailsService.loadUserByUsername(username)).andThrow(new UsernameNotFoundException(username));
|
||||
|
||||
replay(userDetailsService);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(userDetailsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserDigestValid() throws Exception {
|
||||
User user = new User(username, password, true, true, true, true, new GrantedAuthority[0]);
|
||||
control.expectAndReturn(mock.loadUserByUsername(username), user);
|
||||
control.replay();
|
||||
expect(userDetailsService.loadUserByUsername(username)).andReturn(user);
|
||||
|
||||
replay(userDetailsService);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(userDetailsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserDigestValidInvalid() throws Exception {
|
||||
User user = new User(username, "Big bird", true, true, true, true, new GrantedAuthority[0]);
|
||||
control.expectAndReturn(mock.loadUserByUsername(username), user);
|
||||
control.replay();
|
||||
expect(userDetailsService.loadUserByUsername(username)).andReturn(user);
|
||||
|
||||
replay(userDetailsService);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(userDetailsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserDigestDisbaled() throws Exception {
|
||||
User user = new User(username, "Ernie", false, true, true, true, new GrantedAuthority[0]);
|
||||
control.expectAndReturn(mock.loadUserByUsername(username), user);
|
||||
control.replay();
|
||||
expect(userDetailsService.loadUserByUsername(username)).andReturn(user);
|
||||
|
||||
replay(userDetailsService);
|
||||
|
||||
try {
|
||||
callbackHandler.handleInternal(callback);
|
||||
fail("disabled user authenticated");
|
||||
} catch (
|
||||
DisabledException expected) {
|
||||
Assert.fail("disabled user authenticated");
|
||||
} catch (DisabledException expected) {
|
||||
// expected
|
||||
}
|
||||
verify(userDetailsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCleanUp() throws Exception {
|
||||
TestingAuthenticationToken authentication =
|
||||
new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[0]);
|
||||
@@ -115,7 +133,7 @@ public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase
|
||||
|
||||
CleanupCallback cleanupCallback = new CleanupCallback();
|
||||
callbackHandler.handleInternal(cleanupCallback);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.AuthenticationManager;
|
||||
import org.springframework.security.BadCredentialsException;
|
||||
@@ -29,13 +25,19 @@ import org.springframework.security.providers.TestingAuthenticationToken;
|
||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.ws.soap.security.callback.CleanupCallback;
|
||||
|
||||
public class SpringPlainTextPasswordValidationCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class SpringPlainTextPasswordValidationCallbackHandlerTest {
|
||||
|
||||
private SpringPlainTextPasswordValidationCallbackHandler callbackHandler;
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private AuthenticationManager mock;
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
private PasswordValidationCallback callback;
|
||||
|
||||
@@ -43,12 +45,11 @@ public class SpringPlainTextPasswordValidationCallbackHandlerTest extends TestCa
|
||||
|
||||
private String password;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
callbackHandler = new SpringPlainTextPasswordValidationCallbackHandler();
|
||||
control = MockControl.createControl(AuthenticationManager.class);
|
||||
mock = (AuthenticationManager) control.getMock();
|
||||
callbackHandler.setAuthenticationManager(mock);
|
||||
authenticationManager = createMock(AuthenticationManager.class);
|
||||
callbackHandler.setAuthenticationManager(authenticationManager);
|
||||
username = "Bert";
|
||||
password = "Ernie";
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
@@ -56,34 +57,41 @@ public class SpringPlainTextPasswordValidationCallbackHandlerTest extends TestCa
|
||||
callback = new PasswordValidationCallback(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserPlainTextValid() throws Exception {
|
||||
Authentication authResult = new TestingAuthenticationToken(username, password, new GrantedAuthority[0]);
|
||||
control.expectAndReturn(mock.authenticate(new UsernamePasswordAuthenticationToken(username, password)),
|
||||
authResult);
|
||||
control.replay();
|
||||
expect(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password))).andReturn(authResult);
|
||||
|
||||
replay(authenticationManager);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(authenticationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserPlainTextInvalid() throws Exception {
|
||||
control.expectAndThrow(mock.authenticate(new UsernamePasswordAuthenticationToken(username, password)),
|
||||
new BadCredentialsException(""));
|
||||
control.replay();
|
||||
expect(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password))).andThrow(new BadCredentialsException(""));
|
||||
|
||||
replay(authenticationManager);
|
||||
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
control.verify();
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
|
||||
verify(authenticationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCleanUp() throws Exception {
|
||||
TestingAuthenticationToken authentication =
|
||||
new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[0]);
|
||||
@@ -91,7 +99,7 @@ public class SpringPlainTextPasswordValidationCallbackHandlerTest extends TestCa
|
||||
|
||||
CleanupCallback cleanupCallback = new CleanupCallback();
|
||||
callbackHandler.handleInternal(cleanupCallback);
|
||||
assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,39 +16,44 @@
|
||||
|
||||
package org.springframework.ws.soap.security.xwss.callback;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordCallback;
|
||||
import com.sun.xml.wss.impl.callback.UsernameCallback;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
public class SpringUsernamePasswordCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.PasswordCallback;
|
||||
import com.sun.xml.wss.impl.callback.UsernameCallback;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SpringUsernamePasswordCallbackHandlerTest {
|
||||
|
||||
private SpringUsernamePasswordCallbackHandler handler;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
handler = new SpringUsernamePasswordCallbackHandler();
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken("Bert", "Ernie");
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsernameCallback() throws Exception {
|
||||
UsernameCallback usernameCallback = new UsernameCallback();
|
||||
handler.handleInternal(usernameCallback);
|
||||
assertEquals("Invalid username", "Bert", usernameCallback.getUsername());
|
||||
Assert.assertEquals("Invalid username", "Bert", usernameCallback.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordCallback() throws Exception {
|
||||
PasswordCallback passwordCallback = new PasswordCallback();
|
||||
handler.handleInternal(passwordCallback);
|
||||
assertEquals("Invalid username", "Ernie", passwordCallback.getPassword());
|
||||
Assert.assertEquals("Invalid username", "Ernie", passwordCallback.getPassword());
|
||||
}
|
||||
}
|
||||
@@ -20,19 +20,21 @@ import java.io.InputStream;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
public class JaasCertificateValidationCallbackHandlerTest extends TestCase {
|
||||
import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaasCertificateValidationCallbackHandlerTest {
|
||||
|
||||
private JaasCertificateValidationCallbackHandler callbackHandler;
|
||||
|
||||
private CertificateValidationCallback callback;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
System.setProperty("java.security.auth.login.config", getClass().getResource("jaas.config").toString());
|
||||
callbackHandler = new JaasCertificateValidationCallbackHandler();
|
||||
callbackHandler.setLoginContextName("Certificate");
|
||||
@@ -51,10 +53,11 @@ public class JaasCertificateValidationCallbackHandlerTest extends TestCase {
|
||||
callback = new CertificateValidationCallback(certificate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCertificateValid() throws Exception {
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,35 +17,39 @@
|
||||
package org.springframework.ws.soap.security.xwss.callback.jaas;
|
||||
|
||||
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaasPlainTextPasswordValidationCallbackHandlerTest extends TestCase {
|
||||
public class JaasPlainTextPasswordValidationCallbackHandlerTest {
|
||||
|
||||
private JaasPlainTextPasswordValidationCallbackHandler callbackHandler;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
System.setProperty("java.security.auth.login.config", getClass().getResource("jaas.config").toString());
|
||||
callbackHandler = new JaasPlainTextPasswordValidationCallbackHandler();
|
||||
callbackHandler.setLoginContextName("PlainText");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserPlainTextValid() throws Exception {
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Ernie");
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertTrue("Not authenticated", authenticated);
|
||||
Assert.assertTrue("Not authenticated", authenticated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateUserPlainTextInvalid() throws Exception {
|
||||
PasswordValidationCallback.PlainTextPasswordRequest request =
|
||||
new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Big bird");
|
||||
PasswordValidationCallback callback = new PasswordValidationCallback(request);
|
||||
callbackHandler.handleInternal(callback);
|
||||
boolean authenticated = callback.getResult();
|
||||
assertFalse("Authenticated", authenticated);
|
||||
Assert.assertFalse("Authenticated", authenticated);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user