|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4017728 4079849 6788196 |
|
27 * @summary Check for correct Array Bounds check in read of FileInputStream and |
|
28 * RandomAccessFile |
|
29 */ |
|
30 |
|
31 import java.io.*; |
|
32 |
|
33 /* |
|
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. |
|
40 */ |
|
41 |
|
42 public class ReadBytesBounds { |
|
43 |
|
44 static final FileInputStream fis; |
|
45 static final RandomAccessFile raf; |
|
46 static final byte[] b = new byte[32]; |
|
47 |
|
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); |
|
56 } |
|
57 } |
|
58 |
|
59 public static void main(String argv[]) throws Throwable { |
|
60 try { |
|
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 } finally { |
|
79 fis.close(); |
|
80 raf.close(); |
|
81 } |
|
82 } |
|
83 |
|
84 static void testRead(int off, int len, boolean expected) throws Throwable { |
|
85 System.err.printf("off=%d len=%d expected=%b%n", off, len, expected); |
|
86 boolean result; |
|
87 try { |
|
88 fis.read(b, off, len); |
|
89 raf.read(b, off, len); |
|
90 result = true; |
|
91 } catch (IndexOutOfBoundsException e) { |
|
92 result = false; |
|
93 } |
|
94 |
|
95 if (result != expected) { |
|
96 throw new RuntimeException |
|
97 (String.format("Unexpected result off=%d len=%d expected=%b", |
|
98 off, len, expected)); |
|
99 } |
|
100 } |
|
101 } |