1 /*
   2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5057532
  27  * @summary Tests that host names are parsed correctly in URLs
  28  * @author Eamonn McManus
  29  * @run clean URLTest
  30  * @run build URLTest
  31  * @run main URLTest
  32  */
  33 
  34 import java.net.MalformedURLException;
  35 import java.net.URISyntaxException;
  36 import java.net.URI;
  37 import javax.management.remote.JMXServiceURL;
  38 
  39 public class URLTest {
  40     private static final String[] good = {
  41         "",
  42         "a",
  43         "a.b",
  44         "a.b.c.d.e.f.g",
  45         "aaa.bbb",
  46         "a-a.b-b",
  47         "a-a",
  48         "a--b",
  49         "1.2.3.4",
  50         "1.2.3.x",
  51         "111.222.222.111",
  52         "1",
  53         "23skiddoo",
  54         "23skiddoo.sfbay",
  55         "a1.b2",
  56         "1234.sfbay",
  57         "[::]",
  58         "[ffff::ffff]",
  59     };
  60     private static final String[] bad = {
  61         "-a",
  62         "a-",
  63         "-",
  64         "_",
  65         "a_b",
  66         "a_b.sfbay",
  67         ".",
  68         "..",
  69         ".a",
  70         "a.",
  71         "a..",
  72         "a..b",
  73         ".a.b",
  74         "a.b.",
  75         "a.b..",
  76         "1.2",
  77         "111.222.333.444",
  78         "a.23skiddoo",
  79         "[:::]",
  80         "[:]",
  81     };
  82 
  83     public static void main(String[] args) throws Exception {
  84         System.out.println("Testing that JMXServiceURL accepts the same " +
  85                            "hosts as java.net.URI");
  86         System.out.println("(Except that it allows empty host names and " +
  87                            "forbids a trailing \".\")");
  88         System.out.println();
  89 
  90         int failures = 0;
  91 
  92         for (int pass = 1; pass <= 2; pass++) {
  93             final boolean accept = (pass == 1);
  94             System.out.println("  Hosts that should " +
  95                                (accept ? "" : "not ") + "work");
  96             String[] hosts = accept ? good : bad;
  97 
  98             for (int i = 0; i < hosts.length; i++) {
  99                 final String host = hosts[i];
 100                 System.out.print("    " + host + ": ");
 101 
 102                 boolean jmxAccept = true;
 103                 try {
 104                     new JMXServiceURL("rmi", hosts[i], 0);
 105                 } catch (MalformedURLException e) {
 106                     jmxAccept = false;
 107                 }
 108 
 109                 boolean uriAccept;
 110                 try {
 111                     final URI uri = new URI("http://" + host + "/");
 112                     uriAccept = (uri.getHost() != null);
 113                 } catch (URISyntaxException e) {
 114                     uriAccept = false;
 115                 }
 116 
 117                 final int len = host.length();
 118                 if (accept != uriAccept && len != 0 &&
 119                     !(len > 1 && host.charAt(len - 1) == '.'
 120                       && host.charAt(len - 2) != '.')) {
 121                     // JMXServiceURL allows empty host name; also
 122                     // java.net.URI allows trailing dot in hostname,
 123                     // following RFC 2396, but JMXServiceURL doesn't,
 124                     // following RFC 2609
 125                     System.out.println("TEST BUG: URI accept=" + uriAccept);
 126                     failures++;
 127                 } else {
 128                     if (jmxAccept == accept)
 129                         System.out.println("OK");
 130                     else {
 131                         System.out.println("FAILED");
 132                         failures++;
 133                     }
 134                 }
 135             }
 136 
 137             System.out.println();
 138         }
 139 
 140         if (failures == 0)
 141             System.out.println("Test passed");
 142         else {
 143             System.out.println("TEST FAILURES: " + failures);
 144             System.exit(1);
 145         }
 146     }
 147 }