001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.ldap;
017:
018: import org.acegisecurity.AcegiMessageSource;
019: import org.acegisecurity.BadCredentialsException;
020:
021: import java.util.Hashtable;
022:
023: import javax.naming.Context;
024: import javax.naming.directory.DirContext;
025:
026: /**
027: * Tests {@link org.acegisecurity.ldap.DefaultInitialDirContextFactory}.
028: *
029: * @author Luke Taylor
030: * @version $Id: DefaultInitialDirContextFactoryTests.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public class DefaultInitialDirContextFactoryTests extends
033: AbstractLdapServerTestCase {
034: //~ Instance fields ================================================================================================
035:
036: DefaultInitialDirContextFactory idf;
037:
038: //~ Methods ========================================================================================================
039:
040: public void onSetUp() {
041: idf = getInitialCtxFactory();
042: idf.setMessageSource(new AcegiMessageSource());
043: }
044:
045: public void testAnonymousBindSucceeds() throws Exception {
046: DirContext ctx = idf.newInitialDirContext();
047: // Connection pooling should be set by default for anon users.
048: // Can't rely on this property being there with embedded server
049: // assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
050: ctx.close();
051: }
052:
053: public void testBaseDnIsParsedFromCorrectlyFromUrl() {
054: idf = new DefaultInitialDirContextFactory(
055: "ldap://acegisecurity.org/dc=acegisecurity,dc=org");
056: assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
057:
058: // Check with an empty root
059: idf = new DefaultInitialDirContextFactory(
060: "ldap://acegisecurity.org/");
061: assertEquals("", idf.getRootDn());
062:
063: // Empty root without trailing slash
064: idf = new DefaultInitialDirContextFactory(
065: "ldap://acegisecurity.org");
066: assertEquals("", idf.getRootDn());
067: }
068:
069: public void testBindAsManagerFailsIfNoPasswordSet()
070: throws Exception {
071: idf.setManagerDn(MANAGER_USER);
072:
073: DirContext ctx = null;
074:
075: try {
076: ctx = idf.newInitialDirContext();
077: fail("Binding with no manager password should fail.");
078:
079: // Can't rely on this property being there with embedded server
080: // assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
081: } catch (BadCredentialsException expected) {
082: }
083:
084: LdapUtils.closeContext(ctx);
085: }
086:
087: public void testBindAsManagerSucceeds() throws Exception {
088: idf.setManagerPassword(MANAGER_PASSWORD);
089: idf.setManagerDn(MANAGER_USER);
090:
091: DirContext ctx = idf.newInitialDirContext();
092: // Can't rely on this property being there with embedded server
093: // assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
094: ctx.close();
095: }
096:
097: public void testConnectionAsSpecificUserSucceeds() throws Exception {
098: DirContext ctx = idf.newInitialDirContext(
099: "uid=Bob,ou=people,dc=acegisecurity,dc=org",
100: "bobspassword");
101: // We don't want pooling for specific users.
102: // assertNull(ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
103: // com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
104: ctx.close();
105: }
106:
107: public void testConnectionFailure() throws Exception {
108: // Use the wrong port
109: idf = new DefaultInitialDirContextFactory(
110: "ldap://localhost:60389");
111: idf
112: .setInitialContextFactory("com.sun.jndi.ldap.LdapCtxFactory");
113:
114: Hashtable env = new Hashtable();
115: env.put("com.sun.jndi.ldap.connect.timeout", "200");
116: idf.setExtraEnvVars(env);
117: idf.setUseConnectionPool(false); // coverage purposes only
118:
119: try {
120: idf.newInitialDirContext();
121: fail("Connection succeeded unexpectedly");
122: } catch (LdapDataAccessException expected) {
123: }
124: }
125:
126: public void testEnvironment() {
127: idf = new DefaultInitialDirContextFactory(
128: "ldap://acegisecurity.org/");
129:
130: // check basic env
131: Hashtable env = idf.getEnvironment();
132: //assertEquals("com.sun.jndi.ldap.LdapCtxFactory", env.get(Context.INITIAL_CONTEXT_FACTORY));
133: assertEquals("ldap://acegisecurity.org/", env
134: .get(Context.PROVIDER_URL));
135: assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION));
136: assertNull(env.get(Context.SECURITY_PRINCIPAL));
137: assertNull(env.get(Context.SECURITY_CREDENTIALS));
138:
139: // Ctx factory.
140: idf
141: .setInitialContextFactory("org.acegisecurity.NonExistentCtxFactory");
142: env = idf.getEnvironment();
143: assertEquals("org.acegisecurity.NonExistentCtxFactory", env
144: .get(Context.INITIAL_CONTEXT_FACTORY));
145:
146: // Auth type
147: idf.setAuthenticationType("myauthtype");
148: env = idf.getEnvironment();
149: assertEquals("myauthtype", env
150: .get(Context.SECURITY_AUTHENTICATION));
151:
152: // Check extra vars
153: Hashtable extraVars = new Hashtable();
154: extraVars.put("extravar", "extravarvalue");
155: idf.setExtraEnvVars(extraVars);
156: env = idf.getEnvironment();
157: assertEquals("extravarvalue", env.get("extravar"));
158: }
159:
160: public void testInvalidPasswordCausesBadCredentialsException()
161: throws Exception {
162: idf.setManagerDn(MANAGER_USER);
163: idf.setManagerPassword("wrongpassword");
164:
165: DirContext ctx = null;
166:
167: try {
168: ctx = idf.newInitialDirContext();
169: fail("Binding with wrong credentials should fail.");
170: } catch (BadCredentialsException expected) {
171: }
172:
173: LdapUtils.closeContext(ctx);
174: }
175:
176: public void testMultipleProviderUrlsAreAccepted() {
177: idf = new DefaultInitialDirContextFactory(
178: "ldaps://acegisecurity.org/dc=acegisecurity,dc=org "
179: + "ldap://monkeymachine.co.uk/dc=acegisecurity,dc=org");
180: }
181:
182: public void testMultipleProviderUrlsWithDifferentRootsAreRejected() {
183: try {
184: idf = new DefaultInitialDirContextFactory(
185: "ldap://acegisecurity.org/dc=acegisecurity,dc=org "
186: + "ldap://monkeymachine.co.uk/dc=someotherplace,dc=org");
187: fail("Different root DNs should cause an exception");
188: } catch (IllegalArgumentException expected) {
189: }
190: }
191:
192: public void testSecureLdapUrlIsSupported() {
193: idf = new DefaultInitialDirContextFactory(
194: "ldaps://localhost/dc=acegisecurity,dc=org");
195: assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
196: }
197:
198: // public void testNonLdapUrlIsRejected() throws Exception {
199: // DefaultInitialDirContextFactory idf = new DefaultInitialDirContextFactory();
200: //
201: // idf.setUrl("http://acegisecurity.org/dc=acegisecurity,dc=org");
202: // idf.setInitialContextFactory(CoreContextFactory.class.getName());
203: //
204: // try {
205: // idf.afterPropertiesSet();
206: // fail("Expected exception for non 'ldap://' URL");
207: // } catch(IllegalArgumentException expected) {
208: // }
209: // }
210: public void testServiceLocationUrlIsSupported() {
211: idf = new DefaultInitialDirContextFactory(
212: "ldap:///dc=acegisecurity,dc=org");
213: assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
214: }
215: }
|