Path: blob/master/test/jdk/javax/imageio/plugins/png/PngNegativeDimensionTest.java
41155 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 819051226* @summary Test verifies whether PNGImageReader throws IIOException with proper27* message or not when IHDR chunk contains negative value for width28* and height.29* @run main PngNegativeDimensionTest30*/3132import java.io.ByteArrayInputStream;33import java.io.InputStream;34import java.util.Base64;35import javax.imageio.ImageIO;3637public class PngNegativeDimensionTest {3839private static String negativeWidthString = "iVBORw0KGgoAAAANSUhEUoAAAAEAA"40+ "AABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";4142private static String negativeHeightString = "iVBORw0KGgoAAAANSUhEUgAAAAGAA"43+ "AABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";4445private static InputStream input;46private static Boolean failed = false;4748public static void main(String[] args) {49// Create InputStream50byte[] inputBytes = Base64.getDecoder().decode(negativeWidthString);51input = new ByteArrayInputStream(inputBytes);52// Attempt to read PNG with negative IHDR width53readNegativeIHDRWidthImage();5455inputBytes = Base64.getDecoder().decode(negativeHeightString);56input = new ByteArrayInputStream(inputBytes);57// Attempt to read PNG with negative IHDR height58readNegativeIHDRHeightImage();5960if (failed) {61throw new RuntimeException("Test didnt throw proper IIOException"62+ " when IHDR width/height is negative");63}64}6566private static void readNegativeIHDRWidthImage() {67try {68ImageIO.read(input);69} catch (Exception e) {70/*71* We expect the test case to throw IIOException with message72* under root cause as "Image width <= 0!". If it throws73* any other message or exception test will fail.74*/75Throwable cause = e.getCause();76if (cause == null ||77(!(cause.getMessage().equals("Image width <= 0!"))))78{79failed = true;80}81}82}8384private static void readNegativeIHDRHeightImage() {85try {86ImageIO.read(input);87} catch (Exception e) {88/*89* We expect the test case to throw IIOException with message90* under root cause as "Image height <= 0!". If it throws91* any other message or exception test will fail.92*/93Throwable cause = e.getCause();94if (cause == null ||95(!(cause.getMessage().equals("Image height <= 0!"))))96{97failed = true;98}99}100}101}102103104105