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