1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import java.util.Properties;
20  
21  import org.springframework.ws.WebServiceMessageFactory;
22  import org.springframework.ws.context.DefaultMessageContext;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.soap.SoapMessage;
25  import org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler;
26  
27  import org.apache.ws.security.WSConstants;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  
33  public abstract class Wss4jMessageInterceptorSoapActionTestCase extends Wss4jTestCase {
34  
35      private static final String SOAP_ACTION = "\"http://test\"";
36  
37      private Properties users;
38  
39      private Wss4jSecurityInterceptor interceptor;
40  
41      @Override
42      protected void onSetup() throws Exception {
43          users = new Properties();
44          users.setProperty("Bert", "Ernie");
45          interceptor = new Wss4jSecurityInterceptor();
46          interceptor.setValidationActions("UsernameToken");
47          interceptor.setSecurementActions("UsernameToken");
48          interceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
49          SimplePasswordValidationCallbackHandler callbackHandler = new SimplePasswordValidationCallbackHandler();
50          callbackHandler.setUsers(users);
51          interceptor.setValidationCallbackHandler(callbackHandler);
52  
53          interceptor.afterPropertiesSet();
54      }
55  
56      @Test
57      public void testPreserveSoapActionOnValidation() throws Exception {
58          SoapMessage message = loadSoap11Message("usernameTokenPlainText-soap.xml");
59          message.setSoapAction(SOAP_ACTION);
60          MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
61          interceptor.validateMessage(message, messageContext);
62  
63          assertNotNull("Soap Action must not be null", message.getSoapAction());
64          assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
65      }
66  
67      @Test
68      public void testPreserveSoap12ActionOnValidation() throws Exception {
69          SoapMessage message = loadSoap12Message("usernameTokenPlainText-soap12.xml");
70          message.setSoapAction(SOAP_ACTION);
71          WebServiceMessageFactory messageFactory = getSoap12MessageFactory();
72          MessageContext messageContext = new DefaultMessageContext(message, messageFactory);
73          interceptor.validateMessage(message, messageContext);
74  
75          assertNotNull("Soap Action must not be null", message.getSoapAction());
76          assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
77      }
78  
79      @Test
80      public void testPreserveSoapActionOnSecurement() throws Exception {
81          SoapMessage message = loadSoap11Message("empty-soap.xml");
82          message.setSoapAction(SOAP_ACTION);
83          interceptor.setSecurementUsername("Bert");
84          interceptor.setSecurementPassword("Ernie");
85          MessageContext messageContext = getSoap11MessageContext(message);
86          interceptor.secureMessage(message, messageContext);
87  
88          assertNotNull("Soap Action must not be null", message.getSoapAction());
89          assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
90  
91      }
92  
93      @Test
94      public void testPreserveSoap12ActionOnSecurement() throws Exception {
95          SoapMessage message = loadSoap12Message("empty-soap12.xml");
96          message.setSoapAction(SOAP_ACTION);
97          interceptor.setSecurementUsername("Bert");
98          interceptor.setSecurementPassword("Ernie");
99          MessageContext messageContext = getSoap12MessageContext(message);
100         interceptor.secureMessage(message, messageContext);
101 
102         assertNotNull("Soap Action must not be null", message.getSoapAction());
103         assertEquals("Soap Action is different from expected", SOAP_ACTION, message.getSoapAction());
104 
105     }
106 
107 
108 }