Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/util/DerValue/BadValue.java
38853 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*/2829import java.io.*;30import sun.security.util.*;31import sun.misc.IOUtils;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 = IOUtils.readNBytes(in, 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}58// -1 length results in exception59in = new ByteArrayInputStream(new byte[10]);60try {61bs = IOUtils.readExactlyNBytes(in, -1);62throw new Exception("No exception on -1 length");63} catch (IOException ex) {64// this is expected65}6667// 20>10, readAll means failure68in = new ByteArrayInputStream(new byte[10]);69try {70bs = IOUtils.readExactlyNBytes(in, 20);71throw new Exception("No exception on EOF");72} catch (EOFException e) {73// OK74}75int bignum = 10 * 1024 * 1024;76bs = IOUtils.readExactlyNBytes(new SuperSlowStream(bignum), bignum);77if (bs.length != bignum) {78throw new Exception("Read returned small array");79}8081// Test DerValue82byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};83try {84new DerValue(new ByteArrayInputStream(input));85} catch (IOException ioe) {86// This is OK87}88}89}9091/**92* An InputStream contains a given number of bytes, but only returns one byte93* per read.94*/95class SuperSlowStream extends InputStream {96private int p;97/**98* @param Initial capacity99*/100public SuperSlowStream(int capacity) {101p = capacity;102}103@Override104public int read() throws IOException {105if (p > 0) {106p--;107return 0;108} else {109return -1;110}111}112@Override113public int read(byte b[], int off, int len) throws IOException {114if (len == 0) return 0;115if (p > 0) {116p--;117b[off] = 0;118return 1;119} else {120return -1;121}122}123}124125126