blob: 341ba63f5ed795a30b855e782c27b471953b5cad [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 1999 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 4221901
27 * @summary Ensure that negative offset or negative len parameter for
28 * write(String str, int off, int len) throws
29 * IndexOutOfBoundsException.
30 */
31
32import java.io.*;
33
34public class BoundsCheck {
35 public static void main(String args[]) throws Exception {
36 ByteArrayOutputStream bos = new ByteArrayOutputStream();
37 OutputStreamWriter osw = new OutputStreamWriter(bos);
38 String data = "Data to be written";
39 char cdata[] = {'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};
40
41 boolean caughtException = false;
42 try {
43 osw.write(data, -3, 5);
44 throw new RuntimeException("Test failed for negative offset");
45 } catch (IndexOutOfBoundsException e){ }
46
47 try {
48 osw.write(data, 3, -5);
49 throw new RuntimeException("Test failed for negative length");
50 } catch (IndexOutOfBoundsException e){ }
51
52 try {
53 osw.write(data, 3, 75);
54 throw new RuntimeException("Test failed for len+off > str.length");
55 } catch (IndexOutOfBoundsException e){ }
56
57 try {
58 osw.write(cdata, -3, 5);
59 throw new RuntimeException("Test failed for negative offset");
60 } catch (IndexOutOfBoundsException e){ }
61
62 try {
63 osw.write(cdata, 3, -5);
64 throw new RuntimeException("Test failed for negative length");
65 } catch (IndexOutOfBoundsException e){ }
66
67 try {
68 osw.write(cdata, 3, 75);
69 throw new RuntimeException("Test failed for len+off > str.length");
70 } catch (IndexOutOfBoundsException e){ }
71 }
72}