blob: e483929cdf0a3ae185cfe738975bd8e303a43d3e [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/*
25 @test
26 @bug 4030253 4030278 4030243
27 @summary Test for correct parameter checking in read(byte[], int, int),
28 readFully(byte[], int, int) and write(byte[], int, int) of RandomAccessFile
29 */
30
31import java.io.*;
32
33public class ParameterCheck {
34
35 static int off[] = {-1, -1, 0, 0, 33, 33, 0, 32,
36 32, 4, 1, 0, -1, Integer.MAX_VALUE, 1};
37 static int len[] = {-1, 0, -1, 33, 0, 4, 32,
38 0, 4, 16, 31, 0, Integer.MAX_VALUE,
39 Integer.MAX_VALUE, Integer.MAX_VALUE};
40 static boolean results[] = { false, false, false, false, false, false,
41 true, true, false, true, true, true, false,
42 false, false };
43 static int numBad = 0;
44
45 private static void doTest(String method) throws Exception {
46 File fn = new File("x.ParameterCheck");
alanb1ece2762010-06-19 15:17:36 +010047 RandomAccessFile raf = null;
duke6e45e102007-12-01 00:00:00 +000048
49 try {
50 byte b[] = new byte[32];
51 int numCases = off.length;
52 int[] got = new int[numCases];
53 int numGood = 0;
54 FileOutputStream fout = new FileOutputStream(fn);
55 for (int i = 0; i < 32; i++) {
56 fout.write(i);
57 }
58 fout.close();
alanb1ece2762010-06-19 15:17:36 +010059 raf = new RandomAccessFile(fn , "rw");
duke6e45e102007-12-01 00:00:00 +000060
61 System.err.println("-----------------------------" +
62 "-----------------------------");
63 System.err.println("\nRandomAccessFile." + method +
64 "\nTotal test cases = " + (off.length+1));
65 System.err.println("-----------------------------" +
66 "-----------------------------");
67 for(int i = 0; i < numCases; i++) {
68 try {
69 if (method.equals("readFully")) {
70 raf.readFully(b , off[i] , len[i]);
71 }
72 if (method.equals("read")) {
73 raf.read(b , off[i] , len[i]);
74 }
75 if (method.equals("write")) {
76 raf.write(b , off[i] , len[i]);
77 }
78 raf.seek(0);
79 } catch(IndexOutOfBoundsException aiobe) {
80 if (results[i]) {
81 printErr(method , numGood,
82 i, "java.lang.IndexOutOfBoundsException");
83 } else {
84 numGood++;
85 }
86 continue;
87 } catch(OutOfMemoryError ome) {
88 printErr(method, numGood,
89 i, "java.lang.OutOfMemoryError");
90 continue;
91 }
92
93 if (results[i]) {
94 numGood++;
95 }
96 else {
97 printErr(method, numGood,
98 i, "No java.lang.IndexOutOfBoundsException");
99 }
100
101 }
102
103 raf.seek(0);
104 boolean thrown = false;
105 try {
106 if (method.equals("readFully")) {
107 raf.readFully(null, 1, 2);
108 }
109 if (method.equals("read")) {
110 raf.read(null, 1, 2);
111 }
112 if (method.equals("write")) {
113 raf.write(null, 1, 2);
114 }
115
116 } catch(NullPointerException npe) {
117 numGood++;
118 thrown = true;
119 }
120 if (!thrown) {
121 printErr(method, numGood, -1,
122 "no NullPointerException for null b");
123 }
124
125 System.err.println("\nTotal passed = " + numGood);
126 System.err.println("-----------------------------" +
127 "-----------------------------");
128 } finally {
alanb1ece2762010-06-19 15:17:36 +0100129 if (raf != null)
130 raf.close();
duke6e45e102007-12-01 00:00:00 +0000131 fn.delete();
132 }
133
134 }
135
136 private static void printErr(String method, int numGood,
137 int i, String expStr) {
138 numBad++;
139 System.err.println("\nNumber passed so far = " + numGood +
140 "\nUnexpected " + expStr);
141 if ( i < 0 ) {
142 System.err.println("for case : b = null");
143 } else {
144 System.err.println("for case : b.length = " + 32 +
145 " off = " + off[i] +
146 " len = " + len[i]);
147 }
148 }
149
150 public static void main(String argv[]) throws Exception{
151 doTest("read");
152 doTest("readFully");
153 doTest("write");
154
155 if (numBad > 0) {
156 throw new RuntimeException("Failed " + numBad + " tests");
157 }
158 }
159}