KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > sudo > SudoTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * SudoTest.java
20  *
21  * JUnit based test
22  */

23
24 // package path
25
package com.rift.coad.lib.security.sudo;
26
27 // java imports
28
import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 // junit imports
32
import junit.framework.*;
33
34 // log 4j imports
35
import org.apache.log4j.Logger;
36
37 // imports
38
import com.rift.coad.lib.security.AuthorizationException;
39 import com.rift.coad.lib.security.UserSession;
40 import com.rift.coad.lib.security.RoleManager;
41 import com.rift.coad.lib.security.SessionManager;
42 import com.rift.coad.lib.security.ThreadPermissionSession;
43 import com.rift.coad.lib.security.ThreadsPermissionContainer;
44 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
45 import com.rift.coad.lib.security.login.LoginManager;
46 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
47 import com.rift.coad.lib.security.login.SessionLogin;
48 import com.rift.coad.lib.security.user.UserSessionManager;
49 import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
50 import com.rift.coad.lib.security.user.UserStoreManager;
51 import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
52 import com.rift.coad.lib.thread.BasicThread;
53 import com.rift.coad.lib.thread.CoadunationThreadGroup;
54
55 /**
56  * Sudo test
57  *
58  * @author Brett Chaldecott
59  */

60 public class SudoTest extends TestCase implements SudoCallbackHandler {
61     
62     /**
63      * This object will sudo by user
64      */

65     public class SudoByUserTest extends BasicThread {
66         
67         private SudoCallbackHandler handler = null;
68         
69         /**
70          * The constructor of the handler
71          */

72         public SudoByUserTest (SudoCallbackHandler handler) throws Exception JavaDoc {
73             this.handler = handler;
74         }
75         
76         
77         /**
78          * This method will process the result
79          */

80         public void process() {
81             try {
82                 Sudo.sudoThreadByUser("test", handler);
83             } catch (Exception JavaDoc ex) {
84                 System.out.println("Failed to process : " + ex.getMessage());
85                 ex.printStackTrace(System.out);
86             }
87         }
88     }
89     
90     
91     /**
92      * This object will sudo by user
93      */

94     public class SudoBySessionIdTest extends BasicThread {
95         
96         private String JavaDoc sessionId = null;
97         private SudoCallbackHandler handler = null;
98         
99         /**
100          * The constructor of the handler
101          */

102         public SudoBySessionIdTest (String JavaDoc sessionId,
103                 SudoCallbackHandler handler) throws Exception JavaDoc {
104             this.sessionId = sessionId;
105             this.handler = handler;
106         }
107         
108         
109         /**
110          * This method will process the result
111          */

112         public void process() {
113             try {
114                 Sudo.sudoThreadBySessionId(sessionId, handler);
115             } catch (Exception JavaDoc ex) {
116                 System.out.println("Failed to process : " + ex.getMessage());
117                 ex.printStackTrace(System.out);
118             }
119         }
120     }
121     
122     
123     // private member variables
124
private boolean called = false;
125     
126     public SudoTest(String JavaDoc testName) {
127         super(testName);
128     }
129
130     protected void setUp() throws Exception JavaDoc {
131     }
132
133     protected void tearDown() throws Exception JavaDoc {
134     }
135
136     public static Test suite() {
137         TestSuite suite = new TestSuite(SudoTest.class);
138         
139         return suite;
140     }
141
142     /**
143      * Test of testSudo method, of class com.rift.coad.lib.security.sudo.Sudo.
144      */

145     public void testSudo() throws Exception JavaDoc {
146         System.out.println("testSudo");
147         
148         ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
149         ThreadsPermissionContainerAccessor.init(permissions);
150         SessionManager.init(permissions);
151         UserStoreManager userStoreManager = new UserStoreManager();
152         UserStoreManagerAccessor.init(userStoreManager);
153         UserSessionManager sessionManager = new UserSessionManager(permissions,
154                 userStoreManager);
155         UserSessionManagerAccessor.init(sessionManager);
156         LoginManager.init(sessionManager,userStoreManager);
157         
158         // add a user to the session for the current thread
159
RoleManager.getInstance();
160         
161         // instanciate the thread manager
162
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
163             userStoreManager);
164         
165         // sudo the user
166
called = false;
167         SudoByUserTest sudoByUserTest = new SudoByUserTest(this);
168         threadGroup.addThread(sudoByUserTest,"test1");
169         sudoByUserTest.start();
170         sudoByUserTest.join();
171         if (called == false) {
172             fail("Failed to call as user test1");
173         }
174         
175         // log the user in
176
SessionLogin sessionLogin = new SessionLogin(
177                 new PasswordInfoHandler("test","112233"));
178         sessionLogin.login();
179         
180         called = false;
181         SudoBySessionIdTest sudoBySessionIdTest = new SudoBySessionIdTest(
182                 sessionLogin.getUser().getSessionId(),this);
183         threadGroup.addThread(sudoBySessionIdTest,"test1");
184         sudoBySessionIdTest.start();
185         sudoBySessionIdTest.join();
186         if (called == false) {
187             fail("Failed to sudo the user to the session id");
188         }
189         
190         
191         called = false;
192         
193         // add a new user object and add to the permission
194
Set JavaDoc set = new HashSet JavaDoc();
195         set.add("test");
196         set.add("test1");
197         set.add("test2");
198         set.add("test3");
199         UserSession user = new UserSession("test", set);
200         ThreadPermissionSession currentSession = new ThreadPermissionSession(
201                 new Long JavaDoc(Thread.currentThread().getId()),user);
202         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
203                 currentSession);
204         
205         Sudo.sudoThreadByUser("test2", this);
206         
207         if (called == false) {
208             fail("Failed to sudo the user to the session id");
209         }
210         
211         if (currentSession != permissions.getSession(
212                 new Long JavaDoc(Thread.currentThread().getId()))) {
213             fail("Failed permission were not reset properly");
214         }
215         
216         called = false;
217         Sudo.sudoThreadBySessionId(sessionLogin.getUser().getSessionId(),this);
218         
219         
220         if (called == false) {
221             fail("Failed to sudo the user to the session id");
222         }
223         
224         if (currentSession != permissions.getSession(
225                 new Long JavaDoc(Thread.currentThread().getId()))) {
226             fail("Failed permission were not reset properly");
227         }
228         
229     }
230     
231     
232     /**
233      * The method that will run the thread as a user.
234      */

235     public void process() {
236         called = true;
237     }
238 }
239
Popular Tags