001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * SudoTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package path
025: package com.rift.coad.lib.security.sudo;
026:
027: // java imports
028: import java.util.Set;
029: import java.util.HashSet;
030:
031: // junit imports
032: import junit.framework.*;
033:
034: // log 4j imports
035: import org.apache.log4j.Logger;
036:
037: // imports
038: import com.rift.coad.lib.security.AuthorizationException;
039: import com.rift.coad.lib.security.UserSession;
040: import com.rift.coad.lib.security.RoleManager;
041: import com.rift.coad.lib.security.SessionManager;
042: import com.rift.coad.lib.security.ThreadPermissionSession;
043: import com.rift.coad.lib.security.ThreadsPermissionContainer;
044: import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
045: import com.rift.coad.lib.security.login.LoginManager;
046: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
047: import com.rift.coad.lib.security.login.SessionLogin;
048: import com.rift.coad.lib.security.user.UserSessionManager;
049: import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
050: import com.rift.coad.lib.security.user.UserStoreManager;
051: import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
052: import com.rift.coad.lib.thread.BasicThread;
053: import com.rift.coad.lib.thread.CoadunationThreadGroup;
054:
055: /**
056: * Sudo test
057: *
058: * @author Brett Chaldecott
059: */
060: public class SudoTest extends TestCase implements SudoCallbackHandler {
061:
062: /**
063: * This object will sudo by user
064: */
065: public class SudoByUserTest extends BasicThread {
066:
067: private SudoCallbackHandler handler = null;
068:
069: /**
070: * The constructor of the handler
071: */
072: public SudoByUserTest(SudoCallbackHandler handler)
073: throws Exception {
074: this .handler = handler;
075: }
076:
077: /**
078: * This method will process the result
079: */
080: public void process() {
081: try {
082: Sudo.sudoThreadByUser("test", handler);
083: } catch (Exception ex) {
084: System.out.println("Failed to process : "
085: + ex.getMessage());
086: ex.printStackTrace(System.out);
087: }
088: }
089: }
090:
091: /**
092: * This object will sudo by user
093: */
094: public class SudoBySessionIdTest extends BasicThread {
095:
096: private String sessionId = null;
097: private SudoCallbackHandler handler = null;
098:
099: /**
100: * The constructor of the handler
101: */
102: public SudoBySessionIdTest(String sessionId,
103: SudoCallbackHandler handler) throws Exception {
104: this .sessionId = sessionId;
105: this .handler = handler;
106: }
107:
108: /**
109: * This method will process the result
110: */
111: public void process() {
112: try {
113: Sudo.sudoThreadBySessionId(sessionId, handler);
114: } catch (Exception ex) {
115: System.out.println("Failed to process : "
116: + ex.getMessage());
117: ex.printStackTrace(System.out);
118: }
119: }
120: }
121:
122: // private member variables
123: private boolean called = false;
124:
125: public SudoTest(String testName) {
126: super (testName);
127: }
128:
129: protected void setUp() throws Exception {
130: }
131:
132: protected void tearDown() throws Exception {
133: }
134:
135: public static Test suite() {
136: TestSuite suite = new TestSuite(SudoTest.class);
137:
138: return suite;
139: }
140:
141: /**
142: * Test of testSudo method, of class com.rift.coad.lib.security.sudo.Sudo.
143: */
144: public void testSudo() throws Exception {
145: System.out.println("testSudo");
146:
147: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
148: ThreadsPermissionContainerAccessor.init(permissions);
149: SessionManager.init(permissions);
150: UserStoreManager userStoreManager = new UserStoreManager();
151: UserStoreManagerAccessor.init(userStoreManager);
152: UserSessionManager sessionManager = new UserSessionManager(
153: permissions, userStoreManager);
154: UserSessionManagerAccessor.init(sessionManager);
155: LoginManager.init(sessionManager, userStoreManager);
156:
157: // add a user to the session for the current thread
158: RoleManager.getInstance();
159:
160: // instanciate the thread manager
161: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
162: sessionManager, userStoreManager);
163:
164: // sudo the user
165: called = false;
166: SudoByUserTest sudoByUserTest = new SudoByUserTest(this );
167: threadGroup.addThread(sudoByUserTest, "test1");
168: sudoByUserTest.start();
169: sudoByUserTest.join();
170: if (called == false) {
171: fail("Failed to call as user test1");
172: }
173:
174: // log the user in
175: SessionLogin sessionLogin = new SessionLogin(
176: new PasswordInfoHandler("test", "112233"));
177: sessionLogin.login();
178:
179: called = false;
180: SudoBySessionIdTest sudoBySessionIdTest = new SudoBySessionIdTest(
181: sessionLogin.getUser().getSessionId(), this );
182: threadGroup.addThread(sudoBySessionIdTest, "test1");
183: sudoBySessionIdTest.start();
184: sudoBySessionIdTest.join();
185: if (called == false) {
186: fail("Failed to sudo the user to the session id");
187: }
188:
189: called = false;
190:
191: // add a new user object and add to the permission
192: Set set = new HashSet();
193: set.add("test");
194: set.add("test1");
195: set.add("test2");
196: set.add("test3");
197: UserSession user = new UserSession("test", set);
198: ThreadPermissionSession currentSession = new ThreadPermissionSession(
199: new Long(Thread.currentThread().getId()), user);
200: permissions.putSession(
201: new Long(Thread.currentThread().getId()),
202: currentSession);
203:
204: Sudo.sudoThreadByUser("test2", this );
205:
206: if (called == false) {
207: fail("Failed to sudo the user to the session id");
208: }
209:
210: if (currentSession != permissions.getSession(new Long(Thread
211: .currentThread().getId()))) {
212: fail("Failed permission were not reset properly");
213: }
214:
215: called = false;
216: Sudo.sudoThreadBySessionId(sessionLogin.getUser()
217: .getSessionId(), this );
218:
219: if (called == false) {
220: fail("Failed to sudo the user to the session id");
221: }
222:
223: if (currentSession != permissions.getSession(new Long(Thread
224: .currentThread().getId()))) {
225: fail("Failed permission were not reset properly");
226: }
227:
228: }
229:
230: /**
231: * The method that will run the thread as a user.
232: */
233: public void process() {
234: called = true;
235: }
236: }
|