blob: 75b0a31199875bc71920b8255788aa9ba7b42176 [file] [log] [blame]
J. Duke319a3b92007-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/*
25 @test
26 @bug 4017728 4079849
27 @summary Check for correct Array Bounds check in read of FileInputStream and
28 RandomAccessFile
29 */
30
31import java.io.*;
32
33
34/*
35 * The test calls the read(byte buf[] , int off , int len) of FileInputStream with
36 * different values of off and len to see if the ArrayOutOfBoundsException is
37 * thrown according to the JLS1.0 specification. The read(...) method calls
38 * readBytes(...) in native code(io_util.c). The read(...) method in RandomAccessFile
39 * also calls the same native method. So one should see similar results.
40 */
41
42
43public class ReadBytesBounds {
44
45 public static void main(String argv[]) throws Exception{
46
47 int num_test_cases = 12;
48 int off[] = {-1 , -1 , 0 , 0 , 33 , 33 , 0 , 32 , 32 , 4 , 1 , 0};
49 int len[] = {-1 , 0 , -1 , 33 , 0 , 4 , 32 , 0 , 4 , 16 , 31 , 0};
50 boolean results[] = { false , false , false , false , false , false ,
51 true , true , false , true , true , true};
52
53
54 FileInputStream fis = null;
55 RandomAccessFile raf = null;
56 byte b[] = new byte[32];
57
58 int num_good = 0;
59 int num_bad = 0;
60
61 String dir = System.getProperty("test.src", ".");
62 File testFile = new File(dir, "input.txt");
63 fis = new FileInputStream(testFile);
64 for(int i = 0; i < num_test_cases; i++) {
65
66 try {
67 int bytes_read = fis.read(b , off[i] , len[i]);
68 } catch(IndexOutOfBoundsException aiobe) {
69 if (results[i]) {
70 throw new RuntimeException("Unexpected result");
71 }
72 else {
73 num_good++;
74 }
75 continue;
76 }
77
78 if (results[i]) {
79 num_good++;
80 }
81 else {
82 throw new RuntimeException("Unexpected result");
83 }
84
85 }
86 System.out.println("Results for FileInputStream.read");
87 System.out.println("\nTotal number of test cases = " + num_test_cases +
88 "\nNumber succeded = " + num_good +
89 "\nNumber failed = " + num_bad);
90
91
92
93 num_good = 0;
94 num_bad = 0;
95
96 raf = new RandomAccessFile(testFile , "r");
97 for(int i = 0; i < num_test_cases; i++) {
98
99 try {
100 int bytes_read = raf.read(b , off[i] , len[i]);
101 } catch(IndexOutOfBoundsException aiobe) {
102 if (results[i]) {
103 throw new RuntimeException("Unexpected result");
104 }
105 else {
106 num_good++;
107 }
108 continue;
109 }
110
111 if (results[i]) {
112 num_good++;
113 }
114 else {
115 throw new RuntimeException("Unexpected result");
116 }
117
118 }
119
120 System.out.println("Results for RandomAccessFile.read");
121 System.out.println("\nTotal number of test cases = " + num_test_cases +
122 "\nNumber succeded = " + num_good +
123 "\nNumber failed = " + num_bad);
124
125
126 }
127
128}