KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > network > protocol > SubjectCarryingProtocolTest


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.network.protocol;
19
20 // Removed to allow building Geronimo on non-Sun JDK. When this test case is updated and enabled
21
// again it should be updated so that it is not specific to the Sun JDK.
22
// import com.sun.security.auth.login.ConfigFile;
23
import org.activeio.AcceptListener;
24 import org.activeio.AsyncChannelServer;
25 import org.activeio.Channel;
26 import org.activeio.Packet;
27 import org.activeio.RequestChannel;
28 import org.activeio.RequestListener;
29 import org.activeio.adapter.AsyncChannelToClientRequestChannel;
30 import org.activeio.adapter.AsyncChannelToServerRequestChannel;
31 import org.activeio.adapter.AsyncToSyncChannel;
32 import org.activeio.adapter.SyncToAsyncChannel;
33 import org.activeio.adapter.SyncToAsyncChannelServer;
34 import org.activeio.filter.PacketAggregatingAsyncChannel;
35 import org.activeio.net.SocketSyncChannelFactory;
36 import org.activeio.packet.ByteArrayPacket;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.geronimo.gbean.AbstractName;
40 import org.apache.geronimo.gbean.GBeanData;
41 import org.apache.geronimo.security.AbstractTest;
42 import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
43 import org.apache.geronimo.security.jaas.LoginModuleGBean;
44 import org.apache.geronimo.security.realm.GenericSecurityRealm;
45
46 import javax.security.auth.Subject JavaDoc;
47 import javax.security.auth.login.Configuration JavaDoc;
48 import javax.security.auth.login.LoginContext JavaDoc;
49 import java.io.File JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.net.URI JavaDoc;
52 import java.security.AccessController JavaDoc;
53 import java.security.PrivilegedExceptionAction JavaDoc;
54 import java.util.Properties JavaDoc;
55
56
57 /**
58  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
59  */

