001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.util.test.SimpleTest;
005: import org.bouncycastle.x509.X509CertificatePair;
006:
007: import java.io.ByteArrayInputStream;
008: import java.security.Security;
009: import java.security.cert.CertificateEncodingException;
010: import java.security.cert.CertificateFactory;
011: import java.security.cert.X509Certificate;
012:
013: public class X509CertificatePairTest extends SimpleTest {
014: public void performTest() throws Exception {
015: CertificateFactory cf = CertificateFactory.getInstance("X.509",
016: "BC");
017:
018: X509Certificate rootCert = (X509Certificate) cf
019: .generateCertificate(new ByteArrayInputStream(
020: CertPathTest.rootCertBin));
021: X509Certificate interCert = (X509Certificate) cf
022: .generateCertificate(new ByteArrayInputStream(
023: CertPathTest.interCertBin));
024: X509Certificate finalCert = (X509Certificate) cf
025: .generateCertificate(new ByteArrayInputStream(
026: CertPathTest.finalCertBin));
027:
028: X509CertificatePair pair1 = new X509CertificatePair(rootCert,
029: interCert);
030: X509CertificatePair pair2 = new X509CertificatePair(rootCert,
031: interCert);
032: X509CertificatePair pair3 = new X509CertificatePair(interCert,
033: finalCert);
034: X509CertificatePair pair4 = new X509CertificatePair(rootCert,
035: finalCert);
036: X509CertificatePair pair5 = new X509CertificatePair(rootCert,
037: null);
038: X509CertificatePair pair6 = new X509CertificatePair(rootCert,
039: null);
040: X509CertificatePair pair7 = new X509CertificatePair(null,
041: rootCert);
042: X509CertificatePair pair8 = new X509CertificatePair(null,
043: rootCert);
044:
045: if (!pair1.equals(pair2)) {
046: fail("pair1 pair2 equality test");
047: }
048:
049: if (!pair5.equals(pair6)) {
050: fail("pair1 pair2 equality test");
051: }
052:
053: if (!pair7.equals(pair8)) {
054: fail("pair1 pair2 equality test");
055: }
056:
057: if (pair1.equals(null)) {
058: fail("pair1 null equality test");
059: }
060:
061: if (pair1.hashCode() != pair2.hashCode()) {
062: fail("pair1 pair2 hashCode equality test");
063: }
064:
065: if (pair1.equals(pair3)) {
066: fail("pair1 pair3 inequality test");
067: }
068:
069: if (pair1.equals(pair4)) {
070: fail("pair1 pair4 inequality test");
071: }
072:
073: if (pair1.equals(pair5)) {
074: fail("pair1 pair5 inequality test");
075: }
076:
077: if (pair1.equals(pair7)) {
078: fail("pair1 pair7 inequality test");
079: }
080:
081: if (pair5.equals(pair1)) {
082: fail("pair5 pair1 inequality test");
083: }
084:
085: if (pair7.equals(pair1)) {
086: fail("pair7 pair1 inequality test");
087: }
088:
089: if (pair1.getForward() != rootCert) {
090: fail("pair1 forward test");
091: }
092:
093: if (pair1.getReverse() != interCert) {
094: fail("pair1 reverse test");
095: }
096:
097: if (!areEqual(pair1.getEncoded(), pair2.getEncoded())) {
098: fail("encoding check");
099: }
100:
101: pair4 = new X509CertificatePair(rootCert, TestUtils
102: .createExceptionCertificate(false));
103:
104: try {
105: pair4.getEncoded();
106:
107: fail("no exception on bad getEncoded()");
108: } catch (CertificateEncodingException e) {
109: // expected
110: }
111:
112: pair4 = new X509CertificatePair(rootCert, TestUtils
113: .createExceptionCertificate(true));
114:
115: try {
116: pair4.getEncoded();
117:
118: fail("no exception on exception getEncoded()");
119: } catch (CertificateEncodingException e) {
120: // expected
121: }
122: }
123:
124: public String getName() {
125: return "X509CertificatePair";
126: }
127:
128: public static void main(String[] args) {
129: Security.addProvider(new BouncyCastleProvider());
130:
131: runTest(new X509CertificatePairTest());
132: }
133:
134: }
|