1 /*
2  * Copyright (c) 1998, 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 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 */
doTest(InputStream in)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 */
doTest1(InputStream in)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 
main(String args[])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         oos.close();
141         ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));
142         doTest(ois);
143         doTest1(ois);
144         ois.close();
145 
146         DataInputStream dis = new DataInputStream(new MyInputStream(1024));
147         doTest(dis);
148         doTest1(dis);
149         dis.close();
150 
151         LineNumberInputStream lis =
152             new LineNumberInputStream(new MyInputStream(1024));
153         doTest(lis);
154         doTest1(lis);
155         lis.close();
156 
157         PipedOutputStream pos = new PipedOutputStream();
158         PipedInputStream pis = new PipedInputStream();
159         pos.connect(pis);
160         pos.write(b, 0, 64);
161         doTest(pis);
162         doTest1(pis);
163         pis.close();
164 
165         PushbackInputStream pbis =
166             new PushbackInputStream(new MyInputStream(1024));
167         doTest(pbis);
168         doTest1(pbis);
169         pbis.close();
170 
171         StringBufferInputStream sbis =
172             new StringBufferInputStream(new String(b));
173         doTest(sbis);
174         doTest1(sbis);
175         sbis.close();
176 
177         SequenceInputStream sis =
178             new SequenceInputStream(new MyInputStream(1024),
179                                     new MyInputStream(1024));
180         doTest(sis);
181         doTest1(sis);
182         sis.close();
183 
184         ZipInputStream zis = new ZipInputStream(new FileInputStream(fn));
185         doTest(zis);
186         doTest1(zis);
187         zis.close();
188 
189         byte[] data = new byte[256];
190         ByteArrayOutputStream bos = new ByteArrayOutputStream();
191         DeflaterOutputStream dos = new DeflaterOutputStream(bos);
192         dos.write(data, 0, data.length);
193         dos.close();
194         InflaterInputStream ifs = new InflaterInputStream
195             (new ByteArrayInputStream(bos.toByteArray()));
196         doTest(ifs);
197         doTest1(ifs);
198         ifs.close();
199 
200         /* cleanup */
201         fn.delete();
202     }
203 }
204 
205 /* An InputStream class used in the above tests */
206 class MyInputStream extends InputStream {
207 
208     private int readctr = 0;
209     private long endoffile;
210 
MyInputStream(long endoffile)211     public MyInputStream(long endoffile) {
212         this.endoffile = endoffile;
213     }
214 
read()215     public int read() {
216         if (readctr == endoffile) {
217             return -1;
218         }
219         else {
220             readctr++;
221             return 0;
222         }
223     }
224 
available()225     public int available() { return 0; }
226 }
227