60 public class SubjectCarryingProtocolTest extends AbstractTest implements RequestListener {
61     
62     final static private Log log = LogFactory.getLog(SubjectCarryingProtocolTest.class);
63     
64     protected AbstractName testCE;
65     protected AbstractName testRealm;
66
67     private Subject JavaDoc clientSubject;
68     private Subject JavaDoc serverSubject;
69     private URI JavaDoc serverURI;
70     private AsyncChannelServer server;
71
72     public void testNothing() throws Exception JavaDoc {
73     }
74
75     /*
76      * Enable this test again once its working.
77      */

78     public void disabledtest() throws Exception JavaDoc {
79
80         SocketSyncChannelFactory factory = new SocketSyncChannelFactory();
81         final RequestChannel channel =
82             new AsyncChannelToClientRequestChannel(
83                 AsyncToSyncChannel.adapt(
84                     new SubjectCarryingChannel(
85                         new PacketAggregatingAsyncChannel(
86                             SyncToAsyncChannel.adapt(
87                                  factory.openSyncChannel(serverURI))))));
88         try {
89             channel.start();
90             Subject.doAs(clientSubject, new PrivilegedExceptionAction JavaDoc() {
91                 public Object JavaDoc run() throws Exception JavaDoc {
92
93                     Subject JavaDoc subject = Subject.getSubject(AccessController.getContext());
94                     String JavaDoc p = subject.getPrincipals().iterator().next().toString();
95                     log.info("Sending request as: "+p);
96
97                     Packet request = new ByteArrayPacket("whoami".getBytes());
98                     Packet response = channel.request(request, 1000*5*1000);
99
100                     assertNotNull(response);
101                     assertEquals( p, new String JavaDoc(response.sliceAsBytes()) );
102                     return null;
103                 }
104             });
105         } finally {
106             channel.dispose();
107         }
108     }
109
110
111     public void setUp() throws Exception JavaDoc {
112         needServerInfo = true;
113         super.setUp();
114
115         GBeanData gbean;
116
117         gbean = buildGBeanData ("name", "PropertiesLoginModule", LoginModuleGBean.getGBeanInfo());
118         testCE = gbean.getAbstractName();
119         gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule");
120         gbean.setAttribute("serverSide", new Boolean JavaDoc(true));
121         Properties JavaDoc props = new Properties JavaDoc();
122         props.put("usersURI", new File JavaDoc(BASEDIR, "src/test/data/data/users.properties").toURI().toString());
123         props.put("groupsURI", new File JavaDoc(BASEDIR, "src/test/data/data/groups.properties").toURI().toString());
124         gbean.setAttribute("options", props);
125         gbean.setAttribute("loginDomainName", "PropertiesDomain");
126         kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
127
128         gbean = buildGBeanData("name", "PropertiesLoginModuleUse", JaasLoginModuleUse.getGBeanInfo());
129         AbstractName testUseName = gbean.getAbstractName();
130         gbean.setAttribute("controlFlag", "REQUIRED");
131         gbean.setReferencePattern("LoginModule", testCE);
132         kernel.loadGBean(gbean, JaasLoginModuleUse.class.getClassLoader());
133
134         gbean = buildGBeanData("name", "PropertiesSecurityRealm", GenericSecurityRealm.getGBeanInfo());
135         testRealm = gbean.getAbstractName();
136         gbean.setAttribute("realmName", "properties-realm");
137         gbean.setReferencePattern("LoginModuleConfiguration", testUseName);
138         gbean.setReferencePattern("ServerInfo", serverInfo);
139         kernel.loadGBean(gbean, GenericSecurityRealm.class.getClassLoader());
140
141         kernel.startGBean(testCE);
142         kernel.startGBean(testUseName);
143         kernel.startGBean(testRealm);
144
145         LoginContext JavaDoc context = new LoginContext JavaDoc("properties", new AbstractTest.UsernamePasswordCallback("alan", "starcraft"));
146         context.login();
147         clientSubject = context.getSubject();
148
149         context = new LoginContext JavaDoc("properties", new AbstractTest.UsernamePasswordCallback("izumi", "violin"));
150         context.login();
151         serverSubject = context.getSubject();
152
153         SocketSyncChannelFactory factory = new SocketSyncChannelFactory();
154         server = new SyncToAsyncChannelServer(
155                 factory.bindSyncChannel(new URI JavaDoc("tcp://localhost:0")));
156
157         server.setAcceptListener(new AcceptListener() {
158             public void onAccept(Channel channel) {
159                 RequestChannel requestChannel=null;
160                 try {
161
162                     requestChannel =
163                         new AsyncChannelToServerRequestChannel(
164                             new SubjectCarryingChannel(
165                                 new PacketAggregatingAsyncChannel(
166                                     SyncToAsyncChannel.adapt(channel))));
167
168                     requestChannel.setRequestListener(SubjectCarryingProtocolTest.this);
169                     requestChannel.start();
170
171                 } catch (IOException JavaDoc e) {
172                     log.info("Failed to accept connection.", e);
173                     if( requestChannel!=null )
174                         requestChannel.dispose();
175                     else
176                         channel.dispose();
177                 }
178             }
179             public void onAcceptError(IOException JavaDoc error) {
180                 log.info("Accept Failed: "+error);
181             }
182         });
183
184         server.start();
185         serverURI = server.getConnectURI();
186
187     }
188
189     public void tearDown() throws Exception JavaDoc {
190         server.dispose();
191
192         kernel.stopGBean(testRealm);
193         kernel.stopGBean(testCE);
194         kernel.stopGBean(serverInfo);
195         kernel.unloadGBean(testCE);
196         kernel.unloadGBean(testRealm);
197         kernel.unloadGBean(serverInfo);
198         super.tearDown();
199 // Configuration.setConfiguration(new ConfigFile());
200
}
201
202     public Packet onRequest(Packet packet) {
203
204         String JavaDoc p="";
205         try {
206             SubjectContext ctx = (SubjectContext)packet.narrow(SubjectContext.class);
207             Subject JavaDoc subject = ctx.getSubject();
208             p = subject.getPrincipals().iterator().next().toString();
209             log.info("Received request as: "+p);
210         } catch ( Exception JavaDoc e ) {
211             e.printStackTrace();
212         }
213         return new ByteArrayPacket(p.getBytes());
214     }
215
216     public void onRquestError(IOException JavaDoc arg) {
217     }
218
219
220 }
221
Popular Tags