ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gi...@apache.org
Subject svn commit: r1196271 - in /webservices/wss4j/branches/swssf/streaming-xml-security/src: main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
Date Tue, 01 Nov 2011 20:58:06 GMT
Author: giger
Date: Tue Nov  1 20:58:06 2011
New Revision: 1196271

URL: http://svn.apache.org/viewvc?rev=1196271&view=rev
Log:
XMLSecurityStreamReader test and fixes

Added:
    webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
  (with props)
Modified:
    webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java

Modified: webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java?rev=1196271&r1=1196270&r2=1196271&view=diff
==============================================================================
--- webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java
(original)
+++ webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityStreamReader.java
Tue Nov  1 20:58:06 2011
@@ -68,7 +68,10 @@ public class XMLSecurityStreamReader imp
         } catch (XMLSecurityException e) {
             throw new XMLStreamException(e);
         }
-        return getCurrentEvent().getEventType();
+        if (currentEvent.isCharacters() && currentEvent.asCharacters().isIgnorableWhiteSpace())
{
+            return XMLStreamConstants.SPACE;
+        }
+        return currentEvent.getEventType();
     }
 
     private XMLEvent getCurrentEvent() {
@@ -440,7 +443,14 @@ public class XMLSecurityStreamReader imp
     public int getEventType() {
         XMLEvent xmlEvent = getCurrentEvent();
         if (xmlEvent == null) {
-            return XMLStreamConstants.START_DOCUMENT;
+            try {
+                return next();
+            } catch (XMLStreamException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+        if (xmlEvent.isCharacters() && xmlEvent.asCharacters().isIgnorableWhiteSpace())
{
+            return XMLStreamConstants.SPACE;
         }
         return xmlEvent.getEventType();
     }
@@ -533,7 +543,7 @@ public class XMLSecurityStreamReader imp
     }
 
     public String getEncoding() {
-        return null;
+        return inputProcessorChain.getDocumentContext().getEncoding();
     }
 
     public boolean hasText() {

Added: webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java?rev=1196271&view=auto
==============================================================================
--- webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
(added)
+++ webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
Tue Nov  1 20:58:06 2011
@@ -0,0 +1,289 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.swssf.xmlsec.test;
+
+import junit.framework.Assert;
+import org.custommonkey.xmlunit.XMLAssert;
+import org.swssf.xmlsec.ext.*;
+import org.swssf.xmlsec.impl.DocumentContextImpl;
+import org.swssf.xmlsec.impl.InputProcessorChainImpl;
+import org.swssf.xmlsec.impl.SecurityContextImpl;
+import org.swssf.xmlsec.impl.XMLSecurityStreamReader;
+import org.testng.annotations.Test;
+
+import javax.xml.stream.*;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stax.StAXSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author $Author$
+ * @version $Revision$ $Date$
+ */
+public class XMLSecurityStreamReaderTest {
+
+    @Test
+    public void testPassThroughDocumentEvents() throws Exception {
+        XMLSecurityProperties securityProperties = new XMLSecurityProperties();
+        securityProperties.setSkipDocumentEvents(false);
+        SecurityContextImpl securityContext = new SecurityContextImpl();
+        InputProcessorChainImpl inputProcessorChain = new InputProcessorChainImpl(securityContext);
+        inputProcessorChain.addProcessor(new EventReaderProcessor());
+        XMLSecurityStreamReader xmlSecurityStreamReader = new XMLSecurityStreamReader(inputProcessorChain,
securityProperties);
+        int event = xmlSecurityStreamReader.next();
+        Assert.assertEquals(XMLStreamConstants.START_DOCUMENT, event);
+    }
+
+    @Test
+    public void testSkipThroughDocumentEvents() throws Exception {
+        XMLSecurityProperties securityProperties = new XMLSecurityProperties();
+        securityProperties.setSkipDocumentEvents(true);
+        SecurityContextImpl securityContext = new SecurityContextImpl();
+        InputProcessorChainImpl inputProcessorChain = new InputProcessorChainImpl(securityContext);
+        inputProcessorChain.addProcessor(new EventReaderProcessor());
+        XMLSecurityStreamReader xmlSecurityStreamReader = new XMLSecurityStreamReader(inputProcessorChain,
securityProperties);
+        int event = xmlSecurityStreamReader.next();
+        Assert.assertEquals(XMLStreamConstants.CHARACTERS, event);
+    }
+
+    @Test
+    public void testIdentityTransformSource() throws Exception {
+        XMLSecurityProperties securityProperties = new XMLSecurityProperties();
+        SecurityContextImpl securityContext = new SecurityContextImpl();
+        InputProcessorChainImpl inputProcessorChain = new InputProcessorChainImpl(securityContext);
+        inputProcessorChain.addProcessor(new EventReaderProcessor());
+        XMLSecurityStreamReader xmlSecurityStreamReader = new XMLSecurityStreamReader(inputProcessorChain,
securityProperties);
+        //use the sun internal TransformerFactory since the current xalan version don't know
how to handle StaxSources:
+        TransformerFactory transformerFactory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
this.getClass().getClassLoader());
+        javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        transformer.transform(new StAXSource(xmlSecurityStreamReader), new StreamResult(baos));
+        XMLAssert.assertXMLEqual(readTestFile(), baos.toString("UTF-8"));
+    }
+
+    @Test
+    public void testCorrectness() throws Exception {
+        XMLSecurityProperties securityProperties = new XMLSecurityProperties();
+        SecurityContextImpl securityContext = new SecurityContextImpl();
+        DocumentContextImpl documentContext = new DocumentContextImpl();
+        documentContext.setEncoding("UTF-8");
+        InputProcessorChainImpl inputProcessorChain = new InputProcessorChainImpl(securityContext,
documentContext);
+        inputProcessorChain.addProcessor(new EventReaderProcessor());
+        XMLSecurityStreamReader xmlSecurityStreamReader = new XMLSecurityStreamReader(inputProcessorChain,
securityProperties);
+
+        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
+        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
+        XMLStreamReader stdXmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
+
+        //hmm why does a streamreader return a DOCUMENT_EVENT before we did call next() ??
+        int stdXMLEventType = stdXmlStreamReader.getEventType();
+        int secXMLEventType = xmlSecurityStreamReader.getEventType();
+        do {
+            switch (stdXMLEventType) {
+                case XMLStreamConstants.START_ELEMENT:
+                    Assert.assertTrue(xmlSecurityStreamReader.isStartElement());
+                    Assert.assertFalse(xmlSecurityStreamReader.isEndElement());
+                    Assert.assertEquals(stdXmlStreamReader.getLocalName(), xmlSecurityStreamReader.getLocalName());
+                    Assert.assertEquals(stdXmlStreamReader.getName(), xmlSecurityStreamReader.getName());
+                    Assert.assertEquals(stdXmlStreamReader.getNamespaceURI(), xmlSecurityStreamReader.getNamespaceURI());
+                    if (stdXmlStreamReader.getPrefix() == null) {
+                        Assert.assertEquals("", xmlSecurityStreamReader.getPrefix());
+                    } else {
+                        Assert.assertEquals(stdXmlStreamReader.getPrefix(), xmlSecurityStreamReader.getPrefix());
+                    }
+                    Assert.assertEquals(stdXmlStreamReader.hasName(), xmlSecurityStreamReader.hasName());
+                    Assert.assertEquals(stdXmlStreamReader.hasText(), xmlSecurityStreamReader.hasText());
+                    Assert.assertEquals(stdXmlStreamReader.getAttributeCount(), xmlSecurityStreamReader.getAttributeCount());
+                    Assert.assertEquals(stdXmlStreamReader.getNamespaceCount(), xmlSecurityStreamReader.getNamespaceCount());
+                    for (int i = 0; i < stdXmlStreamReader.getAttributeCount(); i++) {
+                        Assert.assertEquals(stdXmlStreamReader.getAttributeLocalName(i),
xmlSecurityStreamReader.getAttributeLocalName(i));
+                        Assert.assertEquals(stdXmlStreamReader.getAttributeName(i), xmlSecurityStreamReader.getAttributeName(i));
+                        if (stdXmlStreamReader.getAttributeNamespace(i) == null) {
+                            Assert.assertEquals("", xmlSecurityStreamReader.getAttributeNamespace(i));
+                        } else {
+                            Assert.assertEquals(stdXmlStreamReader.getAttributeNamespace(i),
xmlSecurityStreamReader.getAttributeNamespace(i));
+                        }
+                        if (stdXmlStreamReader.getAttributePrefix(i) == null) {
+                            Assert.assertEquals("", xmlSecurityStreamReader.getAttributePrefix(i));
+                        } else {
+                            Assert.assertEquals(stdXmlStreamReader.getAttributePrefix(i),
xmlSecurityStreamReader.getAttributePrefix(i));
+                        }
+                        Assert.assertEquals(stdXmlStreamReader.getAttributeType(i), xmlSecurityStreamReader.getAttributeType(i));
+                        Assert.assertEquals(stdXmlStreamReader.getAttributeValue(i), xmlSecurityStreamReader.getAttributeValue(i));
+                    }
+                    for (int i = 0; i < stdXmlStreamReader.getNamespaceCount(); i++) {
+                        if (stdXmlStreamReader.getNamespacePrefix(i) == null) {
+                            Assert.assertEquals("", xmlSecurityStreamReader.getNamespacePrefix(i));
+                        } else {
+                            Assert.assertEquals(stdXmlStreamReader.getNamespacePrefix(i),
xmlSecurityStreamReader.getNamespacePrefix(i));
+                        }
+                        Assert.assertEquals(stdXmlStreamReader.getNamespaceURI(i), xmlSecurityStreamReader.getNamespaceURI(i));
+                    }
+                    break;
+                case XMLStreamConstants.END_ELEMENT:
+                    Assert.assertFalse(xmlSecurityStreamReader.isStartElement());
+                    Assert.assertTrue(xmlSecurityStreamReader.isEndElement());
+                    Assert.assertEquals(stdXmlStreamReader.getLocalName(), xmlSecurityStreamReader.getLocalName());
+                    Assert.assertEquals(stdXmlStreamReader.getName(), xmlSecurityStreamReader.getName());
+                    Assert.assertEquals(stdXmlStreamReader.getNamespaceURI(), xmlSecurityStreamReader.getNamespaceURI());
+                    if (stdXmlStreamReader.getPrefix() == null) {
+                        Assert.assertEquals("", xmlSecurityStreamReader.getPrefix());
+                    } else {
+                        Assert.assertEquals(stdXmlStreamReader.getPrefix(), xmlSecurityStreamReader.getPrefix());
+                    }
+                    Assert.assertEquals(stdXmlStreamReader.hasName(), xmlSecurityStreamReader.hasName());
+                    Assert.assertEquals(stdXmlStreamReader.hasText(), xmlSecurityStreamReader.hasText());
+                    break;
+                case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    Assert.assertEquals(stdXmlStreamReader.getPITarget(), xmlSecurityStreamReader.getPITarget());
+                    Assert.assertEquals(stdXmlStreamReader.getPIData(), xmlSecurityStreamReader.getPIData());
+                    break;
+                case XMLStreamConstants.CHARACTERS:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    Assert.assertEquals(stdXmlStreamReader.isWhiteSpace(), xmlSecurityStreamReader.isWhiteSpace());
+                    Assert.assertEquals(stdXmlStreamReader.getText(), xmlSecurityStreamReader.getText());
+                    Assert.assertEquals(
+                            new String(stdXmlStreamReader.getTextCharacters(), stdXmlStreamReader.getTextStart(),
stdXmlStreamReader.getTextLength()),
+                            new String(xmlSecurityStreamReader.getTextCharacters(), xmlSecurityStreamReader.getTextStart(),
xmlSecurityStreamReader.getTextLength()));
+                    Assert.assertEquals(stdXmlStreamReader.getTextLength(), xmlSecurityStreamReader.getTextLength());
+                    break;
+                case XMLStreamConstants.COMMENT:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    Assert.assertEquals(stdXmlStreamReader.isWhiteSpace(), xmlSecurityStreamReader.isWhiteSpace());
+                    Assert.assertEquals(stdXmlStreamReader.getText(), xmlSecurityStreamReader.getText());
+                    Assert.assertEquals(
+                            new String(stdXmlStreamReader.getTextCharacters(), stdXmlStreamReader.getTextStart(),
stdXmlStreamReader.getTextLength()),
+                            new String(xmlSecurityStreamReader.getTextCharacters(), xmlSecurityStreamReader.getTextStart(),
xmlSecurityStreamReader.getTextLength()));
+                    Assert.assertEquals(stdXmlStreamReader.getTextLength(), xmlSecurityStreamReader.getTextLength());
+                    break;
+                case XMLStreamConstants.SPACE:
+                    Assert.assertEquals(stdXmlStreamReader.isWhiteSpace(), xmlSecurityStreamReader.isWhiteSpace());
+                    Assert.assertEquals(stdXmlStreamReader.getText(), xmlSecurityStreamReader.getText());
+                    Assert.assertEquals(
+                            new String(stdXmlStreamReader.getTextCharacters(), stdXmlStreamReader.getTextStart(),
stdXmlStreamReader.getTextLength()),
+                            new String(xmlSecurityStreamReader.getTextCharacters(), xmlSecurityStreamReader.getTextStart(),
xmlSecurityStreamReader.getTextLength()));
+                    Assert.assertEquals(stdXmlStreamReader.getTextLength(), xmlSecurityStreamReader.getTextLength());
+                    break;
+                case XMLStreamConstants.START_DOCUMENT:
+                    Assert.assertEquals(stdXmlStreamReader.getCharacterEncodingScheme(),
xmlSecurityStreamReader.getCharacterEncodingScheme());
+                    Assert.assertEquals(stdXmlStreamReader.getEncoding(), xmlSecurityStreamReader.getEncoding());
+                    //Assert.assertEquals(stdXmlStreamReader.getVersion(), xmlSecurityStreamReader.getVersion());
+                    break;
+                case XMLStreamConstants.END_DOCUMENT:
+                    break;
+                case XMLStreamConstants.ENTITY_REFERENCE:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    Assert.assertEquals(stdXmlStreamReader.getText(), xmlSecurityStreamReader.getText());
+                    break;
+                case XMLStreamConstants.ATTRIBUTE:
+                    break;
+                case XMLStreamConstants.DTD:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    break;
+                case XMLStreamConstants.CDATA:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    break;
+                case XMLStreamConstants.NAMESPACE:
+                    break;
+                case XMLStreamConstants.NOTATION_DECLARATION:
+                    break;
+                case XMLStreamConstants.ENTITY_DECLARATION:
+                    Assert.assertEquals(stdXmlStreamReader.isCharacters(), xmlSecurityStreamReader.isCharacters());
+                    break;
+            }
+            //hmm2 an eventreader returns a CHARACTER EVENT for an ignorable whitespace whereby
a streamReader returns it as SPACE
+            if (stdXMLEventType == XMLStreamConstants.SPACE && secXMLEventType ==
XMLStreamConstants.CHARACTERS) {
+                secXMLEventType = XMLStreamConstants.SPACE;
+            }
+            Assert.assertEquals(stdXMLEventType, secXMLEventType);
+            if (stdXmlStreamReader.hasNext()) {
+                Assert.assertTrue(xmlSecurityStreamReader.hasNext());
+                stdXMLEventType = stdXmlStreamReader.next();
+                secXMLEventType = xmlSecurityStreamReader.next();
+            } else {
+                Assert.assertFalse(xmlSecurityStreamReader.hasNext());
+                break;
+            }
+        } while (true);
+    }
+
+    private String readTestFile() throws Exception {
+        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml");
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+        char[] buf = new char[1024];
+        int read;
+        StringBuilder stringBuilder = new StringBuilder();
+        while ((read = bufferedReader.read(buf)) != -1) {
+            stringBuilder.append(buf, 0, read);
+        }
+        return stringBuilder.toString();
+    }
+
+    class EventReaderProcessor implements InputProcessor {
+
+        private XMLEventReader xmlEventReader;
+
+        EventReaderProcessor() throws Exception {
+            XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+            xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
+            xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
+            xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
+        }
+
+        @Override
+        public Set<Object> getBeforeProcessors() {
+            return new HashSet<Object>();
+        }
+
+        @Override
+        public Set<Object> getAfterProcessors() {
+            return new HashSet<Object>();
+        }
+
+        @Override
+        public XMLSecurityConstants.Phase getPhase() {
+            return XMLSecurityConstants.Phase.PROCESSING;
+        }
+
+        @Override
+        public XMLEvent processNextHeaderEvent(InputProcessorChain inputProcessorChain) throws
XMLStreamException, XMLSecurityException {
+            return null;
+        }
+
+        @Override
+        public XMLEvent processNextEvent(InputProcessorChain inputProcessorChain) throws
XMLStreamException, XMLSecurityException {
+            inputProcessorChain.reset();
+            XMLEvent xmlEvent = xmlEventReader.nextEvent();
+            return xmlEvent;
+        }
+
+        @Override
+        public void doFinal(InputProcessorChain inputProcessorChain) throws XMLStreamException,
XMLSecurityException {
+        }
+    }
+}

Propchange: webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityStreamReaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message