jdk/test/sun/security/util/DerValue/BadValue.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     4
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     8
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    13
 * accompanied this code).
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    14
 *
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    21
 * questions.
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    22
 */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    23
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    24
/*
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    25
 * @test
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    26
 * @bug 6864911
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    27
 * @summary ASN.1/DER input stream parser needs more work
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    28
 */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    29
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    30
import java.io.*;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    31
import sun.security.util.*;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    32
import sun.misc.IOUtils;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    33
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    34
public class BadValue {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    35
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    37
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    38
        // Test IOUtils.readFully
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    39
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    40
        // We have 4 bytes
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    41
        InputStream in = new ByteArrayInputStream(new byte[10]);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    42
        byte[] bs = IOUtils.readFully(in, 4, true);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    43
        if (bs.length != 4 || in.available() != 6) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    44
            throw new Exception("First read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    45
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    46
        // But only 6 left
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    47
        bs = IOUtils.readFully(in, 10, false);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    48
        if (bs.length != 6 || in.available() != 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    49
            throw new Exception("Second read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    50
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    51
        // MAX read as much as it can
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    52
        in = new ByteArrayInputStream(new byte[10]);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    53
        bs = IOUtils.readFully(in, Integer.MAX_VALUE, true);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    54
        if (bs.length != 10 || in.available() != 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    55
            throw new Exception("Second read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    56
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    57
        // MAX ignore readAll
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    58
        in = new ByteArrayInputStream(new byte[10]);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    59
        bs = IOUtils.readFully(in, Integer.MAX_VALUE, false);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    60
        if (bs.length != 10 || in.available() != 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    61
            throw new Exception("Second read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    62
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    63
        // 20>10, readAll means failure
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    64
        in = new ByteArrayInputStream(new byte[10]);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    65
        try {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    66
            bs = IOUtils.readFully(in, 20, true);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    67
            throw new Exception("Third read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    68
        } catch (EOFException e) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    69
            // OK
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    70
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    71
        int bignum = 10 * 1024 * 1024;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    72
        bs = IOUtils.readFully(new SuperSlowStream(bignum), -1, true);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    73
        if (bs.length != bignum) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    74
            throw new Exception("Fourth read error");
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    75
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    76
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    77
        // Test DerValue
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    78
        byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    79
        try {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    80
            new DerValue(new ByteArrayInputStream(input));
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    81
        } catch (IOException ioe) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    82
            // This is OK
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    83
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    84
    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    85
}
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    86
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    87
/**
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    88
 * An InputStream contains a given number of bytes, but only returns one byte
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    89
 * per read.
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    90
 */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    91
class SuperSlowStream extends InputStream {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    92
    private int p;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    93
    /**
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    94
     * @param Initial capacity
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    95
     */
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    96
    public SuperSlowStream(int capacity) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    97
        p = capacity;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    98
    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
    99
    @Override
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   100
    public int read() throws IOException {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   101
        if (p > 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   102
            p--;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   103
            return 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   104
        } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   105
            return -1;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   106
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   107
    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   108
    @Override
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   109
    public int read(byte b[], int off, int len) throws IOException {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   110
        if (len == 0) return 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   111
        if (p > 0) {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   112
            p--;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   113
            b[off] = 0;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   114
            return 1;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   115
        } else {
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   116
            return -1;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   117
        }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   118
    }
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
diff changeset
   119
}