01: package org.bouncycastle.asn1.test;
02:
03: import java.io.IOException;
04:
05: import org.bouncycastle.asn1.ASN1InputStream;
06: import org.bouncycastle.util.test.SimpleTest;
07:
08: public class InputStreamTest extends SimpleTest {
09: private static final byte[] outOfBoundsLength = new byte[] {
10: (byte) 0x30, (byte) 0xff, (byte) 0xff, (byte) 0xff,
11: (byte) 0xff, (byte) 0xff };
12: private static final byte[] negativeLength = new byte[] {
13: (byte) 0x30, (byte) 0x84, (byte) 0xff, (byte) 0xff,
14: (byte) 0xff, (byte) 0xff };
15: private static final byte[] outsideLimitLength = new byte[] {
16: (byte) 0x30, (byte) 0x83, (byte) 0x0f, (byte) 0xff,
17: (byte) 0xff };
18:
19: public String getName() {
20: return "InputStream";
21: }
22:
23: public void performTest() throws Exception {
24: ASN1InputStream aIn = new ASN1InputStream(outOfBoundsLength);
25:
26: try {
27: aIn.readObject();
28: fail("out of bounds length not detected.");
29: } catch (IOException e) {
30: if (!e.getMessage().equals("DER length more than 4 bytes")) {
31: fail("wrong exception: " + e.getMessage());
32: }
33: }
34:
35: aIn = new ASN1InputStream(negativeLength);
36:
37: try {
38: aIn.readObject();
39: fail("negative length not detected.");
40: } catch (IOException e) {
41: if (!e.getMessage().equals(
42: "corrupted steam - negative length found")) {
43: fail("wrong exception: " + e.getMessage());
44: }
45: }
46:
47: aIn = new ASN1InputStream(outsideLimitLength);
48:
49: try {
50: aIn.readObject();
51: fail("outside limit length not detected.");
52: } catch (IOException e) {
53: if (!e.getMessage().equals(
54: "corrupted steam - out of bounds length found")) {
55: fail("wrong exception: " + e.getMessage());
56: }
57: }
58: }
59:
60: public static void main(String[] args) {
61: runTest(new InputStreamTest());
62: }
63: }
|