1   /*
2    * $Id: XfireWsSecurityOnInboundTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $ 
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.extras.wssecurity.testcases;
12  
13  import org.mule.extras.client.MuleClient;
14  import org.mule.tck.FunctionalTestCase;
15  import org.mule.umo.UMOMessage;
16  
17  import java.util.Properties;
18  
19  import org.apache.ws.security.WSConstants;
20  import org.apache.ws.security.handler.WSHandlerConstants;
21  
22  public class XfireWsSecurityOnInboundTestCase extends FunctionalTestCase
23  {
24      public void testGoodUserNameTokenAuthentication () throws Exception
25      {
26          MuleClient client = new MuleClient();
27          Properties props = new Properties();
28  
29          // Action to perform : user token
30          props.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
31          // Password type : text or digest
32          props.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
33          // User name to send
34          props.setProperty(WSHandlerConstants.USER, "gooduser");
35          // Callback used to retrive password for given user.
36          props.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "org.mule.extras.wssecurity.callbackhandlers.MuleWsSecurityCallbackHandler");
37  
38          UMOMessage m = client.send("xfire:http://localhost:8282/MySecuredUMO?method=echo", "Test", props);
39          assertNotNull(m);
40          assertTrue(m.getPayload() instanceof String);
41          assertTrue(m.getPayload().equals("Test"));
42      }
43  
44      public void testBadUserNameTokenAuthentication () throws Exception
45      {
46          MuleClient client = new MuleClient();
47          Properties props = new Properties();
48  
49          // Action to perform : user token
50          props.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
51          // Password type : text or digest
52          props.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
53          // User name to send
54          props.setProperty(WSHandlerConstants.USER, "baduser");
55          // Callback used to retrive password for given user.
56          props.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "org.mule.extras.wssecurity.callbackhandlers.MuleWsSecurityCallbackHandler");
57  
58          UMOMessage m = null;
59          try
60          {
61              m = client.send("xfire:http://localhost:8282/MySecuredUMO?method=echo", "Test", props);
62          }
63          catch (Exception e)
64          {
65              assertNotNull(e);
66          }
67          assertNull(m);
68      }
69  
70      public void testGoodUserNameEncrypted () throws Exception
71      {
72          MuleClient client = new MuleClient();
73          Properties props = new Properties();
74  
75          // Action to perform : user token
76          props.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.ENCRYPT);
77          // User name to send
78          props.setProperty(WSHandlerConstants.USER, "mulealias");
79          // Callback used to retrive password for given user.
80          props.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "org.mule.extras.wssecurity.callbackhandlers.MuleWsSecurityCallbackHandler");
81          // Property file containing the Encryption properties
82          props.setProperty(WSHandlerConstants.ENC_PROP_FILE, "out-encrypted-security.properties");
83  
84          UMOMessage m = client.send("xfire:http://localhost:8282/MySecuredUMO?method=echo", "Test", props);
85          assertNotNull(m);
86          assertTrue(m.getPayload() instanceof String);
87          assertTrue(m.getPayload().equals("Test"));
88      }
89  
90      public void testBadUserNameEncrypted () throws Exception
91      {
92          MuleClient client = new MuleClient();
93          Properties props = new Properties();
94  
95          // Action to perform : user token
96          props.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
97          // User name to send
98          props.setProperty(WSHandlerConstants.USER, "myBadAlias");
99          // Callback used to retrive password for given user.
100         props.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "org.mule.extras.wssecurity.callbackhandlers.MuleWsSecurityCallbackHandler");
101         // Property file containing the Encryption properties
102         props.setProperty(WSHandlerConstants.ENC_PROP_FILE, "out-encrypted-security.properties");
103 
104         UMOMessage m = null;
105         try
106         {
107             m = client.send("xfire:http://localhost:8282/MySecuredUMO?method=echo", "Test", props);
108         }
109         catch (Exception e)
110         {
111             assertNotNull(e);
112         }
113         assertNull(m);
114     }
115 
116     public void testSignedSoapMessage () throws Exception
117     {
118         MuleClient client = new MuleClient();
119         Properties props = new Properties();
120 
121         // Action to perform : user token
122         props.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
123         // User in keystore
124         props.setProperty(WSHandlerConstants.USER, "mulealias");
125         // Callback used to retrive password for given user.
126         props.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "org.mule.extras.wssecurity.callbackhandlers.MuleWsSecurityCallbackHandler");
127         // Configuration for accessing private key in keystore
128         props.setProperty(WSHandlerConstants.SIG_PROP_FILE, "out-signed-security.properties");
129         // possible values are : "IssuerSerial" ( recommended ) and "DirectReference"
130         props.setProperty(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
131 
132         UMOMessage m = client.send("xfire:http://localhost:8282/MySecuredUMO?method=echo", "Test", props);
133         assertNotNull(m);
134         assertTrue(m.getPayload() instanceof String);
135         assertTrue(m.getPayload().equals("Test"));
136     }
137 
138     protected String getConfigResources ()
139     {
140         return "wssecurity-mule-config-for-inbound.xml";
141     }
142 }