Path: blob/master/test/jdk/javax/imageio/plugins/png/ReadMalformedPngTest.java
41155 views
/*1* Copyright (c) 2014, 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 694517426* @summary Test verifies that PNG image readr throw correct exception27* if image contains a chunk with incorrect length.28* @run main ReadMalformedPngTest29*/3031import java.awt.Color;32import java.awt.GradientPaint;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.ByteArrayInputStream;36import java.io.ByteArrayOutputStream;37import java.io.IOException;38import javax.imageio.IIOException;39import javax.imageio.IIOImage;40import javax.imageio.ImageIO;41import javax.imageio.ImageTypeSpecifier;42import javax.imageio.ImageWriteParam;43import javax.imageio.ImageWriter;44import javax.imageio.metadata.IIOMetadata;45import javax.imageio.metadata.IIOMetadataNode;46import javax.imageio.stream.ImageOutputStream;47import org.w3c.dom.Node;4849public class ReadMalformedPngTest {5051public static void main(String[] args) throws IOException {52ByteArrayInputStream bais = new ByteArrayInputStream(createTestPng());5354IIOException expected = null;55try {56ImageIO.read(bais);57} catch (IIOException e) {58expected = e;59} catch (Throwable e) {60throw new RuntimeException("Test failed!", e);61}6263if (expected == null) {64throw new RuntimeException("Test failed.");65}6667System.out.println("Test passed.");68}6970private static byte[] createTestPng() throws IOException {71ByteArrayOutputStream baos = new ByteArrayOutputStream();7273BufferedImage img = createTestImage();7475try {76ImageOutputStream ios = ImageIO.createImageOutputStream(baos);7778ImageWriter w = ImageIO.getImageWritersByFormatName("PNG").next();7980w.setOutput(ios);8182ImageWriteParam p = w.getDefaultWriteParam();8384ImageTypeSpecifier t = ImageTypeSpecifier.createFromRenderedImage(img);8586IIOMetadata m = w.getDefaultImageMetadata(t, p);8788String nativeMetadataFormat = m.getNativeMetadataFormatName();8990Node root = m.getAsTree(nativeMetadataFormat);9192IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");93textEntry.setAttribute("keyword", "comment");94textEntry.setAttribute("value", "This is a test image for JDK-6945174");9596IIOMetadataNode text = new IIOMetadataNode("tEXt");97text.appendChild(textEntry);9899root.appendChild(text);100101m.mergeTree(nativeMetadataFormat, root);102103IIOImage iio_img = new IIOImage(img, null, m);104105w.write(iio_img);106107w.dispose();108ios.flush();109ios.close();110} catch (IOException e) {111throw new RuntimeException("Test failed.", e);112}113114baos.flush();115116byte[] data = baos.toByteArray();117118adjustCommentLength(Integer.MAX_VALUE + 0x1000, data);119120return data;121}122123private static void adjustCommentLength(int v, byte[] data) {124final int pos = getCommentPos(data);125data[pos + 3] = (byte) (v & 0xFF);126v = v >> 8;127data[pos + 2] = (byte) (v & 0xFF);128v = v >> 8;129data[pos + 1] = (byte) (v & 0xFF);130v = v >> 8;131data[pos + 0] = (byte) (v & 0xFF);132}133134private static int getCommentPos(byte[] d) {135int p = 8;136while (p + 8 < d.length) {137if (d[p + 4] == (byte) 0x74 && d[p + 5] == (byte) 0x45 &&138d[p + 6] == (byte) 0x58 && d[p + 7] == (byte) 0x74)139{140return p;141}142p++;143}144throw new RuntimeException("Test chunk was not found!");145}146147private static BufferedImage createTestImage() {148final int w = 128;149final int h = 128;150151BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);152Graphics2D g = img.createGraphics();153g.setPaint(new GradientPaint(0, 0, Color.blue,154w, h, Color.red));155g.fillRect(0, 0, w, h);156g.dispose();157return img;158}159}160161162