01: package org.bouncycastle.asn1.test;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.ByteArrayOutputStream;
05:
06: import org.bouncycastle.util.encoders.Base64;
07: import org.bouncycastle.util.test.Test;
08: import org.bouncycastle.util.test.TestResult;
09: import org.bouncycastle.util.test.SimpleTestResult;
10: import org.bouncycastle.asn1.ASN1Encodable;
11: import org.bouncycastle.asn1.ASN1InputStream;
12: import org.bouncycastle.asn1.ASN1OutputStream;
13: import org.bouncycastle.asn1.DERIA5String;
14: import org.bouncycastle.asn1.DERObject;
15: import org.bouncycastle.asn1.misc.CAST5CBCParameters;
16: import org.bouncycastle.asn1.misc.IDEACBCPar;
17: import org.bouncycastle.asn1.misc.NetscapeCertType;
18: import org.bouncycastle.asn1.misc.NetscapeRevocationURL;
19: import org.bouncycastle.asn1.misc.VerisignCzagExtension;
20:
21: public class MiscTest implements Test {
22: private boolean isSameAs(byte[] a, byte[] b) {
23: if (a.length != b.length) {
24: return false;
25: }
26:
27: for (int i = 0; i != a.length; i++) {
28: if (a[i] != b[i]) {
29: return false;
30: }
31: }
32:
33: return true;
34: }
35:
36: public TestResult perform() {
37: byte[] testIv = { 1, 2, 3, 4, 5, 6, 7, 8 };
38:
39: ASN1Encodable[] values = {
40: new CAST5CBCParameters(testIv, 128),
41: new NetscapeCertType(NetscapeCertType.smime),
42: new VerisignCzagExtension(new DERIA5String("hello")),
43: new IDEACBCPar(testIv),
44: new NetscapeRevocationURL(new DERIA5String(
45: "http://test")) };
46:
47: byte[] data = Base64
48: .decode("MA4ECAECAwQFBgcIAgIAgAMCBSAWBWhlbGxvMAoECAECAwQFBgcIFgtodHRwOi8vdGVzdA==");
49:
50: try {
51: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
52: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
53:
54: for (int i = 0; i != values.length; i++) {
55: aOut.writeObject(values[i]);
56: }
57:
58: DERObject[] readValues = new DERObject[values.length];
59:
60: if (!isSameAs(bOut.toByteArray(), data)) {
61: return new SimpleTestResult(false, getName()
62: + ": Failed data check");
63: }
64:
65: ByteArrayInputStream bIn = new ByteArrayInputStream(bOut
66: .toByteArray());
67: ASN1InputStream aIn = new ASN1InputStream(bIn);
68:
69: for (int i = 0; i != values.length; i++) {
70: DERObject o = aIn.readObject();
71: if (!values[i].equals(o)) {
72: return new SimpleTestResult(false, getName()
73: + ": Failed equality test for " + o);
74: }
75:
76: if (o.hashCode() != values[i].hashCode()) {
77: return new SimpleTestResult(false, getName()
78: + ": Failed hashCode test for " + o);
79: }
80: }
81:
82: return new SimpleTestResult(true, getName() + ": Okay");
83: } catch (Exception e) {
84: return new SimpleTestResult(false, getName()
85: + ": Failed - exception " + e.toString(), e);
86: }
87: }
88:
89: public String getName() {
90: return "Misc";
91: }
92:
93: public static void main(String[] args) {
94: MiscTest test = new MiscTest();
95: TestResult result = test.perform();
96:
97: System.out.println(result);
98: }
99: }
|