View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.stax.test;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.InputStream;
24  import java.util.Properties;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.stream.XMLStreamReader;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.stream.StreamResult;
31  
32  import org.apache.wss4j.dom.handler.WSHandlerConstants;
33  import org.apache.wss4j.stax.ext.WSSConstants;
34  import org.apache.wss4j.stax.ext.WSSSecurityProperties;
35  import org.apache.wss4j.stax.setup.InboundWSSec;
36  import org.apache.wss4j.stax.setup.WSSec;
37  import org.apache.wss4j.stax.test.utils.StAX2DOM;
38  import org.junit.jupiter.api.Test;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.NodeList;
41  
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  
44  public class FaultTest extends AbstractTestBase {
45  
46      @Test
47      public void testSignedFaultInbound() throws Exception {
48  
49          ByteArrayOutputStream baos = new ByteArrayOutputStream();
50          {
51              InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("testdata/soap-fault.xml");
52              String action = WSHandlerConstants.SIGNATURE;
53              Document securedDocument = doOutboundSecurityWithWSS4J(sourceDocument, action, new Properties());
54  
55              //some test that we can really sure we get what we want from WSS4J
56              NodeList nodeList = securedDocument.getElementsByTagNameNS(WSSConstants.TAG_dsig_Signature.getNamespaceURI(), WSSConstants.TAG_dsig_Signature.getLocalPart());
57              assertEquals(nodeList.item(0).getParentNode().getLocalName(), WSSConstants.TAG_WSSE_SECURITY.getLocalPart());
58  
59              javax.xml.transform.Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
60              transformer.transform(new DOMSource(securedDocument), new StreamResult(baos));
61          }
62  
63          //done signature; now test sig-verification:
64          {
65              WSSSecurityProperties securityProperties = new WSSSecurityProperties();
66              securityProperties.loadSignatureVerificationKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
67              InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties);
68              XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray())));
69  
70              StAX2DOM.readDoc(documentBuilderFactory.newDocumentBuilder(), xmlStreamReader);
71          }
72      }
73  
74      @Test
75      public void testEncryptedFaultInbound() throws Exception {
76  
77          ByteArrayOutputStream baos = new ByteArrayOutputStream();
78          {
79              InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("testdata/soap-fault.xml");
80              String action = WSHandlerConstants.ENCRYPT;
81              Document securedDocument = doOutboundSecurityWithWSS4J(sourceDocument, action, new Properties());
82  
83              javax.xml.transform.Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
84              transformer.transform(new DOMSource(securedDocument), new StreamResult(baos));
85          }
86  
87          //done signature; now test decryption
88          {
89              WSSSecurityProperties securityProperties = new WSSSecurityProperties();
90              securityProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
91              securityProperties.setCallbackHandler(new CallbackHandlerImpl());
92              InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties);
93              XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray())));
94  
95              StAX2DOM.readDoc(documentBuilderFactory.newDocumentBuilder(), xmlStreamReader);
96          }
97      }
98  
99      @Test
100     public void testUnsecuredFaultInbound() throws Exception {
101 
102         ByteArrayOutputStream baos = new ByteArrayOutputStream();
103         {
104             InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("testdata/soap-fault.xml");
105             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
106             factory.setNamespaceAware(true);
107             DocumentBuilder builder = factory.newDocumentBuilder();
108             Document securedDocument = builder.parse(sourceDocument);
109 
110             javax.xml.transform.Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
111             transformer.transform(new DOMSource(securedDocument), new StreamResult(baos));
112         }
113 
114         //done signature; now test sig-verification:
115         {
116             WSSSecurityProperties securityProperties = new WSSSecurityProperties();
117             securityProperties.loadSignatureVerificationKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
118             InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties);
119             XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray())));
120 
121             StAX2DOM.readDoc(documentBuilderFactory.newDocumentBuilder(), xmlStreamReader);
122         }
123     }
124 
125 }