1 package org.bouncycastle.asn1.test;
2 
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.util.Date;
6 
7 import org.bouncycastle.asn1.ASN1Boolean;
8 import org.bouncycastle.asn1.ASN1Enumerated;
9 import org.bouncycastle.asn1.ASN1InputStream;
10 import org.bouncycastle.asn1.ASN1Integer;
11 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
12 import org.bouncycastle.asn1.ASN1OutputStream;
13 import org.bouncycastle.asn1.ASN1Primitive;
14 import org.bouncycastle.asn1.BEROctetString;
15 import org.bouncycastle.asn1.BERSequence;
16 import org.bouncycastle.asn1.BERSet;
17 import org.bouncycastle.asn1.BERTaggedObject;
18 import org.bouncycastle.asn1.DERApplicationSpecific;
19 import org.bouncycastle.asn1.DERBMPString;
20 import org.bouncycastle.asn1.DERBitString;
21 import org.bouncycastle.asn1.DERGeneralString;
22 import org.bouncycastle.asn1.DERGeneralizedTime;
23 import org.bouncycastle.asn1.DERGraphicString;
24 import org.bouncycastle.asn1.DERIA5String;
25 import org.bouncycastle.asn1.DERNull;
26 import org.bouncycastle.asn1.DERNumericString;
27 import org.bouncycastle.asn1.DEROctetString;
28 import org.bouncycastle.asn1.DERPrintableString;
29 import org.bouncycastle.asn1.DERSequence;
30 import org.bouncycastle.asn1.DERSet;
31 import org.bouncycastle.asn1.DERT61String;
32 import org.bouncycastle.asn1.DERTaggedObject;
33 import org.bouncycastle.asn1.DERUTCTime;
34 import org.bouncycastle.asn1.DERUTF8String;
35 import org.bouncycastle.asn1.DERUniversalString;
36 import org.bouncycastle.asn1.DERVideotexString;
37 import org.bouncycastle.asn1.DERVisibleString;
38 import org.bouncycastle.util.Strings;
39 import org.bouncycastle.util.encoders.Hex;
40 import org.bouncycastle.util.test.SimpleTestResult;
41 import org.bouncycastle.util.test.Test;
42 import org.bouncycastle.util.test.TestResult;
43 
44 public class EqualsAndHashCodeTest
45     implements Test
46 {
perform()47     public TestResult perform()
48     {
49         byte[]    data = { 0, 1, 0, 1, 0, 0, 1 };
50 
51         ASN1Primitive    values[] = {
52                 new BEROctetString(data),
53                 new BERSequence(new DERPrintableString("hello world")),
54                 new BERSet(new DERPrintableString("hello world")),
55                 new BERTaggedObject(0, new DERPrintableString("hello world")),
56                 new DERApplicationSpecific(0, data),
57                 new DERBitString(data),
58                 new DERBMPString("hello world"),
59                 ASN1Boolean.getInstance(true),
60                 ASN1Boolean.getInstance(false),
61                 new ASN1Enumerated(100),
62                 new DERGeneralizedTime("20070315173729Z"),
63                 new DERGeneralString("hello world"),
64                 new DERIA5String("hello"),
65                 new ASN1Integer(1000),
66                 DERNull.INSTANCE,
67                 new DERNumericString("123456"),
68                 new ASN1ObjectIdentifier("1.1.1.10000.1"),
69                 new DEROctetString(data),
70                 new DERPrintableString("hello world"),
71                 new DERSequence(new DERPrintableString("hello world")),
72                 new DERSet(new DERPrintableString("hello world")),
73                 new DERT61String("hello world"),
74                 new DERTaggedObject(0, new DERPrintableString("hello world")),
75                 new DERUniversalString(data),
76                 new DERUTCTime(new Date()),
77                 new DERUTF8String("hello world"),
78                 new DERVisibleString("hello world") ,
79                 new DERGraphicString(Hex.decode("deadbeef")),
80                 new DERVideotexString(Strings.toByteArray("Hello World"))
81             };
82 
83         try
84         {
85             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
86             ASN1OutputStream aOut = ASN1OutputStream.create(bOut);
87 
88             for (int i = 0; i != values.length; i++)
89             {
90                 aOut.writeObject(values[i]);
91             }
92 
93             ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
94             ASN1InputStream aIn = new ASN1InputStream(bIn);
95 
96             for (int i = 0; i != values.length; i++)
97             {
98                 ASN1Primitive o = aIn.readObject();
99                 if (!o.equals(values[i]))
100                 {
101                     return new SimpleTestResult(false, getName() + ": Failed equality test for " + o.getClass());
102                 }
103 
104                 if (o.hashCode() != values[i].hashCode())
105                 {
106                     return new SimpleTestResult(false, getName() + ": Failed hashCode test for " + o.getClass());
107                 }
108             }
109         }
110         catch (Exception e)
111         {
112             return new SimpleTestResult(false, getName() + ": Failed - exception " + e.toString(), e);
113         }
114 
115         return new SimpleTestResult(true, getName() + ": Okay");
116     }
117 
getName()118     public String getName()
119     {
120         return "EqualsAndHashCode";
121     }
122 
main( String[] args)123     public static void main(
124         String[] args)
125     {
126         EqualsAndHashCodeTest    test = new EqualsAndHashCodeTest();
127         TestResult      result = test.perform();
128 
129         System.out.println(result);
130     }
131 }
132