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