KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > bridge > MappingUserPasswordBridgeTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.security.bridge;
19
20 import java.security.Principal JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.security.auth.Subject JavaDoc;
25
26 import org.apache.geronimo.security.ContextManager;
27 import org.apache.geronimo.security.IdentificationPrincipal;
28
29
30 /**
31  * @version $Rev: 109314 $ $Date: 2004-12-01 00:01:37 -0800 (Wed, 01 Dec 2004) $
32  */

33 public class MappingUserPasswordBridgeTest extends AbstractBridgeTest {
34     private static final String JavaDoc SOURCE_USER_1 = "sourceUser1";
35     private static final String JavaDoc SOURCE_USER_2 = "sourceUser2";
36     private static final String JavaDoc SOURCE_PRINCIPAL_1 = "sourcePrincipal1";
37     private static final String JavaDoc SOURCE_PRINCIPAL_2 = "sourcePrincipal2";
38     private static final String JavaDoc SOURCE_PASSWORD_1 = "sourcePassword1";
39     private static final String JavaDoc SOURCE_PASSWORD_2 = "sourcePassword2";
40
41     private TestMappingBridge bridge;
42
43     protected void setUp() throws Exception JavaDoc {
44         super.setUp();
45         bridge = new TestMappingBridge();
46         bridge.setTargetRealm(TestLoginModule.JAAS_NAME);
47         bridge.setPrincipalSourceType(TestPrincipalPrincipal.class);
48         bridge.setPrincipalTargetCallbackName("Resource Principal");
49         Map JavaDoc principalMap = new HashMap JavaDoc();
50         principalMap.put(SOURCE_PRINCIPAL_1, AbstractBridgeTest.USER);
51         principalMap.put(SOURCE_PRINCIPAL_2, "no-one");
52         bridge.setPrincipalMap(principalMap);
53         bridge.setUserNameSourceType(TestUserNamePrincipal.class);
54         bridge.setUserNameTargetCallbackName("User Name");
55         Map JavaDoc userNameMap = new HashMap JavaDoc();
56         userNameMap.put(SOURCE_USER_1, AbstractBridgeTest.USER);
57         userNameMap.put(SOURCE_USER_2, "no-one");
58         bridge.setUserNameMap(userNameMap);
59         bridge.setPasswordSourceType(TestPasswordPrincipal.class);
60         Map JavaDoc passwordMap = new HashMap JavaDoc();
61         passwordMap.put(SOURCE_PASSWORD_1, AbstractBridgeTest.PASSWORD.toCharArray());
62         passwordMap.put(SOURCE_PASSWORD_2, "no-password".toCharArray());
63         bridge.setPasswordMap(passwordMap);
64     }
65
66     public void testMapping() throws Exception JavaDoc {
67         Subject JavaDoc subject = new Subject JavaDoc();
68         subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1));
69         subject.getPrincipals().add(new TestUserNamePrincipal(SOURCE_USER_1));
70         subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1));
71         Subject JavaDoc targetSubject = bridge.mapSubject(subject);
72         assertTrue("expected non-null client subject", targetSubject != null);
73         Set JavaDoc set = targetSubject.getPrincipals(IdentificationPrincipal.class);
74         assertEquals("client subject should have one ID principal", set.size(), 1);
75         IdentificationPrincipal idp = (IdentificationPrincipal)set.iterator().next();
76         targetSubject = ContextManager.getRegisteredSubject(idp.getId());
77         checkValidSubject(targetSubject);
78     }
79
80     public void testInsufficientSourcePrincipals() throws Exception JavaDoc {
81         Subject JavaDoc subject = new Subject JavaDoc();
82         subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1));
83         subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1));
84         try {
85             bridge.mapSubject(subject);
86             fail();
87         } catch (Throwable JavaDoc e) {
88         }
89     }
90
91     public void testNotInMap() throws Exception JavaDoc {
92         Subject JavaDoc subject = new Subject JavaDoc();
93         subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1 + "xxx"));
94         subject.getPrincipals().add(new TestUserNamePrincipal(SOURCE_USER_1));
95         subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1));
96         try {
97             bridge.mapSubject(subject);
98             fail();
99         } catch (Throwable JavaDoc e) {
100         }
101     }
102
103     public static class TestPrincipalPrincipal implements Principal JavaDoc {
104         private String JavaDoc name;
105
106         public TestPrincipalPrincipal(String JavaDoc name) {
107             this.name = name;
108         }
109
110         public String JavaDoc getName() {
111             return name;
112         }
113     }
114
115     public static class TestUserNamePrincipal implements Principal JavaDoc {
116         private String JavaDoc name;
117
118         public TestUserNamePrincipal(String JavaDoc name) {
119             this.name = name;
120         }
121
122         public String JavaDoc getName() {
123             return name;
124         }
125     }
126
127     public static class TestPasswordPrincipal implements Principal JavaDoc {
128         private String JavaDoc name;
129
130         public TestPasswordPrincipal(String JavaDoc name) {
131             this.name = name;
132         }
133
134         public String JavaDoc getName() {
135             return name;
136         }
137     }
138
139     public static class TestMappingBridge extends AbstractPrincipalMappingUserPasswordRealmBridge {
140
141         public void setPrincipalMap(Map JavaDoc principalMap) {
142             this.principalMap.clear();
143             this.principalMap.putAll(principalMap);
144         }
145
146         public void setUserNameMap(Map JavaDoc userNameMap) {
147             this.userNameMap.clear();
148             this.userNameMap.putAll(userNameMap);
149         }
150
151         public void setPasswordMap(Map JavaDoc passwordMap) {
152             this.passwordMap.clear();
153             this.passwordMap.putAll(passwordMap);
154         }
155
156     }
157 }
158
Popular Tags