001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.asn1.der;
022:
023: import java.io.IOException;
024: import java.util.Arrays;
025:
026: import org.apache.harmony.security.asn1.ASN1Exception;
027: import org.apache.harmony.security.asn1.ASN1Oid;
028: import org.apache.harmony.security.asn1.DerInputStream;
029: import org.apache.harmony.security.asn1.DerOutputStream;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * ASN.1 DER test for OID type
035: *
036: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
037: */
038:
039: public class OidTest extends TestCase {
040:
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(OidTest.class);
043: }
044:
045: private static Object[][] oid = {
046: //oid array format: string / int array / DER encoding
047: { "0.0", // as string
048: new int[] { 0, 0 }, // as int array
049: new byte[] { 0x06, 0x01, 0x00 } },
050: //
051: { "0.0.3", // as string
052: new int[] { 0, 0, 3 }, // as int array
053: new byte[] { 0x06, 0x02, 0x00, 0x03 } },
054: //
055: { "0.1.3", // as string
056: new int[] { 0, 1, 3 }, // as int array
057: new byte[] { 0x06, 0x02, 0x01, 0x03 } },
058: //
059: { "0.5", // as string
060: new int[] { 0, 5 }, // as int array
061: new byte[] { 0x06, 0x01, 0x05 } },
062: //
063: { "0.39.3", // as string
064: new int[] { 0, 39, 3 }, // as int array
065: new byte[] { 0x06, 0x02, 0x27, 0x03 } },
066: //
067: { "1.0.3", // as string
068: new int[] { 1, 0, 3 }, // as int array
069: new byte[] { 0x06, 0x02, 0x28, 0x03 } },
070: //
071: { "1.1", // as string
072: new int[] { 1, 1 }, // as int array
073: new byte[] { 0x06, 0x01, 0x29 } },
074: //
075: { "1.2.1.2.1",// as string
076: new int[] { 1, 2, 1, 2, 1 }, // as int array
077: new byte[] { 0x06, 0x04, 0x2A, 0x01, 0x02, 0x01 } },
078: //
079: {
080: "1.2.840.113554.1.2.2",// as string
081: new int[] { 1, 2, 840, 113554, 1, 2, 2 }, // as int array
082: new byte[] { 0x06, 0x09, 0x2A, (byte) 0x86, 0x48,
083: (byte) 0x86, (byte) 0xF7, 0x12, 0x01, 0x02,
084: 0x02 } },
085: //
086: { "1.39.3",// as string
087: new int[] { 1, 39, 3 }, // as int array
088: new byte[] { 0x06, 0x02, 0x4F, 0x03 } },
089: //
090: { "2.0.3",// as string
091: new int[] { 2, 0, 3 }, // as int array
092: new byte[] { 0x06, 0x02, 0x50, 0x03 } },
093: //
094: { "2.5.4.3",// as string
095: new int[] { 2, 5, 4, 3 }, // as int array
096: new byte[] { 0x06, 0x03, 0x55, 0x04, 0x03 } },
097: //
098: { "2.39.3", // as string
099: new int[] { 2, 39, 3 }, // as int array
100: new byte[] { 0x06, 0x02, 0x77, 0x03 } },
101: //
102: { "2.40.3", // as string
103: new int[] { 2, 40, 3 }, // as int array
104: new byte[] { 0x06, 0x02, 0x78, 0x03 } },
105: //
106: { "2.47", // as string
107: new int[] { 2, 47 }, // as int array
108: new byte[] { 0x06, 0x01, 0x7F } },
109: //
110: { "2.48", // as string
111: new int[] { 2, 48 }, // as int array
112: new byte[] { 0x06, 0x02, (byte) 0x81, 0x00 } },
113: //
114: { "2.48.5", // as string
115: new int[] { 2, 48, 5 }, // as int array
116: new byte[] { 0x06, 0x03, (byte) 0x81, 0x00, 0x05 } },
117: //
118: { "2.100.3", // as string
119: new int[] { 2, 100, 3 }, // as int array
120: new byte[] { 0x06, 0x03, (byte) 0x81, 0x34, 0x03 } } };
121:
122: public void test_MappingToIntArray() throws IOException {
123:
124: // oid decoder/encoder for testing
125: ASN1Oid asn1 = ASN1Oid.getInstance();
126:
127: // testing decoding
128: for (int i = 0; i < oid.length; i++) {
129:
130: int[] decoded = (int[]) asn1.decode(new DerInputStream(
131: (byte[]) oid[i][2]));
132:
133: assertTrue("Failed to decode oid: " + oid[i][0], // error message
134: Arrays.equals((int[]) oid[i][1], // expected array
135: decoded));
136: }
137:
138: // testing encoding
139: for (int i = 0; i < oid.length; i++) {
140:
141: byte[] encoded = new DerOutputStream(ASN1Oid.getInstance(),
142: oid[i][1]).encoded;
143:
144: assertTrue("Failed to encode oid: " + oid[i][0], // error message
145: Arrays.equals((byte[]) oid[i][2], // expected encoding
146: encoded));
147: }
148: }
149:
150: public void testDecode_Invalid() throws IOException {
151: byte[][] invalid = new byte[][] {
152: // wrong tag: tag is not 0x06
153: new byte[] { 0x02, 0x01, 0x00 },
154: // wrong length: length is 0
155: new byte[] { 0x06, 0x00 },
156: // wrong content: bit 8 of the last byte is not 0
157: new byte[] { 0x06, 0x02, (byte) 0x81, (byte) 0x80 },
158: // wrong content: is not encoded in fewest number of bytes
159: //FIXME new byte[] { 0x06, 0x02, (byte) 0x80, (byte) 0x01 }
160: };
161:
162: for (int i = 0; i < invalid.length; i++) {
163: try {
164: DerInputStream in = new DerInputStream(invalid[i]);
165: ASN1Oid.getInstance().decode(in);
166: fail("No expected ASN1Exception for:" + i);
167: } catch (ASN1Exception e) {
168: }
169: }
170: }
171:
172: public void test_MappingToString() throws IOException {
173:
174: // oid decoder/encoder for testing
175: ASN1Oid asn1 = ASN1Oid.getInstanceForString();
176:
177: // testing decoding
178: for (int i = 0; i < oid.length; i++) {
179: assertEquals("Failed to decode oid: " + oid[i][0], // error message
180: oid[i][0], // expected string
181: asn1.decode(new DerInputStream((byte[]) oid[i][2])));
182: }
183:
184: // testing encoding
185: for (int i = 0; i < oid.length; i++) {
186: assertTrue(
187: "Failed to encode oid: " + oid[i][0], // error message
188: Arrays
189: .equals(
190: (byte[]) oid[i][2], // expected encoding
191: new DerOutputStream(asn1, oid[i][0]).encoded));
192: }
193: }
194: }
|