Code example for CertPath

Methods: CertPath.getEncoded, getCertificates

0
        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. 
        final List<? extends Certificate> actualCerts;
        if (!"PKCS7".equals(encoding)) {
            actualCerts = actualPath.getCertificates();
            assertEquals(providerName, expectedCerts, actualCerts);
        } else { 
            actualCerts = pathFromList.getCertificates();
        } 
 
        try { 
            actualCerts.remove(0);
            fail("List of certificate should be immutable");