1 /*
   2  * Copyright (c) 2004, 2015, 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  * @modules java.management
  30  * @run clean URLTest
  31  * @run build URLTest
  32  * @run main URLTest
  33  */
  34 
  35 import java.net.MalformedURLException;
  36 import java.net.URISyntaxException;
  37 import java.net.URI;
  38 import javax.management.remote.JMXServiceURL;
  39 
  40 public class URLTest {
  41     private static final String[] good = {
  42         "",
  43         "a",
  44         "a.b",
  45         "a.b.c.d.e.f.g",
  46         "aaa.bbb",
  47         "a-a.b-b",
  48         "a-a",
  49         "a--b",
  50         "1.2.3.4",
  51         "1.2.3.x",
  52         "111.222.222.111",
  53         "1",
  54         "23skiddoo",
  55         "23skiddoo.sfbay",
  56         "a1.b2",
  57         "1234.sfbay",
  58         "[::]",
  59         "[ffff::ffff]",
  60     };
  61     private static final String[] bad = {
  62         "-a",
  63         "a-",
  64         "-",
  65         "_",
  66         "a_b",
  67         "a_b.sfbay",
  68         ".",
  69         "..",
  70         ".a",
  71         "a.",
  72         "a..",
  73         "a..b",
  74         ".a.b",
  75         "a.b.",
  76         "a.b..",
  77         "1.2",
  78         "111.222.333.444",
  79         "a.23skiddoo",
  80         "[:::]",
  81         "[:]",
  82     };
  83 
  84     public static void main(String[] args) throws Exception {
  85         System.out.println("Testing that JMXServiceURL accepts the same " +
  86                            "hosts as java.net.URI");
  87         System.out.println("(Except that it allows empty host names and " +
  88                            "forbids a trailing \".\")");
  89         System.out.println();
  90 
  91         int failures = 0;
  92 
  93         for (int pass = 1; pass <= 2; pass++) {
  94             final boolean accept = (pass == 1);
  95             System.out.println("  Hosts that should " +
  96                                (accept ? "" : "not ") + "work");
  97             String[] hosts = accept ? good : bad;
  98 
  99             for (int i = 0; i < hosts.length; i++) {
 100                 final String host = hosts[i];
 101                 System.out.print("    " + host + ": ");
 102 
 103                 boolean jmxAccept = true;
 104                 try {
 105                     new JMXServiceURL("rmi", hosts[i], 0);
 106                 } catch (MalformedURLException e) {
 107                     jmxAccept = false;
 108                 }
 109 
 110                 boolean uriAccept;
 111                 try {
 112                     final URI uri = new URI("http://" + host + "/");
 113                     uriAccept = (uri.getHost() != null);
 114                 } catch (URISyntaxException e) {
 115                     uriAccept = false;
 116                 }
 117 
 118                 final int len = host.length();
 119                 if (accept != uriAccept && len != 0 &&
 120                     !(len > 1 && host.charAt(len - 1) == '.'
 121                       && host.charAt(len - 2) != '.')) {
 122                     // JMXServiceURL allows empty host name; also
 123                     // java.net.URI allows trailing dot in hostname,
 124                     // following RFC 2396, but JMXServiceURL doesn't,
 125                     // following RFC 2609
 126                     System.out.println("TEST BUG: URI accept=" + uriAccept);
 127                     failures++;
 128                 } else {
 129                     if (jmxAccept == accept)
 130                         System.out.println("OK");
 131                     else {
 132                         System.out.println("FAILED");
 133                         failures++;
 134                     }
 135                 }
 136             }
 137 
 138             System.out.println();
 139         }
 140 
 141         if (failures == 0)
 142             System.out.println("Test passed");
 143         else {
 144             System.out.println("TEST FAILURES: " + failures);
 145             System.exit(1);
 146         }
 147     }
 148 }