public static void main(String[] args) {
    Security.addProvider(new BouncyCastleProvider());

    Test test = new CertPathTest();
    TestResult result = test.perform();

    System.out.println(result.toString());
  }
  public TestResult perform() {
    try {
      CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");

      X509Certificate rootCert =
          (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(rootCertBin));
      X509Certificate interCert =
          (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(interCertBin));
      X509Certificate finalCert =
          (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(finalCertBin));

      // Testing CertPath generation from List
      List list = new ArrayList();
      list.add(interCert);
      CertPath certPath1 = cf.generateCertPath(list);

      // Testing CertPath encoding as PkiPath
      byte[] encoded = certPath1.getEncoded("PkiPath");

      // Testing CertPath generation from InputStream
      ByteArrayInputStream inStream = new ByteArrayInputStream(encoded);
      CertPath certPath2 = cf.generateCertPath(inStream, "PkiPath");

      // Comparing both CertPathes
      if (!certPath2.equals(certPath1)) {
        return new SimpleTestResult(
            false, this.getName() + ": CertPath differ after encoding and decoding.");
      }

      encoded = certPath1.getEncoded("PKCS7");

      // Testing CertPath generation from InputStream
      inStream = new ByteArrayInputStream(encoded);
      certPath2 = cf.generateCertPath(inStream, "PKCS7");

      // Comparing both CertPathes
      if (!certPath2.equals(certPath1)) {
        return new SimpleTestResult(
            false, this.getName() + ": CertPath differ after encoding and decoding.");
      }

      encoded = certPath1.getEncoded("PEM");

      // Testing CertPath generation from InputStream
      inStream = new ByteArrayInputStream(encoded);
      certPath2 = cf.generateCertPath(inStream, "PEM");

      // Comparing both CertPathes
      if (!certPath2.equals(certPath1)) {
        return new SimpleTestResult(
            false, this.getName() + ": CertPath differ after encoding and decoding.");
      }

      TestResult res = testExceptions();

      if (!res.isSuccessful()) {
        return res;
      }
    } catch (Exception e) {
      return new SimpleTestResult(false, this.getName() + ": exception - " + e.toString(), e);
    }

    return new SimpleTestResult(true, this.getName() + ": Okay");
  }