001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1OutputStream;
005: import org.bouncycastle.asn1.BERConstructedOctetString;
006: import org.bouncycastle.asn1.BERSequence;
007: import org.bouncycastle.asn1.BERSet;
008: import org.bouncycastle.asn1.BERTaggedObject;
009: import org.bouncycastle.asn1.DERApplicationSpecific;
010: import org.bouncycastle.asn1.DERBMPString;
011: import org.bouncycastle.asn1.DERBitString;
012: import org.bouncycastle.asn1.DERBoolean;
013: import org.bouncycastle.asn1.DEREnumerated;
014: import org.bouncycastle.asn1.DERGeneralString;
015: import org.bouncycastle.asn1.DERGeneralizedTime;
016: import org.bouncycastle.asn1.DERIA5String;
017: import org.bouncycastle.asn1.DERInteger;
018: import org.bouncycastle.asn1.DERNull;
019: import org.bouncycastle.asn1.DERNumericString;
020: import org.bouncycastle.asn1.DERObject;
021: import org.bouncycastle.asn1.DERObjectIdentifier;
022: import org.bouncycastle.asn1.DEROctetString;
023: import org.bouncycastle.asn1.DERPrintableString;
024: import org.bouncycastle.asn1.DERSequence;
025: import org.bouncycastle.asn1.DERSet;
026: import org.bouncycastle.asn1.DERT61String;
027: import org.bouncycastle.asn1.DERTaggedObject;
028: import org.bouncycastle.asn1.DERTags;
029: import org.bouncycastle.asn1.DERUTCTime;
030: import org.bouncycastle.asn1.DERUTF8String;
031: import org.bouncycastle.asn1.DERUniversalString;
032: import org.bouncycastle.asn1.DERUnknownTag;
033: import org.bouncycastle.asn1.DERVisibleString;
034: import org.bouncycastle.util.test.SimpleTestResult;
035: import org.bouncycastle.util.test.Test;
036: import org.bouncycastle.util.test.TestResult;
037:
038: import java.io.ByteArrayInputStream;
039: import java.io.ByteArrayOutputStream;
040: import java.util.Date;
041:
042: public class EqualsAndHashCodeTest implements Test {
043: public TestResult perform() {
044: byte[] data = { 0, 1, 0, 1, 0, 0, 1 };
045:
046: DERObject values[] = {
047: new BERConstructedOctetString(data),
048: new BERSequence(new DERPrintableString("hello world")),
049: new BERSet(new DERPrintableString("hello world")),
050: new BERTaggedObject(0, new DERPrintableString(
051: "hello world")),
052: new DERApplicationSpecific(0, data),
053: new DERBitString(data),
054: new DERBMPString("hello world"),
055: new DERBoolean(true),
056: new DERBoolean(false),
057: new DEREnumerated(100),
058: new DERGeneralizedTime("20070315173729Z"),
059: new DERGeneralString("hello world"),
060: new DERIA5String("hello"),
061: new DERInteger(1000),
062: new DERNull(),
063: new DERNumericString("123456"),
064: new DERObjectIdentifier("1.1.1.10000.1"),
065: new DEROctetString(data),
066: new DERPrintableString("hello world"),
067: new DERSequence(new DERPrintableString("hello world")),
068: new DERSet(new DERPrintableString("hello world")),
069: new DERT61String("hello world"),
070: new DERTaggedObject(0, new DERPrintableString(
071: "hello world")),
072: new DERUniversalString(data),
073: new DERUnknownTag(
074: 0xff & (~(DERTags.TAGGED | DERTags.APPLICATION)),
075: data), new DERUTCTime(new Date()),
076: new DERUTF8String("hello world"),
077: new DERVisibleString("hello world") };
078:
079: try {
080: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
081: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
082:
083: for (int i = 0; i != values.length; i++) {
084: aOut.writeObject(values[i]);
085: }
086:
087: DERObject[] readValues = new DERObject[values.length];
088:
089: ByteArrayInputStream bIn = new ByteArrayInputStream(bOut
090: .toByteArray());
091: ASN1InputStream aIn = new ASN1InputStream(bIn);
092:
093: for (int i = 0; i != values.length; i++) {
094: DERObject o = aIn.readObject();
095: if (!o.equals(values[i])) {
096: return new SimpleTestResult(false, getName()
097: + ": Failed equality test for " + o);
098: }
099:
100: if (o.hashCode() != values[i].hashCode()) {
101: return new SimpleTestResult(false, getName()
102: + ": Failed hashCode test for " + o);
103: }
104: }
105: } catch (Exception e) {
106: return new SimpleTestResult(false, getName()
107: + ": Failed - exception " + e.toString(), e);
108: }
109:
110: return new SimpleTestResult(true, getName() + ": Okay");
111: }
112:
113: public String getName() {
114: return "EqualsAndHashCode";
115: }
116:
117: public static void main(String[] args) {
118: EqualsAndHashCodeTest test = new EqualsAndHashCodeTest();
119: TestResult result = test.perform();
120:
121: System.out.println(result);
122: }
123: }
|