BadValue.java 3.8 KB
Newer Older
1
/*
A
andrew 已提交
2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */

/*
 * @test
 * @bug 6864911
 * @summary ASN.1/DER input stream parser needs more work
 */

import java.io.*;
import sun.security.util.*;
import sun.misc.IOUtils;

public class BadValue {

    public static void main(String[] args) throws Exception {

A
andrew 已提交
38
        // Test IOUtils.
39 40 41

        // We have 4 bytes
        InputStream in = new ByteArrayInputStream(new byte[10]);
A
andrew 已提交
42
        byte[] bs = IOUtils.readExactlyNBytes(in, 4);
43 44 45 46
        if (bs.length != 4 || in.available() != 6) {
            throw new Exception("First read error");
        }
        // But only 6 left
A
andrew 已提交
47
        bs = IOUtils.readNBytes(in, 10);
48 49 50
        if (bs.length != 6 || in.available() != 0) {
            throw new Exception("Second read error");
        }
51
        // MAX length results in exception
52
        in = new ByteArrayInputStream(new byte[10]);
53
        try {
A
andrew 已提交
54
            bs = IOUtils.readExactlyNBytes(in, Integer.MAX_VALUE);
55 56 57
            throw new Exception("No exception on MAX_VALUE length");
        } catch (EOFException ex) {
            // this is expected
58
        }
59
        // -1 length results in exception
60
        in = new ByteArrayInputStream(new byte[10]);
61
        try {
A
andrew 已提交
62
            bs = IOUtils.readExactlyNBytes(in, -1);
63 64 65
            throw new Exception("No exception on -1 length");
        } catch (IOException ex) {
            // this is expected
66
        }
67

68 69 70
        // 20>10, readAll means failure
        in = new ByteArrayInputStream(new byte[10]);
        try {
A
andrew 已提交
71
            bs = IOUtils.readExactlyNBytes(in, 20);
72
            throw new Exception("No exception on EOF");
73 74 75 76
        } catch (EOFException e) {
            // OK
        }
        int bignum = 10 * 1024 * 1024;
A
andrew 已提交
77
        bs = IOUtils.readExactlyNBytes(new SuperSlowStream(bignum), bignum);
78
        if (bs.length != bignum) {
79
            throw new Exception("Read returned small array");
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        }

        // Test DerValue
        byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};
        try {
            new DerValue(new ByteArrayInputStream(input));
        } catch (IOException ioe) {
            // This is OK
        }
    }
}

/**
 * An InputStream contains a given number of bytes, but only returns one byte
 * per read.
 */
class SuperSlowStream extends InputStream {
    private int p;
    /**
     * @param Initial capacity
     */
    public SuperSlowStream(int capacity) {
        p = capacity;
    }
    @Override
    public int read() throws IOException {
        if (p > 0) {
            p--;
            return 0;
        } else {
            return -1;
        }
    }
    @Override
    public int read(byte b[], int off, int len) throws IOException {
        if (len == 0) return 0;
        if (p > 0) {
            p--;
            b[off] = 0;
            return 1;
        } else {
            return -1;
        }
    }
}