1 /*
   2  * Copyright (c) 1998, 1999, 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 1267039 1267043 4193729
  27  * @summary Check for correct handling of parameters to
  28  *          XXXXOutputStream.write(b, off, len).
  29  *
  30  */
  31 
  32 import java.io.*;
  33 import java.util.zip.DeflaterOutputStream;
  34 
  35 public class WriteParams {
  36 
  37     /* check for correct handling of different values of off and len */
  38     public static void doTest(OutputStream out) throws Exception {
  39 
  40         int off[] = {-1, -1,  0, 0, 33, 33, 0, 32, 32, 4, 1, 0,  -1,
  41                      Integer.MAX_VALUE, 1, Integer.MIN_VALUE,
  42                      Integer.MIN_VALUE, 1};
  43         int len[] = {-1,  0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0,
  44                      Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,
  45                      1, -1, Integer.MIN_VALUE};
  46         boolean results[] = { false,  false,  false, false, false, false,
  47                               true, true, false, true, true, true,  false,
  48                               false, false, false, false, false};
  49         int numCases = off.length;
  50         byte b[] = new byte[32];
  51         int numBad = 0;
  52 
  53         for(int i = 0; i < numCases; i++) {
  54             try {
  55                 out.write(b , off[i] , len[i]);
  56             } catch (IndexOutOfBoundsException aiobe) {
  57                 if (results[i]) {
  58                     System.err.println("Error:IndexOutOfBoundsException thrown"+
  59                                        " for write(b, " + off[i] + " " + len[i] +
  60                                        " ) on " + out + "\nb.length = 32");
  61                     numBad++;
  62                 } else {
  63                     /* System.err.println("PassE: " + off[i] + " " + len[i]); */
  64                 }
  65                 continue;
  66             } catch (OutOfMemoryError ome) {
  67                 System.err.println("Error: OutOfMemoryError in write(b, " +
  68                                    off[i] + " " + len[i] + " ) on " + out +
  69                                    "\nb.length = 32");
  70                 numBad++;
  71                 continue;
  72             }
  73             if (!results[i]) {
  74                 System.err.println("Error:No IndexOutOfBoundsException thrown"+
  75                                    " for write(b, " + off[i] + " " + len[i] +
  76                                    " ) on " + out + "\nb.length = 32");
  77                 numBad++;
  78             } else {
  79                 /* System.err.println("Pass: " + off[i] + " " + len[i]); */
  80             }
  81         }
  82 
  83         if (numBad > 0) {
  84             throw new RuntimeException(out + " Failed " + numBad + " cases");
  85         } else {
  86             System.err.println("Successfully completed bounds tests on " + out);
  87         }
  88     }
  89 
  90     /* check for correct handling of null b */
  91     public static void doTest1(OutputStream out) throws Exception {
  92         byte b[] = null;
  93         try {
  94             out.write(b, 0, 32);
  95         } catch (NullPointerException npe) {
  96             System.err.println("SuccessFully completed null b test on " + out);
  97             return;
  98         }
  99         throw new RuntimeException(out + " Failed null b test");
 100     }
 101 
 102     public static void main(String args[]) throws Exception{
 103         /* initialise stuff here */
 104         File fn = new File("x.WriteBounds");
 105         FileOutputStream fout = new FileOutputStream(fn);
 106         for (int i = 0; i < 32; i++) {
 107             fout.write(i);
 108         }
 109         fout.close();
 110 
 111         byte b[] = new byte[64];
 112         for(int i = 0; i < 64; i++) {
 113             b[i] = 1;
 114         }
 115 
 116         /* test for different output streams */
 117         FileOutputStream fos = new FileOutputStream(fn);
 118         doTest(fos);
 119         doTest1(fos);
 120         fos.close();
 121 
 122         ObjectOutputStream oos = new ObjectOutputStream(new MyOutputStream());
 123         doTest(oos);
 124         doTest1(oos);
 125         oos.close();
 126 
 127         BufferedOutputStream bos =
 128             new BufferedOutputStream(new MyOutputStream());
 129         doTest(bos);
 130         doTest1(bos);
 131         bos.close();
 132 
 133         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 134         doTest(baos);
 135         doTest1(baos);
 136         baos.close();
 137 
 138         DataOutputStream dos = new DataOutputStream(new MyOutputStream());
 139         doTest(dos);
 140         doTest1(dos);
 141         dos.close();
 142 
 143         PipedInputStream pis = new PipedInputStream();
 144         PipedOutputStream pos = new PipedOutputStream();
 145         pos.connect(pis);
 146         doTest(pos);
 147         doTest1(pos);
 148         pos.close();
 149 
 150         DeflaterOutputStream dfos = new DeflaterOutputStream(new MyOutputStream());
 151         doTest(dfos);
 152         doTest1(dfos);
 153         dfos.close();
 154 
 155         /* cleanup */
 156         fn.delete();
 157 
 158     }
 159 }
 160 
 161 /* An OutputStream class used in the above tests */
 162 class MyOutputStream extends OutputStream {
 163 
 164     public MyOutputStream() {
 165     }
 166 
 167     public void write(int b) throws IOException {
 168     }
 169 }