01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.client;
18:
19: import junit.framework.TestCase;
20:
21: import javax.security.auth.Subject;
22: import javax.security.auth.login.FailedLoginException;
23: import javax.security.auth.login.LoginContext;
24: import javax.security.auth.login.LoginException;
25:
26: public class ClientLoginTest extends TestCase {
27: protected void setUp() throws Exception {
28: super .setUp();
29: LoginTestUtil.initialize();
30: }
31:
32: public void testAuthGranted() throws LoginException {
33: // setup the server response
34: LoginTestUtil.setAuthGranted();
35:
36: // attempt a login
37: LoginContext context = new LoginContext("ClientLogin",
38: new UsernamePasswordCallbackHandler("jonathan",
39: "secret"));
40: context.login();
41:
42: // Verify stored server request
43: assertTrue(
44: "serverRequest should be an instance of AuthenticationRequest",
45: LoginTestUtil.serverRequest instanceof AuthenticationRequest);
46: AuthenticationRequest authenticationRequest = (AuthenticationRequest) LoginTestUtil.serverRequest;
47: assertEquals("jonathan", authenticationRequest.getUsername());
48: assertEquals("secret", authenticationRequest.getCredentials());
49:
50: // get the subject
51: Subject subject = context.getSubject();
52:
53: // verify subject
54: assertEquals("Should have one principal", 1, subject
55: .getPrincipals().size());
56: assertEquals("Should have one user principal", 1, subject
57: .getPrincipals(ClientIdentityPrincipal.class).size());
58: ClientIdentityPrincipal principal = subject.getPrincipals(
59: ClientIdentityPrincipal.class).iterator().next();
60: assertEquals("jonathan", principal.getName());
61: assertEquals("SecretIdentity", principal.getClientIdentity());
62:
63: // logout
64: context.logout();
65:
66: // verify we are logged out
67: assertEquals("Should have zero principals", 0, subject
68: .getPrincipals().size());
69: assertNull("ClientSecurity.getIdentity() is not null",
70: ClientSecurity.getIdentity());
71: }
72:
73: public void testAuthDenied() throws Exception {
74: LoginTestUtil.setAuthDenied();
75:
76: LoginContext context = new LoginContext("ClientLogin",
77: new UsernamePasswordCallbackHandler("nobody", "secret"));
78: try {
79: context.login();
80: fail("Should have thrown a FailedLoginException");
81: } catch (FailedLoginException doNothing) {
82: }
83: }
84: }
|