Exemplo n.º 1
0
  private TestResult testExceptions() {
    byte[] enc = {(byte) 0, (byte) 2, (byte) 3, (byte) 4, (byte) 5};
    MyCertPath mc = new MyCertPath(enc);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ByteArrayInputStream is = null;
    byte[] arr = null;

    try {
      ObjectOutputStream oos = new ObjectOutputStream(os);
      oos.writeObject(mc);
      oos.flush();
      oos.close();
    } catch (IOException e) {
      return new SimpleTestResult(false, getName() + ": unexpected exception.", e);
    }

    try {
      CertificateFactory cFac = CertificateFactory.getInstance("X.509", "BC");
      arr = os.toByteArray();
      is = new ByteArrayInputStream(arr);
      cFac.generateCertPath(is);
    } catch (CertificateException e) {
      // ignore okay
    } catch (Exception e) {
      return new SimpleTestResult(false, getName() + ": failed exception test.", e);
    }

    return new SimpleTestResult(true, getName() + ": Okay");
  }
Exemplo n.º 2
0
  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");
  }