duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4008296 4008293 4190090 4193729 |
| 27 | * @summary Check for correct handling of parameters to |
| 28 | * XXXXInputStream.read(b, off, len). |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | import java.io.*; |
| 33 | import java.util.zip.ZipInputStream; |
| 34 | import java.util.zip.InflaterInputStream; |
| 35 | import java.util.zip.DeflaterOutputStream; |
| 36 | |
| 37 | public class ReadParams { |
| 38 | |
| 39 | /* check for correct handling of different values of off and len */ |
| 40 | public static void doTest(InputStream in) throws Exception { |
| 41 | |
| 42 | int off[] = {-1, -1, 0, 0, 33, 33, 0, 32, 32, 4, 1, 0, -1, |
| 43 | Integer.MAX_VALUE, 1, Integer.MIN_VALUE, |
| 44 | Integer.MIN_VALUE, 1}; |
| 45 | int len[] = {-1, 0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0, |
| 46 | Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, |
| 47 | 1, -1, Integer.MIN_VALUE}; |
| 48 | boolean results[] = { false, false, false, false, false, false, |
| 49 | true, true, false, true, true, true, false, |
| 50 | false, false, false, false, false}; |
| 51 | int numCases = off.length; |
| 52 | byte b[] = new byte[32]; |
| 53 | int numBad = 0; |
| 54 | |
| 55 | for(int i = 0; i < numCases; i++) { |
| 56 | try { |
| 57 | in.read(b , off[i] , len[i]); |
| 58 | } catch (IndexOutOfBoundsException aiobe) { |
| 59 | if (results[i]) { |
| 60 | System.err.println("Error:IndexOutOfBoundsException thrown"+ |
| 61 | " for read(b, " + off[i] + " " + len[i] + |
| 62 | " ) on " + in + "\nb.length = 32"); |
| 63 | numBad++; |
| 64 | } else { |
| 65 | /* System.err.println("PassE: " + off[i] + " " + len[i]); */ |
| 66 | } |
| 67 | continue; |
| 68 | } catch (OutOfMemoryError ome) { |
| 69 | System.err.println("Error: OutOfMemoryError in read(b, " + |
| 70 | off[i] + " " + len[i] + " ) on " + in + |
| 71 | "\nb.length = 32"); |
| 72 | numBad++; |
| 73 | continue; |
| 74 | } |
| 75 | if (!results[i]) { |
| 76 | System.err.println("Error:No IndexOutOfBoundsException thrown"+ |
| 77 | " for read(b, " + off[i] + " " + len[i] + |
| 78 | " ) on " + in + "\nb.length = 32"); |
| 79 | numBad++; |
| 80 | } else { |
| 81 | /* System.err.println("Pass: " + off[i] + " " + len[i]); */ |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (numBad > 0) { |
| 86 | throw new RuntimeException(in + " Failed " + numBad + " cases"); |
| 87 | } else { |
| 88 | System.err.println("Successfully completed bounds tests on " + in); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* check for correct handling of null b */ |
| 93 | public static void doTest1(InputStream in) throws Exception { |
| 94 | byte b[] = null; |
| 95 | try { |
| 96 | in.read(b, 0, 32); |
| 97 | } catch (NullPointerException npe) { |
| 98 | System.err.println("SuccessFully completed null b test on " + in); |
| 99 | return; |
| 100 | } |
| 101 | throw new RuntimeException(in + " Failed null b test"); |
| 102 | } |
| 103 | |
| 104 | public static void main(String args[]) throws Exception{ |
| 105 | /* initialise stuff */ |
| 106 | File fn = new File("x.ReadBounds"); |
| 107 | FileOutputStream fout = new FileOutputStream(fn); |
| 108 | for (int i = 0; i < 32; i++) { |
| 109 | fout.write(i); |
| 110 | } |
| 111 | fout.close(); |
| 112 | |
| 113 | byte b[] = new byte[64]; |
| 114 | for(int i = 0; i < 64; i++) { |
| 115 | b[i] = 1; |
| 116 | } |
| 117 | |
| 118 | /* test all input streams */ |
| 119 | FileInputStream fis = new FileInputStream(fn); |
| 120 | doTest(fis); |
| 121 | doTest1(fis); |
| 122 | fis.close(); |
| 123 | |
| 124 | BufferedInputStream bis = |
| 125 | new BufferedInputStream(new MyInputStream(1024)); |
| 126 | doTest(bis); |
| 127 | doTest1(bis); |
| 128 | bis.close(); |
| 129 | |
| 130 | ByteArrayInputStream bais = new ByteArrayInputStream(b); |
| 131 | doTest(bais); |
| 132 | doTest1(bais); |
| 133 | bais.close(); |
| 134 | |
| 135 | FileOutputStream fos = new FileOutputStream(fn); |
| 136 | ObjectOutputStream oos = new ObjectOutputStream(fos); |
| 137 | oos.writeInt(12345); |
| 138 | oos.writeObject("Today"); |
| 139 | oos.writeObject(new Integer(32)); |
| 140 | ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn)); |
| 141 | doTest(ois); |
| 142 | doTest1(ois); |
| 143 | ois.close(); |
| 144 | |
| 145 | DataInputStream dis = new DataInputStream(new MyInputStream(1024)); |
| 146 | doTest(dis); |
| 147 | doTest1(dis); |
| 148 | dis.close(); |
| 149 | |
| 150 | LineNumberInputStream lis = |
| 151 | new LineNumberInputStream(new MyInputStream(1024)); |
| 152 | doTest(lis); |
| 153 | doTest1(lis); |
| 154 | lis.close(); |
| 155 | |
| 156 | PipedOutputStream pos = new PipedOutputStream(); |
| 157 | PipedInputStream pis = new PipedInputStream(); |
| 158 | pos.connect(pis); |
| 159 | pos.write(b, 0, 64); |
| 160 | doTest(pis); |
| 161 | doTest1(pis); |
| 162 | pis.close(); |
| 163 | |
| 164 | PushbackInputStream pbis = |
| 165 | new PushbackInputStream(new MyInputStream(1024)); |
| 166 | doTest(pbis); |
| 167 | doTest1(pbis); |
| 168 | pbis.close(); |
| 169 | |
| 170 | StringBufferInputStream sbis = |
| 171 | new StringBufferInputStream(new String(b)); |
| 172 | doTest(sbis); |
| 173 | doTest1(sbis); |
| 174 | sbis.close(); |
| 175 | |
| 176 | SequenceInputStream sis = |
| 177 | new SequenceInputStream(new MyInputStream(1024), |
| 178 | new MyInputStream(1024)); |
| 179 | doTest(sis); |
| 180 | doTest1(sis); |
| 181 | sis.close(); |
| 182 | |
| 183 | ZipInputStream zis = new ZipInputStream(new FileInputStream(fn)); |
| 184 | doTest(zis); |
| 185 | doTest1(zis); |
| 186 | zis.close(); |
| 187 | |
| 188 | byte[] data = new byte[256]; |
| 189 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 190 | DeflaterOutputStream dos = new DeflaterOutputStream(bos); |
| 191 | dos.write(data, 0, data.length); |
| 192 | dos.close(); |
| 193 | InflaterInputStream ifs = new InflaterInputStream |
| 194 | (new ByteArrayInputStream(bos.toByteArray())); |
| 195 | doTest(ifs); |
| 196 | doTest1(ifs); |
| 197 | ifs.close(); |
| 198 | |
| 199 | /* cleanup */ |
| 200 | fn.delete(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* An InputStream class used in the above tests */ |
| 205 | class MyInputStream extends InputStream { |
| 206 | |
| 207 | private int readctr = 0; |
| 208 | private long endoffile; |
| 209 | |
| 210 | public MyInputStream(long endoffile) { |
| 211 | this.endoffile = endoffile; |
| 212 | } |
| 213 | |
| 214 | public int read() { |
| 215 | if (readctr == endoffile) { |
| 216 | return -1; |
| 217 | } |
| 218 | else { |
| 219 | readctr++; |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public int available() { return 0; } |
| 225 | } |