8152561: Is it allowed to have zero value for count in TIFFField.createArrayForType() for the rationals
authorjdv
Fri, 27 Jan 2017 12:16:57 +0530
changeset 43515 a089f3441356
parent 43514 0405ddec4224
child 43516 f23912ce3308
8152561: Is it allowed to have zero value for count in TIFFField.createArrayForType() for the rationals Reviewed-by: psadhukhan, bpb, prr
jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java
jdk/test/javax/imageio/plugins/tiff/TIFFCreateArrayForTypeTest.java
--- a/jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java	Thu Jan 26 09:19:33 2017 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/imageio/plugins/tiff/TIFFField.java	Fri Jan 27 12:16:57 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -567,7 +567,7 @@
                 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
         } else if (type == TIFFTag.TIFF_IFD_POINTER && count != 1) {
             throw new IllegalArgumentException
-                ("Type is TIFF_IFD_POINTER count != 1");
+                ("Type is TIFF_IFD_POINTER and count != 1");
         } else if(data == null) {
             throw new NullPointerException("data == null!");
         }
@@ -698,6 +698,11 @@
      * data type for the supplied {@code TIFFTag}.
      * @throws IllegalArgumentException if {@code count < 0}.
      * @see #TIFFField(TIFFTag,int,int,Object)
+     * @throws IllegalArgumentException if {@code count < 1}
+     * and {@code type} is {@code TIFF_RATIONAL} or
+     * {@code TIFF_SRATIONAL}.
+     * @throws IllegalArgumentException if {@code count != 1}
+     * and {@code type} is {@code TIFF_IFD_POINTER}.
      */
     public TIFFField(TIFFTag tag, int type, int count) {
         this(tag, type, count, createArrayForType(type, count));
@@ -885,11 +890,26 @@
      * @throws IllegalArgumentException if {@code dataType} is not
      * one of the {@code TIFFTag.TIFF_*} data type constants.
      * @throws IllegalArgumentException if {@code count < 0}.
+     * @throws IllegalArgumentException if {@code count < 1}
+     * and {@code type} is {@code TIFF_RATIONAL} or
+     * {@code TIFF_SRATIONAL}.
+     * @throws IllegalArgumentException if {@code count != 1}
+     * and {@code type} is {@code TIFF_IFD_POINTER}.
      */
     public static Object createArrayForType(int dataType, int count) {
+
         if(count < 0) {
             throw new IllegalArgumentException("count < 0!");
+        } else if ((dataType == TIFFTag.TIFF_RATIONAL
+                   || dataType == TIFFTag.TIFF_SRATIONAL)
+                  && count < 1) {
+            throw new IllegalArgumentException
+                ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
+        } else if (dataType == TIFFTag.TIFF_IFD_POINTER && count != 1) {
+            throw new IllegalArgumentException
+                ("Type is TIFF_IFD_POINTER and count != 1");
         }
+
         switch (dataType) {
         case TIFFTag.TIFF_BYTE:
         case TIFFTag.TIFF_SBYTE:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/imageio/plugins/tiff/TIFFCreateArrayForTypeTest.java	Fri Jan 27 12:16:57 2017 +0530
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug     8152561
+ * @summary Test verifies whether all boundary conditions are checked
+ *          properly in TIFFField.createArrayForType().
+ * @run     main TIFFCreateArrayForTypeTest
+ */
+
+import javax.imageio.plugins.tiff.TIFFField;
+import javax.imageio.plugins.tiff.TIFFTag;
+
+public class TIFFCreateArrayForTypeTest {
+
+    static int count = 0;
+    static boolean unknownDataType, negativeCount, zeroCount, countNotOne;
+    static String errorMsg = "";
+
+    private static void testCase1() {
+        // check passing unknown data type to createArrayForType()
+        count = 2;
+        int dataType = 15;
+        try {
+            TIFFField.createArrayForType(dataType, count);
+        } catch (IllegalArgumentException e) {
+            unknownDataType = true;
+        } catch (Exception e) {
+            // just consume if it throws any other exception.
+        }
+        if (!unknownDataType) {
+            errorMsg = errorMsg + "testCase1 ";
+        }
+    }
+
+    private static void testCase2() {
+        // check passing negative count value for createArrayForType()
+        count = -1;
+        try {
+            TIFFField.createArrayForType(TIFFTag.TIFF_LONG, count);
+        } catch (IllegalArgumentException e) {
+            negativeCount = true;
+        } catch (Exception e) {
+            // just consume if it throws any other exception.
+        }
+        if (!negativeCount) {
+            errorMsg = errorMsg + "testCase2 ";
+        }
+    }
+
+    private static void testCase3() {
+        /*
+         * check passing zero count value for createArrayForType() with
+         * TIFFTag.TIFF_RATIONAL or TIFFTag.TIFF_SRATIONAL data type.
+         */
+        count = 0;
+        try {
+            TIFFField.createArrayForType(TIFFTag.TIFF_RATIONAL, count);
+        } catch (IllegalArgumentException e) {
+            zeroCount = true;
+        } catch (Exception e) {
+            // just consume if it throws any other exception.
+        }
+        if (!zeroCount) {
+            errorMsg = errorMsg + "testCase3 ";
+        }
+    }
+
+    private static void testCase4() {
+        /*
+         * check passing count value other than 1 for createArrayForType() with
+         * TIFFTag.TIFF_IFD_POINTER data type.
+         */
+        count = 2;
+        try {
+            TIFFField.createArrayForType(TIFFTag.TIFF_IFD_POINTER, count);
+        } catch (IllegalArgumentException e) {
+            countNotOne = true;
+        } catch (Exception e) {
+            // just consume if it throws any other exception.
+        }
+        if (!countNotOne) {
+            errorMsg = errorMsg + "testCase4 ";
+        }
+    }
+
+    public static void main(String[] args) {
+        /*
+         * test different scenarios where TIFFField.createArrayForType()
+         * is required to throw IllegalArgumentException.
+         */
+        testCase1();
+        testCase2();
+        testCase3();
+        testCase4();
+        if ((!unknownDataType) ||
+            (!negativeCount) ||
+            (!zeroCount) ||
+            (!countNotOne))
+        {
+            throw new RuntimeException(errorMsg + "is/are not throwing"
+                    + " required IllegalArgumentException");
+        }
+    }
+}
+