001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import junit.framework.TestCase;
020:
021: import javax.security.auth.login.FailedLoginException;
022: import java.util.concurrent.CountDownLatch;
023: import java.util.concurrent.TimeUnit;
024:
025: public class ClientSecurityTest extends TestCase {
026: protected void setUp() throws Exception {
027: super .setUp();
028: LoginTestUtil.initialize();
029: ClientSecurity.logout();
030: ClientSecurity.setIdentityResolver(null);
031: System.getProperties().remove(
032: ClientSecurity.IDENTITY_RESOLVER_STRATEGY);
033: }
034:
035: public void testDefaultStrategy() {
036: IdentityResolver identityResolver = ClientSecurity
037: .getIdentityResolver();
038: assertNotNull("identityResolver is null", identityResolver);
039: assertTrue(
040: "identityResolver should be an instance of JaasIdentityResolver",
041: identityResolver instanceof JaasIdentityResolver);
042: }
043:
044: public void testSimpleStrategy() {
045: System.setProperty(ClientSecurity.IDENTITY_RESOLVER_STRATEGY,
046: "simple");
047: IdentityResolver identityResolver = ClientSecurity
048: .getIdentityResolver();
049: assertNotNull("identityResolver is null", identityResolver);
050: assertTrue(
051: "identityResolver should be an instance of ClientSecurity.SimpleIdentityResolver",
052: identityResolver instanceof ClientSecurity.SimpleIdentityResolver);
053: }
054:
055: public void testJaasStrategy() {
056: System.setProperty(ClientSecurity.IDENTITY_RESOLVER_STRATEGY,
057: "jaas");
058: IdentityResolver identityResolver = ClientSecurity
059: .getIdentityResolver();
060: assertNotNull("identityResolver is null", identityResolver);
061: assertTrue(
062: "identityResolver should be an instance of JaasIdentityResolver",
063: identityResolver instanceof JaasIdentityResolver);
064: }
065:
066: public void testLogin() throws FailedLoginException {
067: // setup the server response
068: LoginTestUtil.setAuthGranted();
069:
070: // attempt a login
071: ClientSecurity.login("jonathan", "secret");
072:
073: // Verify stored server request
074: assertTrue(
075: "serverRequest should be an instance of AuthenticationRequest",
076: LoginTestUtil.serverRequest instanceof AuthenticationRequest);
077: AuthenticationRequest authenticationRequest = (AuthenticationRequest) LoginTestUtil.serverRequest;
078: assertEquals("jonathan", authenticationRequest.getUsername());
079: assertEquals("secret", authenticationRequest.getCredentials());
080:
081: // verify client identity
082: assertEquals("SecretIdentity", ClientSecurity.getIdentity());
083:
084: // verify we are using the simple client identity strategy
085: assertTrue(
086: "ClientSecurity.getIdentityResolver() should be an instance of ClientSecurity.SimpleIdentityResolver",
087: ClientSecurity.getIdentityResolver() instanceof ClientSecurity.SimpleIdentityResolver);
088:
089: // logout
090: ClientSecurity.logout();
091:
092: // verify we are logged out
093: assertNull("ClientSecurity.getIdentity() is not null",
094: ClientSecurity.getIdentity());
095: }
096:
097: private static Throwable threadException;
098:
099: public void testThreadLogin() throws Exception {
100: // setup the server response
101: LoginTestUtil.setAuthGranted();
102:
103: // Perform a thread scoped login using a new thread
104: final CountDownLatch loginLatch = new CountDownLatch(1);
105: final CountDownLatch loginVerifiedLatch = new CountDownLatch(1);
106: Thread loginThread = new Thread() {
107: public void run() {
108: try {
109: // attempt a login
110: ClientSecurity.login("jonathan", "secret", true);
111:
112: // Verify stored server request
113: assertTrue(
114: "serverRequest should be an instance of AuthenticationRequest",
115: LoginTestUtil.serverRequest instanceof AuthenticationRequest);
116: AuthenticationRequest authenticationRequest = (AuthenticationRequest) LoginTestUtil.serverRequest;
117: assertEquals("jonathan", authenticationRequest
118: .getUsername());
119: assertEquals("secret", authenticationRequest
120: .getCredentials());
121:
122: // verify client identity
123: assertEquals("SecretIdentity", ClientSecurity
124: .getIdentity());
125:
126: // verify we are using the simple client identity strategy
127: assertTrue(
128: "ClientSecurity.getIdentityResolver() should be an instance of ClientSecurity.SimpleIdentityResolver",
129: ClientSecurity.getIdentityResolver() instanceof ClientSecurity.SimpleIdentityResolver);
130:
131: // notify outer thread that we are logged in
132: loginLatch.countDown();
133:
134: // wait for outer thread to verify that it is not also logged in
135: loginVerifiedLatch.await(5, TimeUnit.SECONDS);
136:
137: // logout
138: ClientSecurity.logout();
139:
140: // verify we are logged out
141: assertNull(
142: "ClientSecurity.getIdentity() is not null",
143: ClientSecurity.getIdentity());
144: } catch (Throwable e) {
145: threadException = e;
146: }
147:
148: }
149: };
150: loginThread.start();
151:
152: // wait for login thread to login
153: loginLatch.await(5, TimeUnit.SECONDS);
154:
155: // verify we are not logged in
156: assertNull("ClientSecurity.getIdentity() is not null",
157: ClientSecurity.getIdentity());
158:
159: // notify the login thread that we are done with out verifications
160: loginVerifiedLatch.countDown();
161:
162: // wait for login thread to finish
163: loginThread.join(5000);
164:
165: if (threadException != null) {
166: if (threadException instanceof Exception)
167: throw (Exception) threadException;
168: if (threadException instanceof Error)
169: throw (Error) threadException;
170: throw new Error("login thread threw an exception",
171: threadException);
172: }
173: }
174:
175: public void testAuthDenied() throws Exception {
176: LoginTestUtil.setAuthDenied();
177:
178: try {
179: ClientSecurity.login("nobody", "secret");
180: fail("Should have thrown a FailedLoginException");
181: } catch (FailedLoginException doNothing) {
182: }
183:
184: // verify we are not logged in
185: assertNull("ClientSecurity.getIdentity() is not null",
186: ClientSecurity.getIdentity());
187: }
188: }
|