Path: blob/master/test/jdk/javax/imageio/plugins/tiff/BogusSecondImageTest.java
41153 views
/*1* Copyright (c) 2016, 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 814501426* @summary Verify reader correctly fails for zero-entry IFDs and EOFs27* encountered in locateImage().28*/2930import java.awt.Image;31import java.awt.image.*;32import java.io.*;33import java.util.Iterator;34import javax.imageio.*;35import javax.imageio.stream.*;3637public class BogusSecondImageTest {38public static void main(String[] args) throws Throwable {39int failures = 0;4041try {42testZeroEntryIFD();43} catch (Exception e) {44System.out.printf("Failed testZeroEntryIFD: %s%n", e);45failures++;46}4748try {49testOutOfStreamIFD();50} catch (Exception e) {51System.out.printf("Failed testOutOfStreamIFD: %s%n", e);52failures++;53}5455if (failures == 0) {56System.out.println("Test succeeded");57} else {58throw new RuntimeException59("Test failed with " + failures + " errors");60}61}6263private static void testZeroEntryIFD() throws Exception {64// Create an image.65File f = createImageFile();6667ImageOutputStream s = new FileImageOutputStream(f);68long length = s.length();6970// Skip the endianness and magic number71s.skipBytes(4);7273// Read and seek to offset of 0th IFD74long ifd0 = s.readUnsignedInt();75s.seek(ifd0);7677// Read number of 0th IFD entries and skip over them78int entries0 = s.readUnsignedShort();79s.skipBytes(12*entries0);8081// Write the offset of the 1st IFD as the current file length82s.write((int)length);8384// Seek to the 1st IFD and write a zero entry count to it85s.seek(length);86s.writeShort(0);87s.close();8889try {90Load(f);91} catch (Exception e) {92throw e;93} finally {94f.delete();95}96}9798private static void testOutOfStreamIFD() throws Exception {99// Create an image.100File f = createImageFile();101ImageOutputStream s = new FileImageOutputStream(f);102long length = s.length();103104// Skip the endianness and magic number105s.skipBytes(4);106107// Read and seek to offset of 0th IFD108long ifd0 = s.readUnsignedInt();109s.seek(ifd0);110111// Read number of 0th IFD entries and skip over them112int entries0 = s.readUnsignedShort();113s.skipBytes(12*entries0);114115// Write the offset of the 1st IFD as the current file length + 7116s.write((int)length + 7);117s.close();118119try {120Load(f);121} catch (Exception e) {122throw e;123} finally {124f.delete();125}126}127128private static File createImageFile() throws Exception {129BufferedImage im =130new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);131File f = File.createTempFile("BogusSecondImage", "tif", new File("."));132f.deleteOnExit();133if (!ImageIO.write(im, "TIFF", f)) {134throw new RuntimeException("Failed to write " + f);135}136return f;137}138139private final static boolean printTrace = false;140141public static void Load(File file) {142if (!file.exists()) {143throw new IllegalArgumentException(file + " does not exist");144} else if (!file.isFile()) {145throw new IllegalArgumentException(file + " is not a regular file");146} else if (!file.canRead()) {147throw new IllegalArgumentException(file + " cannot be read");148}149150ImageInputStream input = null;151try {152input = ImageIO.createImageInputStream(file);153} catch (Throwable e) {154System.err.println("NOK: createImageInputStream()\t" + e.getMessage());155if (printTrace) { e.printStackTrace(); }156return;157}158159Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("TIFF");160if (!readers.hasNext()) { throw new RuntimeException("No readers available for TIFF"); }161ImageReader reader = readers.next();162reader.setInput(input);163164Image images[] = null;165int numImages = 0;166167int failures = 0;168try {169numImages = reader.getNumImages(true);170images = new Image[numImages];171} catch (Throwable e) {172failures++;173System.err.println("NOK: getNumImages()\t" + e.getMessage());174if (printTrace) { e.printStackTrace(); }175}176System.out.printf("numImages %d%n", numImages);177178for (int i = 0; i < numImages; i++) {179System.out.printf("reading image %d%n", i);180try {181images[i] = reader.read(i);182} catch (Throwable e) {183failures++;184System.err.println("NOK: read()\t" + e.getMessage());185if (printTrace) { e.printStackTrace(); }186}187}188189if (failures == 0) {190System.err.println("OK");191} else {192throw new RuntimeException("NOK");193}194}195}196197198