You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2003/10/14 14:06:26 UTC
cvs commit: ws-axis/contrib/wss4j/test/wssec TestWSSecurity.java TestWSSecurity2.java
dims 2003/10/14 05:06:26
Added: contrib/wss4j/test/wssec TestWSSecurity.java
TestWSSecurity2.java
Log:
******* WORK IN PROGRESS *******
Initial check-in of my sandbox for ws-security related code.
Revision Changes Path
1.1 ws-axis/contrib/wss4j/test/wssec/TestWSSecurity.java
Index: TestWSSecurity.java
===================================================================
package wssec;
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Axis" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.AxisClient;
import org.apache.axis.configuration.NullProvider;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.utils.XMLUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.security.WSSecurityEngine;
import org.apache.ws.security.message.WSEnvelopeBuilder;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
/**
* WS-Security Test Case
* <p>
* @author Davanum Srinivas (dims@yahoo.com)
*/
public class TestWSSecurity extends TestCase {
private static Log log = LogFactory.getLog(TestWSSecurity.class);
static final String NS = "http://www.w3.org/2000/09/xmldsig#";
static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SOAP-ENV:Body>" + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" + "<value xmlns=\"\">15</value>" + "</add>" + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
static final WSSecurityEngine secEngine = new WSSecurityEngine();
MessageContext msgContext;
SOAPEnvelope unsignedEnvelope;
/**
* TestWSSecurity constructor
* <p>
* @param name name of the test
*/
public TestWSSecurity(String name) {
super(name);
}
/**
* JUnit suite
* <p>
* @return a junit test suite
*/
public static Test suite() {
return new TestSuite(TestWSSecurity.class);
}
/**
* Main method
* <p>
* @param args command line args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
/**
* Setup method
* <p>
* @throws java.lang.Exception Thrown when there is a problem in setup
*/
protected void setUp() throws Exception {
AxisClient tmpEngine = new AxisClient(new NullProvider());
msgContext = new MessageContext(tmpEngine);
unsignedEnvelope = getSOAPEnvelope();
}
/**
* Constructs a soap envelope
* <p>
* @return soap envelope
* @throws java.lang.Exception if there is any problem constructing the soap envelope
*/
protected SOAPEnvelope getSOAPEnvelope() throws Exception {
InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
Message msg = new Message(in);
msg.setMessageContext(msgContext);
return msg.getSOAPEnvelope();
}
/**
* Test that signs and verifies a WS-Security envelope
* <p>
* @throws java.lang.Exception Thrown when there is any problem in signing or verification
*/
public void testX509Signature() throws Exception {
SOAPEnvelope envelope = null;
WSEnvelopeBuilder builder = new WSEnvelopeBuilder();
log.info("Before Signing....");
XMLUtils.PrettyElementToWriter(unsignedEnvelope.getAsDOM(), new PrintWriter(new FileOutputStream("before.xml")));
envelope = (SOAPEnvelope) builder.build(unsignedEnvelope);
log.info("After Signing....");
XMLUtils.PrettyElementToWriter(envelope.getAsDOM(), new PrintWriter(new FileOutputStream("after.xml")));
verify(envelope);
}
/**
* Test that signs (twice) and verifies a WS-Security envelope
* <p>
* @throws java.lang.Exception Thrown when there is any problem in signing or verification
*/
public void testDoubleX509Signature() throws Exception {
SOAPEnvelope envelope = null;
WSEnvelopeBuilder builder = new WSEnvelopeBuilder();
envelope = (SOAPEnvelope) builder.build(unsignedEnvelope);
envelope = (SOAPEnvelope) builder.build(envelope);
verify(envelope);
}
/**
* Verifies the soap envelope
* <p>
* @param env soap envelope
* @throws java.lang.Exception Thrown when there is a problem in verification
*/
private void verify(SOAPEnvelope env) throws Exception {
Document doc = secEngine.processSecurityHeader(env, "");
}
}
1.1 ws-axis/contrib/wss4j/test/wssec/TestWSSecurity2.java
Index: TestWSSecurity2.java
===================================================================
package wssec;
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Axis" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.AxisClient;
import org.apache.axis.configuration.NullProvider;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.utils.XMLUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.security.WSSecurityEngine;
import org.apache.ws.security.message.WSEnvelopeBuilder2;
import org.apache.ws.security.util.AxisUtil;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
/**
* WS-Security Test Case
* <p>
* @author Davanum Srinivas (dims@yahoo.com)
*/
public class TestWSSecurity2 extends TestCase {
private static Log log = LogFactory.getLog(TestWSSecurity2.class);
static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
" <soapenv:Body>" +
" <ns1:testMethod xmlns:ns1=\"LogTestService2\"></ns1:testMethod>\"" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
static final WSSecurityEngine secEngine = new WSSecurityEngine();
MessageContext msgContext;
Message message;
/**
* TestWSSecurity constructor
* <p>
* @param name name of the test
*/
public TestWSSecurity2(String name) {
super(name);
}
/**
* JUnit suite
* <p>
* @return a junit test suite
*/
public static Test suite() {
return new TestSuite(TestWSSecurity2.class);
}
/**
* Main method
* <p>
* @param args command line args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
/**
* Setup method
* <p>
* @throws Exception Thrown when there is a problem in setup
*/
protected void setUp() throws Exception {
AxisClient tmpEngine = new AxisClient(new NullProvider());
msgContext = new MessageContext(tmpEngine);
message = getSOAPMessage();
}
/**
* Constructs a soap envelope
* <p>
* @return soap envelope
* @throws Exception if there is any problem constructing the soap envelope
*/
protected Message getSOAPMessage() throws Exception {
InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
Message msg = new Message(in);
msg.setMessageContext(msgContext);
return msg;
}
/**
* Test that encrypt and decrypt a WS-Security envelope
* <p>
* @throws Exception Thrown when there is any problem in signing or verification
*/
public void testEncryptionDecryption() throws Exception {
SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
SOAPEnvelope envelope = null;
WSEnvelopeBuilder2 builder = new WSEnvelopeBuilder2();
log.info("Before Encryption....");
XMLUtils.PrettyElementToWriter(unsignedEnvelope.getAsDOM(), new PrintWriter(new FileOutputStream("before.xml")));
envelope = (SOAPEnvelope) builder.build(unsignedEnvelope);
log.info("After Encryption....");
XMLUtils.PrettyElementToWriter(envelope.getAsDOM(), new PrintWriter(new FileOutputStream("after.xml")));
verify(envelope);
}
/**
* Verifies the soap envelope
* <p>
* @param envelope
* @throws Exception Thrown when there is a problem in verification
*/
private void verify(SOAPEnvelope envelope) throws Exception {
Document doc = secEngine.decryptMessage(envelope);
AxisUtil.updateSOAPMessage(doc, message);
XMLUtils.PrettyElementToWriter(message.getSOAPEnvelope().getAsDOM(), new PrintWriter(System.out));
}
}