Path: blob/master/test/jdk/javax/imageio/plugins/png/PngImproperChunkSizeTest.java
41155 views
/*1* Copyright (c) 2018, 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 819102326* @summary Test verifies that PNGImageReader doesn't throw any undocumented27* Exception when we try to create byte array of negative size because28* when keyword length is more than chunk size.29* @run main PngImproperChunkSizeTest30*/3132import java.io.ByteArrayInputStream;33import java.io.IOException;34import java.io.InputStream;35import java.util.Base64;36import javax.imageio.IIOException;37import javax.imageio.ImageIO;38import javax.imageio.ImageReader;3940public class PngImproperChunkSizeTest {4142private static ImageReader reader;4344private static String zTXTMalformedData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAA" +45"ABCAAAAAA6fptVAAAABHpUWHRhYWFhYWFhYQAAAApJREFUGFdj+A8AAQEBAFpNb" +46"/EAAAAASUVORK5CYIIK";4748private static String tEXTMalformedData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"49+ "CAMAAAA6fptVAAAABHRFWHRhYWFhYWFhYQAAAApJREFUGFdj+A8AAQEBAFpNb"50+ "/EAAAAASUVORK5CYIIK";5152private static String iCCPMalformedData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAA" +53"ABCAAAAAA6fptVAAAABGlDQ1BhYWFhYWFhYQAAAApJREFUGFdj+A8AAQEBAFpNb" +54"/EAAAAASUVORK5CYIIK";5556private static ByteArrayInputStream initializeInputStream(String input) {57byte[] inputBytes = Base64.getDecoder().decode(input);58return new ByteArrayInputStream(inputBytes);59}6061private static Boolean readzTXTData(InputStream input) throws IOException {62// Set input and mark ignoreMetadata = false63reader.setInput(ImageIO.createImageInputStream(input), true, false);64try {65reader.read(0);66} catch (IIOException e) {67Throwable cause = e.getCause();68if (cause == null ||69!cause.getMessage().70equals("zTXt chunk length is not proper"))71{72return true;73}74}75return false;76}7778private static Boolean readtEXTData(InputStream input) throws IOException {79// Set input and mark ignoreMetadata = false80reader.setInput(ImageIO.createImageInputStream(input), true, false);81try {82reader.read(0);83} catch (IIOException e) {84Throwable cause = e.getCause();85if (cause == null ||86!cause.getMessage().87equals("tEXt chunk length is not proper"))88{89return true;90}91}92return false;93}9495private static Boolean readiCCPData(InputStream input) throws IOException {96// Set input and mark ignoreMetadata = false97reader.setInput(ImageIO.createImageInputStream(input), true, false);98try {99reader.read(0);100} catch (IIOException e) {101Throwable cause = e.getCause();102if (cause == null ||103!cause.getMessage().104equals("iCCP chunk length is not proper"))105{106return true;107}108}109return false;110}111112public static void main(String[] args) throws java.io.IOException {113reader = ImageIO.getImageReadersByFormatName("png").next();114115InputStream in = initializeInputStream(zTXTMalformedData);116Boolean zTXTFailed = readzTXTData(in);117118in = initializeInputStream(tEXTMalformedData);119Boolean tEXTFailed = readtEXTData(in);120121in = initializeInputStream(iCCPMalformedData);122Boolean iCCPFailed = readiCCPData(in);123124reader.dispose();125126if (zTXTFailed || tEXTFailed || iCCPFailed) {127throw new RuntimeException("Test didn't throw the required" +128" Exception");129}130}131}132133134135