001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.test;
023:
024: import java.io.FilePermission;
025: import java.net.URL;
026: import java.security.CodeSource;
027: import java.security.Permission;
028: import java.security.PermissionCollection;
029: import java.security.Policy;
030: import java.security.Principal;
031:
032: import javax.security.auth.Subject;
033:
034: import junit.framework.TestCase;
035: import org.jboss.security.SecurityAssociation;
036: import org.jboss.security.SimplePrincipal;
037:
038: /**
039: * Test that the security context thread locals propagate to child threads
040: *
041: @author Scott.Stark@jboss.org
042: @version $Revision: 57211 $
043: */
044: public class SAInheritableThreadLocalUnitTestCase extends TestCase {
045: private Principal authPrincipal;
046: private Subject authSubject;
047:
048: public SAInheritableThreadLocalUnitTestCase(String name) {
049: super (name);
050: }
051:
052: /**
053: * Test the expected security context exists via the SecurityAssociation accessors
054: */
055: public void testSecurityContext() {
056: authPrincipal = new SimplePrincipal("jduke");
057: authSubject = new Subject();
058: authSubject.getPrincipals().add(authPrincipal);
059: SecurityAssociation.pushSubjectContext(authSubject,
060: authPrincipal, "theduke");
061: validateSettings(false);
062: }
063:
064: /**
065: * Validate that a child thread sees its parent
066: */
067: public void testInheritableThreadLocal() throws Exception {
068: authPrincipal = new SimplePrincipal("jduke");
069: authSubject = new Subject();
070: authSubject.getPrincipals().add(authPrincipal);
071: SecurityAssociation.pushSubjectContext(authSubject,
072: authPrincipal, "theduke");
073: validateSettings(false);
074:
075: TestThread t = new TestThread("testInheritableThreadLocal",
076: false);
077: t.start();
078: t.join();
079: if (t.error != null) {
080: t.error.printStackTrace();
081: fail("TestThread saw an error");
082: }
083: }
084:
085: /**
086: * SecurityAssociation.getSubject() == authSubject
087: * SecurityAssociation.getPrincipal() == authPrincipal
088: */
089: private void validateSettings(boolean expectNull) {
090: Subject s = SecurityAssociation.getSubject();
091: Principal p = SecurityAssociation.getPrincipal();
092:
093: if (expectNull) {
094: assertNull("getSubject() == null", s);
095: assertNull("getPrincipal() == null", p);
096: } else {
097: assertTrue("getSubject() == authSubject", authSubject
098: .equals(s));
099: assertTrue("getPrincipal() == authPrincipal", authPrincipal
100: .equals(p));
101: }
102: }
103:
104: class TestThread extends Thread {
105: Throwable error;
106: boolean expectNull;
107:
108: TestThread(String name, boolean expectNull) {
109: super (name);
110: this .expectNull = expectNull;
111: }
112:
113: public void run() {
114: try {
115: validateSettings(expectNull);
116: } catch (Throwable e) {
117: error = e;
118: }
119: }
120: }
121:
122: protected void setUp() {
123: System.setProperty(
124: "org.jboss.security.SecurityAssociation.ThreadLocal",
125: "false");
126: SecurityAssociation.setServer();
127: }
128: }
|