001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.spi.ldap;
018:
019: import org.apache.jetspeed.security.PasswordCredential;
020: import org.apache.jetspeed.security.SecurityException;
021: import org.apache.jetspeed.security.spi.impl.LdapCredentialHandler;
022:
023: import java.util.Set;
024:
025: /**
026: * <p>
027: * Test {@link LdapCredentialHandler}implementation of the SPI
028: * <code>CredentialHandler</code>.
029: * </p>
030: *
031: * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>
032: */
033: public class TestLdapCredentialHandler extends AbstractLdapTest {
034:
035: /**
036: * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#setUp()
037: */
038: protected void setUp() throws Exception {
039: super .setUp();
040: LdapDataHelper.seedUserData(uid1, password);
041: }
042:
043: /**
044: * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#tearDown()
045: */
046: protected void tearDown() throws Exception {
047: super .tearDown();
048: LdapDataHelper.removeUserData(uid1);
049: }
050:
051: /**
052: * <p>
053: * Test <code>getPrivateCredentials</code>
054: * </p>
055: *
056: * @throws Exception An {@link Exception}.
057: */
058: public void testGetPrivateCredentials() throws Exception {
059: Set credentials = crHandler.getPrivateCredentials(uid1);
060:
061: assertTrue(
062: "getPrivateCredentials found no credentials for user:"
063: + uid1, credentials.size() > 0);
064:
065: PasswordCredential cred = (PasswordCredential) credentials
066: .iterator().next();
067:
068: assertEquals(password, String.valueOf(cred.getPassword()));
069: }
070:
071: /**
072: * <p>
073: * Test <code>getPrivateCredentials</code> for a user that does not exist.
074: * </p>
075: *
076: * @throws Exception An {@link Exception}.
077: */
078: public void testGetPrivateCredentialsForNonExistantUser()
079: throws Exception {
080: String nonExistantUser = Integer.toString(rand.nextInt());
081: Set credentials = crHandler
082: .getPrivateCredentials(nonExistantUser);
083:
084: assertTrue(
085: "getPrivateCredentials should not have found credentials for user:"
086: + nonExistantUser, credentials.isEmpty());
087: }
088:
089: /**
090: * <p>
091: * Test <code>setPassword</code>.
092: * </p>
093: *
094: * @throws Exception An {@link Exception}.
095: */
096: public void testSetPassword() throws Exception {
097: crHandler.setPassword(uid1, password, "freddie");
098: assertTrue("Failed to change the password.", crHandler
099: .authenticate(uid1, "freddie"));
100: crHandler.setPassword(uid1, "freddie", password);
101: }
102:
103: /**
104: * <p>
105: * Test <code>setPassword</code> with null password.
106: * </p>
107: *
108: * @throws Exception An {@link Exception}.
109: */
110: public void testVerifyNullSetPassword() throws Exception {
111: crHandler.setPassword(uid1, null, password);
112: }
113:
114: /**
115: * <p>
116: * Test <code>authenticate</code> with correct login.
117: * </p>
118: *
119: * @throws Exception An {@link Exception}.
120: */
121: public void testGoodLogin() throws Exception {
122: assertTrue("The login failed for user.", crHandler
123: .authenticate(uid1, password));
124: }
125:
126: /**
127: * <p>
128: * Test <code>authenticate</code> with no password.
129: * </p>
130: *
131: * @throws Exception An {@link Exception}.
132: */
133: public void testCannotAuthenticateWithNoPassword() throws Exception {
134: try {
135: crHandler.authenticate(uid1, "");
136: fail("Should have thrown a SecurityException.");
137: } catch (Exception e) {
138: assertTrue(
139: "Should have thrown an SecurityException but threw:"
140: + e, e instanceof SecurityException);
141: }
142: }
143:
144: /**
145: * <p>
146: * Test <code>authenticate</code> with bad uid.
147: * </p>
148: *
149: * @throws Exception An {@link Exception}.
150: */
151: public void testBadUID() throws Exception {
152: String nonExistantUser = Integer.toString(rand.nextInt());
153:
154: try {
155: crHandler.authenticate(nonExistantUser, password);
156: fail("Should have thrown an exception for a non-existant user.");
157: } catch (Exception e) {
158: assertTrue(
159: "Should have thrown a SecurityException for a non-existant user.",
160: e instanceof SecurityException);
161: }
162: }
163:
164: /**
165: * <p>
166: * Test <code>authenticate</code> with bad password.
167: * </p>
168: *
169: * @throws Exception An {@link Exception}.
170: */
171: public void testBadPassword() throws Exception {
172: assertFalse(
173: "Should not have authenticated with a bad password.",
174: crHandler.authenticate(uid1, password + "123"));
175: }
176: }
|