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  
20  package org.apache.ws.security.misc;
21  
22  import org.apache.ws.security.WSSecurityEngine;
23  import org.apache.ws.security.WSSecurityException;
24  import org.apache.ws.security.common.SOAPUtil;
25  import org.w3c.dom.Document;
26  
27  /**
28   * This tests how security headers are parsed and processed.
29   */
30  public class SecurityHeaderTest extends org.junit.Assert {
31      private static final String DUPLICATE_NULL_ACTOR_MSG = 
32          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
33          + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
34          + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
35          + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
36          + "<SOAP-ENV:Header>"
37          + "<wsse:Security SOAP-ENV:mustUnderstand=\"1\" "
38          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
39          + "</wsse:Security>"
40          + "<wsse:Security SOAP-ENV:mustUnderstand=\"1\" "
41          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
42          + "</wsse:Security>"
43          + "</SOAP-ENV:Header>"
44          + "<SOAP-ENV:Body>" 
45          + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" 
46          + "<value xmlns=\"\">15</value>" + "</add>" 
47          + "</SOAP-ENV:Body>\r\n       \r\n" + "</SOAP-ENV:Envelope>";
48      private static final String DUPLICATE_ACTOR_MSG = 
49          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
50          + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
51          + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
52          + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
53          + "<SOAP-ENV:Header>"
54          + "<wsse:Security SOAP-ENV:actor=\"user\" SOAP-ENV:mustUnderstand=\"1\" "
55          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
56          + "</wsse:Security>"
57          + "<wsse:Security SOAP-ENV:actor=\"user\" SOAP-ENV:mustUnderstand=\"1\" "
58          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
59          + "</wsse:Security>"
60          + "</SOAP-ENV:Header>"
61          + "<SOAP-ENV:Body>" 
62          + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" 
63          + "<value xmlns=\"\">15</value>" + "</add>" 
64          + "</SOAP-ENV:Body>\r\n       \r\n" + "</SOAP-ENV:Envelope>";
65      private static final String TWO_ACTOR_MSG = 
66          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
67          + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
68          + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
69          + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
70          + "<SOAP-ENV:Header>"
71          + "<wsse:Security SOAP-ENV:mustUnderstand=\"1\" "
72          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
73          + "</wsse:Security>"
74          + "<wsse:Security SOAP-ENV:actor=\"user\" SOAP-ENV:mustUnderstand=\"1\" "
75          + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
76          + "</wsse:Security>"
77          + "</SOAP-ENV:Header>"
78          + "<SOAP-ENV:Body>" 
79          + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">" 
80          + "<value xmlns=\"\">15</value>" + "</add>" 
81          + "</SOAP-ENV:Body>\r\n       \r\n" + "</SOAP-ENV:Envelope>";
82      
83      private WSSecurityEngine secEngine = new WSSecurityEngine();
84  
85      /**
86       * Test for processing multiple security headers with the same (null) actor
87       */
88      @org.junit.Test
89      public void testDuplicateNullActor() throws Exception {
90          Document doc = SOAPUtil.toSOAPPart(DUPLICATE_NULL_ACTOR_MSG);
91          try {
92              secEngine.processSecurityHeader(doc, null, null, null);
93              fail("Failure expected on a null actor");
94          } catch (WSSecurityException ex) {
95              // expected
96          }
97      }
98      
99      /**
100      * Test for processing multiple security headers with the same actor
101      */
102     @org.junit.Test
103     public void testDuplicateActor() throws Exception {
104         Document doc = SOAPUtil.toSOAPPart(DUPLICATE_ACTOR_MSG);
105         try {
106             secEngine.processSecurityHeader(doc, "user", null, null);
107             fail("Failure expected on a duplicate actor");
108         } catch (WSSecurityException ex) {
109             // expected
110         }
111     }
112     
113     /**
114      * Test for processing multiple security headers with different actors
115      */
116     @org.junit.Test
117     public void testTwoActors() throws Exception {
118         Document doc = SOAPUtil.toSOAPPart(TWO_ACTOR_MSG);
119         secEngine.processSecurityHeader(doc, null, null, null);
120         
121         secEngine.processSecurityHeader(doc, "user", null, null);
122     }
123 }