1   /*
2    * $Id: JaasAuthenticationNoJaasConfigFileTestCase.java 11836 2008-05-22 12:12:40Z rossmason $
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.module.jaas;
12  
13  import org.mule.api.EncryptionStrategy;
14  import org.mule.api.ExceptionPayload;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.module.client.MuleClient;
18  import org.mule.security.MuleCredentials;
19  import org.mule.tck.FunctionalTestCase;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  public class JaasAuthenticationNoJaasConfigFileTestCase extends FunctionalTestCase
25  {
26      
27      public void assertExceptionPayload(MuleMessage umoMessage, String exceptionMessage)
28      {
29          assertNotNull(umoMessage.getExceptionPayload());
30          ExceptionPayload exceptionPayload = umoMessage.getExceptionPayload();
31          assertNotNull(exceptionPayload);
32          assertEquals(exceptionMessage, exceptionPayload.getMessage());
33      }
34  
35      public void testCaseGoodAuthentication() throws Exception
36      {
37          MuleClient client = new MuleClient();
38  
39          Map props = new HashMap();
40          EncryptionStrategy strategy = muleContext
41              .getSecurityManager()
42              .getEncryptionStrategy("PBE");
43          String header = MuleCredentials.createHeader("Marie.Rizzo", "dragon", "PBE", strategy);
44          props.put(MuleProperties.MULE_USER_PROPERTY, header);
45          MuleMessage m = client.send("vm://test", "Test", props);
46  
47          assertNotNull(m);
48          assertTrue(m.getPayload() instanceof String);
49          assertEquals("Test Received", m.getPayloadAsString());
50      }
51  
52      public void testCaseDifferentGoodAuthentication() throws Exception
53      {
54          MuleClient client = new MuleClient();
55  
56          Map props = new HashMap();
57          EncryptionStrategy strategy = muleContext
58              .getSecurityManager()
59              .getEncryptionStrategy("PBE");
60          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
61          props.put(MuleProperties.MULE_USER_PROPERTY, header);
62          MuleMessage m = client.send("vm://test", "Test", props);
63  
64          assertNotNull(m);
65          assertTrue(m.getPayload() instanceof String);
66          assertEquals("Test Received", m.getPayloadAsString());
67      }
68  
69      public void testCaseWrongCombinationOfCorrectUsernameAndPassword() throws Exception
70      {
71          MuleClient client = new MuleClient();
72  
73          Map props = new HashMap();
74          EncryptionStrategy strategy = muleContext
75              .getSecurityManager()
76              .getEncryptionStrategy("PBE");
77          String header = MuleCredentials.createHeader("Marie.Rizzo", "anon", "PBE", strategy);
78          props.put(MuleProperties.MULE_USER_PROPERTY, header);
79          MuleMessage m = client.send("vm://test", "Test", props);
80  
81          assertNotNull(m);
82          assertTrue(m.getPayload() instanceof String);
83          assertFalse(m.getPayloadAsString().equals("Test Received"));
84          
85          assertExceptionPayload(m, "Authentication failed for principal Marie.Rizzo. Message payload is of type: String");
86          
87      }
88  
89      public void testCaseBadUserName() throws Exception
90      {
91          MuleClient client = new MuleClient();
92          Map props = new HashMap();
93          EncryptionStrategy strategy = muleContext
94              .getSecurityManager()
95              .getEncryptionStrategy("PBE");
96          String header = MuleCredentials.createHeader("Evil", "dragon", "PBE", strategy);
97          props.put(MuleProperties.MULE_USER_PROPERTY, header);
98          MuleMessage m = client.send("vm://test", "Test", props);
99  
100         assertNotNull(m);
101         assertTrue(m.getPayload() instanceof String);
102         assertFalse(m.getPayloadAsString().equals("Test Received"));
103         
104         assertExceptionPayload(m, "Authentication failed for principal Evil. Message payload is of type: String");
105     }
106 
107     public void testCaseBadPassword() throws Exception
108     {
109         MuleClient client = new MuleClient();
110         Map props = new HashMap();
111         EncryptionStrategy strategy = muleContext
112             .getSecurityManager()
113             .getEncryptionStrategy("PBE");
114         String header = MuleCredentials.createHeader("Marie.Rizzo", "evil", "PBE", strategy);
115         props.put(MuleProperties.MULE_USER_PROPERTY, header);
116         MuleMessage m = client.send("vm://test", "Test", props);
117 
118         assertNotNull(m);
119         assertTrue(m.getPayload() instanceof String);
120         assertFalse(m.getPayloadAsString().equals("Test Received"));
121         
122         assertExceptionPayload(m, "Authentication failed for principal Marie.Rizzo. Message payload is of type: String");
123     }
124 
125     protected String getConfigResources()
126     {
127         return "mule-conf-with-no-jaas-config-file.xml";
128     }
129 }