Path: blob/master/test/jdk/java/io/readBytes/ReadBytesBounds.java
41149 views
/*1* Copyright (c) 1997, 2010, 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 4017728 4079849 678819626* @summary Check for correct Array Bounds check in read of FileInputStream and27* RandomAccessFile28*/2930import java.io.*;3132/*33* The test calls the read(byte buf[] , int off , int len) of34* FileInputStream with different values of off and len to see if the35* IndexOutOfBoundsException is thrown. The read(...) method calls36* readBytes(...) in native code(io_util.c). The read(...) method in37* RandomAccessFile also calls the same native method. So one should38* see similar results.39*/4041public class ReadBytesBounds {4243static final FileInputStream fis;44static final RandomAccessFile raf;45static final byte[] b = new byte[32];4647static {48try {49String dir = System.getProperty("test.src", ".");50File testFile = new File(dir, "input.txt");51fis = new FileInputStream(testFile);52raf = new RandomAccessFile(testFile , "r");53} catch (Throwable t) {54throw new Error(t);55}56}5758public static void main(String argv[]) throws Throwable {59try {60testRead(-1, -1, false);61testRead(-1, 0, false);62testRead( 0, -1, false);63testRead( 0, 33, false);64testRead(33, 0, false);65testRead(33, 4, false);66testRead( 0, 32, true);67testRead(32, 0, true);68testRead(32, 4, false);69testRead( 4, 16, true);70testRead( 1, 31, true);71testRead( 0, 0, true);72testRead(31, Integer.MAX_VALUE, false);73testRead( 0, Integer.MAX_VALUE, false);74testRead(-1, Integer.MAX_VALUE, false);75testRead(-4, Integer.MIN_VALUE, false);76testRead( 0, Integer.MIN_VALUE, false);77} finally {78fis.close();79raf.close();80}81}8283static void testRead(int off, int len, boolean expected) throws Throwable {84System.err.printf("off=%d len=%d expected=%b%n", off, len, expected);85boolean result;86try {87fis.read(b, off, len);88raf.read(b, off, len);89result = true;90} catch (IndexOutOfBoundsException e) {91result = false;92}9394if (result != expected) {95throw new RuntimeException96(String.format("Unexpected result off=%d len=%d expected=%b",97off, len, expected));98}99}100}101102103