diff --git a/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java b/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java new file mode 100644 index 00000000..abfc4daa --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.support.authentication; + +import junit.framework.TestCase; + + +import org.acegisecurity.Authentication; +import org.acegisecurity.GrantedAuthority; +import org.acegisecurity.context.SecurityContextHolder; +import org.acegisecurity.userdetails.User; +import org.acegisecurity.userdetails.ldap.LdapUserDetails; +import org.easymock.MockControl; +import org.springframework.ldap.support.authentication.AcegiAuthenticationSource; + +public class AcegiAuthenticationSourceTest extends TestCase { + + private MockControl authenticationControl; + + private Authentication authenticationMock; + + private MockControl ldapUserDetailsControl; + + private LdapUserDetails ldapUserDetailsMock; + + private AcegiAuthenticationSource tested; + + protected void setUp() throws Exception { + super.setUp(); + + authenticationControl = MockControl.createControl(Authentication.class); + authenticationMock = (Authentication) authenticationControl.getMock(); + + ldapUserDetailsControl = MockControl + .createControl(LdapUserDetails.class); + ldapUserDetailsMock = (LdapUserDetails) ldapUserDetailsControl + .getMock(); + + tested = new AcegiAuthenticationSource(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + authenticationControl = null; + authenticationMock = null; + + ldapUserDetailsControl = null; + ldapUserDetailsMock = null; + + tested = null; + } + + protected void replay() { + authenticationControl.replay(); + ldapUserDetailsControl.replay(); + } + + protected void verify() { + authenticationControl.verify(); + ldapUserDetailsControl.verify(); + } + + public void testGetPrincipalAndCredentials() { + authenticationControl.expectAndReturn( + authenticationMock.getPrincipal(), ldapUserDetailsMock); + authenticationControl.expectAndReturn(authenticationMock + .getCredentials(), "secret"); + + ldapUserDetailsControl.expectAndDefaultReturn(ldapUserDetailsMock + .getDn(), "cn=Manager"); + + SecurityContextHolder.getContext() + .setAuthentication(authenticationMock); + + replay(); + + String principal = tested.getPrincipal(); + String credentials = tested.getCredentials(); + + verify(); + + assertEquals("secret", credentials); + assertEquals("cn=Manager", principal); + } + + public void testGetPrincipalAndCredentials_nullAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); + + replay(); + + assertEquals("", tested.getPrincipal()); + assertEquals("", tested.getCredentials()); + verify(); + } + + public void testGetPrincipal_InvalidPrincipal() { + authenticationControl.expectAndReturn( + authenticationMock.getPrincipal(), new User("dummy", "dummy", + true, true, true, true, new GrantedAuthority[0])); + + SecurityContextHolder.getContext() + .setAuthentication(authenticationMock); + + replay(); + + try { + tested.getPrincipal(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + verify(); + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java b/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java new file mode 100644 index 00000000..26bdd7a7 --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java @@ -0,0 +1,119 @@ +package org.springframework.ldap.support.authentication; + +import org.easymock.MockControl; +import org.springframework.ldap.core.AuthenticationSource; +import org.springframework.ldap.support.authentication.DefaultValuesAuthenticationSourceDecorator; + +import junit.framework.TestCase; + +public class DefaultValuesAuthenticationSourceDecoratorTest extends TestCase { + + private static final String DEFAULT_PASSWORD = "defaultPassword"; + + private static final String DEFAULT_USER = "cn=defaultUser"; + + private DefaultValuesAuthenticationSourceDecorator tested; + + private MockControl authenticationSourceControl; + + private AuthenticationSource authenticationSourceMock; + + protected void setUp() throws Exception { + super.setUp(); + + authenticationSourceControl = MockControl + .createControl(AuthenticationSource.class); + authenticationSourceMock = (AuthenticationSource) authenticationSourceControl + .getMock(); + tested = new DefaultValuesAuthenticationSourceDecorator(); + tested.setDefaultUser(DEFAULT_USER); + tested.setDefaultPassword(DEFAULT_PASSWORD); + tested.setTarget(authenticationSourceMock); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + tested = null; + authenticationSourceControl = null; + authenticationSourceMock = null; + } + + public void testGetPrincipal_TargetHasPrincipal() { + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getPrincipal(), "cn=someUser"); + authenticationSourceControl.replay(); + + String principal = tested.getPrincipal(); + + authenticationSourceControl.verify(); + assertEquals("cn=someUser", principal); + } + + public void testGetPrincipal_TargetHasNoPrincipal() { + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getPrincipal(), ""); + authenticationSourceControl.replay(); + + String principal = tested.getPrincipal(); + + authenticationSourceControl.verify(); + assertEquals(DEFAULT_USER, principal); + } + + public void testGetCredentials_TargetHasPrincipal() { + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getPrincipal(), "cn=someUser"); + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getCredentials(), "somepassword"); + authenticationSourceControl.replay(); + + String credentials = tested.getCredentials(); + + authenticationSourceControl.verify(); + assertEquals("somepassword", credentials); + } + + public void testGetCredentials_TargetHasNoPrincipal() { + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getPrincipal(), ""); + authenticationSourceControl.expectAndDefaultReturn( + authenticationSourceMock.getCredentials(), "somepassword"); + authenticationSourceControl.replay(); + + String credentials = tested.getCredentials(); + + authenticationSourceControl.verify(); + assertEquals(DEFAULT_PASSWORD, credentials); + } + + public void testAfterPropertiesSet_noTarget() throws Exception { + tested.setTarget(null); + try { + tested.afterPropertiesSet(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + public void testAfterPropertiesSet_noDefaultUser() throws Exception { + tested.setDefaultUser(null); + try { + tested.afterPropertiesSet(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + public void testAfterPropertiesSet_noDefaultPassword() throws Exception { + tested.setDefaultPassword(null); + try { + tested.afterPropertiesSet(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java new file mode 100644 index 00000000..07a904d5 --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java @@ -0,0 +1,144 @@ +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.support.control; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; +import javax.naming.ldap.Control; +import javax.naming.ldap.LdapContext; + +import junit.framework.TestCase; + +import org.easymock.MockControl; + +public class AbstractRequestControlDirContextProcessorTest extends TestCase { + + private AbstractRequestControlDirContextProcessor tested; + + private MockControl requestControlControl; + + private Control requestControlMock; + + private MockControl requestControl2Control; + + private Control requestControl2Mock; + + private MockControl ldapContextControl; + + private LdapContext ldapContextMock; + + private MockControl dirContextControl; + + private DirContext dirContextMock; + + protected void setUp() throws Exception { + super.setUp(); + // Create requestControl mock + requestControlControl = MockControl.createControl(Control.class); + requestControlMock = (Control) requestControlControl.getMock(); + + // Create requestControl2 mock + requestControl2Control = MockControl.createControl(Control.class); + requestControl2Mock = (Control) requestControl2Control.getMock(); + + // Create ldapContext mock + ldapContextControl = MockControl.createControl(LdapContext.class); + ldapContextMock = (LdapContext) ldapContextControl.getMock(); + + // Create dirContext mock + dirContextControl = MockControl.createControl(DirContext.class); + dirContextMock = (DirContext) dirContextControl.getMock(); + + tested = new AbstractRequestControlDirContextProcessor() { + + public Control createRequestControl() { + return requestControlMock; + } + + public void postProcess(DirContext ctx) throws NamingException { + } + + }; + } + + protected void tearDown() throws Exception { + super.tearDown(); + + requestControlControl = null; + requestControlMock = null; + + requestControl2Control = null; + requestControl2Mock = null; + + ldapContextControl = null; + ldapContextMock = null; + + dirContextControl = null; + dirContextMock = null; + + } + + protected void replay() { + requestControlControl.replay(); + requestControl2Control.replay(); + ldapContextControl.replay(); + dirContextControl.replay(); + } + + protected void verify() { + requestControlControl.verify(); + requestControl2Control.verify(); + ldapContextControl.verify(); + dirContextControl.verify(); + } + + public void testPreProcess() throws NamingException { + ldapContextControl.setDefaultMatcher(MockControl.ARRAY_MATCHER); + ldapContextControl.expectAndDefaultReturn(ldapContextMock + .getRequestControls(), new Control[] { requestControl2Mock }); + ldapContextMock.setRequestControls(new Control[] { requestControl2Mock, + requestControlMock }); + + replay(); + + tested.preProcess(ldapContextMock); + + verify(); + } + + public void testPreProcess_NoExistingControls() throws NamingException { + ldapContextControl.setDefaultMatcher(MockControl.ARRAY_MATCHER); + ldapContextControl.expectAndDefaultReturn(ldapContextMock + .getRequestControls(), new Control[0]); + ldapContextMock + .setRequestControls(new Control[] { requestControlMock }); + + replay(); + + tested.preProcess(ldapContextMock); + + verify(); + } + + public void testPreProcess_NotLdapContext() throws Exception { + try { + tested.preProcess(dirContextMock); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java new file mode 100644 index 00000000..1401b74c --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.support.control; + +import java.util.LinkedList; +import java.util.List; + +import com.gargoylesoftware.base.testing.EqualsTester; + +import junit.framework.TestCase; + +public class PagedResultTest extends TestCase { + public void testEquals() throws Exception { + List expectedList = new LinkedList(); + expectedList.add("dummy"); + List otherList = new LinkedList(); + otherList.add("different"); + + PagedResult originalObject = new PagedResult(expectedList, + new PagedResultsCookie(null)); + PagedResult identicalObject = new PagedResult(expectedList, + new PagedResultsCookie(null)); + PagedResult differentObject = new PagedResult(otherList, + new PagedResultsCookie(null)); + PagedResult subclassObject = new PagedResult(expectedList, + new PagedResultsCookie(null)) { + + }; + + new EqualsTester(originalObject, identicalObject, differentObject, + subclassObject); + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java new file mode 100644 index 00000000..39ea3c52 --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.support.control; + +import com.gargoylesoftware.base.testing.EqualsTester; + +import junit.framework.TestCase; + +public class PagedResultsCookieTest extends TestCase { + public void testEquals() { + byte[] expectedCookie = new byte[] { 1, 2 }; + byte[] differentCookie = new byte[] { 2, 3 }; + + PagedResultsCookie originalObject = new PagedResultsCookie( + expectedCookie); + PagedResultsCookie identicalObject = new PagedResultsCookie( + expectedCookie); + PagedResultsCookie differentObject = new PagedResultsCookie( + differentCookie); + PagedResultsCookie subclassObject = new PagedResultsCookie( + expectedCookie) { + + }; + + new EqualsTester(originalObject, identicalObject, differentObject, + subclassObject); + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java new file mode 100644 index 00000000..2cf0e4dc --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java @@ -0,0 +1,188 @@ +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.support.control; + +import java.io.IOException; + +import javax.naming.ldap.Control; +import javax.naming.ldap.LdapContext; + +import junit.framework.TestCase; + +import org.easymock.MockControl; + +import com.sun.jndi.ldap.Ber; +import com.sun.jndi.ldap.BerDecoder; +import com.sun.jndi.ldap.BerEncoder; +import com.sun.jndi.ldap.ctl.DirSyncResponseControl; +import com.sun.jndi.ldap.ctl.PagedResultsControl; +import com.sun.jndi.ldap.ctl.PagedResultsResponseControl; + +public class PagedResultsRequestControlTest extends TestCase { + + private MockControl ldapContextControl; + + private LdapContext ldapContextMock; + + protected void setUp() throws Exception { + super.setUp(); + + // Create ldapContext mock + ldapContextControl = MockControl.createControl(LdapContext.class); + ldapContextMock = (LdapContext) ldapContextControl.getMock(); + + } + + protected void tearDown() throws Exception { + super.tearDown(); + + ldapContextControl = null; + ldapContextMock = null; + + } + + protected void replay() { + ldapContextControl.replay(); + } + + protected void verify() { + ldapContextControl.verify(); + } + + public void testCreateRequestControl() throws Exception { + PagedResultsRequestControl tested = new PagedResultsRequestControl(20); + + PagedResultsControl control = (PagedResultsControl) tested + .createRequestControl(); + assertNotNull(control); + } + + public void testCreateRequestControl_CookieSet() throws Exception { + PagedResultsCookie cookie = new PagedResultsCookie(new byte[0]); + PagedResultsRequestControl tested = new PagedResultsRequestControl(20, + cookie); + + PagedResultsControl control = (PagedResultsControl) tested + .createRequestControl(); + assertNotNull(control); + } + + public void testPostProcess() throws Exception { + int resultSize = 50; + byte pageSize = 8; + + byte[] value = new byte[1]; + value[0] = pageSize; + byte[] cookie = encodeValue(resultSize, value); + PagedResultsResponseControl control = new PagedResultsResponseControl( + "dummy", true, cookie); + + ldapContextControl.expectAndDefaultReturn(ldapContextMock + .getResponseControls(), new Control[] { control }); + + PagedResultsRequestControl tested = new PagedResultsRequestControl(20); + + replay(); + + tested.postProcess(ldapContextMock); + + verify(); + + PagedResultsCookie returnedCookie = tested.getCookie(); + assertEquals(8, returnedCookie.getCookie()[0]); + assertEquals(20, tested.getPageSize()); + assertEquals(50, tested.getResultSize()); + } + + public void testPostProcess_InvalidResponseControl() throws Exception { + int resultSize = 50; + byte pageSize = 8; + + byte[] value = new byte[1]; + value[0] = pageSize; + byte[] cookie = encodeDirSyncValue(resultSize, value); + + // Using another response control to verify that it is ignored + DirSyncResponseControl control = new DirSyncResponseControl( + "dummy", true, cookie); + + ldapContextControl.expectAndDefaultReturn(ldapContextMock + .getResponseControls(), new Control[] { control }); + + PagedResultsRequestControl tested = new PagedResultsRequestControl(20); + + replay(); + + tested.postProcess(ldapContextMock); + + verify(); + + assertNull(tested.getCookie()); + assertEquals(20, tested.getPageSize()); + assertEquals(0, tested.getResultSize()); + } + + public void testBerDecoding() throws Exception { + byte[] value = new byte[1]; + value[0] = 8; + int pageSize = 20; + byte[] cookie = encodeValue(pageSize, value); + + BerDecoder ber = new BerDecoder(cookie, 0, cookie.length); + + ber.parseSeq(null); + int actualPageSize = ber.parseInt(); + byte[] actualValue = ber.parseOctetString(Ber.ASN_OCTET_STR, null); + + assertEquals("pageSize,", 20, actualPageSize); + assertEquals("value length", value.length, actualValue.length); + for (int i = 0; i < value.length; i++) { + assertEquals("value (index " + i + "),", value[i], actualValue[i]); + } + } + + private byte[] encodeValue(int pageSize, byte[] cookie) + throws IOException { + + // build the ASN.1 encoding + BerEncoder ber = new BerEncoder(10 + cookie.length); + + ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR); + ber.encodeInt(pageSize); + ber.encodeOctetString(cookie, Ber.ASN_OCTET_STR); + ber.endSeq(); + + return ber.getTrimmedBuf(); + } + + /** + * Encode a value suitable for the DirSyncResponseControl used in a test. + */ + private byte[] encodeDirSyncValue(int pageSize, byte[] cookie) + throws IOException { + + // build the ASN.1 encoding + BerEncoder ber = new BerEncoder(10 + cookie.length); + + ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR); + ber.encodeInt(1); // flag + ber.encodeInt(pageSize); // maxReturnLength + ber.encodeOctetString(cookie, Ber.ASN_OCTET_STR); + ber.endSeq(); + + return ber.getTrimmedBuf(); + } +}