View Javadoc

1   /*
2    * $Id: SimplePasswordJmxAuthenticatorTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.management.support;
12  
13  import org.mule.module.management.support.SimplePasswordJmxAuthenticator;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.security.Principal;
17  import java.util.Collections;
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import javax.management.remote.JMXPrincipal;
23  import javax.security.auth.Subject;
24  
25  public class SimplePasswordJmxAuthenticatorTestCase extends AbstractMuleTestCase
26  {
27      private static final String[] VALID_AUTH_TOKEN = {"mule", "mulepassword"};
28      private SimplePasswordJmxAuthenticator authenticator;
29  
30      protected void doSetUp () throws Exception
31      {
32          super.doSetUp();
33          authenticator = new SimplePasswordJmxAuthenticator();
34      }
35  
36      public void testSuccessfulAuthentication()
37      {
38          Map<String, String> credentials = getValidCredentials();
39  
40          authenticator.setCredentials(credentials);
41  
42          Subject subject = authenticator.authenticate(VALID_AUTH_TOKEN);
43          assertNotNull(subject);
44          assertTrue(subject.isReadOnly());
45  
46          Set<Object> publicCredentials = subject.getPublicCredentials();
47          assertNotNull(publicCredentials);
48          assertEquals(0, publicCredentials.size());
49  
50          Set<Object> privateCredentials = subject.getPrivateCredentials();
51          assertNotNull(privateCredentials);
52          assertEquals(0, privateCredentials.size());
53  
54          Set<Principal> principals = subject.getPrincipals();
55          assertNotNull(principals);
56          assertEquals(1, principals.size());
57  
58          Object ref = principals.iterator().next();
59          assertTrue(ref instanceof JMXPrincipal);
60  
61          JMXPrincipal jmxPrincipal = (JMXPrincipal) ref;
62          String name = jmxPrincipal.getName();
63          assertNotNull(name);
64          assertEquals(VALID_AUTH_TOKEN[0], name);
65      }
66  
67      public void testNullOrEmptyCredentialsConfigured()
68      {
69          Map<String, String> credentials = Collections.emptyMap();
70  
71          // shouldn't fail
72          authenticator.setCredentials(credentials);
73  
74          try
75          {
76              authenticator.authenticate(VALID_AUTH_TOKEN);
77              fail("Should've thrown an exception");
78          }
79          catch (SecurityException e)
80          {
81              // expected
82          }
83  
84          // shouldn't fail
85          authenticator.setCredentials(null);
86  
87          try
88          {
89              authenticator.authenticate(VALID_AUTH_TOKEN);
90              fail("Should've thrown an exception");
91          }
92          catch (SecurityException e)
93          {
94              // expected
95          }
96  
97      }
98  
99      public void testNullAuthToken()
100     {
101         try
102         {
103             authenticator.authenticate(null);
104             fail("Should've thrown an exception");
105         }
106         catch (SecurityException e)
107         {
108             // expected
109         }
110     }
111 
112     public void testInvalidAuthToken ()
113     {
114         try
115         {
116             final String token = "not a String array";
117             authenticator.authenticate(token);
118             fail("Should've thrown an exception");
119         }
120         catch (SecurityException e)
121         {
122             // expected
123         }
124     }
125 
126     public void testAuthTokenTooLong()
127     {
128         try
129         {
130             final String[] token = {"token", "too", "long"};
131             authenticator.authenticate(token);
132             fail("Should've thrown an exception");
133         }
134         catch (SecurityException e)
135         {
136             // expected
137         }
138     }
139 
140     public void testAuthTokenTooShort()
141     {
142         try
143         {
144             final String[] token = {"token_too_short"};
145             authenticator.authenticate(token);
146             fail("Should've thrown an exception");
147         }
148         catch (SecurityException e)
149         {
150             // expected
151         }
152     }
153 
154     public void testNoSuchUser()
155     {
156         try
157         {
158             final String[] token = {"nosuchuser", "thepassword"};
159             authenticator.authenticate(token);
160             fail("Should've thrown an exception");
161         }
162         catch (SecurityException e)
163         {
164             // expected
165         }
166     }
167 
168     public void testInvalidPassword()
169     {
170         authenticator.setCredentials(getValidCredentials());
171         
172         try
173         {
174             final String[] token = {"mule", "wrongpassword"};
175             authenticator.authenticate(token);
176             fail("Should've thrown an exception");
177         }
178         catch (SecurityException e)
179         {
180             // expected
181         }
182     }
183 
184     protected Map<String, String> getValidCredentials ()
185     {
186         Map<String, String> credentials = new HashMap<String, String>(1);
187         credentials.put(VALID_AUTH_TOKEN[0], VALID_AUTH_TOKEN[1]);
188 
189         return credentials;
190     }
191 
192 }