Search in sources :

Example 1 with RootDSE

use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.

the class SubtreeDeleter method delete.

/**
 * Attempts to delete the specified subtree using the current settings.
 *
 * @param  connection
 *              The {@link LDAPInterface} instance to use to communicate with
 *              the directory server.  While this may be an individual
 *              {@link LDAPConnection}, it may be better as a connection
 *              pool with automatic retry enabled so that it's more likely to
 *              succeed in the event that a connection becomes invalid or an
 *              operation experiences a transient failure.  It must not be
 *              {@code null}.
 * @param  baseDN
 *              The base DN for the subtree to delete.  It must not be
 *              {@code null}.
 *
 * @return  An object with information about the results of the subtree
 *          delete processing.
 */
@NotNull()
public SubtreeDeleterResult delete(@NotNull final LDAPInterface connection, @NotNull final DN baseDN) {
    final AtomicReference<RootDSE> rootDSE = new AtomicReference<>();
    final boolean useSetSubtreeAccessibility = useSetSubtreeAccessibilityOperationIfAvailable && supportsExtendedRequest(connection, rootDSE, SetSubtreeAccessibilityExtendedRequest.SET_SUBTREE_ACCESSIBILITY_REQUEST_OID) && supportsExtendedRequest(connection, rootDSE, WhoAmIExtendedRequest.WHO_AM_I_REQUEST_OID);
    final boolean usePagedResults = useSimplePagedResultsControlIfAvailable && supportsControl(connection, rootDSE, SimplePagedResultsControl.PAGED_RESULTS_OID);
    final boolean useSubentries = useSubentriesControlIfAvailable && supportsControl(connection, rootDSE, DraftLDUPSubentriesRequestControl.SUBENTRIES_REQUEST_OID);
    final List<Control> searchControls = new ArrayList<>(10);
    searchControls.addAll(additionalSearchControls);
    final List<Control> deleteControls = new ArrayList<>(10);
    deleteControls.addAll(additionalDeleteControls);
    if (useHardDeleteControlIfAvailable && supportsControl(connection, rootDSE, HardDeleteRequestControl.HARD_DELETE_REQUEST_OID)) {
        deleteControls.add(new HardDeleteRequestControl(false));
    }
    if (useManageDSAITControlIfAvailable && supportsControl(connection, rootDSE, ManageDsaITRequestControl.MANAGE_DSA_IT_REQUEST_OID)) {
        final ManageDsaITRequestControl c = new ManageDsaITRequestControl(false);
        searchControls.add(c);
        deleteControls.add(c);
    }
    if (usePermitUnindexedSearchControlIfAvailable && supportsControl(connection, rootDSE, PermitUnindexedSearchRequestControl.PERMIT_UNINDEXED_SEARCH_REQUEST_OID)) {
        searchControls.add(new PermitUnindexedSearchRequestControl(false));
    }
    if (useReturnConflictEntriesRequestControlIfAvailable && supportsControl(connection, rootDSE, ReturnConflictEntriesRequestControl.RETURN_CONFLICT_ENTRIES_REQUEST_OID)) {
        searchControls.add(new ReturnConflictEntriesRequestControl(false));
    }
    if (useSoftDeletedEntryAccessControlIfAvailable && supportsControl(connection, rootDSE, SoftDeletedEntryAccessRequestControl.SOFT_DELETED_ENTRY_ACCESS_REQUEST_OID)) {
        searchControls.add(new SoftDeletedEntryAccessRequestControl(false, true, false));
    }
    return delete(connection, baseDN, deleteBaseEntry, useSetSubtreeAccessibility, usePagedResults, searchRequestSizeLimit, simplePagedResultsPageSize, useSubentries, searchControls, deleteControls, deleteRateLimiter);
}
Also used : PermitUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PermitUnindexedSearchRequestControl) Control(com.unboundid.ldap.sdk.Control) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) HardDeleteRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.HardDeleteRequestControl) SoftDeletedEntryAccessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeletedEntryAccessRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) ReturnConflictEntriesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReturnConflictEntriesRequestControl) DraftLDUPSubentriesRequestControl(com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl) SoftDeletedEntryAccessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeletedEntryAccessRequestControl) HardDeleteRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.HardDeleteRequestControl) PermitUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PermitUnindexedSearchRequestControl) ArrayList(java.util.ArrayList) ReturnConflictEntriesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReturnConflictEntriesRequestControl) AtomicReference(java.util.concurrent.atomic.AtomicReference) RootDSE(com.unboundid.ldap.sdk.RootDSE) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)

