Path: blob/master/test/jdk/sun/security/util/DerValue/BadValue.java
41152 views
/*1* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 686491126* @summary ASN.1/DER input stream parser needs more work27* @modules java.base/sun.security.util28*/2930import java.io.*;31import sun.security.util.*;3233public class BadValue {3435public static void main(String[] args) throws Exception {3637// Test IOUtils.3839// We have 4 bytes40InputStream in = new ByteArrayInputStream(new byte[10]);41byte[] bs = IOUtils.readExactlyNBytes(in, 4);42if (bs.length != 4 || in.available() != 6) {43throw new Exception("First read error");44}45// But only 6 left46bs = in.readNBytes(10);47if (bs.length != 6 || in.available() != 0) {48throw new Exception("Second read error");49}50// MAX length results in exception51in = new ByteArrayInputStream(new byte[10]);52try {53bs = IOUtils.readExactlyNBytes(in, Integer.MAX_VALUE);54throw new Exception("No exception on MAX_VALUE length");55} catch (EOFException ex) {56// this is expected57} catch (IOException ex) {58throw ex;59}60// -1 length results in exception61in = new ByteArrayInputStream(new byte[10]);62try {63bs = IOUtils.readExactlyNBytes(in, -1);64throw new Exception("No exception on -1 length");65} catch (IOException ex) {66// this is expected67}6869// 20>10, readAll means failure70in = new ByteArrayInputStream(new byte[10]);71try {72bs = IOUtils.readExactlyNBytes(in, 20);73throw new Exception("No exception on EOF");74} catch (EOFException e) {75// OK76}77int bignum = 10 * 1024 * 1024;78bs = IOUtils.readExactlyNBytes(new SuperSlowStream(bignum), bignum);79if (bs.length != bignum) {80throw new Exception("Read returned small array");81}8283// Test DerValue84byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};85try {86new DerValue(new ByteArrayInputStream(input));87} catch (IOException ioe) {88// This is OK89}90}91}9293/**94* An InputStream contains a given number of bytes, but only returns one byte95* per read.96*/97class SuperSlowStream extends InputStream {98private int p;99/**100* @param Initial capacity101*/102public SuperSlowStream(int capacity) {103p = capacity;104}105@Override106public int read() throws IOException {107if (p > 0) {108p--;109return 0;110} else {111return -1;112}113}114@Override115public int read(byte b[], int off, int len) throws IOException {116if (len == 0) return 0;117if (p > 0) {118p--;119b[off] = 0;120return 1;121} else {122return -1;123}124}125}126127128