1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security;
18  
19  import java.security.Principal;
20  
21  import javax.security.auth.login.LoginContext;
22  import javax.security.auth.login.LoginException;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.jetspeed.security.impl.PassiveCallbackHandler;
28  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
29  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
30  
31  /***
32   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
33   */
34  public class TestLoginModule extends AbstractSecurityTestcase
35  {
36      /*** <p>The JAAS login context.</p> */
37      private LoginContext loginContext = null;
38  
39      /***
40       * @see junit.framework.TestCase#setUp()
41       */
42      public void setUp() throws Exception
43      {
44          super.setUp();
45          initUserObject();
46  
47          // Set up login context.
48          try {
49              PassiveCallbackHandler pch = new PassiveCallbackHandler("anonlogin", "password");
50              loginContext = new LoginContext("Jetspeed", pch);
51          }
52          catch (LoginException le)
53          {
54              le.printStackTrace();
55              assertTrue("\t\t[TestLoginModule] Failed to setup test.", false);
56          }
57      }
58  
59      /***
60       * @see junit.framework.TestCase#tearDown()
61       */
62      public void tearDown() throws Exception
63      {
64          destroyUserObject();
65          super.tearDown();
66          
67      }
68  
69      public static Test suite()
70      {
71          // All methods starting with "test" will be executed in the test suite.
72          return new TestSuite(TestLoginModule.class);
73      }
74  
75      public void testLogin() throws LoginException
76      {
77          loginContext.login();
78          Principal found = SecurityHelper.getPrincipal(loginContext.getSubject(), UserPrincipal.class);
79          assertNotNull("found principal is null", found);
80          assertTrue("found principal should be anonlogin, " + found.getName(), found.getName().equals((new UserPrincipalImpl("anonlogin")).getName()));      
81      }
82      
83      public void testLogout() throws LoginException
84      {
85          loginContext.login();
86          loginContext.logout();
87          Principal found = SecurityHelper.getBestPrincipal(loginContext.getSubject(), UserPrincipal.class);
88          assertNull("found principal is not null", found);
89      }
90  
91      /***
92       * <p>Initialize user test object.</p>
93       */
94      protected void initUserObject()
95      {
96          try
97          {
98              ums.addUser("anonlogin", "password");
99          }
100         catch (SecurityException sex)
101         {
102         }
103     }
104 
105     /***
106      * <p>Destroy user test object.</p>
107      */
108     protected void destroyUserObject() throws Exception
109     {
110         ums.removeUser("anonlogin");
111     }
112 
113 }