1 /*
   2  * Copyright (c) 2015, 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     8074954
  27  * @summary Test verifies that an IOException is triggered if input stream
  28  *          does not contain enough data to read a multi-byte type.
  29  *
  30  * @run     main ShortStreamTest
  31  */
  32 
  33 import javax.imageio.ImageIO;
  34 import javax.imageio.stream.ImageInputStream;
  35 import java.io.ByteArrayInputStream;
  36 import java.io.IOException;
  37 
  38 public class ShortStreamTest {
  39     public static void main(String[] args) throws IOException {
  40         TestCase[]  tests = createTests();
  41 
  42         for (TestCase t : tests) {
  43             t.test();
  44         }
  45     }
  46 
  47     private static abstract class TestCase {
  48         abstract void testRead(ImageInputStream iis) throws IOException;
  49 
  50         public void test() {
  51             boolean gotException = false;
  52 
  53             ImageInputStream iis = createShortStream();
  54 
  55             try {
  56                 testRead(iis);
  57             } catch (IOException e) {
  58                 e.printStackTrace(System.out);
  59                 gotException = true;
  60             }
  61 
  62             if (!gotException) {
  63                 throw new RuntimeException("Test failed.");
  64             }
  65             System.out.println("Test PASSED");
  66         }
  67     }
  68 
  69 
  70     private static ImageInputStream createShortStream() {
  71         try {
  72             byte[] integerTestArray = new byte[] { 80 };
  73             ByteArrayInputStream bais = new ByteArrayInputStream(integerTestArray);
  74 
  75             return ImageIO.createImageInputStream(bais);
  76         } catch (IOException e) {
  77             return null;
  78         }
  79     }
  80 
  81     private static TestCase[] createTests() {
  82         return new TestCase[]{
  83                 new TestCase() {
  84                     @Override
  85                     void testRead(ImageInputStream iis) throws IOException {
  86                         iis.readInt();
  87                     }
  88                 },
  89                 new TestCase() {
  90                     @Override
  91                     void testRead(ImageInputStream iis) throws IOException {
  92                         iis.readShort();
  93                     }
  94                 },
  95                 new TestCase() {
  96                     @Override
  97                     void testRead(ImageInputStream iis) throws IOException {
  98                         iis.readDouble();
  99                     }
 100                 },
 101                 new TestCase() {
 102                     @Override
 103                     void testRead(ImageInputStream iis) throws IOException {
 104                         iis.readFloat();
 105                     }
 106                 },
 107                 new TestCase() {
 108                     @Override
 109                     void testRead(ImageInputStream iis) throws IOException {
 110                         iis.readLong();
 111                     }
 112                 },
 113                 new TestCase() {
 114                     @Override
 115                     void testRead(ImageInputStream iis) throws IOException {
 116                         iis.readUnsignedInt();
 117                     }
 118                 },
 119                 new TestCase() {
 120                     @Override
 121                     void testRead(ImageInputStream iis) throws IOException {
 122                         iis.readUnsignedShort();
 123                     }
 124                 }
 125         };
 126     }
 127 }