1 /*
   2  * Copyright (c) 2017, 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     8190512
  27  * @summary Test verifies whether PNGImageReader throws IIOException with proper
  28  *          message or not when IHDR chunk contains negative value for width
  29  *          and height.
  30  * @run     main PngNegativeDimensionTest
  31  */
  32 import java.io.ByteArrayInputStream;
  33 import java.io.InputStream;
  34 import java.util.Base64;
  35 import javax.imageio.ImageIO;
  36 
  37 public class PngNegativeDimensionTest {
  38 
  39     private static String negativeWidthString = "iVBORw0KGgoAAAANSUhEUoAAAAEAA"
  40             + "AABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";
  41 
  42     private static String negativeHeightString = "iVBORw0KGgoAAAANSUhEUgAAAAGAA"
  43             + "AABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";
  44 
  45     private static InputStream input;
  46     private static Boolean failed = false;
  47 
  48     public static void main(String[] args) {
  49         // Create InputStream
  50         byte[] inputBytes = Base64.getDecoder().decode(negativeWidthString);
  51         input = new ByteArrayInputStream(inputBytes);
  52         // Attempt to read PNG with negative IHDR width
  53         readNegativeIHDRWidthImage();
  54 
  55         inputBytes = Base64.getDecoder().decode(negativeHeightString);
  56         input = new ByteArrayInputStream(inputBytes);
  57         // Attempt to read PNG with negative IHDR height
  58         readNegativeIHDRHeightImage();
  59 
  60         if (failed) {
  61             throw new RuntimeException("Test didnt throw proper IIOException"
  62                     + " when IHDR width/height is negative");
  63         }
  64     }
  65 
  66     private static void readNegativeIHDRWidthImage() {
  67         try {
  68             ImageIO.read(input);
  69         } catch (Exception e) {
  70             /*
  71              * We expect the test case to throw IIOException with message
  72              * under root cause as "Image width <= 0!". If it throws
  73              * any other message or exception test will fail.
  74              */
  75             Throwable cause = e.getCause();
  76             if (cause == null ||
  77                 (!(cause.getMessage().equals("Image width <= 0!"))))
  78             {
  79                 failed = true;
  80             }
  81         }
  82     }
  83 
  84     private static void readNegativeIHDRHeightImage() {
  85         try {
  86             ImageIO.read(input);
  87         } catch (Exception e) {
  88             /*
  89              * We expect the test case to throw IIOException with message
  90              * under root cause as "Image height <= 0!". If it throws
  91              * any other message or exception test will fail.
  92              */
  93             Throwable cause = e.getCause();
  94             if (cause == null ||
  95                 (!(cause.getMessage().equals("Image height <= 0!"))))
  96             {
  97                 failed = true;
  98             }
  99         }
 100     }
 101 }
 102