use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.
the class JSONLDAPConnectionLoggerTestCase method testAddWithRedactionNotNeeded.
/**
* Provides coverage for the case in which no redaction is required because
* there are no defined attributes to redact.
*
* Tests the behavior when logging search operations.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAddWithRedactionNotNeeded() throws Exception {
// Get the in-memory directory server instance.
final InMemoryDirectoryServer ds = getTestDS(false, false);
// Create a logger to use for the test.
final TestLogHandler logHandler = new TestLogHandler();
final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
properties.setAttributesToRedact();
properties.setIncludeAddAttributeNames(true);
properties.setIncludeAddAttributeValues(true);
final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
final LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setConnectionLogger(logger);
// Generate a log message.
try (LDAPConnection connection = ds.getConnection()) {
final AddRequest addRequest = new AddRequest("dn: userPassword=password,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");
logger.logAddRequest(connection, 1, addRequest);
}
// Make sure that we can decode the message.
assertEquals(logHandler.getMessageCount(), 1, logHandler.getMessagesString());
final List<JSONObject> logMessages = parseLogMessages(logHandler);
assertMessageIs(logMessages.get(0), "request", OperationType.ADD);
}
use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.
the class JSONLDAPConnectionLoggerTestCase method testSearchLogging.
/**
* Tests the behavior when logging search operations.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSearchLogging() throws Exception {
// Get the in-memory directory server instance.
final InMemoryDirectoryServer ds = getTestDS(true, true);
ds.add("dn: ou=Users,dc=example,dc=com", "objectClass: top", "objectClass: referral", "objectClass: extensibleObject", "ref: ldap://localhost:" + ds.getListenPort() + "/ou=People,dc=example,dc=com");
// Create a logger to use for the test.
final TestLogHandler logHandler = new TestLogHandler();
final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
properties.setLogSearchEntries(true);
properties.setLogSearchReferences(true);
properties.setIncludeSearchEntryAttributeNames(true);
properties.setIncludeSearchEntryAttributeValues(true);
final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
final LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setConnectionLogger(logger);
// Establish a connection and perform a search on it.
try (LDAPConnection connection = new LDAPConnection(options, "localhost", ds.getListenPort())) {
connection.search("dc=example,dc=com", SearchScope.SUB, Filter.createPresenceFilter("objectClass"), "*", "+");
}
// Make sure that there were nine log messages:
// - Connect
// - Search request
// - Search result entry (dc=example,dc=com)
// - Search result entry (ou=People,dc=example,dc=com)
// - Search result entry (uid=test.user,ou=People,dc=example,dc=com)
// - Search result reference
// - Search result done
// - Unbind
// - Disconnect
assertEquals(logHandler.getMessageCount(), 9, logHandler.getMessagesString());
final List<JSONObject> logMessages = parseLogMessages(logHandler);
assertMessageIs(logMessages.get(0), "connect", null);
assertMessageIs(logMessages.get(1), "request", OperationType.SEARCH);
assertMessageIs(logMessages.get(2), "search-entry", OperationType.SEARCH);
assertMessageIs(logMessages.get(3), "search-entry", OperationType.SEARCH);
assertMessageIs(logMessages.get(4), "search-entry", OperationType.SEARCH);
assertMessageIs(logMessages.get(5), "search-reference", OperationType.SEARCH);
assertMessageIs(logMessages.get(6), "result", OperationType.SEARCH);
assertMessageIs(logMessages.get(7), "request", OperationType.UNBIND);
assertMessageIs(logMessages.get(8), "disconnect", null);
}
use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.
the class JSONLDAPConnectionLoggerTestCase method testAddResultWithAllComponents.
/**
* Provides coverage for the case in which an add result message contains
* all components.
*
* Tests the behavior when logging search operations.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAddResultWithAllComponents() throws Exception {
// Get the in-memory directory server instance.
final InMemoryDirectoryServer ds = getTestDS(false, false);
// Create a logger to use for the test.
final TestLogHandler logHandler = new TestLogHandler();
final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
final LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setConnectionLogger(logger);
// Generate a log message.
try (LDAPConnection connection = ds.getConnection()) {
final LDAPResult addResult = new LDAPResult(1, ResultCode.REFERRAL, "Try the operation somewhere else", "dc=example,dc=com", new String[] { "ldap://ds1.example.com:389/dc=example,dc=com", "ldap://ds2.example.com:389/dc=example,dc=com" }, new Control[] { new Control("1.2.3.4", false, null), new Control("1.2.3.5", true, new ASN1OctetString("foo")) });
logger.logAddResult(connection, 1, addResult);
}
// Make sure that we can decode the message.
assertEquals(logHandler.getMessageCount(), 1, logHandler.getMessagesString());
final List<JSONObject> logMessages = parseLogMessages(logHandler);
assertMessageIs(logMessages.get(0), "result", OperationType.ADD);
}
use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.
the class JSONLDAPConnectionLoggerTestCase method testSimpleBindLogging.
/**
* Tests the behavior when logging simple bind operations.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSimpleBindLogging() throws Exception {
// Get the in-memory directory server instance.
final InMemoryDirectoryServer ds = getTestDS();
// Create a logger to use for the test.
final TestLogHandler logHandler = new TestLogHandler();
final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
final LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setConnectionLogger(logger);
// Establish a connection and send a simple bind request on it.
try (LDAPConnection connection = new LDAPConnection(options, "localhost", ds.getListenPort())) {
connection.bind("cn=Directory Manager", "password");
}
// Make sure that there were five log messages:
// - Connect
// - Bind request
// - Bind result
// - Unbind
// - Disconnect
assertEquals(logHandler.getMessageCount(), 5, logHandler.getMessagesString());
final List<JSONObject> logMessages = parseLogMessages(logHandler);
assertMessageIs(logMessages.get(0), "connect", null);
assertMessageIs(logMessages.get(1), "request", OperationType.BIND);
assertMessageIs(logMessages.get(2), "result", OperationType.BIND);
assertMessageIs(logMessages.get(3), "request", OperationType.UNBIND);
assertMessageIs(logMessages.get(4), "disconnect", null);
}
use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.
the class JSONLDAPConnectionLoggerTestCase method testFailedConnectionLogging.
/**
* Tests the behavior when a connection attempt fails.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testFailedConnectionLogging() throws Exception {
// Get the in-memory directory server instance and shut it down.
final InMemoryDirectoryServer ds = getTestDS();
final int port = ds.getListenPort();
ds.shutDown(true);
try {
// Create a logger to use for the test.
final TestLogHandler logHandler = new TestLogHandler();
final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
final LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setConnectionLogger(logger);
// Try and fail to establish a connection to the offline server.
try (LDAPConnection connection = new LDAPConnection(options, "localhost", port)) {
assertFalse(connection.isConnected());
fail("Unexpectedly connected to an offline server");
} catch (final LDAPException e) {
// This was expected.
}
// Make sure that there was one log message, and that it was a
// connect failure message.
assertEquals(logHandler.getMessageCount(), 1);
final List<JSONObject> logMessages = parseLogMessages(logHandler);
assertMessageIs(logMessages.get(0), "connect-failure", null);
} finally {
ds.startListening();
}
}
Aggregations