1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Stepan M. Mishura 20*/ 21 22package org.apache.harmony.security.tests.asn1.der; 23 24import java.io.IOException; 25import java.util.Arrays; 26 27import org.apache.harmony.security.asn1.ASN1Exception; 28import org.apache.harmony.security.asn1.ASN1Oid; 29import org.apache.harmony.security.asn1.DerInputStream; 30import org.apache.harmony.security.asn1.DerOutputStream; 31 32import junit.framework.TestCase; 33 34 35/** 36 * ASN.1 DER test for OID type 37 * 38 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 39 */ 40 41public class OidTest extends TestCase { 42 43 private static Object[][] oid = { 44 //oid array format: string / int array / DER encoding 45 { "0.0", // as string 46 new int[] { 0, 0 }, // as int array 47 new byte[] { 0x06, 0x01, 0x00 } }, 48 // 49 { "0.0.3", // as string 50 new int[] { 0, 0, 3 }, // as int array 51 new byte[] { 0x06, 0x02, 0x00, 0x03 } }, 52 // 53 { "0.1.3", // as string 54 new int[] { 0, 1, 3 }, // as int array 55 new byte[] { 0x06, 0x02, 0x01, 0x03 } }, 56 // 57 { "0.5", // as string 58 new int[] { 0, 5 }, // as int array 59 new byte[] { 0x06, 0x01, 0x05 } }, 60 // 61 { "0.39.3", // as string 62 new int[] { 0, 39, 3 }, // as int array 63 new byte[] { 0x06, 0x02, 0x27, 0x03 } }, 64 // 65 { "1.0.3", // as string 66 new int[] { 1, 0, 3 }, // as int array 67 new byte[] { 0x06, 0x02, 0x28, 0x03 } }, 68 // 69 { "1.1", // as string 70 new int[] { 1, 1 }, // as int array 71 new byte[] { 0x06, 0x01, 0x29 } }, 72 // 73 { "1.2.1.2.1",// as string 74 new int[] { 1, 2, 1, 2, 1 }, // as int array 75 new byte[] { 0x06, 0x04, 0x2A, 0x01, 0x02, 0x01 } }, 76 // 77 { 78 "1.2.840.113554.1.2.2",// as string 79 new int[] { 1, 2, 840, 113554, 1, 2, 2 }, // as int array 80 new byte[] { 0x06, 0x09, 0x2A, (byte) 0x86, 0x48, 81 (byte) 0x86, (byte) 0xF7, 0x12, 0x01, 0x02, 0x02 } }, 82 // 83 { "1.39.3",// as string 84 new int[] { 1, 39, 3 }, // as int array 85 new byte[] { 0x06, 0x02, 0x4F, 0x03 } }, 86 // 87 { "2.0.3",// as string 88 new int[] { 2, 0, 3 }, // as int array 89 new byte[] { 0x06, 0x02, 0x50, 0x03 } }, 90 // 91 { "2.5.4.3",// as string 92 new int[] { 2, 5, 4, 3 }, // as int array 93 new byte[] { 0x06, 0x03, 0x55, 0x04, 0x03 } }, 94 // 95 { "2.39.3", // as string 96 new int[] { 2, 39, 3 }, // as int array 97 new byte[] { 0x06, 0x02, 0x77, 0x03 } }, 98 // 99 { "2.40.3", // as string 100 new int[] { 2, 40, 3 }, // as int array 101 new byte[] { 0x06, 0x02, 0x78, 0x03 } }, 102 // 103 { "2.47", // as string 104 new int[] { 2, 47 }, // as int array 105 new byte[] { 0x06, 0x01, 0x7F } }, 106 // 107 { "2.48", // as string 108 new int[] { 2, 48 }, // as int array 109 new byte[] { 0x06, 0x02, (byte) 0x81, 0x00 } }, 110 // 111 { "2.48.5", // as string 112 new int[] { 2, 48, 5 }, // as int array 113 new byte[] { 0x06, 0x03, (byte) 0x81, 0x00, 0x05 } }, 114 // 115 { "2.100.3", // as string 116 new int[] { 2, 100, 3 }, // as int array 117 new byte[] { 0x06, 0x03, (byte) 0x81, 0x34, 0x03 } } }; 118 119 public void test_MappingToIntArray() throws IOException { 120 121 // oid decoder/encoder for testing 122 ASN1Oid asn1 = ASN1Oid.getInstance(); 123 124 // testing decoding 125 for (int i = 0; i < oid.length; i++) { 126 127 int[] decoded = (int[]) asn1.decode(new DerInputStream( 128 (byte[]) oid[i][2])); 129 130 assertTrue("Failed to decode oid: " + oid[i][0], // error message 131 Arrays.equals((int[]) oid[i][1], // expected array 132 decoded)); 133 } 134 135 // testing encoding 136 for (int i = 0; i < oid.length; i++) { 137 138 byte[] encoded = new DerOutputStream(ASN1Oid.getInstance(), 139 oid[i][1]).encoded; 140 141 assertTrue("Failed to encode oid: " + oid[i][0], // error message 142 Arrays.equals((byte[]) oid[i][2], // expected encoding 143 encoded)); 144 } 145 } 146 147 public void testDecode_Invalid() throws IOException { 148 byte[][] invalid = new byte[][] { 149 // wrong tag: tag is not 0x06 150 new byte[] { 0x02, 0x01, 0x00 }, 151 // wrong length: length is 0 152 new byte[] { 0x06, 0x00 }, 153 // wrong content: bit 8 of the last byte is not 0 154 new byte[] { 0x06, 0x02, (byte) 0x81, (byte) 0x80 }, 155 // wrong content: is not encoded in fewest number of bytes 156 //FIXME new byte[] { 0x06, 0x02, (byte) 0x80, (byte) 0x01 } 157 }; 158 159 for (int i = 0; i < invalid.length; i++) { 160 try { 161 DerInputStream in = new DerInputStream(invalid[i]); 162 ASN1Oid.getInstance().decode(in); 163 fail("No expected ASN1Exception for:" + i); 164 } catch (ASN1Exception e) { 165 } 166 } 167 } 168 169 public void test_MappingToString() throws IOException { 170 171 // oid decoder/encoder for testing 172 ASN1Oid asn1 = ASN1Oid.getInstanceForString(); 173 174 // testing decoding 175 for (int i = 0; i < oid.length; i++) { 176 assertEquals("Failed to decode oid: " + oid[i][0], // error message 177 oid[i][0], // expected string 178 asn1.decode(new DerInputStream((byte[]) oid[i][2]))); 179 } 180 181 // testing encoding 182 for (int i = 0; i < oid.length; i++) { 183 assertTrue("Failed to encode oid: " + oid[i][0], // error message 184 Arrays.equals((byte[]) oid[i][2], // expected encoding 185 new DerOutputStream(asn1, oid[i][0]).encoded)); 186 } 187 } 188} 189