Search in sources :

Example 11 with TestLogHandler

use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testBindResultWithServerSASLCredentials.

/**
 * Provides coverage for the case in which a bind result message includes
 * server SASL credentials.
 *
 * Tests the behavior when logging search operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBindResultWithServerSASLCredentials() 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 BindResult bindResult = new BindResult(1, ResultCode.SUCCESS, null, null, null, null, new ASN1OctetString("creds"));
        logger.logBindResult(connection, 1, bindResult);
    }
    // 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.BIND);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 12 with TestLogHandler

use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testAddWithControls.

/**
 * Provides coverage for the case in which an add request includes controls,
 * which will  be processed as an array.
 *
 * Tests the behavior when logging search operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAddWithControls() 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 AddRequest addRequest = new AddRequest("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
        addRequest.addControl(new Control("1.2.3.4", false, null));
        addRequest.addControl(new Control("1.2.3.5", true, new ASN1OctetString("foo")));
        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);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 13 with TestLogHandler

use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testDeleteLogging.

/**
 * Tests the behavior when logging delete operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDeleteLogging() throws Exception {
    // Get the in-memory directory server instance.
    final InMemoryDirectoryServer ds = getTestDS(true, true);
    // 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 delete request on it.
    try (LDAPConnection connection = new LDAPConnection(options, "localhost", ds.getListenPort())) {
        connection.delete("uid=test.user,ou=People,dc=example,dc=com");
    }
    // Make sure that there were five log messages:
    // - Connect
    // - Delete request
    // - Delete 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.DELETE);
    assertMessageIs(logMessages.get(2), "result", OperationType.DELETE);
    assertMessageIs(logMessages.get(3), "request", OperationType.UNBIND);
    assertMessageIs(logMessages.get(4), "disconnect", null);
}
Also used : TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 14 with TestLogHandler

use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.

the class ReadOnlyInMemoryDirectoryServerConfigTestCase method testMinimalConfig.

/**
 * Provides a set of tests with a minimal configuration.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMinimalConfig() throws Exception {
    final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    ReadOnlyInMemoryDirectoryServerConfig readOnlyConfig = new ReadOnlyInMemoryDirectoryServerConfig(config);
    // Make sure that it is possible to create a directory server instance using
    // the read-only configuration, and get the read-only configuration from it.
    final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(readOnlyConfig);
    assertNotNull(ds);
    readOnlyConfig = ds.getConfig();
    // Test methods related to the set of base DNs.
    assertNotNull(readOnlyConfig.getBaseDNs());
    assertEquals(readOnlyConfig.getBaseDNs().length, 1);
    assertEquals(readOnlyConfig.getBaseDNs()[0], new DN("dc=example,dc=com"));
    try {
        readOnlyConfig.setBaseDNs("o=example.com");
        fail("Expected an exception when trying to call setBaseDNs");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setBaseDNs(new DN("o=example.com"));
        fail("Expected an exception when trying to call setBaseDNs");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the set of allowed operation types.
    assertNotNull(readOnlyConfig.getAllowedOperationTypes());
    assertEquals(readOnlyConfig.getAllowedOperationTypes(), EnumSet.allOf(OperationType.class));
    try {
        readOnlyConfig.getAllowedOperationTypes().add(OperationType.ADD);
        fail("Expected an exception when trying to alter the set returned by " + "getAllowedOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setAllowedOperationTypes(OperationType.BIND, OperationType.COMPARE, OperationType.SEARCH);
        fail("Expected an exception when trying to call " + "setAllowedOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setAllowedOperationTypes(EnumSet.of(OperationType.BIND, OperationType.COMPARE, OperationType.SEARCH));
        fail("Expected an exception when trying to call " + "setAllowedOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the set of authentication required operation
    // types.
    assertNotNull(readOnlyConfig.getAuthenticationRequiredOperationTypes());
    assertEquals(readOnlyConfig.getAuthenticationRequiredOperationTypes(), EnumSet.noneOf(OperationType.class));
    try {
        readOnlyConfig.getAuthenticationRequiredOperationTypes().add(OperationType.ADD);
        fail("Expected an exception when trying to alter the set returned by " + "getAuthenticationRequiredOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setAuthenticationRequiredOperationTypes(OperationType.ADD, OperationType.DELETE, OperationType.MODIFY, OperationType.MODIFY_DN);
        fail("Expected an exception when trying to call " + "setAuthenticationRequiredOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setAuthenticationRequiredOperationTypes(EnumSet.of(OperationType.ADD, OperationType.DELETE, OperationType.MODIFY, OperationType.MODIFY_DN));
        fail("Expected an exception when trying to call " + "setAuthenticationRequiredOperationTypes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the additional bind credentials.
    assertNotNull(readOnlyConfig.getAdditionalBindCredentials());
    assertTrue(readOnlyConfig.getAdditionalBindCredentials().isEmpty());
    try {
        readOnlyConfig.getAdditionalBindCredentials().put(new DN("cn=Directory Manager"), "password".getBytes("UTF-8"));
        fail("Expected an exception when trying to alter the map returned by " + "getAdditionalBindCredentials");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
        fail("Expected an exception when trying to call " + "addAdditionalBindCredentials");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.addAdditionalBindCredentials("cn=Directory Manager", "password".getBytes("UTF-8"));
        fail("Expected an exception when trying to call " + "addAdditionalBindCredentials");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the listener configs.
    assertNotNull(readOnlyConfig.getListenerConfigs());
    assertFalse(readOnlyConfig.getListenerConfigs().isEmpty());
    try {
        readOnlyConfig.getListenerConfigs().clear();
        fail("Expected an exception when trying to call " + "getListenerConfigs.clear");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP"));
        fail("Expected an exception when trying to call setListenerConfigs");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setListenerConfigs(Arrays.asList(InMemoryListenerConfig.createLDAPConfig("LDAP")));
        fail("Expected an exception when trying to call setListenerConfigs");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the exception handler.
    assertNull(readOnlyConfig.getListenerExceptionHandler());
    try {
        readOnlyConfig.setListenerExceptionHandler(new TestLDAPListenerExceptionHandler());
        fail("Expected an exception when trying to call " + "setListenerExceptionHandler");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the schema.
    assertNotNull(readOnlyConfig.getSchema());
    try {
        readOnlyConfig.setSchema(null);
        fail("Expected an exception when trying to call setSchema");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to attribute syntax enforcement.
    assertTrue(readOnlyConfig.enforceAttributeSyntaxCompliance());
    try {
        readOnlyConfig.setEnforceAttributeSyntaxCompliance(false);
        fail("Expected an exception when trying to call " + "setEnforceAttributeSyntaxCompliance");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to single structural object class enforcement.
    assertTrue(readOnlyConfig.enforceSingleStructuralObjectClass());
    try {
        readOnlyConfig.setEnforceSingleStructuralObjectClass(false);
        fail("Expected an exception when trying to call " + "setEnforceSingleStructuralObjectClass");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the access log handler.
    assertNull(readOnlyConfig.getAccessLogHandler());
    try {
        readOnlyConfig.setAccessLogHandler(new TestLogHandler());
        fail("Expected an exception when trying to call setAccessLogHandler");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the LDAP debug log handler.
    assertNull(readOnlyConfig.getLDAPDebugLogHandler());
    try {
        readOnlyConfig.setLDAPDebugLogHandler(new TestLogHandler());
        fail("Expected an exception when trying to call setLDAPDebugLogHandler");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the extended operation handlers.
    assertNotNull(readOnlyConfig.getExtendedOperationHandlers());
    assertFalse(readOnlyConfig.getExtendedOperationHandlers().isEmpty());
    try {
        readOnlyConfig.getExtendedOperationHandlers().add(new TestExtendedOperationHandler());
        fail("Expected an exception when trying to alter the list returned by " + "getExtendedOperationHandlers");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.addExtendedOperationHandler(new TestExtendedOperationHandler());
        fail("Expected an exception when trying to call " + "addExtendedOperationHandler");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the SASL bind handlers.
    assertNotNull(readOnlyConfig.getSASLBindHandlers());
    assertFalse(readOnlyConfig.getSASLBindHandlers().isEmpty());
    try {
        readOnlyConfig.getSASLBindHandlers().add(new TestSASLBindHandler());
        fail("Expected an exception when trying to alter the list returned by " + "getSASLBindHandlers");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.addSASLBindHandler(new TestSASLBindHandler());
        fail("Expected an exception when trying to call addSASLBindHandler");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to the generation of operational attributes.
    assertTrue(readOnlyConfig.generateOperationalAttributes());
    try {
        readOnlyConfig.setGenerateOperationalAttributes(false);
        fail("Expected an exception when trying to call " + "setGenerateOperationalAttributes");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to changelog entries.
    assertEquals(readOnlyConfig.getMaxChangeLogEntries(), 0);
    try {
        readOnlyConfig.setMaxChangeLogEntries(100);
        fail("Expected an exception when trying to call " + "setMaxChangeLogEntries");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test methods related to equality index attributes.
    assertNotNull(readOnlyConfig.getEqualityIndexAttributes());
    assertTrue(readOnlyConfig.getEqualityIndexAttributes().isEmpty());
    try {
        readOnlyConfig.setEqualityIndexAttributes("member");
        fail("Expected an exception when trying to call " + "setEqualityIndexAttributes");
    } catch (final UnsupportedOperationException e) {
    // This was expected
    }
    try {
        readOnlyConfig.setEqualityIndexAttributes(Arrays.asList("member", "uniqueMember", "owner", "seeAlso"));
        fail("Expected an exception when trying to call " + "setEqualityIndexAttributes");
    } catch (final UnsupportedOperationException e) {
    // This was expected
    }
    // Test methods related to referential integrity attributes.
    assertNotNull(readOnlyConfig.getReferentialIntegrityAttributes());
    assertTrue(readOnlyConfig.getReferentialIntegrityAttributes().isEmpty());
    try {
        readOnlyConfig.setReferentialIntegrityAttributes("member");
        fail("Expected an exception when trying to call " + "setReferentialIntegrityAttributes");
    } catch (final UnsupportedOperationException e) {
    // This was expected
    }
    try {
        readOnlyConfig.setReferentialIntegrityAttributes(Arrays.asList("member", "uniqueMember", "owner", "seeAlso"));
        fail("Expected an exception when trying to call " + "setReferentialIntegrityAttributes");
    } catch (final UnsupportedOperationException e) {
    // This was expected
    }
    try {
        readOnlyConfig.setVendorName(null);
        fail("Expected an exception when trying to call setVendorName");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setVendorName("foo");
        fail("Expected an exception when trying to call setVendorName");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setVendorVersion(null);
        fail("Expected an exception when trying to call setVendorVersion");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    try {
        readOnlyConfig.setVendorVersion("foo");
        fail("Expected an exception when trying to call setVendorVersion");
    } catch (final UnsupportedOperationException e) {
    // This was expected.
    }
    // Test the toString method.
    assertNotNull(readOnlyConfig.toString());
}
Also used : TestLogHandler(com.unboundid.util.TestLogHandler) DN(com.unboundid.ldap.sdk.DN) OperationType(com.unboundid.ldap.sdk.OperationType) Test(org.testng.annotations.Test)

Example 15 with TestLogHandler

use of com.unboundid.util.TestLogHandler in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodingAllJVMDefaultCertificates.

/**
 * Tests to verify that all of the certificates in the JVM-default trust store
 * can be decoded without error.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDecodingAllJVMDefaultCertificates() throws Exception {
    final KeyStore keyStore = CryptoHelper.getKeyStore("JKS");
    final File caCertsFile = JVMDefaultTrustManager.getInstance().getCACertsFile();
    try (FileInputStream inputStream = new FileInputStream(caCertsFile)) {
        keyStore.load(inputStream, null);
    }
    final boolean enabledBeforeStarting = Debug.debugEnabled();
    final Set<DebugType> debugTypesBeforeStarting = Debug.getDebugTypes();
    final Logger logger = Debug.getLogger();
    final Level levelBeforeStarting = logger.getLevel();
    final boolean useParentHandlersBeforeStarting = logger.getUseParentHandlers();
    final TestLogHandler testLogHandler = new TestLogHandler();
    try {
        Debug.setEnabled(true, EnumSet.allOf(DebugType.class));
        logger.setUseParentHandlers(false);
        testLogHandler.setFilter(null);
        testLogHandler.setLevel(Level.ALL);
        logger.addHandler(testLogHandler);
        final Enumeration<String> aliasEnumeration = keyStore.aliases();
        while (aliasEnumeration.hasMoreElements()) {
            final String alias = aliasEnumeration.nextElement();
            final KeyStore.Entry entry = keyStore.getEntry(alias, null);
            if (entry instanceof KeyStore.TrustedCertificateEntry) {
                final KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry) entry;
                new X509Certificate(tce.getTrustedCertificate().getEncoded());
            } else if (entry instanceof KeyStore.PrivateKeyEntry) {
                final KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry) entry;
                for (final Certificate c : pke.getCertificateChain()) {
                    new X509Certificate(c.getEncoded());
                }
            }
        }
    } finally {
        logger.removeHandler(testLogHandler);
        Debug.setEnabled(enabledBeforeStarting, debugTypesBeforeStarting);
        logger.setLevel(levelBeforeStarting);
        logger.setUseParentHandlers(useParentHandlersBeforeStarting);
    }
    assertEquals(testLogHandler.getMessageCount(), 0, testLogHandler.getMessagesString());
}
Also used : TestLogHandler(com.unboundid.util.TestLogHandler) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1UTF8String(com.unboundid.asn1.ASN1UTF8String) DebugType(com.unboundid.util.DebugType) Logger(java.util.logging.Logger) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Level(java.util.logging.Level) File(java.io.File) Certificate(java.security.cert.Certificate) Test(org.testng.annotations.Test)

Aggregations

TestLogHandler (com.unboundid.util.TestLogHandler)26 Test (org.testng.annotations.Test)26 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)21 JSONObject (com.unboundid.util.json.JSONObject)21 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)6 DN (com.unboundid.ldap.sdk.DN)2 ASN1BitString (com.unboundid.asn1.ASN1BitString)1 ASN1UTF8String (com.unboundid.asn1.ASN1UTF8String)1 InMemoryDirectoryServerConfig (com.unboundid.ldap.listener.InMemoryDirectoryServerConfig)1 TestIntermediateResponseExtendedOperationHandler (com.unboundid.ldap.listener.TestIntermediateResponseExtendedOperationHandler)1 OperationType (com.unboundid.ldap.sdk.OperationType)1 DebugType (com.unboundid.util.DebugType)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 Certificate (java.security.cert.Certificate)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1