1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }