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
  28  *          proper message or not when IHDR chunk contains negative value
  29  *          for width.
  30  * @run     main PngNegativeDimensionTest
  31  */
  32 
  33 import java.io.ByteArrayInputStream;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.util.Base64;
  37 import javax.imageio.ImageIO;
  38 
  39 public class PngNegativeDimensionTest {
  40     private static String inputImageBase64 = "iVBORw0KGgoAAAANSUhEUoAAAAEAAAAB"
  41             + "CAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";
  42 
  43     public static void main(String[] args) throws IOException {
  44         // Create InputStream
  45         byte[] inputBytes = Base64.getDecoder().decode(inputImageBase64);
  46         InputStream input = new ByteArrayInputStream(inputBytes);
  47 
  48         // Attempt to read PNG with negative IHDR width
  49         try {
  50             ImageIO.read(input);
  51         } catch (IOException e) {
  52             /*
  53              * We expect the test case to throw IOException with message
  54              * under root cause as "Image width <= 0!". If it throws
  55              * any other message or exception test will fail.
  56              */
  57             Throwable cause = e.getCause();
  58             if (cause == null ||
  59                 (!(cause.getMessage().equals("Image width <= 0!"))))
  60             {
  61                 throw e;
  62             }
  63         }
  64     }
  65 }