Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/URLTest.java
38855 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 505753226* @summary Tests that host names are parsed correctly in URLs27* @author Eamonn McManus28* @run clean URLTest29* @run build URLTest30* @run main URLTest31*/3233import java.net.MalformedURLException;34import java.net.URISyntaxException;35import java.net.URI;36import javax.management.remote.JMXServiceURL;3738public class URLTest {39private static final String[] good = {40"",41"a",42"a.b",43"a.b.c.d.e.f.g",44"aaa.bbb",45"a-a.b-b",46"a-a",47"a--b",48"1.2.3.4",49"1.2.3.x",50"111.222.222.111",51"1",52"23skiddoo",53"23skiddoo.sfbay",54"a1.b2",55"1234.sfbay",56"[::]",57"[ffff::ffff]",58};59private static final String[] bad = {60"-a",61"a-",62"-",63"_",64"a_b",65"a_b.sfbay",66".",67"..",68".a",69"a.",70"a..",71"a..b",72".a.b",73"a.b.",74"a.b..",75"1.2",76"111.222.333.444",77"a.23skiddoo",78"[:::]",79"[:]",80};8182public static void main(String[] args) throws Exception {83System.out.println("Testing that JMXServiceURL accepts the same " +84"hosts as java.net.URI");85System.out.println("(Except that it allows empty host names and " +86"forbids a trailing \".\")");87System.out.println();8889int failures = 0;9091for (int pass = 1; pass <= 2; pass++) {92final boolean accept = (pass == 1);93System.out.println(" Hosts that should " +94(accept ? "" : "not ") + "work");95String[] hosts = accept ? good : bad;9697for (int i = 0; i < hosts.length; i++) {98final String host = hosts[i];99System.out.print(" " + host + ": ");100101boolean jmxAccept = true;102try {103new JMXServiceURL("rmi", hosts[i], 0);104} catch (MalformedURLException e) {105jmxAccept = false;106}107108boolean uriAccept;109try {110final URI uri = new URI("http://" + host + "/");111uriAccept = (uri.getHost() != null);112} catch (URISyntaxException e) {113uriAccept = false;114}115116final int len = host.length();117if (accept != uriAccept && len != 0 &&118!(len > 1 && host.charAt(len - 1) == '.'119&& host.charAt(len - 2) != '.')) {120// JMXServiceURL allows empty host name; also121// java.net.URI allows trailing dot in hostname,122// following RFC 2396, but JMXServiceURL doesn't,123// following RFC 2609124System.out.println("TEST BUG: URI accept=" + uriAccept);125failures++;126} else {127if (jmxAccept == accept)128System.out.println("OK");129else {130System.out.println("FAILED");131failures++;132}133}134}135136System.out.println();137}138139if (failures == 0)140System.out.println("Test passed");141else {142System.out.println("TEST FAILURES: " + failures);143System.exit(1);144}145}146}147148149