use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.
the class ExampleUsagesTestCase method testSSLUtilExample2.
/**
* Tests the second example in the {@code SSLUtil} class.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSSLUtilExample2() throws Exception {
/* ----- BEGIN PRE-EXAMPLE SETUP ----- */
final File resourceDir = new File(System.getProperty("unit.resource.dir"));
final File serverKeyStore = new File(resourceDir, "server.keystore");
assertTrue(serverKeyStore.exists());
final String serverKeyStorePath = serverKeyStore.getAbsolutePath();
final char[] serverKeyStorePIN = "password".toCharArray();
final File serverTrustStore = new File(resourceDir, "server.truststore");
assertTrue(serverTrustStore.exists());
final String serverTrustStorePath = serverTrustStore.getAbsolutePath();
final String trustStorePath = serverKeyStorePath;
final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
final SSLUtil serverSSLUtil = new SSLUtil(new KeyStoreKeyManager(serverKeyStorePath, serverKeyStorePIN, "JKS", "server-cert"), new TrustStoreTrustManager(serverTrustStorePath));
final SSLUtil clientSSLUtil = new SSLUtil(new TrustStoreTrustManager(trustStorePath));
config.setListenerConfigs(// Listener name
InMemoryListenerConfig.createLDAPConfig(// Listener name
"LDAP", // Listen address. (null = listen on all interfaces)
null, // Listen port (0 = automatically choose an available port)
0, // StartTLS factory
serverSSLUtil.createSSLSocketFactory()));
final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.startListening();
final String serverAddress = "localhost";
final int serverPort = ds.getListenPort();
/* ----- BEGIN EXAMPLE CODE ----- */
// Establish a non-secure connection to the server.
LDAPConnection connection = new LDAPConnection(serverAddress, serverPort);
// Create an SSLUtil instance that is configured to trust certificates in
// a specified trust store file, and use it to create an SSLContext that
// will be used for StartTLS processing.
SSLUtil sslUtil = new SSLUtil(new TrustStoreTrustManager(trustStorePath));
SSLContext sslContext = sslUtil.createSSLContext();
// Use the StartTLS extended operation to secure the connection.
StartTLSExtendedRequest startTLSRequest = new StartTLSExtendedRequest(sslContext);
ExtendedResult startTLSResult;
try {
startTLSResult = connection.processExtendedOperation(startTLSRequest);
} catch (LDAPException le) {
startTLSResult = new ExtendedResult(le);
}
LDAPTestUtils.assertResultCodeEquals(startTLSResult, ResultCode.SUCCESS);
// Process operations using the connection....
RootDSE rootDSE = connection.getRootDSE();
connection.close();
/* ----- END EXAMPLE CODE ----- */
/* ----- BEGIN POST-EXAMPLE CLEANUP ----- */
ds.shutDown(true);
assertNotNull(rootDSE);
}
use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.
the class StartInteractiveTransactionExtendedRequestTestCase method testAbortTransaction.
/**
* Tests the process of creating a transaction, including multiple operations
* as part of that transaction, and then aborting it.
* <BR><BR>
* Access to a Directory Server instance is required for complete processing.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAbortTransaction() throws Exception {
if (!isDirectoryInstanceAvailable()) {
return;
}
LDAPConnection conn = getAdminConnection();
RootDSE rootDSE = conn.getRootDSE();
if ((rootDSE == null) || (!rootDSE.supportsExtendedOperation(StartInteractiveTransactionExtendedRequest.START_INTERACTIVE_TRANSACTION_REQUEST_OID))) {
conn.close();
return;
}
// Start the interactive transaction.
StartInteractiveTransactionExtendedResult startTxnResult = (StartInteractiveTransactionExtendedResult) conn.processExtendedOperation(new StartInteractiveTransactionExtendedRequest(getTestBaseDN()));
assertEquals(startTxnResult.getResultCode(), ResultCode.SUCCESS);
ASN1OctetString txnID = startTxnResult.getTransactionID();
assertNotNull(txnID);
assertNotNull(startTxnResult.toString());
Control[] controls = { new com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationRequestControl(txnID, true, true) };
// Add the base entry.
AddRequest addRequest = new AddRequest(getTestBaseDN(), getBaseEntryAttributes(), controls);
LDAPResult addResult = conn.add(addRequest);
assertEquals(addResult.getResultCode(), ResultCode.SUCCESS);
Control c = addResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Ensure that we can retrieve the base entry as part of the transaction.
// Note that the search needs to be indexed, since unindexed searches won't
// be allowed as part of a transaction.
SearchRequest searchRequest = new SearchRequest(getTestBaseDN(), SearchScope.SUB, "(objectClass=top)");
searchRequest.setControls(controls);
SearchResult searchResult = conn.search(searchRequest);
assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
assertEquals(searchResult.getEntryCount(), 1, searchResult.getSearchEntries().toString());
c = searchResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Add an "ou=People" entry.
addRequest = new AddRequest("dn: ou=People," + getTestBaseDN(), "objectClass: top", "objectClass: organizationalUnit", "ou: People", "description: foo");
addRequest.setControls(controls);
addResult = conn.add(addRequest);
assertEquals(addResult.getResultCode(), ResultCode.SUCCESS);
c = addResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Ensure that we can now retrieve the both entries as part of the
// transaction.
searchResult = conn.search(searchRequest);
assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
assertEquals(searchResult.getEntryCount(), 2, searchResult.getSearchEntries().toString());
c = searchResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Perform a compare against the entry.
CompareRequest compareRequest = new CompareRequest("ou=People," + getTestBaseDN(), "description", "foo", controls);
CompareResult compareResult = conn.compare(compareRequest);
assertTrue(compareResult.compareMatched());
c = compareResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Modify the entry.
ModifyRequest modifyRequest = new ModifyRequest("dn: ou=People," + getTestBaseDN(), "changetype: modify", "replace: description", "description: bar");
modifyRequest.setControls(controls);
LDAPResult modifyResult = conn.modify(modifyRequest);
assertEquals(modifyResult.getResultCode(), ResultCode.SUCCESS);
c = modifyResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Perform another compare against the entry to verify the change.
compareRequest = new CompareRequest("ou=People," + getTestBaseDN(), "description", "bar", controls);
compareResult = conn.compare(compareRequest);
assertTrue(compareResult.compareMatched());
c = compareResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Rename the target entry.
ModifyDNRequest modifyDNRequest = new ModifyDNRequest("ou=People," + getTestBaseDN(), "ou=Users", true, controls);
LDAPResult modifyDNResult = conn.modifyDN(modifyDNRequest);
assertEquals(modifyDNResult.getResultCode(), ResultCode.SUCCESS);
c = modifyDNResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Perform a search below the base entry and verify that we still get two
// entries returned.
searchResult = conn.search(searchRequest);
assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
assertEquals(searchResult.getEntryCount(), 2, searchResult.getSearchEntries().toString());
c = searchResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Delete the "ou=Users" entry.
DeleteRequest deleteRequest = new DeleteRequest("ou=Users," + getTestBaseDN(), controls);
LDAPResult deleteResult = conn.delete(deleteRequest);
assertEquals(deleteResult.getResultCode(), ResultCode.SUCCESS);
c = deleteResult.getResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl.INTERACTIVE_TRANSACTION_SPECIFICATION_RESPONSE_OID);
assertNotNull(c);
assertTrue(c instanceof com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl);
itsrc = (com.unboundid.ldap.sdk.unboundidds.controls.InteractiveTransactionSpecificationResponseControl) c;
assertTrue(itsrc.transactionValid());
// Abort the transaction.
ExtendedResult endTxnResult = conn.processExtendedOperation(new EndInteractiveTransactionExtendedRequest(txnID, false));
assertEquals(endTxnResult.getResultCode(), ResultCode.INTERACTIVE_TRANSACTION_ABORTED);
// Verify that the base entry does not exist.
try {
assertNull(conn.getEntry(getTestBaseDN()));
} finally {
conn.close();
}
}
use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.
the class StartBatchedTransactionExtendedRequestTestCase method testCommitTransaction.
/**
* Tests the process of creating a transaction, including multiple operations
* as part of that transaction, and then committing it.
* <BR><BR>
* Access to a Directory Server instance is required for complete processing.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testCommitTransaction() throws Exception {
if (!isDirectoryInstanceAvailable()) {
return;
}
LDAPConnection conn = getAdminConnection();
RootDSE rootDSE = conn.getRootDSE();
if ((rootDSE == null) || (!rootDSE.supportsExtendedOperation(StartBatchedTransactionExtendedRequest.START_BATCHED_TRANSACTION_REQUEST_OID))) {
conn.close();
return;
}
StartBatchedTransactionExtendedResult startTxnResult = (StartBatchedTransactionExtendedResult) conn.processExtendedOperation(new StartBatchedTransactionExtendedRequest());
assertEquals(startTxnResult.getResultCode(), ResultCode.SUCCESS);
ASN1OctetString txnID = startTxnResult.getTransactionID();
assertNotNull(txnID);
assertNotNull(startTxnResult.toString());
Control[] controls = { new BatchedTransactionSpecificationRequestControl(txnID), new PostReadRequestControl(true) };
AddRequest addRequest = new AddRequest(getTestBaseDN(), getBaseEntryAttributes(), controls);
conn.add(addRequest);
Modification[] mods = { new Modification(ModificationType.REPLACE, "description", "foo") };
ModifyRequest modifyRequest = new ModifyRequest(getTestBaseDN(), mods, controls);
conn.modify(modifyRequest);
EndBatchedTransactionExtendedResult endTxnResult = (EndBatchedTransactionExtendedResult) conn.processExtendedOperation(new EndBatchedTransactionExtendedRequest(txnID, true));
assertEquals(endTxnResult.getResultCode(), ResultCode.SUCCESS);
assertNotNull(endTxnResult.getOperationResponseControls());
assertFalse(endTxnResult.getOperationResponseControls().isEmpty());
assertNotNull(endTxnResult.toString());
conn.delete(getTestBaseDN());
conn.close();
}
use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.
the class GetUserResourceLimitsRequestControlTestCase method testAdvertisesExcludeGroupsFeature.
/**
* Tests the behavior of the {@code serverAdvertisesExcludeGroupsFeature}
* method.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAdvertisesExcludeGroupsFeature() throws Exception {
final RootDSE defaultRootDSE = getTestDS().getRootDSE();
assertNotNull(defaultRootDSE);
assertFalse(GetUserResourceLimitsRequestControl.serverAdvertisesExcludeGroupsFeature(defaultRootDSE));
final Entry updatedRootDSEEntry = defaultRootDSE.duplicate();
updatedRootDSEEntry.addAttribute(RootDSE.ATTR_SUPPORTED_FEATURE, "1.3.6.1.4.1.30221.2.12.6");
final RootDSE updatedRootDSE = new RootDSE(updatedRootDSEEntry);
assertTrue(GetUserResourceLimitsRequestControl.serverAdvertisesExcludeGroupsFeature(updatedRootDSE));
}
use of com.unboundid.ldap.sdk.RootDSE in project ldapsdk by pingidentity.
the class ExampleUsagesTestCase method testStartTLSExtendedRequestExample.
/**
* Tests the example in the {@code StartTLSExtendedRequest} class.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testStartTLSExtendedRequestExample() throws Exception {
/* ----- BEGIN PRE-EXAMPLE SETUP ----- */
final File resourceDir = new File(System.getProperty("unit.resource.dir"));
final File serverKeyStore = new File(resourceDir, "server.keystore");
assertTrue(serverKeyStore.exists());
// The client trust store will be the same as the server key store.
final String trustStorePath = serverKeyStore.getAbsolutePath();
final SSLUtil serverSSLUtil = new SSLUtil(new KeyStoreKeyManager(serverKeyStore, "password".toCharArray(), "JKS", "server-cert"), new TrustAllTrustManager());
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
config.setListenerConfigs(// Listener name
InMemoryListenerConfig.createLDAPConfig(// Listener name
"LDAP", // Listen address. (null = listen on all interfaces)
null, // Listen port (0 = automatically choose an available port)
0, // StartTLS factory
serverSSLUtil.createSSLSocketFactory()));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.startListening();
LDAPConnection connection = ds.getConnection();
/* ----- BEGIN EXAMPLE CODE ----- */
// Create an SSLContext that will be used to perform the cryptographic
// processing.
SSLUtil sslUtil = new SSLUtil(new TrustStoreTrustManager(trustStorePath));
SSLContext sslContext = sslUtil.createSSLContext();
// Create and process the extended request to secure a connection.
StartTLSExtendedRequest startTLSRequest = new StartTLSExtendedRequest(sslContext);
ExtendedResult startTLSResult;
try {
startTLSResult = connection.processExtendedOperation(startTLSRequest);
// This doesn't necessarily mean that the operation was successful, since
// some kinds of extended operations return non-success results under
// normal conditions.
} catch (LDAPException le) {
// For an extended operation, this generally means that a problem was
// encountered while trying to send the request or read the result.
startTLSResult = new ExtendedResult(le);
}
// Make sure that we can use the connection to interact with the server.
RootDSE rootDSE = connection.getRootDSE();
/* ----- END EXAMPLE CODE ----- */
/* ----- BEGIN POST-EXAMPLE CLEANUP ----- */
connection.close();
ds.shutDown(true);
assertResultCodeEquals(startTLSResult, ResultCode.SUCCESS);
assertNotNull(rootDSE);
}
Aggregations