View Javadoc

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