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.ByteArrayInputStream;
024: import java.io.IOException;
025: import java.math.BigInteger;
026: import java.util.Arrays;
027:
028: import junit.framework.TestCase;
029:
030: import org.apache.harmony.security.asn1.ASN1Constants;
031: import org.apache.harmony.security.asn1.ASN1Exception;
032: import org.apache.harmony.security.asn1.BerInputStream;
033:
034: /**
035: * Tests BerInputStream implementation
036: *
037: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
038: */
039:
040: public class BerInputStreamTest extends TestCase {
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(BerInputStreamTest.class);
044: }
045:
046: /**
047: * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(
048: * java.io.ByteArrayInputStream)
049: */
050: public void test_CtorLjava_io_ByteArrayInputStream()
051: throws IOException {
052:
053: //
054: // tests for decoding initial length of encodings
055: //
056: Object[][] testcase = {
057: // length = 0x01
058: { new byte[] { 0x30, (byte) 0x81, 0x01 },
059: BigInteger.valueOf(1) },
060: // length = 0xFF
061: { new byte[] { 0x30, (byte) 0x81, (byte) 0xFF },
062: BigInteger.valueOf(0xFF) },
063: // length = 0x0101
064: { new byte[] { 0x30, (byte) 0x82, 0x01, 0x01 },
065: BigInteger.valueOf(0x0101) },
066: // length = 0xFFFF
067: {
068: new byte[] { 0x30, (byte) 0x82, (byte) 0xFF,
069: (byte) 0xFF },
070: BigInteger.valueOf(0xFFFF) },
071: // length = 0x0FFFFF
072: {
073: new byte[] { 0x30, (byte) 0x83, 0x0F,
074: (byte) 0xFF, (byte) 0xFF },
075: BigInteger.valueOf(0x0FFFFF) },
076: // length = 0xFFFFFF
077: {
078: new byte[] { 0x30, (byte) 0x83, (byte) 0xFF,
079: (byte) 0xFF, (byte) 0xFF },
080: BigInteger.valueOf(0xFFFFFF) },
081: // length = 0xFFFFFF (encoded length has extra byte)
082: {
083: new byte[] { 0x30, (byte) 0x84, 0x00,
084: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF },
085: BigInteger.valueOf(0xFFFFFF) }, };
086:
087: // positive testcases
088: for (int i = 0; i < testcase.length; i++) {
089: try {
090: BerInputStream in = new BerInputStream(
091: new ByteArrayInputStream(
092: (byte[]) testcase[i][0]));
093:
094: int expected = ((BigInteger) testcase[i][1]).intValue();
095:
096: assertEquals(expected, in.getLength());
097: } catch (IOException e) {
098: e.printStackTrace();
099: fail("Testcase: " + i + "\nUnexpected exception." + e);
100: }
101: }
102:
103: // negative testcase
104: try {
105: new BerInputStream(new ByteArrayInputStream(new byte[] {
106: 0x30, (byte) 0x84, 0x01, 0x01, 0x01, 0x01 }));
107: fail("No expected ASN1Exception");
108: } catch (ASN1Exception e) {
109: assertTrue(e.getMessage().startsWith("Too long"));
110: }
111:
112: //
113: // Test for correct internal array reallocation
114: // Regression for HARMONY-5054
115: //
116:
117: // must be greater then buffer initial size (16K)
118: int arrayLength = 17000;
119:
120: // 1 byte for tag and 3 for length
121: byte[] encoding = new byte[arrayLength + 4];
122:
123: // fill tag and length bytes
124: encoding[0] = ASN1Constants.TAG_OCTETSTRING;
125: encoding[1] = (byte) 0x82; // length is encoded in two bytes
126: encoding[2] = (byte) (arrayLength >> 8);
127: encoding[3] = (byte) (arrayLength & 0xFF);
128:
129: BerInputStream in = new BerInputStream(
130: new ByteArrayInputStream(encoding));
131: assertEquals(encoding.length, in.getBuffer().length);
132: }
133:
134: /**
135: * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(byte[],
136: * int,int)
137: */
138: public void test_Ctor$LbyteLintLint() throws IOException {
139:
140: //
141: // tests for 'expectedLength' parameter
142: //
143: byte[] encoded = new byte[] { 0x01, 0x01, 0x03, // boolean bytes
144: 0x06, 0x02, 0x01, 0x03, // oid bytes
145: 0x01, 0x00 // just random bytes
146: };
147:
148: // pass boolean encoding
149: BerInputStream in = new BerInputStream(encoded, 0, 3);
150: assertEquals("boolean", 1, in.getLength());
151:
152: // pass oid encoding
153: in = new BerInputStream(encoded, 3, 4);
154: assertEquals("boolean", 2, in.getLength());
155:
156: // pass random encoding (equals to ANY)
157: in = new BerInputStream(encoded, 7, 2);
158: assertEquals("any", 0, in.getLength());
159:
160: // extra bytes for oid
161: try {
162: new BerInputStream(encoded, 3, 5);
163: fail("No expected ASN1Exception");
164: } catch (ASN1Exception e) {
165: assertEquals("Wrong content length", e.getMessage());
166: }
167:
168: // less bytes for oid
169: try {
170: new BerInputStream(encoded, 3, 3);
171: fail("No expected ASN1Exception");
172: } catch (ASN1Exception e) {
173: assertEquals("Wrong content length", e.getMessage());
174: }
175: }
176:
177: /**
178: * @tests org.apache.harmony.security.asn1.BerInputStream#readContent()
179: */
180: public void test_readContent() throws IOException {
181:
182: byte[] encoding = { ASN1Constants.TAG_OCTETSTRING, 0x0F, 0x01,
183: 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
184: 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
185:
186: // a custom input stream that doesn't return all data at once
187: ByteArrayInputStream in = new ByteArrayInputStream(encoding) {
188: public int read(byte[] b, int off, int len) {
189: if (len < 2) {
190: return super .read(b, off, len);
191: } else {
192: return super .read(b, off, 4);
193: }
194:
195: }
196: };
197:
198: BerInputStream berIn = new BerInputStream(in);
199: berIn.readContent();
200:
201: assertTrue(Arrays.equals(encoding, berIn.getEncoded()));
202:
203: //
204: // negative test case: the stream returns only 4 bytes of content
205: //
206: in = new ByteArrayInputStream(encoding) {
207:
208: int i = 0;
209:
210: public int read(byte[] b, int off, int len) {
211: if (i == 0) {
212: i++;
213: return super .read(b, off, 4);
214: } else {
215: return 0;
216: }
217:
218: }
219: };
220: berIn = new BerInputStream(in);
221: try {
222: berIn.readContent();
223: fail("No expected ASN1Exception");
224: } catch (ASN1Exception e) {
225: }
226: }
227: }
|