01: package org.bouncycastle.sasn1.test;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.IOException;
05:
06: import junit.framework.TestCase;
07:
08: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
09: import org.bouncycastle.sasn1.Asn1InputStream;
10: import org.bouncycastle.sasn1.Asn1ObjectIdentifier;
11: import org.bouncycastle.util.encoders.Hex;
12:
13: /**
14: * X.690 test example
15: */
16: public class OIDTest extends TestCase {
17: byte[] req1 = Hex.decode("0603813403");
18: byte[] req2 = Hex.decode("06082A36FFFFFFDD6311");
19:
20: private void recodeCheck(String oid, byte[] enc) throws IOException {
21: ByteArrayInputStream bIn = new ByteArrayInputStream(enc);
22: Asn1InputStream aIn = new Asn1InputStream(bIn);
23:
24: Asn1ObjectIdentifier o = new Asn1ObjectIdentifier(oid);
25: Asn1ObjectIdentifier encO = (Asn1ObjectIdentifier) aIn
26: .readObject();
27:
28: if (!o.equals(encO)) {
29: fail("oid ID didn't match - got: " + o + " expected "
30: + encO);
31: }
32:
33: byte[] bytes = o.getEncoded();
34:
35: if (bytes.length != enc.length) {
36: fail("failed length test");
37: }
38:
39: for (int i = 0; i != enc.length; i++) {
40: if (bytes[i] != enc[i]) {
41: fail("failed comparison test - got: "
42: + new String(Hex.encode(enc)) + " expected "
43: + new String(Hex.encode(bytes)));
44: }
45: }
46: }
47:
48: private void valueCheck(String oid) throws IOException {
49: Asn1ObjectIdentifier o = new Asn1ObjectIdentifier(oid);
50: ByteArrayInputStream bIn = new ByteArrayInputStream(o
51: .getEncoded());
52: Asn1InputStream aIn = new Asn1InputStream(bIn);
53:
54: o = (Asn1ObjectIdentifier) aIn.readObject();
55:
56: if (!o.toString().equals(oid)) {
57: fail("failed oid check for " + oid);
58: }
59: }
60:
61: public void testRecode() throws IOException {
62: recodeCheck("2.100.3", req1);
63: recodeCheck("1.2.54.34359733987.17", req2);
64: }
65:
66: public void testValue() throws IOException {
67: valueCheck(PKCSObjectIdentifiers.pkcs_9_at_contentType.getId());
68: valueCheck("1.1.127.32512.8323072.2130706432.545460846592.139637976727552.35747322042253312.9151314442816847872");
69: valueCheck("1.2.123.12345678901.1.1.1");
70: valueCheck("2.25.196556539987194312349856245628873852187.1");
71: }
72: }
|