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