KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > TimeoutTest


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
18 package org.apache.geronimo.security.jaas;
19
20 import org.apache.geronimo.gbean.AbstractName;
21 import org.apache.geronimo.gbean.GBeanData;
22 import org.apache.geronimo.security.AbstractTest;
23 import org.apache.geronimo.security.ContextManager;
24 import org.apache.geronimo.security.DomainPrincipal;
25 import org.apache.geronimo.security.IdentificationPrincipal;
26 import org.apache.geronimo.security.RealmPrincipal;
27 import org.apache.geronimo.security.realm.GenericSecurityRealm;
28
29 import javax.security.auth.Subject JavaDoc;
30 import javax.security.auth.login.LoginContext JavaDoc;
31 import java.io.File JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /**
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class TimeoutTest extends AbstractTest {
40     
41     protected AbstractName testCE;
42     protected AbstractName testRealm;
43     protected AbstractName clientLM;
44     protected AbstractName clientCE;
45
46     public void setUp() throws Exception JavaDoc {
47         timeoutTest = true;
48         needServerInfo = true;
49         needLoginConfiguration = true;
50         super.setUp();
51
52         GBeanData gbean;
53
54         // Create all the parts
55

56         gbean = buildGBeanData ("name", "PropertiesLoginModule", LoginModuleGBean.getGBeanInfo());
57         testCE = gbean.getAbstractName();
58         gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule");
59         gbean.setAttribute("serverSide", Boolean.TRUE);
60         Properties JavaDoc props = new Properties JavaDoc();
61         props.put("usersURI", new File JavaDoc(BASEDIR, "src/test/data/data/users.properties").toURI().toString());
62         props.put("groupsURI", new File JavaDoc(BASEDIR, "src/test/data/data/groups.properties").toURI().toString());
63         gbean.setAttribute("options", props);
64         gbean.setAttribute("loginDomainName", "PropertiesDomain");
65         gbean.setAttribute("wrapPrincipals", Boolean.TRUE);
66         kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
67
68         gbean = buildGBeanData("name", "PropertiesLoginModuleUse", JaasLoginModuleUse.getGBeanInfo());
69         AbstractName testUseName = gbean.getAbstractName();
70         gbean.setAttribute("controlFlag", "REQUIRED");
71         gbean.setReferencePattern("LoginModule", testCE);
72         kernel.loadGBean(gbean, JaasLoginModuleUse.class.getClassLoader());
73
74         gbean = buildGBeanData("name", "PropertiesSecurityRealm", GenericSecurityRealm.getGBeanInfo());
75         testRealm = gbean.getAbstractName();
76         gbean.setAttribute("realmName", "properties-realm");
77         gbean.setReferencePattern("LoginModuleConfiguration", testUseName);
78         gbean.setReferencePattern("ServerInfo", serverInfo);
79         gbean.setReferencePattern("LoginService", loginService);
80         kernel.loadGBean(gbean, GenericSecurityRealm.class.getClassLoader());
81
82         gbean = buildGBeanData("name", "ClientPropertiesLoginModule", LoginModuleGBean.getGBeanInfo());
83         clientLM = gbean.getAbstractName();
84         gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.jaas.client.JaasLoginCoordinator");
85         gbean.setAttribute("serverSide", Boolean.FALSE);
86         props = new Properties JavaDoc();
87         props.put("host", "localhost");
88         props.put("port", "4242");
89         props.put("realm", "properties-realm");
90         gbean.setAttribute("options", props);
91         kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
92
93         gbean = buildGBeanData("name", "ClientConfigurationEntry", DirectConfigurationEntry.getGBeanInfo());
94         clientCE = gbean.getAbstractName();
95         gbean.setAttribute("applicationConfigName", "properties-client");
96         gbean.setAttribute("controlFlag", LoginModuleControlFlag.REQUIRED);
97         gbean.setReferencePattern("Module", clientLM);
98         kernel.loadGBean(gbean, DirectConfigurationEntry.class.getClassLoader());
99
100         kernel.startGBean(loginConfiguration);
101         kernel.startGBean(clientLM);
102         kernel.startGBean(clientCE);
103         kernel.startGBean(testCE);
104         kernel.startGBean(testUseName);
105         kernel.startGBean(testRealm);
106     }
107
108     public void tearDown() throws Exception JavaDoc {
109         kernel.stopGBean(testRealm);
110         kernel.stopGBean(testCE);
111         kernel.stopGBean(clientCE);
112         kernel.stopGBean(clientLM);
113         kernel.stopGBean(loginConfiguration);
114         kernel.stopGBean(serverInfo);
115
116         kernel.unloadGBean(testCE);
117         kernel.unloadGBean(testRealm);
118         kernel.unloadGBean(clientCE);
119         kernel.unloadGBean(clientLM);
120         kernel.unloadGBean(loginConfiguration);
121         kernel.unloadGBean(serverInfo);
122
123         kernel.stopGBean(serverStub);
124         kernel.stopGBean(loginService);
125
126         kernel.unloadGBean(loginService);
127         kernel.unloadGBean(serverStub);
128
129         kernel.shutdown();
130     }
131
132     public void testNothing() {
133     }
134
135     public void testTimeout() throws Exception JavaDoc {
136
137         LoginContext JavaDoc context = new LoginContext JavaDoc("properties-client", new AbstractTest.UsernamePasswordCallback("alan", "starcraft"));
138
139         context.login();
140         Subject JavaDoc subject = context.getSubject();
141         assertTrue("expected non-null client subject", subject != null);
142         Set JavaDoc set = subject.getPrincipals(IdentificationPrincipal.class);
143         assertEquals("client subject should have one ID principal", set.size(), 1);
144         IdentificationPrincipal idp = (IdentificationPrincipal) set.iterator().next();
145         subject = ContextManager.getRegisteredSubject(idp.getId());
146
147         assertTrue("expected non-null server subject", subject != null);
148         assertTrue("server subject should have one remote principal", subject.getPrincipals(IdentificationPrincipal.class).size() == 1);
149         IdentificationPrincipal remote = (IdentificationPrincipal) subject.getPrincipals(IdentificationPrincipal.class).iterator().next();
150         assertTrue("server subject should be associated with remote id", ContextManager.getRegisteredSubject(remote.getId()) != null);
151         assertEquals("server-side subject should have seven principal", 7, subject.getPrincipals().size());
152         assertTrue("server subject should have two realm principal", subject.getPrincipals(RealmPrincipal.class).size() == 2);
153         assertTrue("server subject should have two domain principal", subject.getPrincipals(DomainPrincipal.class).size() == 2);
154
155         assertTrue("id of server subject should be non-null", ContextManager.getSubjectId(subject) != null);
156
157         Thread.sleep(3000); // wait for timeout to kick in
158

159         assertTrue("id of server subject should be non-null", ContextManager.getSubjectId(subject) != null);
160
161         Thread.sleep(7000); // wait for timeout to kick in
162

163         assertTrue("id of server subject should be null", ContextManager.getSubjectId(subject) == null);
164     }
165 }
166
Popular Tags