Code example for CertificateFactory

Methods: generateCertPath, getProvider

0
        } 
    } 
 
    private void testCertPathEncoding(CertificateFactory cf, List<X509Certificate> expectedCerts,
            String encoding) throws Exception {
        final String providerName = cf.getProvider().getName() + "[" + encoding + "]";
 
        final CertPath pathFromList = cf.generateCertPath(expectedCerts);
 
        // Create a copy we can modify and discard. 
        final byte[] encodedCopy;
        if (encoding == null) {
            encodedCopy = pathFromList.getEncoded();
            assertNotNull(providerName, encodedCopy);
 
            // check idempotence 
            assertEquals(providerName, Arrays.toString(pathFromList.getEncoded()),
                    Arrays.toString(encodedCopy));
        } else { 
            encodedCopy = pathFromList.getEncoded(encoding);
            assertNotNull(providerName, encodedCopy);
 
            // check idempotence 
            assertEquals(providerName, Arrays.toString(pathFromList.getEncoded(encoding)),
                    Arrays.toString(encodedCopy));
        } 
 
        // Try to modify byte array. 
        encodedCopy[0] ^= (byte) 0xFF;
 
        // Get a real copy we will use if the test proceeds. 
        final byte[] encoded;
        if (encoding == null) {
            encoded = pathFromList.getEncoded();
            assertNotNull(providerName, encodedCopy);
 
            // check idempotence 
            assertEquals(providerName, Arrays.toString(pathFromList.getEncoded()),
                    Arrays.toString(encoded));
        } else { 
            encoded = pathFromList.getEncoded(encoding);
            assertNotNull(providerName, encodedCopy);
 
            // check idempotence 
            assertEquals(providerName, Arrays.toString(pathFromList.getEncoded(encoding)),
                    Arrays.toString(encoded));
        } 
        assertFalse(providerName, Arrays.toString(encoded).equals(Arrays.toString(encodedCopy)));
 
        encodedCopy[0] ^= (byte) 0xFF;
        assertEquals(providerName, Arrays.toString(encoded), Arrays.toString(encodedCopy));
 
        final CertPath actualPath;
        if (encoding == null) {
            actualPath = cf.generateCertPath(new ByteArrayInputStream(encoded));
        } else { 
            actualPath = cf.generateCertPath(new ByteArrayInputStream(encoded), encoding);
        } 
 
        // PKCS7 certificate bags are not guaranteed to be in order.