duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
martin | 1084761 | 2009-01-07 11:50:32 -0800 | [diff] [blame] | 25 | * @test |
| 26 | * @bug 4017728 4079849 6788196 |
| 27 | * @summary Check for correct Array Bounds check in read of FileInputStream and |
| 28 | * RandomAccessFile |
| 29 | */ |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 30 | |
| 31 | import java.io.*; |
| 32 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 33 | /* |
martin | 1084761 | 2009-01-07 11:50:32 -0800 | [diff] [blame] | 34 | * The test calls the read(byte buf[] , int off , int len) of |
| 35 | * FileInputStream with different values of off and len to see if the |
| 36 | * IndexOutOfBoundsException is thrown. The read(...) method calls |
| 37 | * readBytes(...) in native code(io_util.c). The read(...) method in |
| 38 | * RandomAccessFile also calls the same native method. So one should |
| 39 | * see similar results. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 40 | */ |
| 41 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 42 | public class ReadBytesBounds { |
| 43 | |
martin | 1084761 | 2009-01-07 11:50:32 -0800 | [diff] [blame] | 44 | static final FileInputStream fis; |
| 45 | static final RandomAccessFile raf; |
| 46 | static final byte[] b = new byte[32]; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 47 | |
martin | 1084761 | 2009-01-07 11:50:32 -0800 | [diff] [blame] | 48 | static { |
| 49 | try { |
| 50 | String dir = System.getProperty("test.src", "."); |
| 51 | File testFile = new File(dir, "input.txt"); |
| 52 | fis = new FileInputStream(testFile); |
| 53 | raf = new RandomAccessFile(testFile , "r"); |
| 54 | } catch (Throwable t) { |
| 55 | throw new Error(t); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 56 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 57 | } |
| 58 | |
martin | 1084761 | 2009-01-07 11:50:32 -0800 | [diff] [blame] | 59 | public static void main(String argv[]) throws Throwable { |
| 60 | byte b[] = new byte[32]; |
| 61 | testRead(-1, -1, false); |
| 62 | testRead(-1, 0, false); |
| 63 | testRead( 0, -1, false); |
| 64 | testRead( 0, 33, false); |
| 65 | testRead(33, 0, false); |
| 66 | testRead(33, 4, false); |
| 67 | testRead( 0, 32, true); |
| 68 | testRead(32, 0, true); |
| 69 | testRead(32, 4, false); |
| 70 | testRead( 4, 16, true); |
| 71 | testRead( 1, 31, true); |
| 72 | testRead( 0, 0, true); |
| 73 | testRead(31, Integer.MAX_VALUE, false); |
| 74 | testRead( 0, Integer.MAX_VALUE, false); |
| 75 | testRead(-1, Integer.MAX_VALUE, false); |
| 76 | testRead(-4, Integer.MIN_VALUE, false); |
| 77 | testRead( 0, Integer.MIN_VALUE, false); |
| 78 | } |
| 79 | |
| 80 | static void testRead(int off, int len, boolean expected) throws Throwable { |
| 81 | System.err.printf("off=%d len=%d expected=%b%n", off, len, expected); |
| 82 | boolean result; |
| 83 | try { |
| 84 | fis.read(b, off, len); |
| 85 | raf.read(b, off, len); |
| 86 | result = true; |
| 87 | } catch (IndexOutOfBoundsException e) { |
| 88 | result = false; |
| 89 | } |
| 90 | |
| 91 | if (result != expected) { |
| 92 | throw new RuntimeException |
| 93 | (String.format("Unexpected result off=%d len=%d expected=%b", |
| 94 | off, len, expected)); |
| 95 | } |
| 96 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 97 | } |