package referral; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.ReferralException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls; import junit.framework.TestCase; /** * Test case to demonstrate DIRSERVER-806 ("No continuation references for * referrals returned for some search filters"). */ public class ContinuationTest extends TestCase { private static final String REF_CN = "theReferral"; private static final String REF_RDN = "cn=" + REF_CN; DirContext ctx = null; /** * Create attributes ror a referral entry */ protected Attributes getReferralEntryAttributes(String cn, String ref) { Attributes refEntry = new BasicAttributes(); Attribute ocls = new BasicAttribute("objectClass"); ocls.add("top"); ocls.add("referral"); ocls.add("extensibleObject"); refEntry.put(ocls); refEntry.put("cn", cn); refEntry.put("ref", ref); return refEntry; } protected void setUp() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=example,dc=com"); env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); env.put(Context.SECURITY_CREDENTIALS, "secret"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.REFERRAL, "throw"); ctx = new InitialDirContext(env); // Create a referral entry Attributes refEntry = getReferralEntryAttributes(REF_CN, "ldap://someHost:389/cn=somewhere"); ctx.createSubcontext(REF_RDN, refEntry); } protected void tearDown() throws NamingException { // Delete the referral entry ctx.addToEnvironment(Context.REFERRAL, "ignore"); ctx.destroySubcontext(REF_RDN); ctx.close(); } /** * Search with default filter (objectClass=*) and check for continuation * reference */ public void testSimpleContinuationWithDefaultFilter() throws NamingException { SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(objectClass=*)"; try { NamingEnumeration enm = ctx.search("", filter, ctls); while (enm.hasMore()) { enm.next(); } fail("No referral exception"); } catch (ReferralException e) { assertNotNull(e.getReferralInfo()); String referralInfo = e.getReferralInfo().toString(); assertTrue(referralInfo.startsWith("ldap://")); } } /** * Search with cn filter and check for continuation reference */ public void testSimpleContinuationWithCnFilter() throws NamingException { SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(cn=Nick Cave)"; try { NamingEnumeration enm = ctx.search("", filter, ctls); while (enm.hasMore()) { enm.next(); } fail("No referral exception"); } catch (ReferralException e) { assertNotNull(e.getReferralInfo()); String referralInfo = e.getReferralInfo().toString(); assertTrue(referralInfo.startsWith("ldap://")); } } }