Path: blob/master/test/jdk/javax/imageio/plugins/png/WriteInvalidKeywordTest.java
41155 views
/*1* Copyright (c) 2020, 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 824255726* @summary Test verifies that PNGImageWriter does not write27* longer than 79 length null terminated strings.28* @run main WriteInvalidKeywordTest29*/3031import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.awt.Color;34import java.io.ByteArrayOutputStream;35import java.io.IOException;36import java.util.Iterator;37import javax.imageio.*;38import javax.imageio.metadata.IIOInvalidTreeException;39import javax.imageio.metadata.IIOMetadata;40import javax.imageio.metadata.IIOMetadataNode;41import javax.imageio.stream.ImageOutputStream;4243public class WriteInvalidKeywordTest {4445private static BufferedImage img;46private static ImageWriter writer;47private static ImageWriteParam param;48private static IIOMetadata metadata;4950private static void initialize(int type) {51int width = 1;52int height = 1;53img = new BufferedImage(width, height, type);54Graphics2D g2D = img.createGraphics();55g2D.setColor(new Color(255, 255, 255));56g2D.fillRect(0, 0, width, width);57g2D.dispose();5859Iterator<ImageWriter> iterWriter =60ImageIO.getImageWritersBySuffix("png");61writer = iterWriter.next();6263param = writer.getDefaultWriteParam();64ImageTypeSpecifier specifier =65ImageTypeSpecifier.66createFromBufferedImageType(type);67metadata = writer.getDefaultImageMetadata(specifier, param);68}6970private static void createTEXTNode()71throws IIOInvalidTreeException {72IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");73// Keyword length greater than 7974tEXt_Entry.setAttribute("keyword", "Authored" +75"AuthoredAuthoredAuthoredAuthoredAuthoredAuthored" +76"AuthoredAuthoredAuthoredAuthored");77tEXt_Entry.setAttribute("value", "");7879IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");80tEXt.appendChild(tEXt_Entry);81IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");82root.appendChild(tEXt);83metadata.mergeTree("javax_imageio_png_1.0", root);84}8586private static void writeImage() throws IOException {87ByteArrayOutputStream baos = new ByteArrayOutputStream();88ImageOutputStream ios = ImageIO.createImageOutputStream(baos);89writer.setOutput(ios);90writer.write(metadata, new IIOImage(img, null, metadata), param);91writer.dispose();92baos.close();93ios.close();94}9596private static void writePNGTEXTChunk() throws IOException {97initialize(BufferedImage.TYPE_BYTE_GRAY);98createTEXTNode();99writeImage();100}101102public static void main(String[] args) throws IOException {103// write PNG image with tEXT chunk having keyword length104// greater than 79.105boolean failed = true;106try {107writePNGTEXTChunk();108} catch (IIOException e) {109// we expect it to throw IIOException110if (e.getCause().getMessage() ==111"tEXt keyword is longer than 79") {112failed = false;113}114}115if (failed) {116throw new RuntimeException("Test failed, did not throw " +117"expected exception");118}119}120}121122123124