ReadBytesBounds.java revision 2497
4680N/A/*
4680N/A * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
4680N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4680N/A *
4680N/A * This code is free software; you can redistribute it and/or modify it
4680N/A * under the terms of the GNU General Public License version 2 only, as
4680N/A * published by the Free Software Foundation.
4680N/A *
4680N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4680N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4680N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4680N/A * version 2 for more details (a copy is included in the LICENSE file that
4680N/A * accompanied this code).
4680N/A *
4680N/A * You should have received a copy of the GNU General Public License version
4680N/A * 2 along with this work; if not, write to the Free Software Foundation,
4680N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4680N/A *
4680N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4680N/A * or visit www.oracle.com if you need additional information or have any
4680N/A * questions.
4680N/A */
4680N/A
4680N/A/*
4680N/A * @test
4680N/A * @bug 4017728 4079849 6788196
4680N/A * @summary Check for correct Array Bounds check in read of FileInputStream and
4680N/A * RandomAccessFile
4680N/A */
4680N/A
4680N/Aimport java.io.*;
4680N/A
4680N/A/*
4680N/A * The test calls the read(byte buf[] , int off , int len) of
4680N/A * FileInputStream with different values of off and len to see if the
4680N/A * IndexOutOfBoundsException is thrown. The read(...) method calls
4680N/A * readBytes(...) in native code(io_util.c). The read(...) method in
4680N/A * RandomAccessFile also calls the same native method. So one should
4680N/A * see similar results.
4680N/A */
4680N/A
4680N/Apublic class ReadBytesBounds {
4680N/A
4680N/A static final FileInputStream fis;
4680N/A static final RandomAccessFile raf;
4680N/A static final byte[] b = new byte[32];
4680N/A
4680N/A static {
4680N/A try {
4680N/A String dir = System.getProperty("test.src", ".");
4680N/A File testFile = new File(dir, "input.txt");
4680N/A fis = new FileInputStream(testFile);
4680N/A raf = new RandomAccessFile(testFile , "r");
4680N/A } catch (Throwable t) {
4680N/A throw new Error(t);
4680N/A }
4680N/A }
4680N/A
4680N/A public static void main(String argv[]) throws Throwable {
4680N/A try {
4680N/A testRead(-1, -1, false);
4680N/A testRead(-1, 0, false);
4680N/A testRead( 0, -1, false);
4680N/A testRead( 0, 33, false);
4680N/A testRead(33, 0, false);
4680N/A testRead(33, 4, false);
4680N/A testRead( 0, 32, true);
4680N/A testRead(32, 0, true);
4680N/A testRead(32, 4, false);
4680N/A testRead( 4, 16, true);
4680N/A testRead( 1, 31, true);
4680N/A testRead( 0, 0, true);
4680N/A testRead(31, Integer.MAX_VALUE, false);
4680N/A testRead( 0, Integer.MAX_VALUE, false);
4680N/A testRead(-1, Integer.MAX_VALUE, false);
4680N/A testRead(-4, Integer.MIN_VALUE, false);
4680N/A testRead( 0, Integer.MIN_VALUE, false);
4680N/A } finally {
4680N/A fis.close();
4680N/A raf.close();
4680N/A }
4680N/A }
4680N/A
4680N/A static void testRead(int off, int len, boolean expected) throws Throwable {
4680N/A System.err.printf("off=%d len=%d expected=%b%n", off, len, expected);
4680N/A boolean result;
4680N/A try {
4680N/A fis.read(b, off, len);
4680N/A raf.read(b, off, len);
4680N/A result = true;
4680N/A } catch (IndexOutOfBoundsException e) {
4680N/A result = false;
4680N/A }
4680N/A
4680N/A if (result != expected) {
4680N/A throw new RuntimeException
4680N/A (String.format("Unexpected result off=%d len=%d expected=%b",
4680N/A off, len, expected));
4680N/A }
4680N/A }
4680N/A}
4680N/A