blob: d0671585631600ed8861384d6cfc5f9de58bc74a [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
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/*
martin10847612009-01-07 11:50:32 -080025 * @test
26 * @bug 4017728 4079849 6788196
27 * @summary Check for correct Array Bounds check in read of FileInputStream and
28 * RandomAccessFile
29 */
duke6e45e102007-12-01 00:00:00 +000030
31import java.io.*;
32
duke6e45e102007-12-01 00:00:00 +000033/*
martin10847612009-01-07 11:50:32 -080034 * 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.
duke6e45e102007-12-01 00:00:00 +000040 */
41
duke6e45e102007-12-01 00:00:00 +000042public class ReadBytesBounds {
43
martin10847612009-01-07 11:50:32 -080044 static final FileInputStream fis;
45 static final RandomAccessFile raf;
46 static final byte[] b = new byte[32];
duke6e45e102007-12-01 00:00:00 +000047
martin10847612009-01-07 11:50:32 -080048 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);
duke6e45e102007-12-01 00:00:00 +000056 }
duke6e45e102007-12-01 00:00:00 +000057 }
58
martin10847612009-01-07 11:50:32 -080059 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 }
duke6e45e102007-12-01 00:00:00 +000097}