1 /*
   2  * Copyright (c) 2006, 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 4679743 8148624
  27  * @summary Test parts of DeflaterInputStream code that don't really do I/O.
  28  */
  29 
  30 import java.io.*;
  31 import java.util.zip.*;
  32 
  33 public class ConstructDeflaterInput {
  34 
  35     static class MyDeflater extends Deflater {
  36         private boolean ended = false;
  37         boolean getEnded() { return ended; }
  38         public void end() {
  39             fail("MyDeflater had end() called");
  40             super.end();
  41         }
  42     }
  43 
  44     private static MyDeflater def = new MyDeflater();
  45     public static void realMain(String[] args) throws Throwable {
  46         ByteArrayInputStream bais = new ByteArrayInputStream(
  47             "hello, world".getBytes());
  48         DeflaterInputStream dis = null;
  49         byte[] b = new byte[512];
  50 
  51         // Check construction
  52         //
  53         try {
  54             dis = new DeflaterInputStream(null);
  55             fail();
  56         } catch (NullPointerException ex) {
  57             pass();
  58         }
  59 
  60         try {
  61             dis = new DeflaterInputStream(bais, null);
  62             fail();
  63         } catch (NullPointerException ex) {
  64             pass();
  65         }
  66 
  67         try {
  68             dis = new DeflaterInputStream(bais, def, 0);
  69             fail();
  70         } catch (IllegalArgumentException ex) {
  71             pass();
  72         }
  73 
  74         // Check sanity checks in read methods
  75         //
  76         dis = new DeflaterInputStream(bais, def);
  77 
  78         try {
  79             dis.read(null, 5, 2);
  80             fail();
  81         } catch (NullPointerException ex) {
  82             pass();
  83         }
  84 
  85         try {
  86             dis.read(b, -1, 0);
  87             fail();
  88         } catch (IndexOutOfBoundsException ex) {
  89             pass();
  90         }
  91 
  92         try {
  93             dis.read(b, 0, -1);
  94             fail();
  95         } catch (IndexOutOfBoundsException ex) {
  96             pass();
  97         }
  98 
  99         try {
 100             dis.read(b, 0, 600);
 101             fail();
 102         } catch (IndexOutOfBoundsException ex) {
 103             pass();
 104         }
 105 
 106         int len = 0;
 107         try {
 108             len = dis.read(b, 0, 0);
 109             check(len == 0);
 110         } catch (IndexOutOfBoundsException ex) {
 111             fail("Read of length 0 should return 0, but returned " + len);
 112         }
 113 
 114         try {
 115             dis.skip(-1);
 116             fail();
 117         } catch (IllegalArgumentException ex) {
 118             pass();
 119         }
 120 
 121         // Check unsupported operations
 122         //
 123         check(!dis.markSupported());
 124         check(dis.available() == 1);
 125         check(!def.getEnded());
 126         try {
 127             dis.reset();
 128             fail();
 129         } catch (IOException ex) {
 130             pass();
 131         }
 132 
 133         // Check close
 134         //
 135         dis.close();
 136         check(!def.getEnded());
 137 
 138         try {
 139             dis.available();
 140             fail();
 141         } catch (IOException ex) {
 142             pass();
 143         }
 144 
 145         try {
 146             int x = dis.read();
 147             fail();
 148         } catch (IOException ex) {
 149             pass();
 150         }
 151 
 152         try {
 153             dis.skip(1);
 154             fail();
 155         } catch (IOException ex) {
 156             pass();
 157         }
 158 
 159     }
 160 
 161     //--------------------- Infrastructure ---------------------------
 162     static volatile int passed = 0, failed = 0;
 163     static void pass() {passed++;}
 164     static void fail() {failed++; Thread.dumpStack();}
 165     static void fail(String msg) {System.out.println(msg); fail();}
 166     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 167     static void check(boolean cond) {if (cond) pass(); else fail();}
 168     static void equal(Object x, Object y) {
 169         if (x == null ? y == null : x.equals(y)) pass();
 170         else fail(x + " not equal to " + y);}
 171     public static void main(String[] args) throws Throwable {
 172         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 173         System.out.println("\nPassed = " + passed + " failed = " + failed);
 174         if (failed > 0) throw new AssertionError("Some tests failed");}
 175 }