test/jdk/javax/imageio/plugins/png/PngPLTEChunkMissingTest.java
author jdv
Tue, 26 Dec 2017 13:38:31 +0530
changeset 48641 35b5da568499
permissions -rw-r--r--
8190997: PNGImageReader throws NullPointerException when PLTE section is missing Reviewed-by: serb, bpb, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48641
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     1
/*
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     4
 *
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     8
 *
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    13
 * accompanied this code).
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    14
 *
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    18
 *
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    21
 * questions.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    22
 */
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    23
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    24
/*
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    25
 * @test
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    26
 * @bug     8190997
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    27
 * @summary Test verifies that ImageIO.read() throws proper IIOException
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    28
 *          when we have a PNG image with color type PNG_COLOR_PALETTE but
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    29
 *          missing the required PLTE chunk.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    30
 * @run     main PngPLTEChunkMissingTest
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    31
 */
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    32
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    34
import java.io.InputStream;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    35
import java.util.Base64;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    36
import javax.imageio.IIOException;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    37
import javax.imageio.ImageIO;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    38
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    39
public class PngPLTEChunkMissingTest {
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    40
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    41
    // PNG image stream missing the required PLTE chunk
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    42
    private static String inputImageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    43
            + "CAMAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJgggo=";
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    44
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    46
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    47
        byte[] inputBytes = Base64.getDecoder().decode(inputImageBase64);
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    48
        InputStream in = new ByteArrayInputStream(inputBytes);
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    49
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    50
        /*
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    51
         * Attempt to read a PNG image of color type PNG_COLOR_PALETTE
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    52
         * but missing the required PLTE chunk.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    53
         */
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    54
        try {
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    55
            ImageIO.read(in);
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    56
        } catch (IIOException e) {
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    57
            /*
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    58
             * We expect ImageIO to throw IIOException with proper message
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    59
             * instead of throwing NullPointerException.
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    60
             */
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    61
            Throwable cause = e.getCause();
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    62
            if (cause == null ||
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    63
                (!(cause.getMessage().
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    64
                 equals("Required PLTE chunk missing"))))
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    65
            {
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    66
                throw e;
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    67
            }
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    68
        }
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    69
    }
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    70
}
35b5da568499 8190997: PNGImageReader throws NullPointerException when PLTE section is missing
jdv
parents:
diff changeset
    71