Path: blob/master/test/jdk/javax/imageio/plugins/png/ItxtUtf8Test.java
41155 views
/*1* Copyright (c) 2008, 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 6541476 678207926* @summary Write and read a PNG file including an non-latin1 iTXt chunk27* Test also verifies that trunkated png images does not cause28* an OoutOfMemory error.29*30* @run main ItxtUtf8Test31*32* @run main/othervm/timeout=10 -Xmx6m ItxtUtf8Test truncate33*/3435import java.awt.image.BufferedImage;36import java.io.ByteArrayInputStream;37import java.io.ByteArrayOutputStream;38import java.io.OutputStream;39import java.util.Arrays;40import java.util.List;41import javax.imageio.IIOException;42import javax.imageio.IIOImage;43import javax.imageio.ImageIO;44import javax.imageio.ImageReader;45import javax.imageio.ImageTypeSpecifier;46import javax.imageio.ImageWriter;47import javax.imageio.metadata.IIOMetadata;48import javax.imageio.stream.ImageInputStream;49import javax.imageio.stream.ImageOutputStream;50import javax.imageio.stream.MemoryCacheImageInputStream;51import javax.imageio.stream.MemoryCacheImageOutputStream;52import org.w3c.dom.DOMImplementation;53import org.w3c.dom.Document;54import org.w3c.dom.Element;55import org.w3c.dom.Node;56import org.w3c.dom.bootstrap.DOMImplementationRegistry;5758public class ItxtUtf8Test {5960public static final String61TEXT = "\u24c9\u24d4\u24e7\u24e3" +62"\ud835\udc13\ud835\udc1e\ud835\udc31\ud835\udc2d" +63"\u24c9\u24d4\u24e7\u24e3", // a repetition for compression64VERBATIM = "\u24e5\u24d4\u24e1\u24d1\u24d0\u24e3\u24d8\u24dc",65COMPRESSED = "\u24d2\u24de\u24dc\u24df\u24e1\u24d4\u24e2\u24e2\u24d4\u24d3";6667public static final byte[]68VBYTES = {69(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x56, // chunk length70(byte)0x69, (byte)0x54, (byte)0x58, (byte)0x74, // chunk type "iTXt"71(byte)0x76, (byte)0x65, (byte)0x72, (byte)0x62,72(byte)0x61, (byte)0x74, (byte)0x69, (byte)0x6d, // keyword "verbatim"73(byte)0x00, // separator terminating keyword74(byte)0x00, // compression flag75(byte)0x00, // compression method, must be zero76(byte)0x78, (byte)0x2d, (byte)0x63, (byte)0x69,77(byte)0x72, (byte)0x63, (byte)0x6c, (byte)0x65,78(byte)0x64, // language tag "x-circled"79(byte)0x00, // separator terminating language tag80(byte)0xe2, (byte)0x93, (byte)0xa5, // '\u24e5'81(byte)0xe2, (byte)0x93, (byte)0x94, // '\u24d4'82(byte)0xe2, (byte)0x93, (byte)0xa1, // '\u24e1'83(byte)0xe2, (byte)0x93, (byte)0x91, // '\u24d1'84(byte)0xe2, (byte)0x93, (byte)0x90, // '\u24d0'85(byte)0xe2, (byte)0x93, (byte)0xa3, // '\u24e3'86(byte)0xe2, (byte)0x93, (byte)0x98, // '\u24d8'87(byte)0xe2, (byte)0x93, (byte)0x9c, // '\u24dc'88(byte)0x00, // separator terminating the translated keyword89(byte)0xe2, (byte)0x93, (byte)0x89, // '\u24c9'90(byte)0xe2, (byte)0x93, (byte)0x94, // '\u24d4'91(byte)0xe2, (byte)0x93, (byte)0xa7, // '\u24e7'92(byte)0xe2, (byte)0x93, (byte)0xa3, // '\u24e3'93(byte)0xf0, (byte)0x9d, (byte)0x90, (byte)0x93, // '\ud835\udc13'94(byte)0xf0, (byte)0x9d, (byte)0x90, (byte)0x9e, // '\ud835\udc1e'95(byte)0xf0, (byte)0x9d, (byte)0x90, (byte)0xb1, // '\ud835\udc31'96(byte)0xf0, (byte)0x9d, (byte)0x90, (byte)0xad, // '\ud835\udc2d'97(byte)0xe2, (byte)0x93, (byte)0x89, // '\u24c9'98(byte)0xe2, (byte)0x93, (byte)0x94, // '\u24d4'99(byte)0xe2, (byte)0x93, (byte)0xa7, // '\u24e7'100(byte)0xe2, (byte)0x93, (byte)0xa3, // '\u24e3'101(byte)0xb5, (byte)0xcc, (byte)0x97, (byte)0x56 // CRC102},103CBYTES = {104// we don't want to check the chunk length,105// as this might depend on implementation.106(byte)0x69, (byte)0x54, (byte)0x58, (byte)0x74, // chunk type "iTXt"107(byte)0x63, (byte)0x6f, (byte)0x6d, (byte)0x70,108(byte)0x72, (byte)0x65, (byte)0x73, (byte)0x73,109(byte)0x65, (byte)0x64, // keyword "compressed"110(byte)0x00, // separator terminating keyword111(byte)0x01, // compression flag112(byte)0x00, // compression method, 0=deflate113(byte)0x78, (byte)0x2d, (byte)0x63, (byte)0x69,114(byte)0x72, (byte)0x63, (byte)0x6c, (byte)0x65,115(byte)0x64, // language tag "x-circled"116(byte)0x00, // separator terminating language tag117// we don't want to check the actual compressed data,118// as this might depend on implementation.119};120/*121*/122123public static void main(String[] args) throws Exception {124List argList = Arrays.asList(args);125if (argList.contains("truncate")) {126try {127runTest(false, true);128throw new AssertionError("Expect an error for truncated file");129}130catch (IIOException e) {131// expected an error for a truncated image file.132}133}134else {135runTest(argList.contains("dump"), false);136}137}138139public static void runTest(boolean dump, boolean truncate)140throws Exception141{142String format = "javax_imageio_png_1.0";143BufferedImage img =144new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);145ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();146ByteArrayOutputStream os = new ByteArrayOutputStream();147ImageOutputStream ios = new MemoryCacheImageOutputStream(os);148iw.setOutput(ios);149IIOMetadata meta =150iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);151DOMImplementationRegistry registry;152registry = DOMImplementationRegistry.newInstance();153DOMImplementation impl = registry.getDOMImplementation("XML 3.0");154Document doc = impl.createDocument(null, format, null);155Element root, itxt, entry;156root = doc.getDocumentElement();157root.appendChild(itxt = doc.createElement("iTXt"));158itxt.appendChild(entry = doc.createElement("iTXtEntry"));159entry.setAttribute("keyword", "verbatim");160entry.setAttribute("compressionFlag", "false");161entry.setAttribute("compressionMethod", "0");162entry.setAttribute("languageTag", "x-circled");163entry.setAttribute("translatedKeyword", VERBATIM);164entry.setAttribute("text", TEXT);165itxt.appendChild(entry = doc.createElement("iTXtEntry"));166entry.setAttribute("keyword", "compressed");167entry.setAttribute("compressionFlag", "true");168entry.setAttribute("compressionMethod", "0");169entry.setAttribute("languageTag", "x-circled");170entry.setAttribute("translatedKeyword", COMPRESSED);171entry.setAttribute("text", TEXT);172meta.mergeTree(format, root);173iw.write(new IIOImage(img, null, meta));174iw.dispose();175176byte[] bytes = os.toByteArray();177if (dump)178System.out.write(bytes);179if (findBytes(VBYTES, bytes) < 0)180throw new AssertionError("verbatim block not found");181if (findBytes(CBYTES, bytes) < 0)182throw new AssertionError("compressed block not found");183int length = bytes.length;184if (truncate)185length = findBytes(VBYTES, bytes) + 32;186187ImageReader ir = ImageIO.getImageReader(iw);188ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, length);189ImageInputStream iis = new MemoryCacheImageInputStream(is);190ir.setInput(iis);191meta = ir.getImageMetadata(0);192Node node = meta.getAsTree(format);193for (node = node.getFirstChild();194!"iTXt".equals(node.getNodeName());195node = node.getNextSibling());196boolean verbatimSeen = false, compressedSeen = false;197for (node = node.getFirstChild();198node != null;199node = node.getNextSibling()) {200entry = (Element)node;201String keyword = entry.getAttribute("keyword");202String translatedKeyword = entry.getAttribute("translatedKeyword");203String text = entry.getAttribute("text");204if ("verbatim".equals(keyword)) {205if (verbatimSeen) throw new AssertionError("Duplicate");206verbatimSeen = true;207if (!VERBATIM.equals(translatedKeyword))208throw new AssertionError("Wrong translated keyword");209if (!TEXT.equals(text))210throw new AssertionError("Wrong text");211}212else if ("compressed".equals(keyword)) {213if (compressedSeen) throw new AssertionError("Duplicate");214compressedSeen = true;215if (!COMPRESSED.equals(translatedKeyword))216throw new AssertionError("Wrong translated keyword");217if (!TEXT.equals(text))218throw new AssertionError("Wrong text");219}220else {221throw new AssertionError("Unexpected keyword");222}223}224if (!(verbatimSeen && compressedSeen))225throw new AssertionError("Missing chunk");226}227228private static final int findBytes(byte[] needle, byte[] haystack) {229HAYSTACK: for (int h = 0; h <= haystack.length - needle.length; ++h) {230for (int n = 0; n < needle.length; ++n) {231if (needle[n] != haystack[h + n]) {232continue HAYSTACK;233}234}235return h;236}237return -1;238}239240}241242243