Example 2 with RootDSE

use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.

the class SubtreeAccessibilityTestCase method testServerInteraction.

/**
 * Tests the behavior of the tool when actually interacting with a server.
 * <BR><BR>
 * Access to a Directory Server instance that supports the get and set subtree
 * accessibility operations is required for complete processing.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testServerInteraction() throws Exception {
    if (!isDirectoryInstanceAvailable()) {
        return;
    }
    final LDAPConnection conn = getAdminConnection();
    try {
        final RootDSE rootDSE = conn.getRootDSE();
        if (!(rootDSE.supportsExtendedOperation(GetSubtreeAccessibilityExtendedRequest.GET_SUBTREE_ACCESSIBILITY_REQUEST_OID) && rootDSE.supportsExtendedOperation(SetSubtreeAccessibilityExtendedRequest.SET_SUBTREE_ACCESSIBILITY_REQUEST_OID))) {
            return;
        }
        // Ensure that the base entry exists.
        conn.add(getTestBaseDN(), getBaseEntryAttributes());
        // Verify that the server doesn't have any restrictions defined.
        GetSubtreeAccessibilityExtendedResult getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertTrue(getResult.getAccessibilityRestrictions().isEmpty());
        // Verify that we can use the tool in "get" mode with no restrictions
        // defined.
        String[] args = { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword() };
        ResultCode resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Use the tool to create a new subtree accessibility restriction.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "ou=subtree," + getTestBaseDN(), "--state", "read-only-bind-allowed", "--bypassUserDN", "uid=bypass," + getTestBaseDN() };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Verify that the server now has a restriction defined.
        getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertFalse(getResult.getAccessibilityRestrictions().isEmpty());
        // Verify that we can use the tool in "get" mode with a restriction
        // defined.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword() };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Use the tool to modify the subtree accessibility restriction.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "ou=subtree," + getTestBaseDN(), "--state", "read-only-bind-denied", "--bypassUserDN", "uid=bypass," + getTestBaseDN() };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Verify that the server still has only one restriction defined.
        getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertEquals(getResult.getAccessibilityRestrictions().size(), 1);
        // Use the tool to add a second restriction.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "ou=subtree2," + getTestBaseDN(), "--state", "hidden" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Verify that the server now has two restrictions defined.
        getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertEquals(getResult.getAccessibilityRestrictions().size(), 2);
        // Verify that we can use the tool in "get" mode with multiple
        // restrictions defined.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword() };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Use the tool to remove the first subtree accessibility restriction.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "ou=subtree," + getTestBaseDN(), "--state", "accessible" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Verify that the server no longer has any restrictions defined.
        getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertEquals(getResult.getAccessibilityRestrictions().size(), 1);
        // Use the tool to remove the remaining subtree accessibility restriction.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "ou=subtree2," + getTestBaseDN(), "--state", "accessible" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertEquals(resultCode, ResultCode.SUCCESS);
        // Verify that the server no longer has any restrictions defined.
        getResult = (GetSubtreeAccessibilityExtendedResult) conn.processExtendedOperation(new GetSubtreeAccessibilityExtendedRequest());
        assertResultCodeEquals(getResult, ResultCode.SUCCESS);
        assertNotNull(getResult.getAccessibilityRestrictions());
        assertTrue(getResult.getAccessibilityRestrictions().isEmpty());
        // Invoke the tool in get mode with the wrong password so it will fail.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", "wrong-" + getTestBindPassword() };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertFalse(resultCode == ResultCode.SUCCESS);
        // Invoke the tool in set mode with a bad base DN so it will fail.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", getTestBindDN(), "--bindPassword", getTestBindPassword(), "--set", "--baseDN", "dc=does,dc=not,dc=exist", "--state", "accessible" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertFalse(resultCode == ResultCode.SUCCESS);
        // Invoke the tool in get mode with no credentials so it will fail.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", "", "--bindPassword", "" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertFalse(resultCode == ResultCode.SUCCESS);
        // Invoke the tool in get mode with no credentials so it will fail.
        args = new String[] { "--hostname", getTestHost(), "--port", String.valueOf(getTestPort()), "--bindDN", "", "--bindPassword", "", "--set", "--baseDN", "ou=subtree,dc=example,dc=com", "--state", "accessible" };
        resultCode = SubtreeAccessibility.main(args, null, null);
        assertFalse(resultCode == ResultCode.SUCCESS);
    } finally {
        try {
            conn.delete(getTestBaseDN());
        } catch (final Exception e) {
        }
        conn.close();
    }
}
Also used : GetSubtreeAccessibilityExtendedResult(com.unboundid.ldap.sdk.unboundidds.extensions.GetSubtreeAccessibilityExtendedResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) GetSubtreeAccessibilityExtendedRequest(com.unboundid.ldap.sdk.unboundidds.extensions.GetSubtreeAccessibilityExtendedRequest) RootDSE(com.unboundid.ldap.sdk.RootDSE) ResultCode(com.unboundid.ldap.sdk.ResultCode) Test(org.testng.annotations.Test)

Example 3 with RootDSE

use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testDefaultUnitTestServerWithSSLAndTestEntries.

/**
 * Tests the ability to communicate securely with the default SSL-enabled
 * server provided by the unit test framework.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDefaultUnitTestServerWithSSLAndTestEntries() throws Exception {
    final InMemoryDirectoryServer ds = getTestDSWithSSL(true, true);
    assertNotNull(ds.getClientSocketFactory());
    // Verify that we can use the server's getConnection method.
    LDAPConnection dsProvidedConn = ds.getConnection();
    assertNotNull(dsProvidedConn.getSSLSession());
    // Work around a bug in the TLSv3 implementation in some versions of Java 11
    // that interfere with the ability to get peer certificates when resuming
    // a TLS session.  To prevent that from happening here, invalidate the
    // TLS session and create a new connection so that it gets a new session.
    assertNotNull(dsProvidedConn.getRootDSE());
    dsProvidedConn.getSSLSession().invalidate();
    dsProvidedConn.close();
    dsProvidedConn = ds.getConnection();
    assertNotNull(dsProvidedConn.getSSLSession());
    // End the workaround.
    assertNotNull(dsProvidedConn.getSSLSession().getPeerCertificateChain());
    assertTrue(dsProvidedConn.getSSLSession().getPeerCertificateChain().length > 0);
    final RootDSE rootDSE = dsProvidedConn.getRootDSE();
    assertNotNull(rootDSE);
    assertEntryExists(dsProvidedConn, "dc=example,dc=com");
    assertEntryExists(dsProvidedConn, "ou=People,dc=example,dc=com");
    assertEntryExists(dsProvidedConn, "uid=test.user,ou=People,dc=example,dc=com");
    dsProvidedConn.close();
    assertNull(dsProvidedConn.getSSLSession());
}
Also used : LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) RootDSE(com.unboundid.ldap.sdk.RootDSE) Test(org.testng.annotations.Test)

Example 4 with RootDSE

use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testCustomRootDSEAttributes.

/**
 * Tests the ability of the directory server to present a dynamically
 * generated root DSE that includes custom static attributes.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testCustomRootDSEAttributes() throws Exception {
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.setMaxChangeLogEntries(Integer.MAX_VALUE);
    InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
    ds.startListening();
    ds.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    RootDSE rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 1L);
    assertFalse(rootDSE.hasAttribute("description"));
    ds.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 2L);
    assertFalse(rootDSE.hasAttribute("description"));
    ds.shutDown(true);
    cfg.setCustomRootDSEAttributes(Collections.singletonList(new Attribute("description", "custom description 1")));
    ds = new InMemoryDirectoryServer(cfg);
    ds.startListening();
    ds.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 1L);
    assertTrue(rootDSE.hasAttribute("description"));
    assertEquals(rootDSE.getAttributeValue("description"), "custom description 1");
    ds.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 2L);
    assertTrue(rootDSE.hasAttribute("description"));
    assertEquals(rootDSE.getAttributeValue("description"), "custom description 1");
    ds.shutDown(true);
    cfg.setCustomRootDSEAttributes(Arrays.asList(new Attribute("description", "custom description 2"), new Attribute("firstChangeNumber", "123"), new Attribute("lastChangeNumber", "456")));
    ds = new InMemoryDirectoryServer(cfg);
    ds.startListening();
    ds.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 123L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 456L);
    assertTrue(rootDSE.hasAttribute("description"));
    assertEquals(rootDSE.getAttributeValue("description"), "custom description 2");
    ds.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 123L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 456L);
    assertTrue(rootDSE.hasAttribute("description"));
    assertEquals(rootDSE.getAttributeValue("description"), "custom description 2");
    ds.shutDown(true);
    cfg.setCustomRootDSEAttributes(null);
    ds = new InMemoryDirectoryServer(cfg);
    ds.startListening();
    ds.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 1L);
    assertFalse(rootDSE.hasAttribute("description"));
    ds.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    rootDSE = ds.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getChangelogDN());
    assertDNsEqual(rootDSE.getChangelogDN(), "cn=changelog");
    assertNotNull(rootDSE.getFirstChangeNumber());
    assertEquals(rootDSE.getFirstChangeNumber().longValue(), 1L);
    assertNotNull(rootDSE.getLastChangeNumber());
    assertEquals(rootDSE.getLastChangeNumber().longValue(), 2L);
    assertFalse(rootDSE.hasAttribute("description"));
    ds.shutDown(true);
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) RootDSE(com.unboundid.ldap.sdk.RootDSE) Test(org.testng.annotations.Test)

Example 5 with RootDSE

use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testBasicIndexing.

/**
 * Tests the ability to perform basic kinds of operations with indexing
 * enabled for a number of attributes.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasicIndexing() throws Exception {
    final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    config.setEqualityIndexAttributes("objectClass", "uid", "givenName", "sn", "cn");
    config.setCodeLogDetails(createTempFile().getAbsolutePath(), true);
    final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
    assertNull(ds.getListenAddress());
    assertEquals(ds.getListenPort(), -1);
    assertNull(ds.getClientSocketFactory());
    assertNotNull(ds.getSchema());
    assertNotNull(ds.getBaseDNs());
    assertFalse(ds.getBaseDNs().isEmpty());
    assertEquals(ds.getBaseDNs().size(), 1);
    assertTrue(ds.getBaseDNs().contains(new DN("dc=example,dc=com")));
    try {
        ds.getConnection();
        fail("Expected an exception when trying to get a connection to a " + "server that hasn't been started yet.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.CONNECT_ERROR);
    }
    ds.startListening();
    final int listenPort = ds.getListenPort();
    assertTrue((listenPort >= 1) && (listenPort <= 65535));
    assertEquals(listenPort, ds.getListenPort());
    assertNull(ds.getListenAddress());
    assertNull(ds.getClientSocketFactory());
    final LDAPConnection conn = ds.getConnection();
    assertNotNull(conn);
    assertTrue(conn.isConnected());
    assertNull(conn.getSSLSession());
    final RootDSE rootDSE = conn.getRootDSE();
    assertNotNull(rootDSE);
    assertNotNull(rootDSE.getNamingContextDNs());
    assertEquals(rootDSE.getNamingContextDNs().length, 1);
    assertEquals(new DN(rootDSE.getNamingContextDNs()[0]), new DN("dc=example,dc=com"));
    assertNotNull(ds.getEntry(""));
    final Schema schema = conn.getSchema();
    assertNotNull(schema);
    assertNotNull(ds.getEntry("cn=schema"));
    LDAPResult result = conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    result = conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    result = conn.add("dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User", "userPassword: password");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    result = conn.bind("uid=test.user,ou=People,dc=example,dc=com", "password");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    result = conn.compare("uid=test.user,ou=People,dc=example,dc=com", "cn", "Test User");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.COMPARE_TRUE);
    result = conn.delete("uid=test.user,ou=People,dc=example,dc=com");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    final ExtendedResult extendedResult = conn.processExtendedOperation("1.2.3.4");
    assertNotNull(extendedResult);
    assertEquals(extendedResult.getResultCode(), ResultCode.UNWILLING_TO_PERFORM);
    result = conn.modify("dn: ou=People,dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    result = conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
    assertNotNull(result);
    assertEquals(result.getResultCode(), ResultCode.SUCCESS);
    SearchResult searchResult = conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 2);
    assertEquals(searchResult.getSearchEntries().get(0).getParsedDN(), new DN("dc=example,dc=com"));
    assertEquals(searchResult.getSearchEntries().get(1).getParsedDN(), new DN("ou=Users,dc=example,dc=com"));
    final Control[] unbindControls = { new Control("1.2.3.4", false), new Control("1.2.3.5", false, new ASN1OctetString("foo")) };
    conn.close(unbindControls);
    final LDAPConnectionPool pool = ds.getConnectionPool(10);
    assertNotNull(pool);
    searchResult = pool.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 2);
    assertEquals(searchResult.getSearchEntries().get(0).getParsedDN(), new DN("dc=example,dc=com"));
    assertEquals(searchResult.getSearchEntries().get(1).getParsedDN(), new DN("ou=Users,dc=example,dc=com"));
    pool.close();
    assertEquals(ds.countEntries(), 2);
    ds.clear();
    assertEquals(ds.countEntries(), 0);
    ds.shutDown(true);
    assertNull(ds.getListenAddress());
    assertEquals(ds.getListenPort(), -1);
    assertNull(ds.getClientSocketFactory());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) Schema(com.unboundid.ldap.sdk.schema.Schema) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) DN(com.unboundid.ldap.sdk.DN) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) RootDSE(com.unboundid.ldap.sdk.RootDSE) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) IgnoreNoUserModificationRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.IgnoreNoUserModificationRequestControl) AuthorizationIdentityResponseControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl) LDAPException(com.unboundid.ldap.sdk.LDAPException) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) Test(org.testng.annotations.Test)

Aggregations

RootDSE (com.unboundid.ldap.sdk.RootDSE)32 Test (org.testng.annotations.Test)26 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)23 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)13 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)12 Control (com.unboundid.ldap.sdk.Control)11 LDAPException (com.unboundid.ldap.sdk.LDAPException)9 DN (com.unboundid.ldap.sdk.DN)7 AddRequest (com.unboundid.ldap.sdk.AddRequest)6 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)6 SSLUtil (com.unboundid.util.ssl.SSLUtil)6 TrustAllTrustManager (com.unboundid.util.ssl.TrustAllTrustManager)6 Modification (com.unboundid.ldap.sdk.Modification)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)4 SearchResult (com.unboundid.ldap.sdk.SearchResult)4 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)4 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)4 PostReadRequestControl (com.unboundid.ldap.sdk.controls.PostReadRequestControl)4 IgnoreNoUserModificationRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.IgnoreNoUserModificationRequestControl)4