001: package org.bouncycastle.asn1.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006:
007: import org.bouncycastle.asn1.ASN1InputStream;
008: import org.bouncycastle.asn1.ASN1OutputStream;
009: import org.bouncycastle.asn1.DERObjectIdentifier;
010: import org.bouncycastle.asn1.DEROutputStream;
011: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
012: import org.bouncycastle.util.encoders.Hex;
013: import org.bouncycastle.util.test.SimpleTest;
014:
015: /**
016: * X.690 test example
017: */
018: public class OIDTest extends SimpleTest {
019: byte[] req1 = Hex.decode("0603813403");
020: byte[] req2 = Hex.decode("06082A36FFFFFFDD6311");
021:
022: public String getName() {
023: return "OID";
024: }
025:
026: private void recodeCheck(String oid, byte[] enc) throws IOException {
027: ByteArrayInputStream bIn = new ByteArrayInputStream(enc);
028: ASN1InputStream aIn = new ASN1InputStream(bIn);
029:
030: DERObjectIdentifier o = new DERObjectIdentifier(oid);
031: DERObjectIdentifier encO = (DERObjectIdentifier) aIn
032: .readObject();
033:
034: if (!o.equals(encO)) {
035: fail("oid ID didn't match", o, encO);
036: }
037:
038: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
039: DEROutputStream dOut = new DEROutputStream(bOut);
040:
041: dOut.writeObject(o);
042:
043: byte[] bytes = bOut.toByteArray();
044:
045: if (bytes.length != enc.length) {
046: fail("failed length test");
047: }
048:
049: for (int i = 0; i != enc.length; i++) {
050: if (bytes[i] != enc[i]) {
051: fail("failed comparison test", new String(Hex
052: .encode(enc)), new String(Hex.encode(bytes)));
053: }
054: }
055: }
056:
057: private void validOidCheck(String oid) throws IOException {
058: DERObjectIdentifier o = new DERObjectIdentifier(oid);
059: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
060: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
061:
062: aOut.writeObject(o);
063:
064: ByteArrayInputStream bIn = new ByteArrayInputStream(bOut
065: .toByteArray());
066: ASN1InputStream aIn = new ASN1InputStream(bIn);
067:
068: o = (DERObjectIdentifier) aIn.readObject();
069:
070: if (!o.getId().equals(oid)) {
071: fail("failed oid check for " + oid);
072: }
073: }
074:
075: private void invalidOidCheck(String oid) {
076: try {
077: new DERObjectIdentifier(oid);
078: fail("failed to catch bad oid: " + oid);
079: } catch (IllegalArgumentException e) {
080: // expected
081: }
082: }
083:
084: public void performTest() throws IOException {
085: recodeCheck("2.100.3", req1);
086: recodeCheck("1.2.54.34359733987.17", req2);
087:
088: validOidCheck(PKCSObjectIdentifiers.pkcs_9_at_contentType
089: .getId());
090: validOidCheck("0.1");
091: validOidCheck("1.1.127.32512.8323072.2130706432.545460846592.139637976727552.35747322042253312.9151314442816847872");
092: validOidCheck("1.2.123.12345678901.1.1.1");
093: validOidCheck("2.25.196556539987194312349856245628873852187.1");
094:
095: invalidOidCheck("0");
096: invalidOidCheck("1");
097: invalidOidCheck("2");
098: invalidOidCheck("3.1");
099: invalidOidCheck("..1");
100: invalidOidCheck("192.168.1.1");
101: invalidOidCheck(".123452");
102: invalidOidCheck("1.");
103: invalidOidCheck("1.345.23.34..234");
104: invalidOidCheck("1.345.23.34.234.");
105: invalidOidCheck(".12.345.77.234");
106: invalidOidCheck(".12.345.77.234.");
107: invalidOidCheck("1.2.3.4.A.5");
108: invalidOidCheck("1,2");
109: }
110:
111: public static void main(String[] args) {
112: runTest(new OIDTest());
113: }
114: }
|