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: */
017: package org.apache.jetspeed.security;
018:
019: import java.security.Principal;
020:
021: import javax.security.auth.login.LoginContext;
022: import javax.security.auth.login.LoginException;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.jetspeed.security.impl.PassiveCallbackHandler;
028: import org.apache.jetspeed.security.impl.UserPrincipalImpl;
029: import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
030:
031: /**
032: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
033: */
034: public class TestLoginModule extends AbstractSecurityTestcase {
035: /** <p>The JAAS login context.</p> */
036: private LoginContext loginContext = null;
037:
038: /**
039: * @see junit.framework.TestCase#setUp()
040: */
041: public void setUp() throws Exception {
042: super .setUp();
043: initUserObject();
044:
045: // Set up login context.
046: try {
047: PassiveCallbackHandler pch = new PassiveCallbackHandler(
048: "anonlogin", "password");
049: loginContext = new LoginContext("Jetspeed", pch);
050: } catch (LoginException le) {
051: le.printStackTrace();
052: assertTrue("\t\t[TestLoginModule] Failed to setup test.",
053: false);
054: }
055: }
056:
057: /**
058: * @see junit.framework.TestCase#tearDown()
059: */
060: public void tearDown() throws Exception {
061: destroyUserObject();
062: super .tearDown();
063:
064: }
065:
066: public static Test suite() {
067: // All methods starting with "test" will be executed in the test suite.
068: return new TestSuite(TestLoginModule.class);
069: }
070:
071: public void testLogin() throws LoginException {
072: loginContext.login();
073: Principal found = SecurityHelper.getPrincipal(loginContext
074: .getSubject(), UserPrincipal.class);
075: assertNotNull("found principal is null", found);
076: assertTrue("found principal should be anonlogin, "
077: + found.getName(), found.getName().equals(
078: (new UserPrincipalImpl("anonlogin")).getName()));
079: }
080:
081: public void testLogout() throws LoginException {
082: loginContext.login();
083: loginContext.logout();
084: Principal found = SecurityHelper.getBestPrincipal(loginContext
085: .getSubject(), UserPrincipal.class);
086: assertNull("found principal is not null", found);
087: }
088:
089: /**
090: * <p>Initialize user test object.</p>
091: */
092: protected void initUserObject() {
093: try {
094: ums.addUser("anonlogin", "password");
095: } catch (SecurityException sex) {
096: }
097: }
098:
099: /**
100: * <p>Destroy user test object.</p>
101: */
102: protected void destroyUserObject() throws Exception {
103: ums.removeUser("anonlogin");
104: }
105:
106: }
|