001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jmxremoting;
017:
018: import java.security.Principal;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Set;
022: import javax.security.auth.Subject;
023: import javax.security.auth.callback.Callback;
024: import javax.security.auth.callback.CallbackHandler;
025: import javax.security.auth.callback.NameCallback;
026: import javax.security.auth.callback.PasswordCallback;
027: import javax.security.auth.callback.UnsupportedCallbackException;
028: import javax.security.auth.login.AppConfigurationEntry;
029: import javax.security.auth.login.Configuration;
030: import javax.security.auth.login.LoginException;
031: import javax.security.auth.login.FailedLoginException;
032: import javax.security.auth.spi.LoginModule;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: *
038: *
039: * @version $Rev: 591213 $ $Date: 2007-11-01 20:46:17 -0700 (Thu, 01 Nov 2007) $
040: */
041: public class AuthenticatorTest extends TestCase {
042: private static final String CONFIG_NAME = "testConfig";
043: private Configuration oldConfiguration;
044: private Authenticator authenticator;
045:
046: public void testLogin() {
047: try {
048: String[] credentials = new String[] { "system", "manager" };
049: Subject s = authenticator.authenticate(credentials);
050: Set principals = s.getPrincipals();
051: assertTrue(principals.contains(new MockPrincipal("system")));
052: } catch (SecurityException e) {
053: e.printStackTrace();
054: fail();
055: }
056: }
057:
058: public void testBadPasswordLogin() throws Exception {
059: testFailure("system", "managerr");
060: }
061:
062: public void testBadUser() throws Exception {
063: testFailure("doesnotexist", "managerr");
064: }
065:
066: public void testNullPasswordLogin() throws Exception {
067: testFailure("system", null);
068: }
069:
070: public void testNullUserLogin() throws Exception {
071: testFailure(null, "manager");
072: }
073:
074: public void testNullCredentialsLogin() throws Exception {
075: testFailure(null, null);
076: }
077:
078: public void testEmptyCredentialsLogin() throws Exception {
079: testFailure("", "");
080: }
081:
082: private void testFailure(String usernane, String password)
083: throws Exception {
084: try {
085: String[] credentials = new String[] { usernane, password };
086: Subject s = authenticator.authenticate(credentials);
087: fail("Did not throw expected exception");
088: } catch (SecurityException e) {
089: // expected
090: }
091: }
092:
093: public void testNoCredentialsLogin() {
094: try {
095: Subject s = authenticator.authenticate(null);
096: fail("Did not throw expected exception");
097: } catch (Exception e) {
098: // expected
099: }
100: }
101:
102: protected void setUp() throws Exception {
103: super .setUp();
104: try {
105: oldConfiguration = Configuration.getConfiguration();
106: } catch (SecurityException e) {
107: oldConfiguration = null;
108: }
109: Configuration loginConfig = new MockConfiguration();
110: Configuration.setConfiguration(loginConfig);
111:
112: authenticator = new Authenticator(CONFIG_NAME, getClass()
113: .getClassLoader());
114: }
115:
116: protected void tearDown() throws Exception {
117: Configuration.setConfiguration(oldConfiguration);
118: super .tearDown();
119: }
120:
121: private class MockConfiguration extends Configuration {
122: public AppConfigurationEntry[] getAppConfigurationEntry(
123: String applicationName) {
124: if (!CONFIG_NAME.equals(applicationName)) {
125: fail();
126: }
127: Map<String, Object> map = new HashMap<String, Object>();
128: map.put("system", "manager");
129: AppConfigurationEntry entry = new AppConfigurationEntry(
130: MockModule.class.getName(),
131: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
132: map);
133: return new AppConfigurationEntry[] { entry };
134: }
135:
136: public void refresh() {
137: }
138: }
139:
140: public static class MockModule implements LoginModule {
141: private Subject subject;
142: private CallbackHandler handler;
143: private Map options;
144: private String username;
145:
146: public void initialize(Subject subject,
147: CallbackHandler callbackHandler, Map sharedState,
148: Map options) {
149: this .subject = subject;
150: this .handler = callbackHandler;
151: this .options = options;
152: }
153:
154: public boolean login() throws LoginException {
155: NameCallback nameCallback = new NameCallback("name");
156: PasswordCallback passwordCallback = new PasswordCallback(
157: "password", false);
158: try {
159: handler.handle(new Callback[] { nameCallback,
160: passwordCallback });
161: username = nameCallback.getName();
162: String password = (String) options.get(username);
163: if (password == null) {
164: throw new FailedLoginException();
165: }
166: if (password.equals(new String(passwordCallback
167: .getPassword()))) {
168: return true;
169: }
170: throw new FailedLoginException();
171: } catch (java.io.IOException e) {
172: throw new FailedLoginException();
173: } catch (UnsupportedCallbackException e) {
174: throw new FailedLoginException();
175: }
176: }
177:
178: public boolean commit() throws LoginException {
179: subject.getPrincipals().add(new MockPrincipal(username));
180: return true;
181: }
182:
183: public boolean logout() throws LoginException {
184: return true;
185: }
186:
187: public boolean abort() throws LoginException {
188: return true;
189: }
190: }
191:
192: private static class MockPrincipal implements Principal {
193: private final String name;
194:
195: public MockPrincipal(String name) {
196: this .name = name;
197: }
198:
199: public String getName() {
200: return name;
201: }
202:
203: public boolean equals(Object o) {
204: if (this == o)
205: return true;
206: if (!(o instanceof MockPrincipal))
207: return false;
208:
209: final MockPrincipal mockPrincipal = (MockPrincipal) o;
210:
211: if (!name.equals(mockPrincipal.name))
212: return false;
213:
214: return true;
215: }
216:
217: public int hashCode() {
218: return name.hashCode();
219: }
220: }
221: }
|