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.message;
21  
22  import org.apache.ws.security.WSSConfig;
23  import org.apache.ws.security.WSSecurityEngine;
24  import org.apache.ws.security.WSConstants;
25  import org.apache.ws.security.common.SOAPUtil;
26  import org.apache.ws.security.components.crypto.Crypto;
27  import org.apache.ws.security.components.crypto.CryptoFactory;
28  import org.w3c.dom.Document;
29  
30  /**
31   * This is a test for WSS-60 - "Problems when SOAP envelope namespace prefix is null"
32   * http://issues.apache.org/jira/browse/WSS-60
33   */
34  public class NoSoapPrefixSignatureTest extends org.junit.Assert {
35      private static final org.apache.commons.logging.Log LOG = 
36          org.apache.commons.logging.LogFactory.getLog(NoSoapPrefixSignatureTest.class);
37      private WSSecurityEngine secEngine = new WSSecurityEngine();
38      private Crypto crypto = null;
39      
40      public NoSoapPrefixSignatureTest() throws Exception {
41          WSSConfig.init();
42          crypto = CryptoFactory.getInstance();
43      }
44  
45      /**
46       * Test signing a SOAP message that has no SOAP namespace prefix
47       */
48      @org.junit.Test
49      public void testNoSOAPNamespacePrefix() throws Exception {
50          WSSecSignature sign = new WSSecSignature();
51          sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
52          sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
53  
54          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
55  
56          WSSecHeader secHeader = new WSSecHeader();
57          secHeader.setActor("bob");
58          secHeader.insertSecurityHeader(doc);
59          Document signedDoc = sign.build(doc, crypto, secHeader);
60          
61          if (LOG.isDebugEnabled()) {
62              String outputString = 
63                  org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
64              LOG.debug(outputString);
65          }
66          verify(signedDoc);
67      }
68      
69      /**
70       * Verifies the soap envelope
71       * <p/>
72       * 
73       * @param doc 
74       * @throws Exception Thrown when there is a problem in verification
75       */
76      private void verify(Document doc) throws Exception {
77          secEngine.processSecurityHeader(doc, null, null, crypto);
78          if (LOG.isDebugEnabled()) {
79              LOG.debug("Verfied and decrypted message:");
80              String outputString = 
81                  org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
82              LOG.debug(outputString);
83          }
84      }
85  
86  }