Search in sources :

Example 1 with DebugType

use of com.unboundid.util.DebugType 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

ASN1BitString (com.unboundid.asn1.ASN1BitString)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 ASN1UTF8String (com.unboundid.asn1.ASN1UTF8String)1 DebugType (com.unboundid.util.DebugType)1 TestLogHandler (com.unboundid.util.TestLogHandler)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 Test (org.testng.annotations.Test)1