8020842: IDN do not throw IAE when hostname ends with a trailing dot
authorxuelei
Mon, 19 Aug 2013 17:42:39 -0700
changeset 19440 c4414bc88602
parent 19439 57876ed3c426
child 19441 89544f16aa6b
8020842: IDN do not throw IAE when hostname ends with a trailing dot Reviewed-by: weijun, michaelm
jdk/src/share/classes/java/net/IDN.java
jdk/test/java/net/IDN/IllegalArg.java
jdk/test/sun/security/ssl/javax/net/ssl/ServerName/IllegalSNIName.java
--- a/jdk/src/share/classes/java/net/IDN.java	Mon Aug 19 17:17:17 2013 -0400
+++ b/jdk/src/share/classes/java/net/IDN.java	Mon Aug 19 17:42:39 2013 -0700
@@ -113,11 +113,18 @@
         int p = 0, q = 0;
         StringBuffer out = new StringBuffer();
 
+        if (isRootLabel(input)) {
+            return ".";
+        }
+
         while (p < input.length()) {
             q = searchDots(input, p);
             out.append(toASCIIInternal(input.substring(p, q),  flag));
+            if (q != (input.length())) {
+               // has more labels, or keep the trailing dot as at present
+               out.append('.');
+            }
             p = q + 1;
-            if (p < input.length()) out.append('.');
         }
 
         return out.toString();
@@ -167,11 +174,18 @@
         int p = 0, q = 0;
         StringBuffer out = new StringBuffer();
 
+        if (isRootLabel(input)) {
+            return ".";
+        }
+
         while (p < input.length()) {
             q = searchDots(input, p);
             out.append(toUnicodeInternal(input.substring(p, q),  flag));
+            if (q != (input.length())) {
+               // has more labels, or keep the trailing dot as at present
+               out.append('.');
+            }
             p = q + 1;
-            if (p < input.length()) out.append('.');
         }
 
         return out.toString();
@@ -263,6 +277,13 @@
             dest = new StringBuffer(label);
         }
 
+        // step 8, move forward to check the smallest number of the code points
+        // the length must be inside 1..63
+        if (dest.length() == 0) {
+            throw new IllegalArgumentException(
+                        "Empty label is not a legal name");
+        }
+
         // step 3
         // Verify the absence of non-LDH ASCII code points
         //   0..0x2c, 0x2e..0x2f, 0x3a..0x40, 0x5b..0x60, 0x7b..0x7f
@@ -311,7 +332,7 @@
 
         // step 8
         // the length must be inside 1..63
-        if(dest.length() > MAX_LABEL_LENGTH){
+        if (dest.length() > MAX_LABEL_LENGTH) {
             throw new IllegalArgumentException("The label in the input is too long");
         }
 
@@ -409,8 +430,7 @@
     private static int searchDots(String s, int start) {
         int i;
         for (i = start; i < s.length(); i++) {
-            char c = s.charAt(i);
-            if (c == '.' || c == '\u3002' || c == '\uFF0E' || c == '\uFF61') {
+            if (isLabelSeparator(s.charAt(i))) {
                 break;
             }
         }
@@ -418,6 +438,19 @@
         return i;
     }
 
+    //
+    // to check if a string is a root label, ".".
+    //
+    private static boolean isRootLabel(String s) {
+        return (s.length() == 1 && isLabelSeparator(s.charAt(0)));
+    }
+
+    //
+    // to check if a character is a label separator, i.e. a dot character.
+    //
+    private static boolean isLabelSeparator(char c) {
+        return (c == '.' || c == '\u3002' || c == '\uFF0E' || c == '\uFF61');
+    }
 
     //
     // to check if a string only contains US-ASCII code point
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/net/IDN/IllegalArg.java	Mon Aug 19 17:42:39 2013 -0700
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2013, 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 8020842
+ * @summary IDN do not throw IAE when hostname ends with a trailing dot
+ */
+
+import java.net.*;
+
+public class IllegalArg {
+
+    public static void main(String[] args) throws Exception {
+        String[] illegalNames = {
+                "com..net",
+                "com..",
+                ".com",
+                ".com."
+            };
+
+        String[] legalNames = {
+                "example.com",
+                "com\u3002",
+                "com.",
+                "."
+            };
+
+        for (String name : illegalNames) {
+            try {
+                IDN.toASCII(name, IDN.USE_STD3_ASCII_RULES);
+                throw new Exception(
+                    "Expected to get IllegalArgumentException for " + name);
+            } catch (IllegalArgumentException iae) {
+                // That's the right behavior.
+            }
+
+            try {
+                IDN.toASCII(name);
+                throw new Exception(
+                    "Expected to get IllegalArgumentException for " + name);
+            } catch (IllegalArgumentException iae) {
+                // That's the right behavior.
+            }
+        }
+
+        for (String name : legalNames) {
+            System.out.println("Convering " + name);
+            System.out.println(IDN.toASCII(name, IDN.USE_STD3_ASCII_RULES));
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/ssl/javax/net/ssl/ServerName/IllegalSNIName.java	Mon Aug 19 17:42:39 2013 -0700
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2013, 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 8020842
+ * @summary SNIHostName does not throw IAE when hostname ends
+ *          with a trailing dot
+ */
+
+import javax.net.ssl.SNIHostName;
+
+public class IllegalSNIName {
+
+    public static void main(String[] args) throws Exception {
+        String[] illegalNames = {
+                "example\u3003\u3002com",
+                "example..com",
+                "com\u3002",
+                "com.",
+                "."
+            };
+
+        for (String name : illegalNames) {
+            try {
+                SNIHostName hostname = new SNIHostName(name);
+                throw new Exception(
+                    "Expected to get IllegalArgumentException for " + name);
+            } catch (IllegalArgumentException iae) {
+                // That's the right behavior.
+            }
+        }
+    }
+}